blob: 6c28ba03d57a2029bb7578b3a2c81d521bee8a8e [file] [log] [blame]
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +02001/***************************************************************************//**
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 Okhrimenko89ecdac2020-02-28 17:05:55 +020020/* Cypress pdl headers */
21#include "cy_pdl.h"
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030022#include "cy_retarget_io_pdl.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020023#include "cy_result.h"
24
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050025#include "cycfg_clocks.h"
26#include "cycfg_peripherals.h"
27#include "cycfg_pins.h"
28
29#include "flash_qspi.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020030#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 Ban4e8d8382020-09-30 08:01:58 +010039#include "bootutil/fault_injection_hardening.h"
Tamas Ban4e8d8382020-09-30 08:01:58 +010040
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030041#include "watchdog.h"
42
43/* WDT time out for reset mode, in milliseconds. */
44#define WDT_TIME_OUT_MS 4000
45
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020046/* Define pins for UART debug output */
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030047#define CYBSP_UART_ENABLED 1U
48#define CYBSP_UART_HW SCB5
49#define CYBSP_UART_IRQ scb_5_interrupt_IRQn
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020050
dmiv8672c8e2020-09-16 12:59:20 +030051#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 */
57uint32_t smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
58#endif
59
dmiv8672c8e2020-09-16 12:59:20 +030060
61void hw_deinit(void);
62
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020063static 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 Kovalchuk8416f352020-07-16 10:29:58 +030070 BOOT_LOG_INF("Start Address: 0x%08lx", app_addr);
Roman Okhrimenkoff026122020-09-23 12:58:07 +030071 BOOT_LOG_INF("Deinitializing hardware...");
72
73 cy_retarget_io_wait_tx_complete(CYBSP_UART_HW, 10);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020074
dmiv8672c8e2020-09-16 12:59:20 +030075 hw_deinit();
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020076
dmiv8672c8e2020-09-16 12:59:20 +030077 Cy_SysEnableCM4(app_addr);
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020078}
79
80int main(void)
81{
Bohdan Kovalchuka333a452020-07-09 16:55:58 +030082 struct boot_rsp rsp;
Roman Okhrimenkoff026122020-09-23 12:58:07 +030083 cy_rslt_t rc = CY_RSLT_TYPE_ERROR;
84 bool boot_succeeded = false;
Tamas Ban4e8d8382020-09-30 08:01:58 +010085 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020086
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030087 init_cycfg_clocks();
88 init_cycfg_peripherals();
89 init_cycfg_pins();
dmiv8672c8e2020-09-16 12:59:20 +030090
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020091 /* enable interrupts */
92 __enable_irq();
93
Bohdan Kovalchuk77256522020-04-15 18:03:43 +030094 /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */
Roman Okhrimenkoff026122020-09-23 12:58:07 +030095 rc = cy_retarget_io_pdl_init(115200u);
dmiv8672c8e2020-09-16 12:59:20 +030096
Roman Okhrimenkoff026122020-09-23 12:58:07 +030097 if (rc != CY_RSLT_SUCCESS)
dmiv8672c8e2020-09-16 12:59:20 +030098 {
99 CY_ASSERT(0);
100 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200101
102 BOOT_LOG_INF("MCUBoot Bootloader Started");
103
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500104#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300105 rc = CY_SMIF_CMD_NOT_FOUND;
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300106
107 #undef MCUBOOT_MAX_IMG_SECTORS
108 /* redefine number of sectors as there 2MB will be
109 * available on PSoC062-2M in case of external
110 * memory usage */
111 #define MCUBOOT_MAX_IMG_SECTORS 4096
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500112 rc = qspi_init_sfdp(smif_id);
dmiv8672c8e2020-09-16 12:59:20 +0300113 if (rc == CY_SMIF_SUCCESS)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500114 {
115 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
116 }
117 else
118 {
119 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%02x", (int)rc);
120 }
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300121 if (CY_SMIF_SUCCESS == rc)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500122#endif
123 {
Tamas Ban4e8d8382020-09-30 08:01:58 +0100124
125 FIH_CALL(boot_go, fih_rc, &rsp);
126 if (fih_eq(fih_rc, FIH_SUCCESS))
dmiv8672c8e2020-09-16 12:59:20 +0300127 {
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500128 BOOT_LOG_INF("User Application validated successfully");
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300129 /* initialize watchdog timer. it should be updated from user app
130 * to mark successful start up of this app. if the watchdog is not updated,
131 * reset will be initiated by watchdog timer and swap revert operation started
132 * to roll back to operable image.
133 */
134 cy_wdg_init(WDT_TIME_OUT_MS);
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500135 do_boot(&rsp);
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300136 boot_succeeded = true;
dmiv8672c8e2020-09-16 12:59:20 +0300137 }
138 else
139 {
140 BOOT_LOG_INF("MCUBoot Bootloader found none of bootable images");
141 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500142 }
dmiv8672c8e2020-09-16 12:59:20 +0300143
144 while (1)
145 {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300146 if (boot_succeeded) {
147 Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
148 }
149 else {
150 __WFI();
151 }
dmiv8672c8e2020-09-16 12:59:20 +0300152 }
153
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200154 return 0;
155}
dmiv8672c8e2020-09-16 12:59:20 +0300156
157void hw_deinit(void)
158{
159 cy_retarget_io_pdl_deinit();
160 Cy_GPIO_Port_Deinit(CYBSP_UART_RX_PORT);
161 Cy_GPIO_Port_Deinit(CYBSP_UART_TX_PORT);
162
163#ifdef CY_BOOT_USE_EXTERNAL_FLASH
164 qspi_deinit(smif_id);
165#endif
166}