1 /*
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 "platform/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 
44 extern "C" {
default_CSPRNG(uint8_t * dest,unsigned int size)45 int default_CSPRNG(uint8_t *dest, unsigned int size) { return 0; }
46 }
47 #endif
48 
main()49 int main()
50 {
51     int rc;
52 
53 #ifdef MCUBOOT_HAVE_LOGGING
54     mbed_trace_init();
55 #if MCUBOOT_LOG_BOOTLOADER_ONLY
56     mbed_trace_include_filters_set("MCUb,BL");
57 #endif //MCUBOOT_LOG_BOOTLOADER_ONLY
58 #endif //MCUBOOT_HAVE_LOGGING
59 
60     tr_info("Starting MCUboot");
61 
62 #if (MCUBOOT_CRYPTO_BACKEND == MBEDTLS)
63     // Initialize mbedtls crypto for use by MCUboot
64     mbedtls_platform_context unused_ctx;
65     rc = mbedtls_platform_setup(&unused_ctx);
66     if(rc != 0) {
67         tr_error("Failed to setup Mbed TLS, error: %d", rc);
68         exit(rc);
69     }
70 #elif (MCUBOOT_CRYPTO_BACKEND == TINYCRYPT)
71     uECC_set_rng(0);
72 #endif
73 
74     struct boot_rsp rsp;
75     rc = boot_go(&rsp);
76     if(rc != 0) {
77         tr_error("Failed to locate firmware image, error: %d", rc);
78         exit(rc);
79     }
80 
81     uint32_t address = rsp.br_image_off + rsp.br_hdr->ih_hdr_size;
82 
83     // Workaround: The extra \n ensures the last trace gets flushed
84     // before mbed_start_application() destroys the stack and jumps
85     // to the application
86     tr_info("Booting firmware image at 0x%x\n", address);
87 
88     // Run the application in the primary slot
89     // Add header size offset to calculate the actual start address of application
90     mbed_start_application(address);
91 }
92 
93 #endif // MCUBOOT_BOOTLOADER_BUILD
94