Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 3 | * Copyright (c) 2017-2018 Arm Limited. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 19 | #include "bl2_util.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 20 | #include "target.h" |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 21 | #include "cmsis.h" |
| 22 | #include "uart_stdout.h" |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 23 | #include "Driver_Flash.h" |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 24 | #include "mbedtls/memory_buffer_alloc.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 25 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 26 | #include "bootutil/bootutil_log.h" |
| 27 | #include "bootutil/image.h" |
| 28 | #include "bootutil/bootutil.h" |
| 29 | #include "flash_map/flash_map.h" |
| 30 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 31 | /* Avoids the semihosting issue */ |
| 32 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 33 | __asm(" .global __ARM_use_no_argv\n"); |
| 34 | #endif |
| 35 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 36 | /* Flash device name must be specified by target */ |
| 37 | extern ARM_DRIVER_FLASH FLASH_DEV_NAME; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 38 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 39 | #define BL2_MBEDTLS_MEM_BUF_LEN 0x2000 |
| 40 | /* Static buffer to be used by mbedtls for memory allocation */ |
| 41 | static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 42 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 43 | struct arm_vector_table { |
| 44 | uint32_t msp; |
| 45 | uint32_t reset; |
| 46 | }; |
| 47 | |
| 48 | static void do_boot(struct boot_rsp *rsp) |
| 49 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 50 | /* Clang at O0, stores variables on the stack with SP relative addressing. |
| 51 | * When manually set the SP then the place of reset vector is lost. |
| 52 | * Static variables are stored in 'data' or 'bss' section, change of SP has |
| 53 | * no effect on them. |
| 54 | */ |
| 55 | static struct arm_vector_table *vt; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 56 | uintptr_t flash_base; |
| 57 | int rc; |
| 58 | |
| 59 | /* The beginning of the image is the ARM vector table, containing |
| 60 | * the initial stack pointer address and the reset vector |
| 61 | * consecutively. Manually set the stack pointer and jump into the |
| 62 | * reset vector |
| 63 | */ |
| 64 | rc = flash_device_base(rsp->br_flash_dev_id, &flash_base); |
| 65 | assert(rc == 0); |
| 66 | |
Oliver Swede | f998244 | 2018-08-24 18:37:44 +0100 | [diff] [blame] | 67 | if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) { |
| 68 | /* The image has been copied to SRAM, find the vector table |
| 69 | * at the load address instead of image's address in flash |
| 70 | */ |
| 71 | vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr + |
| 72 | rsp->br_hdr->ih_hdr_size); |
| 73 | } else { |
| 74 | /* Using the flash address as not executing in SRAM */ |
| 75 | vt = (struct arm_vector_table *)(flash_base + |
| 76 | rsp->br_image_off + |
| 77 | rsp->br_hdr->ih_hdr_size); |
| 78 | } |
| 79 | |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame^] | 80 | rc = FLASH_DEV_NAME.Uninitialize(); |
| 81 | if(rc != ARM_DRIVER_OK) { |
| 82 | BOOT_LOG_ERR("Error while uninitializing Flash Interface"); |
| 83 | } |
| 84 | |
David Vincze | 8da7f10 | 2018-09-24 10:53:46 +0200 | [diff] [blame] | 85 | stdio_uninit(); |
| 86 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 87 | __disable_irq(); |
| 88 | __set_MSP(vt->msp); |
| 89 | __DSB(); |
| 90 | __ISB(); |
| 91 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 92 | ((void (*)(void))vt->reset)(); |
| 93 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 94 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 95 | int main(void) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 96 | { |
| 97 | struct boot_rsp rsp; |
| 98 | int rc; |
| 99 | |
Gabor Kertesz | eb953f5 | 2018-07-17 13:36:28 +0200 | [diff] [blame] | 100 | stdio_init(); |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 101 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 102 | BOOT_LOG_INF("Starting bootloader"); |
| 103 | |
Tamas Ban | bd3f751 | 2018-01-26 15:45:03 +0000 | [diff] [blame] | 104 | /* Initialise the mbedtls static memory allocator so that mbedtls allocates |
| 105 | * memory from the provided static buffer instead of from the heap. |
| 106 | */ |
| 107 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 108 | |
David Vincze | b57989f | 2018-09-24 10:59:04 +0200 | [diff] [blame^] | 109 | rc = FLASH_DEV_NAME.Initialize(NULL); |
| 110 | if(rc != ARM_DRIVER_OK) { |
| 111 | BOOT_LOG_ERR("Error while initializing Flash Interface"); |
| 112 | while (1) |
| 113 | ; |
| 114 | } |
David Vincze | 26e8c8a | 2018-08-28 16:59:41 +0200 | [diff] [blame] | 115 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 116 | rc = boot_go(&rsp); |
| 117 | if (rc != 0) { |
| 118 | BOOT_LOG_ERR("Unable to find bootable image"); |
| 119 | while (1) |
| 120 | ; |
| 121 | } |
| 122 | |
| 123 | BOOT_LOG_INF("Bootloader chainload address offset: 0x%x", |
| 124 | rsp.br_image_off); |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 125 | flash_area_warn_on_open(); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 126 | BOOT_LOG_INF("Jumping to the first image slot"); |
| 127 | do_boot(&rsp); |
| 128 | |
| 129 | BOOT_LOG_ERR("Never should get here"); |
| 130 | while (1) |
| 131 | ; |
| 132 | } |