Skip to content

Commit 5c7341b

Browse files
committed
Add STM32 Middleware - External Memory Manager
1 parent 8985e97 commit 5c7341b

29 files changed

+9220
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
This software component is provided to you as part of a software package and
2+
applicable license terms are in the Package_license file. If you received this
3+
software component outside of a package or without applicable license terms,
4+
the terms of the SLA0044 license shall apply and are fully reproduced below:
5+
6+
SLA0044 Rev5/February 2018
7+
8+
Software license agreement
9+
10+
ULTIMATE LIBERTY SOFTWARE LICENSE AGREEMENT
11+
12+
BY INSTALLING, COPYING, DOWNLOADING, ACCESSING OR OTHERWISE USING THIS SOFTWARE
13+
OR ANY PART THEREOF (AND THE RELATED DOCUMENTATION) FROM STMICROELECTRONICS
14+
INTERNATIONAL N.V, SWISS BRANCH AND/OR ITS AFFILIATED COMPANIES
15+
(STMICROELECTRONICS), THE RECIPIENT, ON BEHALF OF HIMSELF OR HERSELF, OR ON
16+
BEHALF OF ANY ENTITY BY WHICH SUCH RECIPIENT IS EMPLOYED AND/OR ENGAGED AGREES
17+
TO BE BOUND BY THIS SOFTWARE LICENSE AGREEMENT.
18+
19+
Under STMicroelectronics’ intellectual property rights, the redistribution,
20+
reproduction and use in source and binary forms of the software or any part
21+
thereof, with or without modification, are permitted provided that the following
22+
conditions are met:
23+
24+
1. Redistribution of source code (modified or not) must retain any copyright
25+
notice, this list of conditions and the disclaimer set forth below as items 10
26+
and 11.
27+
28+
2. Redistributions in binary form, except as embedded into microcontroller or
29+
microprocessor device manufactured by or for STMicroelectronics or a software
30+
update for such device, must reproduce any copyright notice provided with the
31+
binary code, this list of conditions, and the disclaimer set forth below as
32+
items 10 and 11, in documentation and/or other materials provided with the
33+
distribution.
34+
35+
3. Neither the name of STMicroelectronics nor the names of other contributors to
36+
this software may be used to endorse or promote products derived from this
37+
software or part thereof without specific written permission.
38+
39+
4. This software or any part thereof, including modifications and/or derivative
40+
works of this software, must be used and execute solely and exclusively on or in
41+
combination with a microcontroller or microprocessor device manufactured by or
42+
for STMicroelectronics.
43+
44+
5. No use, reproduction or redistribution of this software partially or totally
45+
may be done in any manner that would subject this software to any Open Source
46+
Terms. “Open Source Terms” shall mean any open source license which requires as
47+
part of distribution of software that the source code of such software is
48+
distributed therewith or otherwise made available, or open source license that
49+
substantially complies with the Open Source definition specified at
50+
www.opensource.org and any other comparable open source license such as for
51+
example GNU General Public License (GPL), Eclipse Public License (EPL), Apache
52+
Software License, BSD license or MIT license.
53+
54+
6. STMicroelectronics has no obligation to provide any maintenance, support or
55+
updates for the software.
56+
57+
7. The software is and will remain the exclusive property of STMicroelectronics
58+
and its licensors. The recipient will not take any action that jeopardizes
59+
STMicroelectronics and its licensors' proprietary rights or acquire any rights
60+
in the software, except the limited rights specified hereunder.
61+
62+
8. The recipient shall comply with all applicable laws and regulations affecting
63+
the use of the software or any part thereof including any applicable export
64+
control law or regulation.
65+
66+
9. Redistribution and use of this software or any part thereof other than as
67+
permitted under this license is void and will automatically terminate your
68+
rights under this license.
69+
70+
10. THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND
71+
ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72+
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
73+
NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY RIGHTS, WHICH ARE
74+
DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT SHALL
75+
STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
76+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
77+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
78+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
79+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
80+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
81+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82+
83+
11. EXCEPT AS EXPRESSLY PERMITTED HEREUNDER, NO LICENSE OR OTHER RIGHTS, WHETHER
84+
EXPRESS OR IMPLIED, ARE GRANTED UNDER ANY PATENT OR OTHER INTELLECTUAL PROPERTY
85+
RIGHTS OF STMICROELECTRONICS OR ANY THIRD PARTY.
86+
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
/**
2+
******************************************************************************
3+
* @file stm32_boot_lrun.c
4+
* @author MCD Application Team
5+
* @brief this file manages the boot in the mode load and run.
6+
******************************************************************************
7+
* @attention
8+
*
9+
* Copyright (c) 2022 STMicroelectronics.
10+
* All rights reserved.
11+
*
12+
* This software is licensed under terms that can be found in the LICENSE file
13+
* in the root directory of this software component.
14+
* If no LICENSE file comes with this software, it is provided AS-IS.
15+
*
16+
******************************************************************************
17+
*/
18+
19+
/* Includes ------------------------------------------------------------------*/
20+
#include "stm32_boot_lrun.h"
21+
22+
/** @defgroup BOOT
23+
* @{
24+
*/
25+
26+
/** @defgroup BOOT_LRUN
27+
* @{
28+
*/
29+
30+
/* Private typedefs ----------------------------------------------------------*/
31+
/* Private defines -----------------------------------------------------------*/
32+
33+
/* offset of the vector table from the start of the image. Should be set in extmem_conf.h if needed */
34+
#ifndef EXTMEM_HEADER_OFFSET
35+
#define EXTMEM_HEADER_OFFSET 0
36+
#endif
37+
#if defined(EXTMEM_LRUN_TS_ENABLE_NS) && (!defined(EXTMEM_LRUN_DESTINATION_ADDRESS_NS) \
38+
|| !defined(EXTMEM_LRUN_SOURCE_ADDRESS_NS))
39+
#error "ExtMem user configuration incorrect : undefined parameters for Non-Secure image loading"
40+
#endif
41+
/* Private macros ------------------------------------------------------------*/
42+
/* Private variables ---------------------------------------------------------*/
43+
/* Private function prototypes -----------------------------------------------*/
44+
BOOTStatus_TypeDef MapMemory(void);
45+
BOOTStatus_TypeDef CopyApplication(void);
46+
BOOTStatus_TypeDef JumpToApplication(void);
47+
BOOTStatus_TypeDef GetBaseAddress(uint32_t MemIndex, uint32_t *BaseAddress);
48+
49+
/**
50+
* @addtogroup BOOT_LRUN_Exported_Functions Boot LRUN exported functions
51+
* @{
52+
*/
53+
BOOTStatus_TypeDef BOOT_Application(void)
54+
{
55+
BOOTStatus_TypeDef retr;
56+
57+
/* mount the memory */
58+
retr = MapMemory();
59+
if (BOOT_OK == retr)
60+
{
61+
retr = CopyApplication();
62+
if (BOOT_OK == retr)
63+
{
64+
/* jump on the application */
65+
retr = JumpToApplication();
66+
}
67+
}
68+
return retr;
69+
}
70+
71+
/**
72+
* @}
73+
*/
74+
75+
/**
76+
* @defgroup BOOT_LRUN_Private_Functions Boot LRUN private functions
77+
* @{
78+
*/
79+
80+
/**
81+
* @brief this function maps the memory
82+
* @return @ref BOOTStatus_TypeDef
83+
*/
84+
BOOTStatus_TypeDef MapMemory(void)
85+
{
86+
BOOTStatus_TypeDef retr = BOOT_OK;
87+
uint32_t BaseAddress = 0;
88+
89+
/* Map all the memory */
90+
for (uint8_t index = 0; index < (sizeof(extmem_list_config) / sizeof(EXTMEM_DefinitionTypeDef)); index++)
91+
{
92+
switch(EXTMEM_GetMapAddress(index, &BaseAddress))
93+
{
94+
case EXTMEM_OK :{
95+
if (EXTMEM_MemoryMappedMode(index, EXTMEM_ENABLE) != EXTMEM_OK)
96+
{
97+
retr = BOOT_ERROR_MAPPEDMODEFAIL;
98+
}
99+
break;
100+
}
101+
case EXTMEM_ERROR_NOTSUPPORTED :{
102+
/* the memory doesn't support map mode, nothing to do */
103+
break;
104+
}
105+
default :{
106+
retr = BOOT_ERROR_NOBASEADDRESS;
107+
break;
108+
}
109+
}
110+
}
111+
return retr;
112+
}
113+
114+
/**
115+
* @brief This function copy the data from source to destination
116+
* @return @ref BOOTStatus_TypeDef
117+
*/
118+
BOOTStatus_TypeDef CopyApplication(void)
119+
{
120+
BOOTStatus_TypeDef retr = BOOT_OK;
121+
uint8_t *source;
122+
uint8_t *destination;
123+
uint32_t MapAddress;
124+
uint32_t img_size;
125+
126+
#if defined(EXTMEM_LRUN_DESTINATION_INTERNAL)
127+
/* this case correspond to copy the SW from external memory into internal memory */
128+
destination = (uint8_t *)EXTMEM_LRUN_DESTINATION_ADDRESS;
129+
#else
130+
if (EXTMEM_OK != EXTMEM_GetMapAddress(EXTMEM_LRUN_DESTINATION, &MapAddress))
131+
{
132+
return BOOT_ERROR_MAPPEDMODEFAIL;
133+
}
134+
destination = (uint8_t *)(MapAddress + EXTMEM_LRUN_DESTINATION_ADDRESS);
135+
#endif
136+
137+
/* get the map address of the source memory */
138+
switch(EXTMEM_GetMapAddress(EXTMEM_LRUN_SOURCE, &MapAddress)){
139+
case EXTMEM_OK :{
140+
/* manage the copy in mapped mode */
141+
source = (uint8_t*)(MapAddress + EXTMEM_LRUN_SOURCE_ADDRESS);
142+
img_size = BOOT_GetApplicationSize((uint32_t) source);
143+
/* copy form source to destination in mapped mode */
144+
for (uint32_t index=0; index < img_size; index++)
145+
{
146+
destination[index] = source[index];
147+
}
148+
#if defined(EXTMEM_LRUN_TZ_ENABLE_NS)
149+
source = (uint8_t*)(MapAddress + EXTMEM_LRUN_SOURCE_ADDRESS_NS);
150+
img_size = BOOT_GetApplicationSize((uint32_t) source);
151+
destination = (uint8_t *)EXTMEM_LRUN_DESTINATION_ADDRESS_NS;
152+
/* copy Non-Secure form source to destination in mapped mode */
153+
for (uint32_t index=0; index < img_size; index++)
154+
{
155+
destination[index] = source[index];
156+
}
157+
#endif
158+
break;
159+
}
160+
161+
case EXTMEM_ERROR_NOTSUPPORTED:{
162+
img_size = BOOT_GetApplicationSize(EXTMEM_LRUN_SOURCE_ADDRESS);
163+
/* manage the copy using EXTMEM_Read */
164+
if (EXTMEM_OK != EXTMEM_Read(EXTMEM_LRUN_SOURCE, EXTMEM_LRUN_SOURCE_ADDRESS, destination, img_size))
165+
{
166+
retr = BOOT_ERROR_COPY;
167+
}
168+
#if defined(EXTMEM_LRUN_TZ_ENABLE_NS)
169+
img_size = BOOT_GetApplicationSize(EXTMEM_LRUN_SOURCE_ADDRESS_NS);
170+
destination = (uint8_t *)EXTMEM_LRUN_DESTINATION_ADDRESS_NS;
171+
/* copy Non-Secure form source to destination in mapped mode */
172+
if (EXTMEM_OK != EXTMEM_Read(EXTMEM_LRUN_SOURCE, EXTMEM_LRUN_SOURCE_ADDRESS_NS, destination, img_size))
173+
{
174+
retr = BOOT_ERROR_COPY;
175+
}
176+
#endif
177+
break;
178+
}
179+
180+
default :{
181+
/* return an error */
182+
retr = BOOT_ERROR_MAPPEDMODEFAIL;
183+
break;
184+
}
185+
}
186+
return retr;
187+
}
188+
189+
/**
190+
* @brief This function jumps to the application through its vector table
191+
* @return @ref BOOTStatus_TypeDef
192+
*/
193+
BOOTStatus_TypeDef JumpToApplication(void)
194+
{
195+
uint32_t primask_bit;
196+
typedef void (*pFunction)(void);
197+
static pFunction JumpToApp;
198+
uint32_t Application_vector;
199+
/* Suspend SysTick */
200+
HAL_SuspendTick();
201+
202+
#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
203+
/* if I-Cache is enabled, disable I-Cache-----------------------------------*/
204+
if (SCB->CCR & SCB_CCR_IC_Msk)
205+
{
206+
SCB_DisableICache();
207+
}
208+
#endif /* defined(ICACHE_PRESENT) && (ICACHE_PRESENT == 1U) */
209+
210+
#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
211+
/* if D-Cache is enabled, disable D-Cache-----------------------------------*/
212+
if (SCB->CCR & SCB_CCR_DC_Msk)
213+
{
214+
SCB_DisableDCache();
215+
}
216+
#endif /* defined(DCACHE_PRESENT) && (DCACHE_PRESENT == 1U) */
217+
218+
/* Initialize user application's Stack Pointer & Jump to user application */
219+
primask_bit = __get_PRIMASK();
220+
__disable_irq();
221+
222+
Application_vector = BOOT_GetApplicationVectorTable();
223+
224+
SCB->VTOR = (uint32_t)Application_vector;
225+
JumpToApp = (pFunction) (*(__IO uint32_t *)(Application_vector + 4));
226+
227+
#if ((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
228+
(defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) || \
229+
(defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) )
230+
/* on ARM v8m, set MSPLIM before setting MSP to avoid unwanted stack overflow faults */
231+
__set_MSPLIM(0x00000000);
232+
#endif /* __ARM_ARCH_8M_MAIN__ or __ARM_ARCH_8M_BASE__ */
233+
234+
__set_MSP(*(__IO uint32_t*)Application_vector);
235+
236+
/* Re-enable the interrupts */
237+
__set_PRIMASK(primask_bit);
238+
239+
JumpToApp();
240+
return BOOT_OK;
241+
}
242+
243+
244+
__weak uint32_t BOOT_GetApplicationSize(uint32_t img_addr)
245+
{
246+
UNUSED(img_addr);
247+
return EXTMEM_LRUN_SOURCE_SIZE;
248+
}
249+
250+
__weak uint32_t BOOT_GetApplicationVectorTable(void)
251+
{
252+
uint32_t vector_table;
253+
#if defined(EXTMEM_LRUN_DESTINATION_INTERNAL)
254+
vector_table = EXTMEM_LRUN_DESTINATION_ADDRESS;
255+
#else
256+
if (EXTMEM_OK != EXTMEM_GetMapAddress(EXTMEM_LRUN_DESTINATION, &vector_table))
257+
{
258+
return 0xffffffff;
259+
}
260+
vector_table += EXTMEM_LRUN_DESTINATION_ADDRESS;
261+
#endif
262+
vector_table += EXTMEM_HEADER_OFFSET;
263+
return vector_table;
264+
}
265+
/**
266+
* @}
267+
*/
268+
269+
/**
270+
* @}
271+
*/
272+
273+
/**
274+
* @}
275+
*/

0 commit comments

Comments
 (0)