Skip to content

Commit 5432acb

Browse files
Merge branch 'mcu-tools:main' into usb.dfu.optimization
2 parents 0ca65eb + 11982df commit 5432acb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+778
-523
lines changed

.github/workflows/zephyr_build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
- name: Setup Zephyr
7070
working-directory: repos/zephyr
7171
run: |
72-
west config --system manifest.project-filter -- -.*,+cmsis,+hal_nordic,+hal_nxp,+hal_stm32,+libmetal,+littlefs,+mbedtls,+mcuboot,+open-amp,+picolibc,+segger,+tinycrypt,+trusted-firmware-m,+zcbor
72+
west config --system manifest.project-filter -- -.*,+cmsis,+cmsis_6,+hal_nordic,+hal_nxp,+hal_stm32,+libmetal,+littlefs,+mbedtls,+mcuboot,+open-amp,+picolibc,+segger,+tinycrypt,+trusted-firmware-m,+zcbor
7373
west init -l .
7474
west update
7575

README.md

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

21-
This is MCUboot version 2.2.0-rc1
21+
This is MCUboot version 2.2.0
2222

2323
MCUboot is a secure bootloader for 32-bits microcontrollers. It defines a
2424
common infrastructure for the bootloader and the system flash layout on

boot/boot_serial/src/boot_serial.c

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,19 @@ static int boot_serial_get_hash(const struct image_header *hdr,
177177
#endif
178178
#endif
179179

180-
static zcbor_state_t cbor_state[2];
180+
#ifdef ZCBOR_CANONICAL
181+
/* Allow 3 extra states for backup states for all commands */
182+
#define CBOR_EXTRA_STATES 3
183+
#else
184+
#define CBOR_EXTRA_STATES 0
185+
#endif
186+
187+
static zcbor_state_t cbor_state[2 + CBOR_EXTRA_STATES];
181188

182189
void reset_cbor_state(void)
183190
{
184-
zcbor_new_encode_state(cbor_state, 2, (uint8_t *)bs_obuf,
185-
sizeof(bs_obuf), 0);
191+
zcbor_new_encode_state(cbor_state, ARRAY_SIZE(cbor_state), (uint8_t *)bs_obuf,
192+
sizeof(bs_obuf), 0);
186193
}
187194

188195
/**
@@ -497,6 +504,7 @@ bs_set(char *buf, int len)
497504
* "hash":<hash of image (OPTIONAL for single image only)>
498505
* }
499506
*/
507+
uint32_t slot;
500508
uint8_t image_index = 0;
501509
size_t decoded = 0;
502510
uint8_t hash[IMAGE_HASH_SIZE];
@@ -509,8 +517,8 @@ bs_set(char *buf, int len)
509517
bool found = false;
510518
#endif
511519

512-
zcbor_state_t zsd[4];
513-
zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
520+
zcbor_state_t zsd[4 + CBOR_EXTRA_STATES];
521+
zcbor_new_decode_state(zsd, ARRAY_SIZE(zsd), (uint8_t *)buf, len, 1, NULL, 0);
514522

515523
struct zcbor_map_decode_key_val image_set_state_decode[] = {
516524
ZCBOR_MAP_DECODE_KEY_DECODER("confirm", zcbor_bool_decode, &confirm),
@@ -536,98 +544,106 @@ bs_set(char *buf, int len)
536544
}
537545

538546
if (img_hash.len != 0) {
539-
for (image_index = 0; image_index < BOOT_IMAGE_NUMBER; ++image_index) {
540-
struct image_header hdr;
541-
uint32_t area_id;
542-
const struct flash_area *fap;
543-
uint8_t tmpbuf[64];
547+
IMAGES_ITER(image_index) {
548+
#ifdef MCUBOOT_SWAP_USING_OFFSET
549+
int swap_status = boot_swap_type_multi(image_index);
550+
#endif
551+
552+
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
553+
struct image_header hdr;
554+
uint32_t area_id;
555+
const struct flash_area *fap;
556+
uint8_t tmpbuf[64];
544557

545558
#ifdef MCUBOOT_SWAP_USING_OFFSET
546-
uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN;
547-
struct flash_sector sector_data;
548-
uint32_t start_off = 0;
559+
uint32_t num_sectors = SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN;
560+
struct flash_sector sector_data;
561+
uint32_t start_off = 0;
549562
#endif
550563

551-
area_id = flash_area_id_from_multi_image_slot(image_index, 1);
552-
if (flash_area_open(area_id, &fap)) {
553-
BOOT_LOG_ERR("Failed to open flash area ID %d", area_id);
554-
continue;
555-
}
564+
area_id = flash_area_id_from_multi_image_slot(image_index, slot);
565+
if (flash_area_open(area_id, &fap)) {
566+
BOOT_LOG_ERR("Failed to open flash area ID %d", area_id);
567+
continue;
568+
}
556569

557570
#ifdef MCUBOOT_SWAP_USING_OFFSET
558-
rc = flash_area_sectors(fap, &num_sectors, &sector_data);
571+
if (slot == BOOT_SECONDARY_SLOT && swap_status != BOOT_SWAP_TYPE_REVERT) {
572+
rc = flash_area_sectors(fap, &num_sectors, &sector_data);
559573

560-
if ((rc != 0 && rc != -ENOMEM) ||
561-
num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN) {
562-
flash_area_close(fap);
563-
continue;
564-
}
574+
if ((rc != 0 && rc != -ENOMEM) ||
575+
num_sectors != SWAP_USING_OFFSET_SECTOR_UPDATE_BEGIN) {
576+
flash_area_close(fap);
577+
continue;
578+
}
565579

566-
start_off = sector_data.fs_size;
580+
start_off = sector_data.fs_size;
581+
}
567582
#endif
568583

569-
rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
570-
BOOT_HOOK_REGULAR, image_index, 1, &hdr);
571-
if (rc == BOOT_HOOK_REGULAR)
572-
{
584+
rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
585+
BOOT_HOOK_REGULAR, image_index, 1, &hdr);
586+
if (rc == BOOT_HOOK_REGULAR)
587+
{
573588
#ifdef MCUBOOT_SWAP_USING_OFFSET
574-
flash_area_read(fap, start_off, &hdr, sizeof(hdr));
589+
flash_area_read(fap, start_off, &hdr, sizeof(hdr));
575590
#else
576-
flash_area_read(fap, 0, &hdr, sizeof(hdr));
591+
flash_area_read(fap, 0, &hdr, sizeof(hdr));
577592
#endif
578-
}
579-
580-
if (hdr.ih_magic == IMAGE_MAGIC)
581-
{
582-
FIH_DECLARE(fih_rc, FIH_FAILURE);
593+
}
583594

584-
BOOT_HOOK_CALL_FIH(boot_image_check_hook,
585-
FIH_BOOT_HOOK_REGULAR,
586-
fih_rc, image_index, 1);
587-
if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
595+
if (hdr.ih_magic == IMAGE_MAGIC)
588596
{
597+
FIH_DECLARE(fih_rc, FIH_FAILURE);
598+
599+
BOOT_HOOK_CALL_FIH(boot_image_check_hook,
600+
FIH_BOOT_HOOK_REGULAR,
601+
fih_rc, image_index, 1);
602+
if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
603+
{
589604
#ifdef MCUBOOT_ENC_IMAGES
590-
if (IS_ENCRYPTED(&hdr)) {
605+
if (IS_ENCRYPTED(&hdr)) {
591606
#ifdef MCUBOOT_SWAP_USING_OFFSET
592-
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
593-
&hdr, tmpbuf, sizeof(tmpbuf), start_off);
607+
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
608+
&hdr, tmpbuf, sizeof(tmpbuf), start_off);
594609
#else
595-
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
596-
&hdr, tmpbuf, sizeof(tmpbuf));
610+
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
611+
&hdr, tmpbuf, sizeof(tmpbuf));
597612
#endif
598-
} else {
613+
} else {
599614
#endif
600615
#ifdef MCUBOOT_SWAP_USING_OFFSET
601-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
602-
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
616+
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
617+
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL, start_off);
603618
#else
604-
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
605-
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
619+
FIH_CALL(bootutil_img_validate, fih_rc, NULL, &hdr,
620+
fap, tmpbuf, sizeof(tmpbuf), NULL, 0, NULL);
606621
#endif
607622
#ifdef MCUBOOT_ENC_IMAGES
608-
}
623+
}
609624
#endif
610-
}
625+
}
611626

612-
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
613-
continue;
627+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
628+
continue;
629+
}
614630
}
615-
}
616631

617632
#ifdef MCUBOOT_SERIAL_IMG_GRP_HASH
618-
/* Retrieve hash of image for identification */
633+
/* Retrieve hash of image for identification */
619634
#ifdef MCUBOOT_SWAP_USING_OFFSET
620-
rc = boot_serial_get_hash(&hdr, fap, hash, start_off);
635+
rc = boot_serial_get_hash(&hdr, fap, hash, start_off);
621636
#else
622-
rc = boot_serial_get_hash(&hdr, fap, hash);
637+
rc = boot_serial_get_hash(&hdr, fap, hash);
623638
#endif
624639
#endif
625-
flash_area_close(fap);
640+
flash_area_close(fap);
626641

627-
if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) {
628-
/* Hash matches, set this slot for test or confirmation */
629-
found = true;
630-
break;
642+
if (rc == 0 && memcmp(hash, img_hash.value, sizeof(hash)) == 0) {
643+
/* Hash matches, set this slot for test or confirmation */
644+
found = true;
645+
goto set_image_state;
646+
}
631647
}
632648
}
633649

@@ -640,6 +656,7 @@ bs_set(char *buf, int len)
640656
}
641657
#endif
642658

659+
set_image_state:
643660
rc = boot_set_pending_multi(image_index, confirm);
644661

645662
out:
@@ -683,6 +700,8 @@ bs_list_set(uint8_t op, char *buf, int len)
683700
bs_rc_rsp(MGMT_ERR_ENOTSUP);
684701
#endif
685702
}
703+
704+
reset_cbor_state();
686705
}
687706

688707
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
@@ -856,7 +875,7 @@ static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
856875
BOOT_LOG_DBG("Erasing range 0x%jx:0x%jx", (intmax_t)start,
857876
(intmax_t)(start + size - 1));
858877

859-
rc = boot_erase_region(fap, start, size);
878+
rc = boot_erase_region(fap, start, size, false);
860879
if (rc != 0) {
861880
BOOT_LOG_ERR("Error %d while erasing range", rc);
862881
return -EINVAL;
@@ -901,8 +920,8 @@ bs_upload(char *buf, int len)
901920
static uint32_t start_off = 0;
902921
#endif
903922

904-
zcbor_state_t zsd[4];
905-
zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
923+
zcbor_state_t zsd[4 + CBOR_EXTRA_STATES];
924+
zcbor_new_decode_state(zsd, ARRAY_SIZE(zsd), (uint8_t *)buf, len, 1, NULL, 0);
906925

907926
struct zcbor_map_decode_key_val image_upload_decode[] = {
908927
ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode, &img_num_tmp),
@@ -1000,7 +1019,7 @@ bs_upload(char *buf, int len)
10001019
/* Non-progressive erase erases entire image slot when first chunk of
10011020
* an image is received.
10021021
*/
1003-
rc = boot_erase_region(fap, 0, area_size);
1022+
rc = boot_erase_region(fap, 0, area_size, false);
10041023
if (rc) {
10051024
goto out_invalid_data;
10061025
}
@@ -1210,8 +1229,8 @@ bs_echo(char *buf, int len)
12101229
bool ok;
12111230
uint32_t rc = MGMT_ERR_EINVAL;
12121231

1213-
zcbor_state_t zsd[4];
1214-
zcbor_new_state(zsd, sizeof(zsd) / sizeof(zcbor_state_t), (uint8_t *)buf, len, 1, NULL, 0);
1232+
zcbor_state_t zsd[4 + CBOR_EXTRA_STATES];
1233+
zcbor_new_decode_state(zsd, ARRAY_SIZE(zsd), (uint8_t *)buf, len, 1, NULL, 0);
12151234

12161235
if (!zcbor_map_start_decode(zsd)) {
12171236
goto out;

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "bootutil/bootutil_log.h"
1212
#include "bootutil/bootutil_public.h"
1313
#include "bootutil/fault_injection_hardening.h"
14-
#include "bootutil/enc_key.h"
1514

1615
#include "mcuboot_config/mcuboot_config.h"
1716

@@ -187,7 +186,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
187186
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
188187
blk_off, &buf[idx]);
189188
}
190-
rc = boot_erase_region(fap, off + bytes_copied, chunk_sz);
189+
rc = boot_erase_region(fap, off + bytes_copied, chunk_sz, false);
191190
if (rc != 0) {
192191
return BOOT_EFLASH;
193192
}

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,27 @@
1919
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT or PSA"
2020
#endif
2121

22+
#include "bootutil/enc_key_public.h"
23+
2224
#if defined(MCUBOOT_USE_MBED_TLS)
2325
#include <mbedtls/aes.h>
24-
#include "bootutil/enc_key_public.h"
25-
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE BOOT_ENC_KEY_SIZE
26-
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE (16)
26+
#define BOOT_ENC_BLOCK_SIZE (16)
2727
#endif /* MCUBOOT_USE_MBED_TLS */
2828

2929
#if defined(MCUBOOT_USE_TINYCRYPT)
30-
#if defined(MCUBOOT_AES_256)
31-
#error "Cannot use AES-256 for encryption with Tinycrypt library."
32-
#endif
3330
#include <string.h>
3431
#include <tinycrypt/aes.h>
3532
#include <tinycrypt/ctr_mode.h>
3633
#include <tinycrypt/constants.h>
37-
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE TC_AES_KEY_SIZE
38-
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE TC_AES_BLOCK_SIZE
34+
#if defined(MCUBOOT_AES_256) || (BOOT_ENC_KEY_SIZE != TC_AES_KEY_SIZE)
35+
#error "Cannot use AES-256 for encryption with Tinycrypt library."
36+
#endif
37+
#define BOOT_ENC_BLOCK_SIZE TC_AES_BLOCK_SIZE
3938
#endif /* MCUBOOT_USE_TINYCRYPT */
4039

4140
#if defined(MCUBOOT_USE_PSA_CRYPTO)
4241
#include <psa/crypto.h>
43-
#include "bootutil/enc_key_public.h"
44-
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE BOOT_ENC_KEY_SIZE
45-
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE (16)
42+
#define BOOT_ENC_BLOCK_SIZE (16)
4643
#endif
4744

4845
#include <stdint.h>
@@ -91,18 +88,18 @@ static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
9188

9289
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
9390
{
94-
return mbedtls_aes_setkey_enc(ctx, k, BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE * 8);
91+
return mbedtls_aes_setkey_enc(ctx, k, BOOT_ENC_KEY_SIZE * 8);
9592
}
9693

9794
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)
9895
{
99-
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
96+
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
10097
return mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
10198
}
10299

103100
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)
104101
{
105-
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
102+
uint8_t stream_block[BOOT_ENC_BLOCK_SIZE];
106103
return mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
107104
}
108105
#endif /* MCUBOOT_USE_MBED_TLS */

0 commit comments

Comments
 (0)