blob: b54e7e50b261f4146854b24849aa4cd5892d0c80 [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 Okhrimenko89ecdac2020-02-28 17:05:55 +020025
Roman Okhrimenko977b3752022-03-31 14:40:48 +030026#ifdef CYW20829
27#include "cy_retarget_io.h"
28#include "cybsp.h"
29#include "cyhal_wdt.h"
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000030#include "cyw_platform_utils.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030031#include "cy_service_app.h"
32#else
33#include "cy_retarget_io_pdl.h"
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050034#include "cycfg_clocks.h"
35#include "cycfg_peripherals.h"
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000036#if defined APP_CM0P || defined CM4
37#include "cyw_platform_utils.h"
38#endif /* defined APP_CM0P || defined CM4 */
39#endif /* defined CYW20829 || defined EXPLORER */
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050040
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000041#if defined(CY_BOOT_USE_EXTERNAL_FLASH) || defined(CYW20829)
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -050042#include "flash_qspi.h"
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000043#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) || defined(CYW20829) */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030044
45#include "cycfg_pins.h"
46#include "cy_result.h"
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020047#include "sysflash/sysflash.h"
48#include "flash_map_backend/flash_map_backend.h"
49
50#include "bootutil/image.h"
51#include "bootutil/bootutil.h"
52#include "bootutil/sign_key.h"
53
54#include "bootutil/bootutil_log.h"
55
Tamas Ban4e8d8382020-09-30 08:01:58 +010056#include "bootutil/fault_injection_hardening.h"
Tamas Ban4e8d8382020-09-30 08:01:58 +010057
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030058#include "watchdog.h"
59
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +000060#ifdef USE_EXEC_TIME_CHECK
61#include "misc/timebase_us.h"
62#include "misc/exec_time_check.h"
63#endif /* USE_EXEC_TIME_CHECK */
64
65#ifdef USE_LOG_TIMESTAMP
66#include "timestamp.h"
67#endif /* USE_LOG_TIMESTAMP */
68
Roman Okhrimenko977b3752022-03-31 14:40:48 +030069#define CY_RSLT_MODULE_MCUBOOTAPP 0x500U
70#define CY_RSLT_MODULE_MCUBOOTAPP_MAIN 0x51U
71
72/** General module error */
73#define MCUBOOTAPP_RSLT_ERR \
74 (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MCUBOOTAPP, CY_RSLT_MODULE_MCUBOOTAPP_MAIN, 0))
75
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +030076/* WDT time out for reset mode, in milliseconds. */
77#define WDT_TIME_OUT_MS 4000
78
dmiv8672c8e2020-09-16 12:59:20 +030079#ifdef CY_BOOT_USE_EXTERNAL_FLASH
80/* Choose SMIF slot number (slave select).
81 * Acceptable values are:
82 * 0 - SMIF disabled (no external memory);
83 * 1, 2, 3 or 4 - slave select line memory module is connected to.
84 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030085#define SMIF_ID (1U) /* Assume SlaveSelect_0 is used for External Memory */
86#endif /* CY_BOOT_USE_EXTERNAL_FLASH */
dmiv8672c8e2020-09-16 12:59:20 +030087
Roman Okhrimenko977b3752022-03-31 14:40:48 +030088#define BOOT_MSG_FINISH "MCUBoot Bootloader finished.\n" \
89 "Deinitializing hardware..."
dmiv8672c8e2020-09-16 12:59:20 +030090
Roman Okhrimenko977b3752022-03-31 14:40:48 +030091static void hw_deinit(void);
dmiv8672c8e2020-09-16 12:59:20 +030092
Roman Okhrimenko977b3752022-03-31 14:40:48 +030093static inline __attribute__((always_inline))
94fih_uint calc_app_addr(uintptr_t flash_base, const struct boot_rsp *rsp)
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +020095{
Roman Okhrimenko977b3752022-03-31 14:40:48 +030096 return fih_uint_encode(flash_base +
97 rsp->br_image_off +
98 rsp->br_hdr->ih_hdr_size);
99}
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200100
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300101#ifdef CYW20829
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200102
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300103#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
104CY_RAMFUNC_BEGIN /* SMIF will be deinitialized in this case! */
105#else
106inline __attribute__((always_inline))
107#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
108__NO_RETURN
109static void cyw20829_launch_app(fih_uint app_addr, uint32_t *key, uint32_t *iv)
110{
111#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
112 qspi_deinit(SMIF_ID);
113#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 +0000114 platform_RunNextApp(app_addr, key, iv);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300115}
116#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP)
117CY_RAMFUNC_END /* SMIF will be deinitialized in this case! */
118#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300119
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300120#endif /* CYW20829 */
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200121
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300122static bool do_boot(struct boot_rsp *rsp)
123{
124 uintptr_t flash_base = 0;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200125
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300126#ifdef CYW20829
127 uint32_t *key = NULL;
128 uint32_t *iv = NULL;
129#endif /* CYW20829 */
130
131 if (rsp != NULL) {
132 int rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
133
134 if (0 == rc) {
135 fih_uint app_addr = calc_app_addr(flash_base, rsp);
136
137 BOOT_LOG_INF("Starting User Application (wait)...");
138 if (IS_ENCRYPTED(rsp->br_hdr)) {
139 BOOT_LOG_DBG(" * User application is encrypted");
140 }
141 BOOT_LOG_INF("Start slot Address: 0x%08" PRIx32, (uint32_t)fih_uint_decode(app_addr));
142
143 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
144 if ((rc != 0) || fih_uint_not_eq(calc_app_addr(flash_base, rsp), app_addr)) {
145 return false;
146 }
147
148#ifdef CYW20829
149#ifdef MCUBOOT_ENC_IMAGES_XIP
150 if (IS_ENCRYPTED(rsp->br_hdr)) {
151 key = rsp->xip_key;
152 iv = rsp->xip_iv;
153 } else {
154 BOOT_LOG_ERR("User image is not encrypted, while MCUBootApp is compiled with encryption support.");
155 return false;
156 }
157#endif /* MCUBOOT_ENC_IMAGES_XIP */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000158
159
160#ifdef APP_CM33
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300161 /* This function does not return */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000162 BOOT_LOG_INF("Launching app on CM33 core");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300163 BOOT_LOG_INF(BOOT_MSG_FINISH);
164 hw_deinit();
165 cyw20829_launch_app(app_addr, key, iv);
166#else
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000167#error "Application should run on Cortex-M33"
168#endif /* APP_CM33 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300169
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000170#else /* defined CYW20829 || defined EXPLORER */
171
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300172#ifdef USE_XIP
173 BOOT_LOG_DBG("XIP: Switch to SMIF XIP mode");
174 qspi_set_mode(CY_SMIF_MEMORY);
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000175#endif /* USE_XIP */
176
177#ifdef APP_CM4
178 /* This function turns on CM4 and returns */
179 BOOT_LOG_INF("Launching app on CM4 core");
180 BOOT_LOG_INF(BOOT_MSG_FINISH);
181 hw_deinit();
182#ifdef CM0P
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300183 Cy_SysEnableCM4(fih_uint_decode(app_addr));
184 return true;
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000185#else
186 psoc6_launch_cm4_app(app_addr);
187#endif /* CM0P */
188
189#elif defined APP_CM0P
190#ifdef CM0P
191 /* This function does not return */
192 BOOT_LOG_INF("Launching app on CM0P core");
193 BOOT_LOG_INF(BOOT_MSG_FINISH);
194 hw_deinit();
195 psoc6_launch_cm0p_app(app_addr);
196#else
197#error "Application should run on Cortex-M4"
198#endif /* CM0P */
199
200#else
201#error "Application should run on either Cortex-M0+ or Cortex-M4"
202#endif /* APP_CM4 */
203
204#endif /* defined CYW20829 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300205 } else {
206 BOOT_LOG_ERR("Flash device ID not found");
207 return false;
208 }
209 }
210
211 return false;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200212}
213
214int main(void)
215{
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300216 struct boot_rsp rsp;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300217 cy_rslt_t rc = MCUBOOTAPP_RSLT_ERR;
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300218 bool boot_succeeded = false;
Tamas Ban4e8d8382020-09-30 08:01:58 +0100219 fih_int fih_rc = FIH_FAILURE;
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200220
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300221#ifdef CYW20829
222 rc = cybsp_init();
223 if (rc != CY_RSLT_SUCCESS) {
224 CY_ASSERT((bool)0);
225 /* Loop forever... */
226 while (true) {
227 __WFI();
228 }
229 }
230#else
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200231 SystemInit();
Bohdan Kovalchuk77256522020-04-15 18:03:43 +0300232 init_cycfg_peripherals();
233 init_cycfg_pins();
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300234#endif /* CYW20829 */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000235
236#ifdef USE_EXEC_TIME_CHECK
237 timebase_us_init();
238#endif /* USE_EXEC_TIME_CHECK */
239
240#ifdef USE_LOG_TIMESTAMP
241 log_timestamp_init();
242#endif /* USE_LOG_TIMESTAMP */
243
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300244 /* enable interrupts */
245 __enable_irq();
dmiv8672c8e2020-09-16 12:59:20 +0300246
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300247 /* Certain PSoC 6 devices enable CM4 by default at startup. It must be
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200248 * either disabled or enabled & running a valid application for flash write
249 * to work from CM0+. Since flash write may happen in boot_go() for updating
250 * the image before this bootloader app can enable CM4 in do_boot(), we need
251 * to keep CM4 disabled. Note that debugging of CM4 is not supported when it
252 * is disabled.
253 */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000254#if !defined CYW20829
255#if defined(CY_DEVICE_PSOC6ABLE2) && !defined(CM4)
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300256 if (CY_SYS_CM4_STATUS_ENABLED == Cy_SysGetCM4Status()) {
Roman Okhrimenko4bc28102021-02-01 19:31:41 +0200257 Cy_SysDisableCM4();
258 }
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000259#endif /* defined(CY_DEVICE_PSOC6ABLE2) && !defined(CM4) */
Bohdan Kovalchuk77256522020-04-15 18:03:43 +0300260 /* Initialize retarget-io to use the debug UART port (CYBSP_UART_HW) */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300261 rc = cy_retarget_io_pdl_init(CY_RETARGET_IO_BAUDRATE);
262#else
263 /* Initialize retarget-io to use the debug UART port */
264 rc = cy_retarget_io_init(CYBSP_DEBUG_UART_TX,
265 CYBSP_DEBUG_UART_RX,
266 CY_RETARGET_IO_BAUDRATE);
267#endif /* CYW20829 */
268 if (rc != CY_RSLT_SUCCESS) {
269 CY_ASSERT((bool)0);
270 /* Loop forever... */
271 while (true) {
272 __WFI();
273 }
dmiv8672c8e2020-09-16 12:59:20 +0300274 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200275
276 BOOT_LOG_INF("MCUBoot Bootloader Started");
277
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500278#ifdef CY_BOOT_USE_EXTERNAL_FLASH
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300279 {
280 cy_en_smif_status_t qspi_status = qspi_init_sfdp(SMIF_ID);
Bohdan Kovalchuka333a452020-07-09 16:55:58 +0300281
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300282 if (CY_SMIF_SUCCESS == qspi_status) {
283 rc = CY_RSLT_SUCCESS;
284 BOOT_LOG_INF("External Memory initialized w/ SFDP.");
285 } else {
286 rc = MCUBOOTAPP_RSLT_ERR;
287 BOOT_LOG_ERR("External Memory initialization w/ SFDP FAILED: 0x%08" PRIx32, (uint32_t)qspi_status);
288 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500289 }
Tamas Ban4e8d8382020-09-30 08:01:58 +0100290
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300291 if (CY_RSLT_SUCCESS == rc)
292#endif /* CY_BOOT_USE_EXTERNAL_FLASH */
293 {
294#if defined(CYW20829) && defined(MCUBOOT_HW_ROLLBACK_PROT)
295 /* Check service application completion status */
296 if (check_service_app_status() != 0) {
297 BOOT_LOG_ERR("Service application failed");
298 CY_ASSERT((bool)0);
299 /* Loop forever... */
300 while (true) {
301 __WFI();
302 }
303 }
304#endif /* CYW20829 && MCUBOOT_HW_ROLLBACK_PROT */
305
306 (void)memset(&rsp, 0, sizeof(rsp));
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000307#ifdef USE_EXEC_TIME_CHECK
308 {
309 uint32_t exec_time;
310 EXEC_TIME_CHECK_BEGIN(&exec_time);
311#endif /* USE_EXEC_TIME_CHECK */
312 FIH_CALL(boot_go, fih_rc, &rsp);
313#ifdef USE_EXEC_TIME_CHECK
314 EXEC_TIME_CHECK_END();
315 BOOT_LOG_INF("Exec time: %" PRIu32 " [ms]", exec_time / 1000U);
316 }
317#endif /* USE_EXEC_TIME_CHECK */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300318 if (true == fih_eq(fih_rc, FIH_SUCCESS)) {
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500319 BOOT_LOG_INF("User Application validated successfully");
Roman Okhrimenko0c7aebc2020-09-02 13:37:51 +0300320 /* initialize watchdog timer. it should be updated from user app
321 * to mark successful start up of this app. if the watchdog is not updated,
322 * reset will be initiated by watchdog timer and swap revert operation started
323 * to roll back to operable image.
324 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300325#ifdef CYW20829
326 cyhal_wdt_t *cyw20829_wdt = NULL;
327
328 rc = cyhal_wdt_init(cyw20829_wdt, WDT_TIME_OUT_MS);
329#else
330 rc = cy_wdg_init(WDT_TIME_OUT_MS);
331#endif /* CYW20829 */
332 if (CY_RSLT_SUCCESS == rc) {
333
334 boot_succeeded = do_boot(&rsp);
335
336 if (!boot_succeeded) {
337 BOOT_LOG_ERR("Boot of next app failed");
338 }
339 } else {
340 BOOT_LOG_ERR("Failed to init WDT");
341 }
342 } else {
343 BOOT_LOG_ERR("MCUBoot Bootloader found none of bootable images");
dmiv8672c8e2020-09-16 12:59:20 +0300344 }
Bohdan Kovalchuk0324f1b2020-05-26 08:04:24 -0500345 }
dmiv8672c8e2020-09-16 12:59:20 +0300346
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300347 while (true) {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300348 if (boot_succeeded) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300349 (void)Cy_SysPm_CpuEnterDeepSleep(CY_SYSPM_WAIT_FOR_INTERRUPT);
350 } else {
Roman Okhrimenkoff026122020-09-23 12:58:07 +0300351 __WFI();
352 }
dmiv8672c8e2020-09-16 12:59:20 +0300353 }
Roman Okhrimenko89ecdac2020-02-28 17:05:55 +0200354}
dmiv8672c8e2020-09-16 12:59:20 +0300355
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300356static void hw_deinit(void)
dmiv8672c8e2020-09-16 12:59:20 +0300357{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300358#ifdef CYW20829
359 /* Flush the TX buffer, need to be fixed in retarget_io */
360 Cy_SysLib_Delay(50);
361
362 cy_retarget_io_deinit();
363 cy_wdg_stop();
364 cy_wdg_free();
365 /* Note: qspi_deinit() is called (if needed) in cyw20829_launch_app() above */
366#else
367 cy_retarget_io_wait_tx_complete(CYBSP_UART_HW, 10);
dmiv8672c8e2020-09-16 12:59:20 +0300368 cy_retarget_io_pdl_deinit();
369 Cy_GPIO_Port_Deinit(CYBSP_UART_RX_PORT);
370 Cy_GPIO_Port_Deinit(CYBSP_UART_TX_PORT);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300371#if defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) && !defined(USE_XIP)
372 qspi_deinit(SMIF_ID);
373#endif /* defined(CY_BOOT_USE_EXTERNAL_FLASH) && !defined(MCUBOOT_ENC_IMAGES_XIP) */
374#endif /* CYW20829 */
Dovhal Artem (CSUKR CSS ICW SW FW 1)f7a3d1b2022-04-01 15:07:37 +0000375
376#ifdef USE_EXEC_TIME_CHECK
377 timebase_us_deinit();
378#endif /* USE_EXEC_TIME_CHECK */
379
380#ifdef USE_LOG_TIMESTAMP
381 log_timestamp_deinit();
382#endif /* USE_LOG_TIMESTAMP */
dmiv8672c8e2020-09-16 12:59:20 +0300383}