Skip to content

Commit c71d218

Browse files
authored
Merge: Synchronized up to JuulLabs-OSS@c74c551
merged by GitHub GUI #38 Signed-off-by: Andrzej Puzdrowski <[email protected]>
2 parents e64c5f0 + 710ce7f commit c71d218

File tree

14 files changed

+137
-29
lines changed

14 files changed

+137
-29
lines changed

boot/boot_serial/src/boot_serial.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <os/os_malloc.h>
5252

5353
#include <bootutil/image.h>
54+
#include <bootutil/bootutil.h>
5455

5556
#include "boot_serial/boot_serial.h"
5657
#include "boot_serial_priv.h"
@@ -312,11 +313,17 @@ bs_upload(char *buf, int len)
312313
rc = 0;
313314
goto out;
314315
}
315-
if (curr_off + img_blen < img_size) {
316-
rem_bytes = img_blen % flash_area_align(fap);
317-
if (rem_bytes) {
318-
img_blen -= rem_bytes;
319-
}
316+
317+
if (curr_off + img_blen > img_size) {
318+
rc = MGMT_ERR_EINVAL;
319+
goto out;
320+
}
321+
322+
rem_bytes = img_blen % flash_area_align(fap);
323+
324+
if ((curr_off + img_blen < img_size) && rem_bytes) {
325+
img_blen -= rem_bytes;
326+
rem_bytes = 0;
320327
}
321328

322329
#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
@@ -337,7 +344,32 @@ bs_upload(char *buf, int len)
337344
#endif
338345

339346
BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_blen);
340-
rc = flash_area_write(fap, curr_off, img_data, img_blen);
347+
if (rem_bytes) {
348+
/* the last chunk of the image might be unaligned */
349+
uint8_t wbs_aligned[BOOT_MAX_ALIGN];
350+
size_t w_size = img_blen - rem_bytes;
351+
352+
if (w_size) {
353+
rc = flash_area_write(fap, curr_off, img_data, w_size);
354+
if (rc) {
355+
goto out_invalid_data;
356+
}
357+
curr_off += w_size;
358+
img_blen -= w_size;
359+
img_data += w_size;
360+
}
361+
362+
if (img_blen) {
363+
memcpy(wbs_aligned, img_data, rem_bytes);
364+
memset(wbs_aligned + rem_bytes, flash_area_erased_val(fap),
365+
sizeof(wbs_aligned) - rem_bytes);
366+
rc = flash_area_write(fap, curr_off, wbs_aligned, flash_area_align(fap));
367+
}
368+
369+
} else {
370+
rc = flash_area_write(fap, curr_off, img_data, img_blen);
371+
}
372+
341373
if (rc == 0) {
342374
curr_off += img_blen;
343375
#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY

boot/bootutil/include/bootutil/crypto/ecdsa_p256.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static inline void bootutil_ecdsa_p256_drop(bootutil_ecdsa_p256_context *ctx)
6969
(void)ctx;
7070
}
7171

72-
static inline int bootutil_ecdsa_p256_verify(bootutil_ecdsa_p256_context *ctx, const uint8_t *pk, const uint8_t *hash, const uint8_t *sig)
72+
static inline int bootutil_ecdsa_p256_verify(bootutil_ecdsa_p256_context *ctx, uint8_t *pk, uint8_t *hash, uint8_t *sig)
7373
{
7474
(void)ctx;
7575
return cc310_ecdsa_verify_secp256r1(hash, pk, sig, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE);

boot/bootutil/include/bootutil/fault_injection_hardening.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@ void fih_cfi_decrement(void);
284284
/* Label for interacting with FIH testing tool. Can be parsed from the elf file
285285
* after compilation. Does not require debug symbols.
286286
*/
287+
#if defined(__ICCARM__)
288+
#define FIH_LABEL(str, lin, cnt) __asm volatile ("FIH_LABEL_" str "_" #lin "_" #cnt "::" ::);
289+
#else
287290
#define FIH_LABEL(str) __asm volatile ("FIH_LABEL_" str "_%=:" ::);
291+
#endif
288292

289293
/* Main FIH calling macro. return variable is second argument. Does some setup
290294
* before and validation afterwards. Inserts labels for use with testing script.
@@ -301,6 +305,23 @@ void fih_cfi_decrement(void);
301305
* previously saved value. If this is equal then the function call and all child
302306
* function calls were performed.
303307
*/
308+
#if defined(__ICCARM__)
309+
#define FIH_CALL(f, ret, ...) FIH_CALL2(f, ret, __LINE__, __COUNTER__, __VA_ARGS__)
310+
311+
#define FIH_CALL2(f, ret, l, c, ...) \
312+
do { \
313+
FIH_LABEL("FIH_CALL_START", l, c); \
314+
FIH_CFI_PRECALL_BLOCK; \
315+
ret = FIH_FAILURE; \
316+
if (fih_delay()) { \
317+
ret = f(__VA_ARGS__); \
318+
} \
319+
FIH_CFI_POSTCALL_BLOCK; \
320+
FIH_LABEL("FIH_CALL_END", l, c); \
321+
} while (0)
322+
323+
#else
324+
304325
#define FIH_CALL(f, ret, ...) \
305326
do { \
306327
FIH_LABEL("FIH_CALL_START"); \
@@ -312,6 +333,7 @@ void fih_cfi_decrement(void);
312333
FIH_CFI_POSTCALL_BLOCK; \
313334
FIH_LABEL("FIH_CALL_END"); \
314335
} while (0)
336+
#endif
315337

316338
/* FIH return changes the state of the internal state machine. If you do a
317339
* FIH_CALL then you need to do a FIH_RET else the state machine will detect

boot/bootutil/src/image_ec256.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#ifdef MCUBOOT_SIGN_EC256
3232
/*TODO: remove this after cypress port mbedtls to abstract crypto api */
33+
#ifdef MCUBOOT_USE_CC310
34+
#define NUM_ECC_BYTES (256 / 8)
35+
#endif
3336
#if defined (MCUBOOT_USE_TINYCRYPT) || defined (MCUBOOT_USE_CC310)
3437
#include "bootutil/sign_key.h"
3538

boot/bootutil/src/loader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
22072207
uint32_t img_sz;
22082208
uint32_t img_loaded = 0;
22092209
#endif /* MCUBOOT_RAM_LOAD */
2210-
fih_int fih_rc;
2210+
fih_int fih_rc = FIH_FAILURE;
22112211

22122212
memset(state, 0, sizeof(struct boot_loader_state));
22132213

boot/bootutil/src/swap_move.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ int
264264
swap_status_source(struct boot_loader_state *state)
265265
{
266266
struct boot_swap_state state_primary_slot;
267+
struct boot_swap_state state_secondary_slot;
267268
int rc;
268269
uint8_t source;
269270
uint8_t image_index;
@@ -280,8 +281,15 @@ swap_status_source(struct boot_loader_state *state)
280281

281282
BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
282283

284+
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
285+
&state_secondary_slot);
286+
assert(rc == 0);
287+
288+
BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot);
289+
283290
if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
284-
state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
291+
state_primary_slot.copy_done == BOOT_FLAG_UNSET &&
292+
state_secondary_slot.magic != BOOT_MAGIC_GOOD) {
285293

286294
source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
287295

@@ -315,11 +323,13 @@ boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
315323
old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
316324

317325
if (bs->idx == BOOT_STATUS_IDX_0) {
318-
rc = swap_erase_trailer_sectors(state, fap_pri);
319-
assert(rc == 0);
326+
if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
327+
rc = swap_erase_trailer_sectors(state, fap_pri);
328+
assert(rc == 0);
320329

321-
rc = swap_status_init(state, fap_pri, bs);
322-
assert(rc == 0);
330+
rc = swap_status_init(state, fap_pri, bs);
331+
assert(rc == 0);
332+
}
323333

324334
rc = swap_erase_trailer_sectors(state, fap_sec);
325335
assert(rc == 0);

boot/zephyr/Kconfig

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,20 @@ config BOOT_SIGNATURE_KEY_FILE
147147
config MCUBOOT_CLEANUP_ARM_CORE
148148
bool "Perform core cleanup before chain-load the application"
149149
depends on CPU_CORTEX_M
150-
default y
150+
default y if !ARCH_SUPPORTS_ARCH_HW_INIT
151+
help
152+
This option instructs MCUboot to perform a clean-up of a set of
153+
architecture core HW registers before junping to the application
154+
firmware. The clean-up sets these registers to their warm-reset
155+
values as specified by the architecture.
156+
157+
By default, this option is enabled only if the architecture does
158+
not have the functionality to perform such a register clean-up
159+
during application firmware boot.
160+
161+
Zephyr applications on Cortex-M will perform this register clean-up
162+
by default, if they are chain-loadable by MCUboot, so MCUboot does
163+
not need to perform such a cleanup itself.
151164

152165
config MBEDTLS_CFG_FILE
153166
default "mcuboot-mbedtls-cfg.h"
@@ -276,7 +289,7 @@ config BOOT_MAX_IMG_SECTORS
276289

277290
config BOOT_ERASE_PROGRESSIVELY
278291
bool "Erase flash progressively when receiving new firmware"
279-
default y if SOC_NRF52840
292+
default y if SOC_FAMILY_NRF
280293
help
281294
If enabled, flash is erased as necessary when receiving new firmware,
282295
instead of erasing the whole image slot at once. This is necessary
@@ -429,7 +442,7 @@ config BOOT_SERIAL_DETECT_PIN
429442
default 6 if BOARD_NRF9160DK_NRF9160
430443
default 11 if BOARD_NRF52840DK_NRF52840
431444
default 13 if BOARD_NRF52DK_NRF52832
432-
default 23 if BOARD_NRF5340_DK_NRF5340_CPUAPP || BOARD_NRF5340_DK_NRF5340_CPUAPPNS
445+
default 23 if BOARD_NRF5340PDK_NRF5340_CPUAPP || BOARD_NRF5340PDK_NRF5340_CPUAPPNS
433446
help
434447
Pin on the serial detect port which triggers serial recovery mode.
435448

boot/zephyr/arm_cleanup.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ void cleanup_arm_nvic(void) {
2020
NVIC->ICPR[i] = 0xFFFFFFFF;
2121
}
2222
}
23+
24+
#if CONFIG_CPU_HAS_ARM_MPU
25+
__weak void z_arm_clear_arm_mpu_config(void)
26+
{
27+
int i;
28+
29+
int num_regions =
30+
((MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos);
31+
32+
for (i = 0; i < num_regions; i++) {
33+
ARM_MPU_ClrRegion(i);
34+
}
35+
}
36+
#endif

boot/zephyr/flash_map_extended.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
110110
}
111111

112112
#define ERASED_VAL 0xff
113-
uint8_t flash_area_erased_val(const struct flash_area *fap)
113+
__weak uint8_t flash_area_erased_val(const struct flash_area *fap)
114114
{
115115
(void)fap;
116116
return ERASED_VAL;

boot/zephyr/include/arm_cleanup.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@
1212
* Cleanup interrupt priority and interupt enable registers.
1313
*/
1414
void cleanup_arm_nvic(void);
15+
16+
#if defined(CONFIG_CPU_HAS_ARM_MPU)
17+
/**
18+
* Cleanup all ARM MPU region configuration
19+
*/
20+
void z_arm_clear_arm_mpu_config(void);
21+
#endif
22+
1523
#endif

0 commit comments

Comments
 (0)