blob: 8c7bdd06ac74f16d53521bb7edab48fbbcf48594 [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 Okhrimenko977b3752022-03-31 14:40:48 +030020#include <inttypes.h>
21#include <stdbool.h>
22
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020023/* Cypress pdl headers */
24#include "cy_pdl.h"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030025#include "cyhal.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030026#include "cyhal_wdt.h"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030027
28#if defined CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +030029#include "cy_service_app.h"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030030#endif
31
32#include "cybsp.h"
33#include "cy_retarget_io.h"
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000034#include "cyw_platform_utils.h"
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050035
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000036#if defined(CY_BOOT_USE_EXTERNAL_FLASH) || defined(CYW20829)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050037#include "flash_qspi.h"
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000038#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) || defined(CYW20829) */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030039
40#include "cycfg_pins.h"
41#include "cy_result.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020042#include "sysflash/sysflash.h"
43#include "flash_map_backend/flash_map_backend.h"
44
45#include "bootutil/image.h"
46#include "bootutil/bootutil.h"
47#include "bootutil/sign_key.h"
48
49#include "bootutil/bootutil_log.h"
50
Tamas Ban4e8d8382020-09-30 08:01:58 +010051#include "bootutil/fault_injection_hardening.h"
Tamas Ban4e8d8382020-09-30 08:01:58 +010052
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000053#ifdef USE_EXEC_TIME_CHECK
54#include "misc/timebase_us.h"
55#include "misc/exec_time_check.h"
56#endif /* USE_EXEC_TIME_CHECK */
57
58#ifdef USE_LOG_TIMESTAMP
59#include "timestamp.h"
60#endif /* USE_LOG_TIMESTAMP */
61
Roman Okhrimenko977b3752022-03-31 14:40:48 +030062#define CY_RSLT_MODULE_MCUBOOTAPP 0x500U
63#define CY_RSLT_MODULE_MCUBOOTAPP_MAIN 0x51U
64
65/** General module error */
66#define MCUBOOTAPP_RSLT_ERR \
67 (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MCUBOOTAPP, CY_RSLT_MODULE_MCUBOOTAPP_MAIN, 0))
68
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030069/* WDT time out for reset mode, in milliseconds. */
70#define WDT_TIME_OUT_MS 4000
71
dmiv8672c8e2020-09-16 12:59:20 +030072#ifdef CY_BOOT_USE_EXTERNAL_FLASH
73/* Choose SMIF slot number (slave select).
74 * Acceptable values are:
75 * 0 - SMIF disabled (no external memory);
76 * 1, 2, 3 or 4 - slave select line memory module is connected to.
77 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030078#define SMIF_ID (1U) /* Assume SlaveSelect_0 is used for External Memory */
79#endif /* CY_BOOT_USE_EXTERNAL_FLASH */
dmiv8672c8e2020-09-16 12:59:20 +030080
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030081#define BOOT_MSG_FINISH "MCUBoot Bootloader finished.\r\n" \
Roman Okhrimenko977b3752022-03-31 14:40:48 +030082 "Deinitializing hardware..."
dmiv8672c8e2020-09-16 12:59:20 +030083
Roman Okhrimenko977b3752022-03-31 14:40:48 +030084static void hw_deinit(void);
dmiv8672c8e2020-09-16 12:59:20 +030085
Roman Okhrimenko977b3752022-03-31 14:40:48 +030086static inline __attribute__((always_inline))
87fih_uint calc_app_addr(uintptr_t flash_base, const struct boot_rsp *rsp)
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020088{
Roman Okhrimenko977b3752022-03-31 14:40:48 +030089 return fih_uint_encode(flash_base +
90 rsp->br_image_off +
91 rsp->br_hdr->ih_hdr_size);
92}
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020093
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +020094/*******************************************************************************
95 * Function Name: fih_calc_app_addr
96 ********************************************************************************
97 * Summary:
98 * Calculate start address of user application.
99 *
100 * Parameters:
101 * image_base - base address of flash;
102 *
103 * rsp - provided by the boot loader code; indicates where to jump
104 * to execute the main image;
105 *
106 * output - calculated address of application;
107 *
108 * Return:
109 * fih_int
110 *
111 *******************************************************************************/
112static inline __attribute__((always_inline)) fih_int fih_calc_app_addr(
113 uintptr_t image_base, const struct boot_rsp *rsp, fih_uint *app_address)
114{
115 fih_int fih_rc = FIH_FAILURE;
116
117#if defined(MCUBOOT_RAM_LOAD)
118 if (IS_RAM_BOOTABLE(rsp->br_hdr) == true) {
119 if ((UINT32_MAX - rsp->br_hdr->ih_hdr_size) >= image_base) {
120 *app_address =
121 fih_uint_encode(image_base + rsp->br_hdr->ih_hdr_size);
122 fih_rc = FIH_SUCCESS;
123 }
124 } else
125#endif
126 {
127 if (((UINT32_MAX - rsp->br_image_off) >= image_base) &&
128 ((UINT32_MAX - rsp->br_hdr->ih_hdr_size) >=
129 (image_base + rsp->br_image_off))) {
130 *app_address = fih_uint_encode(image_base + rsp->br_image_off +
131 rsp->br_hdr->ih_hdr_size);
132 fih_rc = FIH_SUCCESS;
133 }
134 }
135
136 FIH_RET(fih_rc);
137}
138
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300139#if defined CYW20829
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200140
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300141#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
142CY_RAMFUNC_BEGIN /* SMIF will be deinitialized in this case! */
143#else
144inline __attribute__((always_inline))
145#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
146__NO_RETURN
147static void cyw20829_launch_app(fih_uint app_addr, uint32_t *key, uint32_t *iv)
148{
149#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
150 qspi_deinit(SMIF_ID);
151#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000152 platform_RunNextApp(app_addr, key, iv);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300153}
154#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
155CY_RAMFUNC_END /* SMIF will be deinitialized in this case! */
156#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300157
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300158#endif /* defined CYW20829 */
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200159
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300160static bool do_boot(struct boot_rsp *rsp)
161{
162 uintptr_t flash_base = 0;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200163
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300164#if defined CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300165 uint32_t *key = NULL;
166 uint32_t *iv = NULL;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300167#endif /* defined CYW20829 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300168
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300169 if ((rsp != NULL) && (rsp->br_hdr != NULL)) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300170
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200171 if (flash_device_base(rsp->br_flash_dev_id, &flash_base) == 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300172 fih_uint app_addr = calc_app_addr(flash_base, rsp);
173
174 BOOT_LOG_INF("Starting User Application (wait)...");
175 if (IS_ENCRYPTED(rsp->br_hdr)) {
176 BOOT_LOG_DBG(" * User application is encrypted");
177 }
178 BOOT_LOG_INF("Start slot Address: 0x%08" PRIx32, (uint32_t)fih_uint_decode(app_addr));
179
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200180 if (flash_device_base(rsp->br_flash_dev_id, &flash_base) != 0) {
181 return false;
182 }
183
184 if (fih_uint_eq(calc_app_addr(flash_base, rsp), app_addr) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300185 return false;
186 }
187
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300188#if defined CYW20829
189
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300190#ifdef MCUBOOT_ENC_IMAGES_XIP
191 if (IS_ENCRYPTED(rsp->br_hdr)) {
192 key = rsp->xip_key;
193 iv = rsp->xip_iv;
194 } else {
195 BOOT_LOG_ERR("User image is not encrypted, while MCUBootApp is compiled with encryption support.");
196 return false;
197 }
198#endif /* MCUBOOT_ENC_IMAGES_XIP */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000199
200
201#ifdef APP_CM33
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300202 /* This function does not return */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000203 BOOT_LOG_INF("Launching app on CM33 core");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300204 BOOT_LOG_INF(BOOT_MSG_FINISH);
205 hw_deinit();
206 cyw20829_launch_app(app_addr, key, iv);
207#else
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000208#error "Application should run on Cortex-M33"
209#endif /* APP_CM33 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300210
Roman Okhrimenko25165622023-05-23 08:58:29 +0300211#else /* defined CYW20829 */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000212
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300213#ifdef USE_XIP
214 BOOT_LOG_DBG("XIP: Switch to SMIF XIP mode");
215 qspi_set_mode(CY_SMIF_MEMORY);
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000216#endif /* USE_XIP */
217
218#ifdef APP_CM4
219 /* This function turns on CM4 and returns */
220 BOOT_LOG_INF("Launching app on CM4 core");
221 BOOT_LOG_INF(BOOT_MSG_FINISH);
222 hw_deinit();
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300223#ifdef BOOT_CM0P
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300224 Cy_SysEnableCM4(fih_uint_decode(app_addr));
225 return true;
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000226#else
227 psoc6_launch_cm4_app(app_addr);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300228#endif /* BOOT_CM0P */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000229
230#elif defined APP_CM0P
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300231#ifdef BOOT_CM0P
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000232 /* This function does not return */
233 BOOT_LOG_INF("Launching app on CM0P core");
234 BOOT_LOG_INF(BOOT_MSG_FINISH);
235 hw_deinit();
236 psoc6_launch_cm0p_app(app_addr);
237#else
238#error "Application should run on Cortex-M4"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300239#endif /* BOOT_CM0P */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000240
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300241#elif defined APP_CM7
242 /* This function does not return */
243 BOOT_LOG_INF("Launching app on CM7 core");
244 BOOT_LOG_INF(BOOT_MSG_FINISH);
245 hw_deinit();
246 xmc7000_launch_cm7_app(app_addr);
247 return true;
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000248#else
249#error "Application should run on either Cortex-M0+ or Cortex-M4"
250#endif /* APP_CM4 */
251
252#endif /* defined CYW20829 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300253 } else {
254 BOOT_LOG_ERR("Flash device ID not found");
255 return false;
256 }
257 }
258
259 return false;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200260}
261
Roman Okhrimenko409941a2023-11-20 02:18:09 +0200262static void DeepSleep_Prepare(void)
263{
264 static cy_stc_syspm_callback_params_t syspmSleepAppParams;
265 static cy_stc_syspm_callback_t syspmAppSleepCallbackHandler =
266 {
267 Cy_SCB_UART_DeepSleepCallback, CY_SYSPM_DEEPSLEEP, 0u, &syspmSleepAppParams,
268 NULL, NULL, 0};
269
270 syspmSleepAppParams.base = cy_retarget_io_uart_obj.base;
271 syspmSleepAppParams.context = (void *)&(cy_retarget_io_uart_obj.context);
272
273 if (!Cy_SysPm_RegisterCallback(&syspmAppSleepCallbackHandler)) {
274 BOOT_LOG_ERR("Failed to register syspmAppSleepCallbackHandler");
275 CY_ASSERT(false);
276 }
277}
278
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200279int main(void)
280{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300281 struct boot_rsp rsp = {};
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300282 bool boot_succeeded = false;
Tamas Ban4e8d8382020-09-30 08:01:58 +0100283 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300284 cy_rslt_t rc = cybsp_init();
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200285
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300286
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300287 if (rc != CY_RSLT_SUCCESS) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200288 CY_ASSERT((bool)0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300289 /* Loop forever... */
290 while (true) {
291 __WFI();
292 }
293 }
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000294
295#ifdef USE_EXEC_TIME_CHECK
296 timebase_us_init();
297#endif /* USE_EXEC_TIME_CHECK */
298
299#ifdef USE_LOG_TIMESTAMP
300 log_timestamp_init();
301#endif /* USE_LOG_TIMESTAMP */
302
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300303 /* enable interrupts */
304 __enable_irq();
dmiv8672c8e2020-09-16 12:59:20 +0300305
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300306 /* Certain PSoC 6 devices enable CM4 by default at startup. It must be
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200307 * either disabled or enabled & running a valid application for flash write
308 * to work from CM0+. Since flash write may happen in boot_go() for updating
309 * the image before this bootloader app can enable CM4 in do_boot(), we need
310 * to keep CM4 disabled. Note that debugging of CM4 is not supported when it
311 * is disabled.
312 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300313#if defined(CY_DEVICE_PSOC6ABLE2) && !defined(BOOT_CM4)
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300314 if (CY_SYS_CM4_STATUS_ENABLED == Cy_SysGetCM4Status()) {
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200315 Cy_SysDisableCM4();
316 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300317#endif /* defined(CY_DEVICE_PSOC6ABLE2) && !defined(BOOT_CM4) */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300318 /* Initialize retarget-io to use the debug UART port */
319 rc = cy_retarget_io_init(CYBSP_DEBUG_UART_TX,
320 CYBSP_DEBUG_UART_RX,
321 CY_RETARGET_IO_BAUDRATE);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300322
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300323 if (rc != CY_RSLT_SUCCESS) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200324 CY_ASSERT((bool)0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300325 /* Loop forever... */
326 while (true) {
327 __WFI();
328 }
dmiv8672c8e2020-09-16 12:59:20 +0300329 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200330
331 BOOT_LOG_INF("MCUBoot Bootloader Started");
332
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500333#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300334 {
335 cy_en_smif_status_t qspi_status = qspi_init_sfdp(SMIF_ID);
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300336
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300337 if (CY_SMIF_SUCCESS == qspi_status) {
338 rc = CY_RSLT_SUCCESS;
339 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
340 } else {
341 rc = MCUBOOTAPP_RSLT_ERR;
342 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%08" PRIx32, (uint32_t)qspi_status);
343 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500344 }
Tamas Ban4e8d8382020-09-30 08:01:58 +0100345
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300346 if (CY_RSLT_SUCCESS == rc)
347#endif /* CY_BOOT_USE_EXTERNAL_FLASH */
348 {
349#if defined(CYW20829) && defined(MCUBOOT_HW_ROLLBACK_PROT)
350 /* Check service application completion status */
351 if (check_service_app_status() != 0) {
352 BOOT_LOG_ERR("Service application failed");
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200353 CY_ASSERT((bool)0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300354 /* Loop forever... */
355 while (true) {
356 __WFI();
357 }
358 }
359#endif /* CYW20829 && MCUBOOT_HW_ROLLBACK_PROT */
360
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000361#ifdef USE_EXEC_TIME_CHECK
362 {
363 uint32_t exec_time;
364 EXEC_TIME_CHECK_BEGIN(&exec_time);
365#endif /* USE_EXEC_TIME_CHECK */
366 FIH_CALL(boot_go, fih_rc, &rsp);
367#ifdef USE_EXEC_TIME_CHECK
368 EXEC_TIME_CHECK_END();
369 BOOT_LOG_INF("Exec time: %" PRIu32 " [ms]", exec_time / 1000U);
370 }
371#endif /* USE_EXEC_TIME_CHECK */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300372 if (FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) {
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500373 BOOT_LOG_INF("User Application validated successfully");
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300374 /* initialize watchdog timer. it should be updated from user app
375 * to mark successful start up of this app. if the watchdog is not updated,
376 * reset will be initiated by watchdog timer and swap revert operation started
377 * to roll back to operable image.
378 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300379 cyhal_wdt_t *wdt = NULL;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300380
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300381 rc = cyhal_wdt_init(wdt, WDT_TIME_OUT_MS);
382
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300383 if (CY_RSLT_SUCCESS == rc) {
384
385 boot_succeeded = do_boot(&rsp);
386
387 if (!boot_succeeded) {
388 BOOT_LOG_ERR("Boot of next app failed");
389 }
390 } else {
391 BOOT_LOG_ERR("Failed to init WDT");
392 }
393 } else {
394 BOOT_LOG_ERR("MCUBoot Bootloader found none of bootable images");
dmiv8672c8e2020-09-16 12:59:20 +0300395 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500396 }
dmiv8672c8e2020-09-16 12:59:20 +0300397
Roman Okhrimenko409941a2023-11-20 02:18:09 +0200398 DeepSleep_Prepare();
399
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300400 while (true) {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300401 if (boot_succeeded) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300402 (void)Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
403 } else {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300404 __WFI();
405 }
dmiv8672c8e2020-09-16 12:59:20 +0300406 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200407}
dmiv8672c8e2020-09-16 12:59:20 +0300408
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300409static void hw_deinit(void)
dmiv8672c8e2020-09-16 12:59:20 +0300410{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300411#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) && !defined(USE_XIP)
412 qspi_deinit(SMIF_ID);
413#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300414
415 /* Flush the TX buffer, need to be fixed in retarget_io */
416 while(cy_retarget_io_is_tx_active()){}
417 cy_retarget_io_deinit();
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000418
419#ifdef USE_EXEC_TIME_CHECK
420 timebase_us_deinit();
421#endif /* USE_EXEC_TIME_CHECK */
422
423#ifdef USE_LOG_TIMESTAMP
424 log_timestamp_deinit();
425#endif /* USE_LOG_TIMESTAMP */
dmiv8672c8e2020-09-16 12:59:20 +0300426}