Skip to content

Commit cfec947

Browse files
authored
synchronized up to the mcu-tool/mcuboot 4eca54f
Synchronized to: mcu-tools/mcuboot@4eca54f - added precise check of the image size - loader: Added post copy hook to swap function - added Kconfig option for setting swap using move as default swap algorithm - zephyr: fixed ram loading for ARM, with correct handling of vector table when code has moved to RAM. - imgtool: add option to export public PEM merged using GitHub web gui. Signed-off-by: Andrzej Puzdrowski <[email protected]>
2 parents 13f6397 + 4eca54f commit cfec947

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
@@ -616,7 +616,7 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
616616
#if (BOOT_IMAGE_NUMBER > 1) || \
617617
defined(MCUBOOT_DIRECT_XIP) || \
618618
defined(MCUBOOT_RAM_LOAD) || \
619-
(defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
619+
defined(MCUBOOT_DOWNGRADE_PREVENTION)
620620
/**
621621
* Compare image version numbers not including the build number
622622
*
@@ -1332,6 +1332,8 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
13321332
boot_status_fails);
13331333
}
13341334
#endif
1335+
rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1336+
BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
13351337

13361338
return 0;
13371339
}
@@ -1903,6 +1905,60 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
19031905
#endif
19041906
}
19051907

1908+
/**
1909+
* Checks test swap downgrade prevention conditions.
1910+
*
1911+
* Function called only for swap upgrades test run. It may prevent
1912+
* swap if slot 1 image has <= version number or < security counter
1913+
*
1914+
* @param state Boot loader status information.
1915+
*
1916+
* @return 0 - image can be swapped, -1 downgrade prevention
1917+
*/
1918+
static int
1919+
check_downgrade_prevention(struct boot_loader_state *state)
1920+
{
1921+
#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1922+
(defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1923+
uint32_t security_counter[2];
1924+
int rc;
1925+
1926+
if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1927+
/* If there was security no counter in slot 0, allow swap */
1928+
rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1929+
BOOT_IMG(state, 0).area,
1930+
&security_counter[0]);
1931+
if (rc != 0) {
1932+
return 0;
1933+
}
1934+
/* If there is no security counter in slot 1, or it's lower than
1935+
* that of slot 0, prevent downgrade */
1936+
rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1937+
BOOT_IMG(state, 1).area,
1938+
&security_counter[1]);
1939+
if (rc != 0 || security_counter[0] > security_counter[1]) {
1940+
rc = -1;
1941+
}
1942+
}
1943+
else {
1944+
rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1945+
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1946+
}
1947+
if (rc < 0) {
1948+
/* Image in slot 0 prevents downgrade, delete image in slot 1 */
1949+
BOOT_LOG_INF("Image in slot 1 erased due to downgrade prevention");
1950+
flash_area_erase(BOOT_IMG(state, 1).area, 0,
1951+
flash_area_get_size(BOOT_IMG(state, 1).area));
1952+
} else {
1953+
rc = 0;
1954+
}
1955+
return rc;
1956+
#else
1957+
(void)state;
1958+
return 0;
1959+
#endif
1960+
}
1961+
19061962
fih_int
19071963
context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
19081964
{
@@ -2031,7 +2087,13 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
20312087
case BOOT_SWAP_TYPE_NONE:
20322088
break;
20332089

2034-
case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2090+
case BOOT_SWAP_TYPE_TEST:
2091+
if (check_downgrade_prevention(state) != 0) {
2092+
/* Downgrade prevented */
2093+
BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2094+
break;
2095+
}
2096+
/* fallthrough */
20352097
case BOOT_SWAP_TYPE_PERM: /* fallthrough */
20362098
case BOOT_SWAP_TYPE_REVERT:
20372099
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)