blob: b526d773f36d766da92803b32707979922ff1cbc [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
INFINEON\DovhalA3d3b51d2024-06-12 17:01:18 +030028#include "cyw_platform_utils.h"
29
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030030#if defined CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +030031#include "cy_service_app.h"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030032#endif
33
34#include "cybsp.h"
35#include "cy_retarget_io.h"
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050036
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000037#if defined(CY_BOOT_USE_EXTERNAL_FLASH) || defined(CYW20829)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050038#include "flash_qspi.h"
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000039#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) || defined(CYW20829) */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030040
41#include "cycfg_pins.h"
42#include "cy_result.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020043#include "sysflash/sysflash.h"
44#include "flash_map_backend/flash_map_backend.h"
45
46#include "bootutil/image.h"
47#include "bootutil/bootutil.h"
48#include "bootutil/sign_key.h"
49
50#include "bootutil/bootutil_log.h"
51
Tamas Ban4e8d8382020-09-30 08:01:58 +010052#include "bootutil/fault_injection_hardening.h"
Tamas Ban4e8d8382020-09-30 08:01:58 +010053
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000054#ifdef USE_EXEC_TIME_CHECK
55#include "misc/timebase_us.h"
56#include "misc/exec_time_check.h"
57#endif /* USE_EXEC_TIME_CHECK */
58
59#ifdef USE_LOG_TIMESTAMP
60#include "timestamp.h"
61#endif /* USE_LOG_TIMESTAMP */
62
Roman Okhrimenko977b3752022-03-31 14:40:48 +030063#define CY_RSLT_MODULE_MCUBOOTAPP 0x500U
64#define CY_RSLT_MODULE_MCUBOOTAPP_MAIN 0x51U
65
66/** General module error */
67#define MCUBOOTAPP_RSLT_ERR \
68 (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MCUBOOTAPP, CY_RSLT_MODULE_MCUBOOTAPP_MAIN, 0))
69
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030070/* WDT time out for reset mode, in milliseconds. */
71#define WDT_TIME_OUT_MS 4000
72
dmiv8672c8e2020-09-16 12:59:20 +030073#ifdef CY_BOOT_USE_EXTERNAL_FLASH
74/* Choose SMIF slot number (slave select).
75 * Acceptable values are:
76 * 0 - SMIF disabled (no external memory);
77 * 1, 2, 3 or 4 - slave select line memory module is connected to.
78 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030079#define SMIF_ID (1U) /* Assume SlaveSelect_0 is used for External Memory */
80#endif /* CY_BOOT_USE_EXTERNAL_FLASH */
dmiv8672c8e2020-09-16 12:59:20 +030081
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030082#define BOOT_MSG_FINISH "MCUBoot Bootloader finished.\r\n" \
Roman Okhrimenko977b3752022-03-31 14:40:48 +030083 "Deinitializing hardware..."
dmiv8672c8e2020-09-16 12:59:20 +030084
Roman Okhrimenko977b3752022-03-31 14:40:48 +030085static void hw_deinit(void);
dmiv8672c8e2020-09-16 12:59:20 +030086
Roman Okhrimenko977b3752022-03-31 14:40:48 +030087static inline __attribute__((always_inline))
88fih_uint calc_app_addr(uintptr_t flash_base, const struct boot_rsp *rsp)
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020089{
Roman Okhrimenko977b3752022-03-31 14:40:48 +030090 return fih_uint_encode(flash_base +
91 rsp->br_image_off +
92 rsp->br_hdr->ih_hdr_size);
93}
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020094
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +020095/*******************************************************************************
96 * Function Name: fih_calc_app_addr
97 ********************************************************************************
98 * Summary:
99 * Calculate start address of user application.
100 *
101 * Parameters:
102 * image_base - base address of flash;
103 *
104 * rsp - provided by the boot loader code; indicates where to jump
105 * to execute the main image;
106 *
107 * output - calculated address of application;
108 *
109 * Return:
110 * fih_int
111 *
112 *******************************************************************************/
113static inline __attribute__((always_inline)) fih_int fih_calc_app_addr(
114 uintptr_t image_base, const struct boot_rsp *rsp, fih_uint *app_address)
115{
116 fih_int fih_rc = FIH_FAILURE;
117
118#if defined(MCUBOOT_RAM_LOAD)
119 if (IS_RAM_BOOTABLE(rsp->br_hdr) == true) {
120 if ((UINT32_MAX - rsp->br_hdr->ih_hdr_size) >= image_base) {
121 *app_address =
122 fih_uint_encode(image_base + rsp->br_hdr->ih_hdr_size);
123 fih_rc = FIH_SUCCESS;
124 }
125 } else
126#endif
127 {
128 if (((UINT32_MAX - rsp->br_image_off) >= image_base) &&
129 ((UINT32_MAX - rsp->br_hdr->ih_hdr_size) >=
130 (image_base + rsp->br_image_off))) {
131 *app_address = fih_uint_encode(image_base + rsp->br_image_off +
132 rsp->br_hdr->ih_hdr_size);
133 fih_rc = FIH_SUCCESS;
134 }
135 }
136
137 FIH_RET(fih_rc);
138}
139
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300140#if defined CYW20829
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200141
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300142#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
143CY_RAMFUNC_BEGIN /* SMIF will be deinitialized in this case! */
144#else
145inline __attribute__((always_inline))
146#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
147__NO_RETURN
148static void cyw20829_launch_app(fih_uint app_addr, uint32_t *key, uint32_t *iv)
149{
150#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
151 qspi_deinit(SMIF_ID);
152#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 +0000153 platform_RunNextApp(app_addr, key, iv);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300154}
155#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
156CY_RAMFUNC_END /* SMIF will be deinitialized in this case! */
157#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300158
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300159#endif /* defined CYW20829 */
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200160
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300161static bool do_boot(struct boot_rsp *rsp)
162{
163 uintptr_t flash_base = 0;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200164
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300165#if defined CYW20829
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300166 uint32_t *key = NULL;
167 uint32_t *iv = NULL;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300168#endif /* defined CYW20829 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300169
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300170 if ((rsp != NULL) && (rsp->br_hdr != NULL)) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300171
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200172 if (flash_device_base(rsp->br_flash_dev_id, &flash_base) == 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300173 fih_uint app_addr = calc_app_addr(flash_base, rsp);
174
175 BOOT_LOG_INF("Starting User Application (wait)...");
176 if (IS_ENCRYPTED(rsp->br_hdr)) {
177 BOOT_LOG_DBG(" * User application is encrypted");
178 }
179 BOOT_LOG_INF("Start slot Address: 0x%08" PRIx32, (uint32_t)fih_uint_decode(app_addr));
180
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200181 if (flash_device_base(rsp->br_flash_dev_id, &flash_base) != 0) {
182 return false;
183 }
184
185 if (fih_uint_eq(calc_app_addr(flash_base, rsp), app_addr) != FIH_TRUE) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300186 return false;
187 }
188
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300189#if defined CYW20829
190
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300191#ifdef MCUBOOT_ENC_IMAGES_XIP
192 if (IS_ENCRYPTED(rsp->br_hdr)) {
193 key = rsp->xip_key;
194 iv = rsp->xip_iv;
195 } else {
196 BOOT_LOG_ERR("User image is not encrypted, while MCUBootApp is compiled with encryption support.");
197 return false;
198 }
199#endif /* MCUBOOT_ENC_IMAGES_XIP */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000200
201
202#ifdef APP_CM33
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300203 /* This function does not return */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000204 BOOT_LOG_INF("Launching app on CM33 core");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300205 BOOT_LOG_INF(BOOT_MSG_FINISH);
206 hw_deinit();
207 cyw20829_launch_app(app_addr, key, iv);
208#else
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000209#error "Application should run on Cortex-M33"
210#endif /* APP_CM33 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300211
Roman Okhrimenko25165622023-05-23 08:58:29 +0300212#else /* defined CYW20829 */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000213
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300214#ifdef USE_XIP
215 BOOT_LOG_DBG("XIP: Switch to SMIF XIP mode");
216 qspi_set_mode(CY_SMIF_MEMORY);
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000217#endif /* USE_XIP */
218
219#ifdef APP_CM4
220 /* This function turns on CM4 and returns */
221 BOOT_LOG_INF("Launching app on CM4 core");
222 BOOT_LOG_INF(BOOT_MSG_FINISH);
223 hw_deinit();
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300224#ifdef BOOT_CM0P
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300225 Cy_SysEnableCM4(fih_uint_decode(app_addr));
226 return true;
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000227#else
228 psoc6_launch_cm4_app(app_addr);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300229#endif /* BOOT_CM0P */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000230
231#elif defined APP_CM0P
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300232#ifdef BOOT_CM0P
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000233 /* This function does not return */
234 BOOT_LOG_INF("Launching app on CM0P core");
235 BOOT_LOG_INF(BOOT_MSG_FINISH);
236 hw_deinit();
237 psoc6_launch_cm0p_app(app_addr);
238#else
239#error "Application should run on Cortex-M4"
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300240#endif /* BOOT_CM0P */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000241
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300242#elif defined APP_CM7
243 /* This function does not return */
244 BOOT_LOG_INF("Launching app on CM7 core");
245 BOOT_LOG_INF(BOOT_MSG_FINISH);
246 hw_deinit();
247 xmc7000_launch_cm7_app(app_addr);
248 return true;
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000249#else
250#error "Application should run on either Cortex-M0+ or Cortex-M4"
251#endif /* APP_CM4 */
252
253#endif /* defined CYW20829 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300254 } else {
255 BOOT_LOG_ERR("Flash device ID not found");
256 return false;
257 }
258 }
259
260 return false;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200261}
262
Roman Okhrimenko409941a2023-11-20 02:18:09 +0200263static void DeepSleep_Prepare(void)
264{
265 static cy_stc_syspm_callback_params_t syspmSleepAppParams;
266 static cy_stc_syspm_callback_t syspmAppSleepCallbackHandler =
267 {
268 Cy_SCB_UART_DeepSleepCallback, CY_SYSPM_DEEPSLEEP, 0u, &syspmSleepAppParams,
269 NULL, NULL, 0};
270
271 syspmSleepAppParams.base = cy_retarget_io_uart_obj.base;
272 syspmSleepAppParams.context = (void *)&(cy_retarget_io_uart_obj.context);
273
274 if (!Cy_SysPm_RegisterCallback(&syspmAppSleepCallbackHandler)) {
275 BOOT_LOG_ERR("Failed to register syspmAppSleepCallbackHandler");
276 CY_ASSERT(false);
277 }
278}
279
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200280int main(void)
281{
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300282 struct boot_rsp rsp = {};
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300283 bool boot_succeeded = false;
Tamas Ban4e8d8382020-09-30 08:01:58 +0100284 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300285 cy_rslt_t rc = cybsp_init();
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200286
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300287
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300288 if (rc != CY_RSLT_SUCCESS) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200289 CY_ASSERT((bool)0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300290 /* Loop forever... */
291 while (true) {
292 __WFI();
293 }
294 }
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000295
296#ifdef USE_EXEC_TIME_CHECK
297 timebase_us_init();
298#endif /* USE_EXEC_TIME_CHECK */
299
300#ifdef USE_LOG_TIMESTAMP
301 log_timestamp_init();
302#endif /* USE_LOG_TIMESTAMP */
303
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300304 /* enable interrupts */
305 __enable_irq();
dmiv8672c8e2020-09-16 12:59:20 +0300306
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300307 /* Certain PSoC 6 devices enable CM4 by default at startup. It must be
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200308 * either disabled or enabled & running a valid application for flash write
309 * to work from CM0+. Since flash write may happen in boot_go() for updating
310 * the image before this bootloader app can enable CM4 in do_boot(), we need
311 * to keep CM4 disabled. Note that debugging of CM4 is not supported when it
312 * is disabled.
313 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300314#if defined(CY_DEVICE_PSOC6ABLE2) && !defined(BOOT_CM4)
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300315 if (CY_SYS_CM4_STATUS_ENABLED == Cy_SysGetCM4Status()) {
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200316 Cy_SysDisableCM4();
317 }
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300318#endif /* defined(CY_DEVICE_PSOC6ABLE2) && !defined(BOOT_CM4) */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300319 /* Initialize retarget-io to use the debug UART port */
320 rc = cy_retarget_io_init(CYBSP_DEBUG_UART_TX,
321 CYBSP_DEBUG_UART_RX,
322 CY_RETARGET_IO_BAUDRATE);
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300323
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300324 if (rc != CY_RSLT_SUCCESS) {
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200325 CY_ASSERT((bool)0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300326 /* Loop forever... */
327 while (true) {
328 __WFI();
329 }
dmiv8672c8e2020-09-16 12:59:20 +0300330 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200331
332 BOOT_LOG_INF("MCUBoot Bootloader Started");
333
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500334#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300335 {
336 cy_en_smif_status_t qspi_status = qspi_init_sfdp(SMIF_ID);
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300337
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300338 if (CY_SMIF_SUCCESS == qspi_status) {
339 rc = CY_RSLT_SUCCESS;
340 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
341 } else {
342 rc = MCUBOOTAPP_RSLT_ERR;
343 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%08" PRIx32, (uint32_t)qspi_status);
344 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500345 }
Tamas Ban4e8d8382020-09-30 08:01:58 +0100346
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300347 if (CY_RSLT_SUCCESS == rc)
348#endif /* CY_BOOT_USE_EXTERNAL_FLASH */
349 {
350#if defined(CYW20829) && defined(MCUBOOT_HW_ROLLBACK_PROT)
351 /* Check service application completion status */
352 if (check_service_app_status() != 0) {
353 BOOT_LOG_ERR("Service application failed");
Roman Okhrimenko883cb5b2024-03-28 17:22:33 +0200354 CY_ASSERT((bool)0);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300355 /* Loop forever... */
356 while (true) {
357 __WFI();
358 }
359 }
360#endif /* CYW20829 && MCUBOOT_HW_ROLLBACK_PROT */
361
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000362#ifdef USE_EXEC_TIME_CHECK
363 {
364 uint32_t exec_time;
365 EXEC_TIME_CHECK_BEGIN(&exec_time);
366#endif /* USE_EXEC_TIME_CHECK */
367 FIH_CALL(boot_go, fih_rc, &rsp);
368#ifdef USE_EXEC_TIME_CHECK
369 EXEC_TIME_CHECK_END();
370 BOOT_LOG_INF("Exec time: %" PRIu32 " [ms]", exec_time / 1000U);
371 }
372#endif /* USE_EXEC_TIME_CHECK */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300373 if (FIH_TRUE == fih_eq(fih_rc, FIH_SUCCESS)) {
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500374 BOOT_LOG_INF("User Application validated successfully");
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300375 /* initialize watchdog timer. it should be updated from user app
376 * to mark successful start up of this app. if the watchdog is not updated,
377 * reset will be initiated by watchdog timer and swap revert operation started
378 * to roll back to operable image.
379 */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300380 cyhal_wdt_t *wdt = NULL;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300381
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300382 rc = cyhal_wdt_init(wdt, WDT_TIME_OUT_MS);
383
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300384 if (CY_RSLT_SUCCESS == rc) {
385
386 boot_succeeded = do_boot(&rsp);
387
388 if (!boot_succeeded) {
389 BOOT_LOG_ERR("Boot of next app failed");
390 }
391 } else {
392 BOOT_LOG_ERR("Failed to init WDT");
393 }
394 } else {
395 BOOT_LOG_ERR("MCUBoot Bootloader found none of bootable images");
dmiv8672c8e2020-09-16 12:59:20 +0300396 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500397 }
dmiv8672c8e2020-09-16 12:59:20 +0300398
Roman Okhrimenko409941a2023-11-20 02:18:09 +0200399 DeepSleep_Prepare();
400
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300401 while (true) {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300402 if (boot_succeeded) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300403 (void)Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
404 } else {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300405 __WFI();
406 }
dmiv8672c8e2020-09-16 12:59:20 +0300407 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200408}
dmiv8672c8e2020-09-16 12:59:20 +0300409
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300410static void hw_deinit(void)
dmiv8672c8e2020-09-16 12:59:20 +0300411{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300412#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) && !defined(USE_XIP)
413 qspi_deinit(SMIF_ID);
414#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300415
416 /* Flush the TX buffer, need to be fixed in retarget_io */
417 while(cy_retarget_io_is_tx_active()){}
418 cy_retarget_io_deinit();
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000419
420#ifdef USE_EXEC_TIME_CHECK
421 timebase_us_deinit();
422#endif /* USE_EXEC_TIME_CHECK */
423
424#ifdef USE_LOG_TIMESTAMP
425 log_timestamp_deinit();
426#endif /* USE_LOG_TIMESTAMP */
dmiv8672c8e2020-09-16 12:59:20 +0300427}