Andrei Narkevitch | b0be461 | 2020-01-27 17:26:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "target_cfg.h" |
| 9 | #include "cmsis.h" |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame^] | 10 | #include "boot_hal.h" |
Andrei Narkevitch | b0be461 | 2020-01-27 17:26:19 -0800 | [diff] [blame] | 11 | #include "Driver_Flash.h" |
| 12 | #include "flash_layout.h" |
| 13 | |
| 14 | /* Flash device name must be specified by target */ |
| 15 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
| 16 | |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame^] | 17 | /*! |
| 18 | * \brief Chain-loading the next image in the boot sequence. |
| 19 | * |
| 20 | * This function calls the Reset_Handler of the next image in the boot sequence, |
| 21 | * usually it is the secure firmware. Before passing the execution to next image |
| 22 | * there is conditional rule to remove the secrets from the memory. This must be |
| 23 | * done if the following conditions are satisfied: |
| 24 | * - Memory is shared between SW components at different stages of the trusted |
| 25 | * boot process. |
| 26 | * - There are secrets in the memory: KDF parameter, symmetric key, |
| 27 | * manufacturer sensitive code/data, etc. |
| 28 | */ |
| 29 | #if defined(__ICCARM__) |
| 30 | #pragma required = boot_clear_bl2_ram_area |
| 31 | #endif |
| 32 | |
| 33 | __WEAK __attribute__((naked)) void boot_jump_to_next_image(uint32_t reset_handler_addr) |
| 34 | { |
| 35 | __ASM volatile( |
| 36 | #if !defined(__ICCARM__) |
| 37 | ".syntax unified \n" |
| 38 | #endif |
| 39 | "mov r7, r0 \n" |
| 40 | "bl boot_clear_bl2_ram_area \n" /* Clear RAM before jump */ |
| 41 | "movs r0, #0 \n" /* Clear registers: R0-R12, */ |
| 42 | "mov r1, r0 \n" /* except R7 */ |
| 43 | "mov r2, r0 \n" |
| 44 | "mov r3, r0 \n" |
| 45 | "mov r4, r0 \n" |
| 46 | "mov r5, r0 \n" |
| 47 | "mov r6, r0 \n" |
| 48 | "mov r8, r0 \n" |
| 49 | "mov r9, r0 \n" |
| 50 | "mov r10, r0 \n" |
| 51 | "mov r11, r0 \n" |
| 52 | "mov r12, r0 \n" |
| 53 | "mov lr, r0 \n" |
| 54 | "bx r7 \n" /* Jump to Reset_handler */ |
| 55 | ); |
| 56 | } |
| 57 | |
Andrei Narkevitch | b0be461 | 2020-01-27 17:26:19 -0800 | [diff] [blame] | 58 | /* bootloader platform-specific hw initialization */ |
| 59 | __WEAK int32_t boot_platform_init(void) |
| 60 | { |
| 61 | int32_t result; |
| 62 | |
| 63 | result = FLASH_DEV_NAME.Initialize(NULL); |
| 64 | if (result == ARM_DRIVER_OK) { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | return 1; |
| 69 | } |
Michel Jaouen | 3ecd622 | 2020-06-17 18:58:00 +0200 | [diff] [blame^] | 70 | |
| 71 | __WEAK void boot_platform_quit(struct boot_arm_vector_table *vt) |
| 72 | { |
| 73 | /* Clang at O0, stores variables on the stack with SP relative addressing. |
| 74 | * When manually set the SP then the place of reset vector is lost. |
| 75 | * Static variables are stored in 'data' or 'bss' section, change of SP has |
| 76 | * no effect on them. |
| 77 | */ |
| 78 | static struct boot_arm_vector_table *vt_cpy; |
| 79 | |
| 80 | vt_cpy = vt; |
| 81 | #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__) |
| 82 | /* Restore the Main Stack Pointer Limit register's reset value |
| 83 | * before passing execution to runtime firmware to make the |
| 84 | * bootloader transparent to it. |
| 85 | */ |
| 86 | __set_MSPLIM(0); |
| 87 | #endif |
| 88 | __set_MSP(vt->msp); |
| 89 | __DSB(); |
| 90 | __ISB(); |
| 91 | |
| 92 | boot_jump_to_next_image(vt_cpy->reset); |
| 93 | } |
| 94 | |