Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 1 | /***************************************************************************//** |
| 2 | * \file main.c |
| 3 | * \version 1.0 |
| 4 | ******************************************************************************** |
| 5 | * \copyright |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | *******************************************************************************/ |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 20 | /* Cypress pdl headers */ |
| 21 | #include "cy_pdl.h" |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 22 | #include "cy_retarget_io_pdl.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 23 | #include "cy_result.h" |
| 24 | |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 25 | #include "cycfg_clocks.h" |
| 26 | #include "cycfg_peripherals.h" |
| 27 | #include "cycfg_pins.h" |
| 28 | |
| 29 | #include "flash_qspi.h" |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 30 | #include "sysflash/sysflash.h" |
| 31 | #include "flash_map_backend/flash_map_backend.h" |
| 32 | |
| 33 | #include "bootutil/image.h" |
| 34 | #include "bootutil/bootutil.h" |
| 35 | #include "bootutil/sign_key.h" |
| 36 | |
| 37 | #include "bootutil/bootutil_log.h" |
| 38 | |
Tamas Ban | 4e8d838 | 2020-09-30 08:01:58 +0100 | [diff] [blame] | 39 | #include "bootutil/fault_injection_hardening.h" |
Tamas Ban | 4e8d838 | 2020-09-30 08:01:58 +0100 | [diff] [blame] | 40 | |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 41 | #include "watchdog.h" |
| 42 | |
| 43 | /* WDT time out for reset mode, in milliseconds. */ |
| 44 | #define WDT_TIME_OUT_MS 4000 |
| 45 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 46 | /* Define pins for UART debug output */ |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 47 | #define CYBSP_UART_ENABLED 1U |
| 48 | #define CYBSP_UART_HW SCB5 |
| 49 | #define CYBSP_UART_IRQ scb_5_interrupt_IRQn |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 50 | |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 51 | #ifdef CY_BOOT_USE_EXTERNAL_FLASH |
| 52 | /* Choose SMIF slot number (slave select). |
| 53 | * Acceptable values are: |
| 54 | * 0 - SMIF disabled (no external memory); |
| 55 | * 1, 2, 3 or 4 - slave select line memory module is connected to. |
| 56 | */ |
| 57 | uint32_t smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */ |
| 58 | #endif |
| 59 | |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 60 | |
| 61 | void hw_deinit(void); |
| 62 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 63 | static void do_boot(struct boot_rsp *rsp) |
| 64 | { |
| 65 | uint32_t app_addr = 0; |
| 66 | |
| 67 | app_addr = (rsp->br_image_off + rsp->br_hdr->ih_hdr_size); |
| 68 | |
| 69 | BOOT_LOG_INF("Starting User Application on CM4 (wait)..."); |
Bohdan Kovalchuk | 8416f35 | 2020-07-16 10:29:58 +0300 | [diff] [blame] | 70 | BOOT_LOG_INF("Start Address: 0x%08lx", app_addr); |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 71 | BOOT_LOG_INF("Deinitializing hardware..."); |
| 72 | |
| 73 | cy_retarget_io_wait_tx_complete(CYBSP_UART_HW, 10); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 74 | |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 75 | hw_deinit(); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 76 | |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 77 | Cy_SysEnableCM4(app_addr); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int main(void) |
| 81 | { |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame] | 82 | struct boot_rsp rsp; |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 83 | cy_rslt_t rc = CY_RSLT_TYPE_ERROR; |
| 84 | bool boot_succeeded = false; |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 85 | FIH_DECLARE(fih_rc, FIH_FAILURE); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 86 | |
Roman Okhrimenko | 4bc2810 | 2021-02-01 19:31:41 +0200 | [diff] [blame] | 87 | SystemInit(); |
| 88 | //init_cycfg_clocks(); |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 89 | init_cycfg_peripherals(); |
| 90 | init_cycfg_pins(); |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 91 | |
Roman Okhrimenko | 4bc2810 | 2021-02-01 19:31:41 +0200 | [diff] [blame] | 92 | /* Certain PSoC 6 devices enable CM4 by default at startup. It must be |
| 93 | * either disabled or enabled & running a valid application for flash write |
| 94 | * to work from CM0+. Since flash write may happen in boot_go() for updating |
| 95 | * the image before this bootloader app can enable CM4 in do_boot(), we need |
| 96 | * to keep CM4 disabled. Note that debugging of CM4 is not supported when it |
| 97 | * is disabled. |
| 98 | */ |
| 99 | #if defined(CY_DEVICE_PSOC6ABLE2) |
| 100 | if (CY_SYS_CM4_STATUS_ENABLED == Cy_SysGetCM4Status()) |
| 101 | { |
| 102 | Cy_SysDisableCM4(); |
| 103 | } |
| 104 | #endif /* #if defined(CY_DEVICE_PSOC6ABLE2) */ |
| 105 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 106 | /* enable interrupts */ |
| 107 | __enable_irq(); |
| 108 | |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 109 | /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */ |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 110 | rc = cy_retarget_io_pdl_init(115200u); |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 111 | |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 112 | if (rc != CY_RSLT_SUCCESS) |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 113 | { |
| 114 | CY_ASSERT(0); |
| 115 | } |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 116 | |
| 117 | BOOT_LOG_INF("MCUBoot Bootloader Started"); |
| 118 | |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 119 | #ifdef CY_BOOT_USE_EXTERNAL_FLASH |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 120 | rc = CY_SMIF_CMD_NOT_FOUND; |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame] | 121 | |
| 122 | #undef MCUBOOT_MAX_IMG_SECTORS |
| 123 | /* redefine number of sectors as there 2MB will be |
| 124 | * available on PSoC062-2M in case of external |
| 125 | * memory usage */ |
| 126 | #define MCUBOOT_MAX_IMG_SECTORS 4096 |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 127 | rc = qspi_init_sfdp(smif_id); |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 128 | if (rc == CY_SMIF_SUCCESS) |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 129 | { |
| 130 | BOOT_LOG_INF("External Memory initialized w/ SFDP."); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc); |
| 135 | } |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 136 | if (CY_SMIF_SUCCESS == rc) |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 137 | #endif |
| 138 | { |
Tamas Ban | 4e8d838 | 2020-09-30 08:01:58 +0100 | [diff] [blame] | 139 | |
| 140 | FIH_CALL(boot_go, fih_rc, &rsp); |
Michael Grand | 5047f03 | 2022-11-24 16:49:56 +0100 | [diff] [blame] | 141 | if (FIH_EQ(fih_rc, FIH_SUCCESS)) |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 142 | { |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 143 | BOOT_LOG_INF("User Application validated successfully"); |
Roman Okhrimenko | 0c7aebc | 2020-09-02 13:37:51 +0300 | [diff] [blame] | 144 | /* initialize watchdog timer. it should be updated from user app |
| 145 | * to mark successful start up of this app. if the watchdog is not updated, |
| 146 | * reset will be initiated by watchdog timer and swap revert operation started |
| 147 | * to roll back to operable image. |
| 148 | */ |
| 149 | cy_wdg_init(WDT_TIME_OUT_MS); |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 150 | do_boot(&rsp); |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 151 | boot_succeeded = true; |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 152 | } |
| 153 | else |
| 154 | { |
| 155 | BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images"); |
| 156 | } |
Bohdan Kovalchuk | 0324f1b | 2020-05-26 08:04:24 -0500 | [diff] [blame] | 157 | } |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 158 | |
| 159 | while (1) |
| 160 | { |
Roman Okhrimenko | ff02612 | 2020-09-23 12:58:07 +0300 | [diff] [blame] | 161 | if (boot_succeeded) { |
| 162 | Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT); |
| 163 | } |
| 164 | else { |
| 165 | __WFI(); |
| 166 | } |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 167 | } |
| 168 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | } |
dmiv | 8672c8e | 2020-09-16 12:59:20 +0300 | [diff] [blame] | 171 | |
| 172 | void hw_deinit(void) |
| 173 | { |
| 174 | cy_retarget_io_pdl_deinit(); |
| 175 | Cy_GPIO_Port_Deinit(CYBSP_UART_RX_PORT); |
| 176 | Cy_GPIO_Port_Deinit(CYBSP_UART_TX_PORT); |
| 177 | |
| 178 | #ifdef CY_BOOT_USE_EXTERNAL_FLASH |
| 179 | qspi_deinit(smif_id); |
| 180 | #endif |
| 181 | } |