Skip to content

Commit c986a90

Browse files
authored
Merge: Synchronized up to mcu-tools/mcuboot@e512181
merged by GitHub GUI #41 Signed-off-by: Andrzej Puzdrowski <[email protected]>
2 parents 6e3825f + cb5a7b3 commit c986a90

File tree

33 files changed

+541
-404
lines changed

33 files changed

+541
-404
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ install:
7070
script:
7171
- ./ci/${TEST}_run.sh
7272

73+
cache:
74+
directories:
75+
- docker
76+
7377
notifications:
7478
slack:
7579
rooms:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[travis]: https://travis-ci.org/mcu-tools/mcuboot
1717
[license]: https://github.com/mcu-tools/mcuboot/blob/master/LICENSE
1818

19-
This is mcuboot version 1.7.0-rc2
19+
This is mcuboot version 1.8.0-dev
2020

2121
MCUboot is a secure bootloader for 32-bit MCUs. The goal of MCUboot is to
2222
define a common infrastructure for the bootloader, system flash layout on

boot/bootutil/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#------------------------------------------------------------------------------
2+
# Copyright (c) 2020, Arm Limited. All rights reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
#------------------------------------------------------------------------------
7+
8+
add_library(bootutil STATIC)
9+
10+
target_include_directories(bootutil
11+
PUBLIC
12+
include
13+
PRIVATE
14+
src
15+
)
16+
17+
target_sources(bootutil
18+
PRIVATE
19+
src/boot_record.c
20+
src/bootutil_misc.c
21+
src/caps.c
22+
src/encrypted.c
23+
src/fault_injection_hardening.c
24+
src/fault_injection_hardening_delay_rng_mbedtls.c
25+
src/image_ec.c
26+
src/image_ec256.c
27+
src/image_ed25519.c
28+
src/image_rsa.c
29+
src/image_validate.c
30+
src/loader.c
31+
src/swap_misc.c
32+
src/swap_move.c
33+
src/swap_scratch.c
34+
src/tlv.c
35+
)

boot/bootutil/include/bootutil/crypto/aes_ctr.h

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,13 @@ static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const
6262
static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c)
6363
{
6464
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
65-
int rc;
66-
rc = mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
67-
memset(stream_block, 0, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
68-
return rc;
65+
return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
6966
}
7067

7168
static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m)
7269
{
7370
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
74-
int rc;
75-
rc = mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
76-
memset(stream_block, 0, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
77-
return rc;
71+
return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
7872
}
7973
#endif /* MCUBOOT_USE_MBED_TLS */
8074

@@ -102,31 +96,9 @@ static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const
10296

10397
static int _bootutil_aes_ctr_crypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *in, uint32_t inlen, uint32_t blk_off, uint8_t *out)
10498
{
105-
uint8_t buf[16];
106-
uint32_t buflen;
10799
int rc;
108-
if (blk_off == 0) {
109-
rc = tc_ctr_mode(out, inlen, in, inlen, counter, ctx);
110-
if (rc != TC_CRYPTO_SUCCESS) {
111-
return -1;
112-
}
113-
} else if (blk_off < 16) {
114-
buflen = ((inlen + blk_off <= 16) ? inlen : (16 - blk_off));
115-
inlen -= buflen;
116-
memcpy(&buf[blk_off], &in[0], buflen);
117-
rc = tc_ctr_mode(buf, 16, buf, 16, counter, ctx);
118-
if (rc != TC_CRYPTO_SUCCESS) {
119-
return -1;
120-
}
121-
memcpy(&out[0], &buf[blk_off], buflen);
122-
memset(&buf[0], 0, 16);
123-
if (inlen > 0) {
124-
rc = tc_ctr_mode(&out[buflen], inlen, &in[buflen], inlen, counter, ctx);
125-
}
126-
if (rc != TC_CRYPTO_SUCCESS) {
127-
return -1;
128-
}
129-
} else {
100+
rc = tc_ctr_mode(out, inlen, in, inlen, counter, &blk_off, ctx);
101+
if (rc != TC_CRYPTO_SUCCESS) {
130102
return -1;
131103
}
132104
return 0;

boot/bootutil/src/bootutil_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
460460
#ifdef MCUBOOT_RAM_LOAD
461461
#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
462462
(memcpy((output),(void*)((hdr)->ih_load_addr + (start)), \
463-
(size)) != (output))
463+
(size)), 0)
464464
#else
465465
#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
466466
(flash_area_read((fap), (start), (output), (size)))

boot/mbed/include/mcuboot_config/mcuboot_logging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
#define TRACE_GROUP "MCUb"
5757
#include "mbed_trace.h"
58+
#include "bootutil/ignore.h"
5859

5960
#define MCUBOOT_LOG_MODULE_DECLARE(domain) /* ignore */
6061
#define MCUBOOT_LOG_MODULE_REGISTER(domain) /* ignore */

boot/mbed/mbed_lib.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@
160160
"help": "Share data (NOT TESTED)",
161161
"macro_name": "MCUBOOT_DATA_SHARING",
162162
"value": null
163+
},
164+
"direct-xip": {
165+
"help": "Enable ability to boot update candidates in-place.",
166+
"macro_name": "MCUBOOT_DIRECT_XIP",
167+
"value": null
168+
},
169+
"direct-xip-revert": {
170+
"help": "Enable XIP revert mechanism. Only valid if direct-xip is also enabled.",
171+
"macro_name": "MCUBOOT_DIRECT_XIP_REVERT",
172+
"value": null
173+
},
174+
"xip-secondary-slot-address": {
175+
"help": "Specify start address for secondary slot address in XIP-accessible memory. This is required if direct-xip is enabled.",
176+
"value": null
163177
}
164178
}
165179
}

boot/mbed/src/flash_map_backend.cpp

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "mcuboot_config/mcuboot_logging.h"
2929

30+
#include "bootutil_priv.h"
31+
3032
#define FLASH_DEVICE_INTERNAL_FLASH 0
3133
#define FLASH_AREAS 3
3234

@@ -36,15 +38,15 @@ mbed::BlockDevice* mcuboot_secondary_bd = get_secondary_bd();
3638
/** Internal application block device */
3739
static FlashIAPBlockDevice mcuboot_primary_bd(MCUBOOT_PRIMARY_SLOT_START_ADDR, MCUBOOT_SLOT_SIZE);
3840

39-
#ifndef MCUBOOT_OVERWRITE_ONLY
41+
#if MCUBOOT_SWAP_USING_SCRATCH
4042
/** Scratch space is at the end of internal flash, after the main application */
4143
static FlashIAPBlockDevice mcuboot_scratch_bd(MCUBOOT_SCRATCH_START_ADDR, MCUBOOT_SCRATCH_SIZE);
4244
#endif
4345

4446
static mbed::BlockDevice* flash_map_bd[FLASH_AREAS] = {
4547
(mbed::BlockDevice*) &mcuboot_primary_bd, /** Primary (loadable) image area */
4648
mcuboot_secondary_bd, /** Secondary (update candidate) image area */
47-
#ifndef MCUBOOT_OVERWRITE_ONLY
49+
#if MCUBOOT_SWAP_USING_SCRATCH
4850
(mbed::BlockDevice*) &mcuboot_scratch_bd /** Scratch space for swapping images */
4951
#else
5052
nullptr
@@ -53,6 +55,8 @@ static mbed::BlockDevice* flash_map_bd[FLASH_AREAS] = {
5355

5456
static struct flash_area flash_areas[FLASH_AREAS];
5557

58+
static unsigned int open_count[FLASH_AREAS] = {0};
59+
5660
int flash_area_open(uint8_t id, const struct flash_area** fapp) {
5761

5862
*fapp = &flash_areas[id];
@@ -64,10 +68,13 @@ int flash_area_open(uint8_t id, const struct flash_area** fapp) {
6468
fap->fa_off = MCUBOOT_PRIMARY_SLOT_START_ADDR;
6569
break;
6670
case SECONDARY_ID:
67-
// The offset of the secondary slot is not currently used.
71+
#if MCUBOOT_DIRECT_XIP
72+
fap->fa_off = MBED_CONF_MCUBOOT_XIP_SECONDARY_SLOT_ADDRESS;
73+
#else
6874
fap->fa_off = 0;
75+
#endif
6976
break;
70-
#ifndef MCUBOOT_OVERWRITE_ONLY
77+
#if MCUBOOT_SWAP_USING_SCRATCH
7178
case SCRATCH_ID:
7279
fap->fa_off = MCUBOOT_SCRATCH_START_ADDR;
7380
break;
@@ -77,17 +84,40 @@ int flash_area_open(uint8_t id, const struct flash_area** fapp) {
7784
return -1;
7885
}
7986

87+
open_count[id]++;
88+
MCUBOOT_LOG_DBG("flash area %d open count: %d (+)", id, open_count[id]);
89+
8090
fap->fa_id = id;
8191
fap->fa_device_id = 0; // not relevant
8292

8393
mbed::BlockDevice* bd = flash_map_bd[id];
8494
fap->fa_size = (uint32_t) bd->size();
85-
return bd->init();
95+
96+
/* Only initialize if this isn't a nested call to open the flash area */
97+
if (open_count[id] == 1) {
98+
MCUBOOT_LOG_DBG("initializing flash area %d...", id);
99+
return bd->init();
100+
} else {
101+
return 0;
102+
}
86103
}
87104

88105
void flash_area_close(const struct flash_area* fap) {
89-
mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
90-
bd->deinit();
106+
uint8_t id = fap->fa_id;
107+
/* No need to close an unopened flash area, avoid an overflow of the counter */
108+
if (!open_count[id]) {
109+
return;
110+
}
111+
112+
open_count[id]--;
113+
MCUBOOT_LOG_DBG("flash area %d open count: %d (-)", id, open_count[id]);
114+
if (!open_count[id]) {
115+
/* mcuboot is not currently consistent in opening/closing flash areas only once at a time
116+
* so only deinitialize the BlockDevice if all callers have closed the flash area. */
117+
MCUBOOT_LOG_DBG("deinitializing flash area block device %d...", id);
118+
mbed::BlockDevice* bd = flash_map_bd[id];
119+
bd->deinit();
120+
}
91121
}
92122

93123
/*
@@ -96,8 +126,8 @@ void flash_area_close(const struct flash_area* fap) {
96126
int flash_area_read(const struct flash_area* fap, uint32_t off, void* dst, uint32_t len) {
97127
mbed::BlockDevice* bd = flash_map_bd[fap->fa_id];
98128

99-
// Note: The address must be aligned to bd->get_read_size(). If MCUBOOT_READ_GRANULARITY
100-
// is defined, the length does not need to be aligned.
129+
/* Note: The address must be aligned to bd->get_read_size(). If MCUBOOT_READ_GRANULARITY
130+
is defined, the length does not need to be aligned. */
101131
#ifdef MCUBOOT_READ_GRANULARITY
102132
uint32_t read_size = bd->get_read_size();
103133
if (read_size == 0) {
@@ -115,13 +145,15 @@ int flash_area_read(const struct flash_area* fap, uint32_t off, void* dst, uint3
115145
if (len != 0) {
116146
#endif
117147
if (!bd->is_valid_read(off, len)) {
118-
MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off, len);
148+
MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
149+
(unsigned int) off, (unsigned int) len);
119150
return -1;
120151
}
121152
else {
122153
int ret = bd->read(dst, off, len);
123154
if (ret != 0) {
124-
MCUBOOT_LOG_ERR("Read failed: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off, len);
155+
MCUBOOT_LOG_ERR("Read failed: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
156+
(unsigned int) off, (unsigned int) len);
125157
return ret;
126158
}
127159
}
@@ -130,7 +162,8 @@ int flash_area_read(const struct flash_area* fap, uint32_t off, void* dst, uint3
130162

131163
if (remainder) {
132164
if (!bd->is_valid_read(off + len, read_size)) {
133-
MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id, off + len, read_size);
165+
MCUBOOT_LOG_ERR("Invalid read: fa_id %d offset 0x%x len 0x%x", fap->fa_id,
166+
(unsigned int) (off + len), (unsigned int) read_size);
134167
return -1;
135168
}
136169
else {
@@ -171,7 +204,7 @@ uint8_t flash_area_erased_val(const struct flash_area* fap) {
171204
int flash_area_get_sectors(int fa_id, uint32_t* count, struct flash_sector* sectors) {
172205
mbed::BlockDevice* bd = flash_map_bd[fa_id];
173206

174-
// Loop through sectors and collect information on them
207+
/* Loop through sectors and collect information on them */
175208
bd_addr_t offset = 0;
176209
*count = 0;
177210
while (*count < MCUBOOT_MAX_IMG_SECTORS && bd->is_valid_read(offset, bd->get_read_size())) {

boot/mbed/src/secondary_bd.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Mbed-OS Microcontroller Library
3+
* Copyright (c) 2020 Embedded Planet
4+
* Copyright (c) 2020 ARM Limited
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License
18+
*/
19+
20+
#if MCUBOOT_DIRECT_XIP
21+
22+
#include "flash_map_backend/secondary_bd.h"
23+
#include "platform/mbed_toolchain.h"
24+
#include "FlashIAPBlockDevice.h"
25+
26+
/**
27+
* For an XIP build, the secondary BD is provided by mcuboot by default.
28+
*
29+
* This is a weak symbol so the user can override it.
30+
*/
31+
MBED_WEAK mbed::BlockDevice* get_secondary_bd(void) {
32+
static FlashIAPBlockDevice secondary_bd(MBED_CONF_MCUBOOT_XIP_SECONDARY_SLOT_ADDRESS,
33+
MCUBOOT_SLOT_SIZE);
34+
35+
return &secondary_bd;
36+
}
37+
38+
#endif
39+
40+

boot/mynewt/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ main(void)
251251
#if MYNEWT_VAL(BOOT_CUSTOM_START)
252252
boot_custom_start(flash_base, &rsp);
253253
#else
254+
hal_bsp_deinit();
254255
hal_system_start((void *)(flash_base + rsp.br_image_off +
255256
rsp.br_hdr->ih_hdr_size));
256257
#endif

0 commit comments

Comments
 (0)