blob: 3c31bdf499bb6373783228d0031b7821dd3fc118 [file] [log] [blame]
George Becksteind82afbf2020-10-29 17:32:11 -04001/*
2 * Copyright (c) 2020 Embedded Planet
3 * Copyright (c) 2020 ARM Limited
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
17 */
18
19#if MCUBOOT_BOOTLOADER_BUILD
20
21#include <stdlib.h>
Jamie Smithd3080f12024-12-04 00:44:09 -080022#include <cinttypes>
George Becksteind82afbf2020-10-29 17:32:11 -040023#include "bootutil/bootutil.h"
24#include "bootutil/image.h"
25#include "hal/serial_api.h"
Artur Tyneckia9101662022-02-08 10:32:32 +010026#include "platform/mbed_application.h"
George Becksteind82afbf2020-10-29 17:32:11 -040027
28#if (MCUBOOT_CRYPTO_BACKEND == MBEDTLS)
29#include "mbedtls/platform.h"
30#elif (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
31#include "tinycrypt/ecc.h"
32#endif
33
34#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
35#define TRACE_GROUP "BL"
36#include "mbed-trace/mbed_trace.h"
37
38#if (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
39/* XXX add this global definition for linking only
40 * TinyCrypt is used for signature verification and ECIES using secp256r1 and AES encryption;
41 * RNG is not required. So here we provide a stub.
42 * See https://github.com/mcu-tools/mcuboot/pull/791#discussion_r514480098
43 */
44
45extern "C" {
46int default_CSPRNG(uint8_t *dest, unsigned int size) { return 0; }
47}
48#endif
49
50int main()
51{
52 int rc;
53
Artur Tynecki41c568a2022-02-08 10:55:24 +010054#ifdef MCUBOOT_HAVE_LOGGING
George Becksteind82afbf2020-10-29 17:32:11 -040055 mbed_trace_init();
56#if MCUBOOT_LOG_BOOTLOADER_ONLY
57 mbed_trace_include_filters_set("MCUb,BL");
Artur Tynecki41c568a2022-02-08 10:55:24 +010058#endif //MCUBOOT_LOG_BOOTLOADER_ONLY
59#endif //MCUBOOT_HAVE_LOGGING
George Becksteind82afbf2020-10-29 17:32:11 -040060
61 tr_info("Starting MCUboot");
62
63#if (MCUBOOT_CRYPTO_BACKEND == MBEDTLS)
64 // Initialize mbedtls crypto for use by MCUboot
65 mbedtls_platform_context unused_ctx;
66 rc = mbedtls_platform_setup(&unused_ctx);
67 if(rc != 0) {
68 tr_error("Failed to setup Mbed TLS, error: %d", rc);
69 exit(rc);
70 }
71#elif (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
72 uECC_set_rng(0);
73#endif
74
75 struct boot_rsp rsp;
76 rc = boot_go(&rsp);
77 if(rc != 0) {
78 tr_error("Failed to locate firmware image, error: %d", rc);
79 exit(rc);
80 }
81
82 uint32_t address = rsp.br_image_off + rsp.br_hdr->ih_hdr_size;
83
84 // Workaround: The extra \n ensures the last trace gets flushed
85 // before mbed_start_application() destroys the stack and jumps
86 // to the application
Jamie Smithd3080f12024-12-04 00:44:09 -080087 tr_info("Booting firmware image at 0x%" PRIx32 "\n", address);
George Becksteind82afbf2020-10-29 17:32:11 -040088
89 // Run the application in the primary slot
90 // Add header size offset to calculate the actual start address of application
91 mbed_start_application(address);
92}
93
94#endif // MCUBOOT_BOOTLOADER_BUILD