|
| 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