Skip to content

Commit a343542

Browse files
committed
[nrf mergeup] Merge upstream up to commit cfec947
Signed-off-by: Tomasz Moń <[email protected]>
2 parents 19486e4 + cfec947 commit a343542

37 files changed

+761
-66
lines changed

boot/boot_serial/src/boot_serial.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <inttypes.h>
2222
#include <ctype.h>
2323
#include <stdio.h>
24+
#include <errno.h>
2425

2526
#include "sysflash/sysflash.h"
2627

@@ -32,6 +33,7 @@
3233
#include <zephyr/sys/byteorder.h>
3334
#include <zephyr/sys/__assert.h>
3435
#include <zephyr/drivers/flash.h>
36+
#include <zephyr/kernel.h>
3537
#include <zephyr/sys/crc.h>
3638
#include <zephyr/sys/base64.h>
3739
#include <hal/hal_flash.h>

boot/bootutil/src/bootutil_misc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,27 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
387387
return 0;
388388
}
389389
#endif
390+
391+
uint32_t bootutil_max_image_size(const struct flash_area *fap)
392+
{
393+
#if defined(MCUBOOT_SWAP_USING_SCRATCH)
394+
return boot_status_off(fap);
395+
#elif defined(MCUBOOT_SWAP_USING_MOVE)
396+
struct flash_sector sector;
397+
/* get the last sector offset */
398+
int rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
399+
if (rc) {
400+
BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
401+
return 0; /* Returning of zero here should cause any check which uses
402+
* this value to fail.
403+
*/
404+
}
405+
return flash_sector_get_off(&sector);
406+
#elif defined(MCUBOOT_OVERWRITE_ONLY)
407+
return boot_swap_info_off(fap);
408+
#elif defined(MCUBOOT_DIRECT_XIP)
409+
return boot_swap_info_off(fap);
410+
#elif defined(MCUBOOT_RAM_LOAD)
411+
return boot_swap_info_off(fap);
412+
#endif
413+
}

boot/bootutil/src/bootutil_priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ struct bootsim_ram_info *bootsim_get_ram_info(void);
463463
(flash_area_read((fap), (start), (output), (size)))
464464
#endif /* MCUBOOT_RAM_LOAD */
465465

466+
uint32_t bootutil_max_image_size(const struct flash_area *fap);
467+
466468
#ifdef __cplusplus
467469
}
468470
#endif

boot/bootutil/src/image_validate.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
268268
#endif /* !MCUBOOT_HW_KEY */
269269
#endif
270270

271-
#ifdef MCUBOOT_HW_ROLLBACK_PROT
272271
/**
273272
* Reads the value of an image's security counter.
274273
*
@@ -328,7 +327,6 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
328327

329328
return 0;
330329
}
331-
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
332330

333331
/*
334332
* Verify the integrity of the image.
@@ -378,6 +376,11 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
378376
goto out;
379377
}
380378

379+
if (it.tlv_end > bootutil_max_image_size(fap)) {
380+
rc = -1;
381+
goto out;
382+
}
383+
381384
/*
382385
* Traverse through all of the TLVs, performing any checks we know
383386
* and are able to do.

boot/bootutil/src/loader.c

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
629629
#if (BOOT_IMAGE_NUMBER > 1) || \
630630
defined(MCUBOOT_DIRECT_XIP) || \
631631
defined(MCUBOOT_RAM_LOAD) || \
632-
(defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
632+
defined(MCUBOOT_DOWNGRADE_PREVENTION)
633633
/**
634634
* Compare image version numbers not including the build number
635635
*
@@ -1438,6 +1438,8 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
14381438
boot_status_fails);
14391439
}
14401440
#endif
1441+
rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1442+
BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
14411443

14421444
return 0;
14431445
}
@@ -2012,6 +2014,60 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
20122014
#endif
20132015
}
20142016

2017+
/**
2018+
* Checks test swap downgrade prevention conditions.
2019+
*
2020+
* Function called only for swap upgrades test run. It may prevent
2021+
* swap if slot 1 image has <= version number or < security counter
2022+
*
2023+
* @param state Boot loader status information.
2024+
*
2025+
* @return 0 - image can be swapped, -1 downgrade prevention
2026+
*/
2027+
static int
2028+
check_downgrade_prevention(struct boot_loader_state *state)
2029+
{
2030+
#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
2031+
(defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
2032+
uint32_t security_counter[2];
2033+
int rc;
2034+
2035+
if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
2036+
/* If there was security no counter in slot 0, allow swap */
2037+
rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
2038+
BOOT_IMG(state, 0).area,
2039+
&security_counter[0]);
2040+
if (rc != 0) {
2041+
return 0;
2042+
}
2043+
/* If there is no security counter in slot 1, or it's lower than
2044+
* that of slot 0, prevent downgrade */
2045+
rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
2046+
BOOT_IMG(state, 1).area,
2047+
&security_counter[1]);
2048+
if (rc != 0 || security_counter[0] > security_counter[1]) {
2049+
rc = -1;
2050+
}
2051+
}
2052+
else {
2053+
rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
2054+
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
2055+
}
2056+
if (rc < 0) {
2057+
/* Image in slot 0 prevents downgrade, delete image in slot 1 */
2058+
BOOT_LOG_INF("Image in slot 1 erased due to downgrade prevention");
2059+
flash_area_erase(BOOT_IMG(state, 1).area, 0,
2060+
flash_area_get_size(BOOT_IMG(state, 1).area));
2061+
} else {
2062+
rc = 0;
2063+
}
2064+
return rc;
2065+
#else
2066+
(void)state;
2067+
return 0;
2068+
#endif
2069+
}
2070+
20152071
fih_int
20162072
context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
20172073
{
@@ -2140,7 +2196,13 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
21402196
case BOOT_SWAP_TYPE_NONE:
21412197
break;
21422198

2143-
case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2199+
case BOOT_SWAP_TYPE_TEST:
2200+
if (check_downgrade_prevention(state) != 0) {
2201+
/* Downgrade prevented */
2202+
BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2203+
break;
2204+
}
2205+
/* fallthrough */
21442206
case BOOT_SWAP_TYPE_PERM: /* fallthrough */
21452207
case BOOT_SWAP_TYPE_REVERT:
21462208
rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,

boot/espressif/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ set(port_srcs
213213
${CMAKE_CURRENT_LIST_DIR}/port/esp_mcuboot.c
214214
${CMAKE_CURRENT_LIST_DIR}/port/esp_loader.c
215215
${CMAKE_CURRENT_LIST_DIR}/os.c
216-
${CMAKE_CURRENT_LIST_DIR}/serial_adapter.c
217216
)
218217

219218
if(CONFIG_ESP_MCUBOOT_SERIAL)
@@ -227,7 +226,7 @@ if(CONFIG_ESP_MCUBOOT_SERIAL)
227226
${BOOT_SERIAL_DIR}/src/zcbor_common.c
228227
)
229228
list(APPEND port_srcs
230-
${CMAKE_CURRENT_LIST_DIR}/serial_adapter.c
229+
${CMAKE_CURRENT_LIST_DIR}/port/${MCUBOOT_TARGET}/serial_adapter.c
231230
${MBEDTLS_DIR}/library/base64.c
232231
)
233232
list(APPEND CRYPTO_INC
@@ -247,6 +246,7 @@ target_include_directories(
247246
${APP_EXECUTABLE}
248247
PUBLIC
249248
${BOOTUTIL_DIR}/include
249+
${BOOTUTIL_DIR}/src
250250
${BOOT_SERIAL_DIR}/include
251251
${CRYPTO_INC}
252252
${CMAKE_CURRENT_LIST_DIR}/include

boot/espressif/hal/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ list(APPEND include_dirs
4141
${esp_idf_dir}/components/efuse/${MCUBOOT_TARGET}/include
4242
${esp_idf_dir}/components/efuse/private_include
4343
${esp_idf_dir}/components/efuse/${MCUBOOT_TARGET}/private_include
44+
${esp_idf_dir}/components/esp_system/include
4445
${esp_idf_dir}/components/newlib/platform_include
4546
)
4647

boot/espressif/hal/include/mcuboot_config/mcuboot_config.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@
143143
#define CONFIG_MCUBOOT_SERIAL
144144
#endif
145145

146+
/*
147+
* When a serial recovery process is receiving the image data, this option
148+
* enables it to erase flash progressively (by sectors) instead of the
149+
* default behavior that is erasing whole image size of flash area after
150+
* receiving first frame.
151+
* Enabling this options prevents stalling the beginning of transfer
152+
* for the time needed to erase large chunk of flash.
153+
*/
154+
#ifdef CONFIG_ESP_MCUBOOT_ERASE_PROGRESSIVELY
155+
#define MCUBOOT_ERASE_PROGRESSIVELY
156+
#endif
157+
146158
/* Serial extensions are not implemented
147159
*/
148160
#define MCUBOOT_PERUSER_MGMT_GROUP_ENABLED 0

boot/espressif/hal/src/esp32c3/bootloader_init.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "bootloader_init.h"
2121
#include "bootloader_common.h"
22+
#include "bootloader_console.h"
2223
#include "bootloader_clock.h"
2324
#include "bootloader_flash_config.h"
2425
#include "bootloader_mem.h"
@@ -31,6 +32,10 @@
3132
#include "soc/efuse_reg.h"
3233
#include "soc/rtc.h"
3334

35+
#include "hal/gpio_hal.h"
36+
#include <hal/gpio_ll.h>
37+
#include <hal/uart_ll.h>
38+
3439
#include "esp32c3/rom/cache.h"
3540
#include "esp32c3/rom/spi_flash.h"
3641

@@ -39,6 +44,12 @@
3944

4045
extern esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
4146

47+
#if CONFIG_ESP_CONSOLE_UART_CUSTOM
48+
static uart_dev_t *alt_console_uart_dev = (CONFIG_ESP_CONSOLE_UART_NUM == 0) ?
49+
&UART0 :
50+
&UART1;
51+
#endif
52+
4253
void IRAM_ATTR bootloader_configure_spi_pins(int drv)
4354
{
4455
const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
@@ -161,15 +172,13 @@ static void bootloader_super_wdt_auto_feed(void)
161172
REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, 0);
162173
}
163174

164-
static void bootloader_init_uart_console(void)
175+
#if CONFIG_ESP_CONSOLE_UART_CUSTOM
176+
void IRAM_ATTR esp_rom_uart_putc(char c)
165177
{
166-
const int uart_num = 0;
167-
168-
esp_rom_install_uart_printf();
169-
esp_rom_uart_tx_wait_idle(0);
170-
uint32_t clock_hz = UART_CLK_FREQ_ROM;
171-
esp_rom_uart_set_clock_baudrate(uart_num, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
178+
while (uart_ll_get_txfifo_len(alt_console_uart_dev) == 0);
179+
uart_ll_write_txfifo(alt_console_uart_dev, (const uint8_t *) &c, 1);
172180
}
181+
#endif
173182

174183
esp_err_t bootloader_init(void)
175184
{
@@ -190,7 +199,7 @@ esp_err_t bootloader_init(void)
190199
// config clock
191200
bootloader_clock_configure();
192201
/* initialize uart console, from now on, we can use ets_printf */
193-
bootloader_init_uart_console();
202+
bootloader_console_init();
194203
// update flash ID
195204
bootloader_flash_update_id();
196205
// read bootloader header

boot/espressif/include/flash_map_backend/flash_map_backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ uint8_t flash_area_erased_val(const struct flash_area *area);
7777
int flash_area_get_sectors(int fa_id, uint32_t *count,
7878
struct flash_sector *sectors);
7979

80+
//! Retrieve the flash sector a given offset belongs to.
81+
int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector);
82+
8083
//! Returns the `fa_id` for slot, where slot is 0 (primary) or 1 (secondary).
8184
//!
8285
//! `image_index` (0 or 1) is the index of the image. Image index is

0 commit comments

Comments
 (0)