blob: c5f413789c00c655437526ad6903c41bb2186fe4 [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>
22#include "bootutil/bootutil.h"
23#include "bootutil/image.h"
24#include "hal/serial_api.h"
25#include "mbed_application.h"
26
27#if (MCUBOOT_CRYPTO_BACKEND == MBEDTLS)
28#include "mbedtls/platform.h"
29#elif (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
30#include "tinycrypt/ecc.h"
31#endif
32
33#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
34#define TRACE_GROUP "BL"
35#include "mbed-trace/mbed_trace.h"
36
37#if (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
38/* XXX add this global definition for linking only
39 * TinyCrypt is used for signature verification and ECIES using secp256r1 and AES encryption;
40 * RNG is not required. So here we provide a stub.
41 * See https://github.com/mcu-tools/mcuboot/pull/791#discussion_r514480098
42 */
43
44extern "C" {
45int default_CSPRNG(uint8_t *dest, unsigned int size) { return 0; }
46}
47#endif
48
49int main()
50{
51 int rc;
52
53 mbed_trace_init();
54#if MCUBOOT_LOG_BOOTLOADER_ONLY
55 mbed_trace_include_filters_set("MCUb,BL");
56#endif
57
58 tr_info("Starting MCUboot");
59
60#if (MCUBOOT_CRYPTO_BACKEND == MBEDTLS)
61 // Initialize mbedtls crypto for use by MCUboot
62 mbedtls_platform_context unused_ctx;
63 rc = mbedtls_platform_setup(&unused_ctx);
64 if(rc != 0) {
65 tr_error("Failed to setup Mbed TLS, error: %d", rc);
66 exit(rc);
67 }
68#elif (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
69 uECC_set_rng(0);
70#endif
71
72 struct boot_rsp rsp;
73 rc = boot_go(&rsp);
74 if(rc != 0) {
75 tr_error("Failed to locate firmware image, error: %d", rc);
76 exit(rc);
77 }
78
79 uint32_t address = rsp.br_image_off + rsp.br_hdr->ih_hdr_size;
80
81 // Workaround: The extra \n ensures the last trace gets flushed
82 // before mbed_start_application() destroys the stack and jumps
83 // to the application
84 tr_info("Booting firmware image at 0x%x\n", address);
85
86 // Run the application in the primary slot
87 // Add header size offset to calculate the actual start address of application
88 mbed_start_application(address);
89}
90
91#endif // MCUBOOT_BOOTLOADER_BUILD