Skip to content

Commit 02ae1c8

Browse files
sigvartmhde-nordic
authored andcommitted
[nrf noup] bootutil: loader.c: Fix network core update external flash
Using sequential updates on the nRF53 with external flash would cause network core updates to fail. This is because the update would be located in the external flash which is network core cannot access. This fixes the issue by copying the data from the external flash into RAM of the application core which can be read by the network core. This commit also contains some minor renaming to align with the naming scheme used in the `loader.c` file for flash areas. Ref: NCSDK-21379 Signed-off-by: Sigvart Hovland <[email protected]>
1 parent 8fe7070 commit 02ae1c8

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

boot/bootutil/src/loader.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#include <dfu/pcd.h>
5353
#endif
5454

55+
#if defined(CONFIG_FLASH_SIMULATOR)
56+
#include <zephyr/drivers/flash/flash_simulator.h>
57+
#endif
58+
5559
#ifdef MCUBOOT_ENC_IMAGES
5660
#include "bootutil/enc_key.h"
5761
#endif
@@ -936,7 +940,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
936940
bool upgrade_valid = false;
937941

938942
#if defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP)
939-
const struct flash_area *secondary_fa =
943+
const struct flash_area *fap_secondary_slot =
940944
BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
941945
struct image_header *hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
942946
uint32_t reset_addr = 0;
@@ -950,7 +954,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
950954
*/
951955

952956
if (hdr->ih_magic == IMAGE_MAGIC) {
953-
rc = flash_area_read(secondary_fa, hdr->ih_hdr_size +
957+
rc = flash_area_read(fap_secondary_slot, hdr->ih_hdr_size +
954958
sizeof(uint32_t), &reset_addr,
955959
sizeof(reset_addr));
956960
if (rc != 0) {
@@ -1018,24 +1022,52 @@ boot_validated_swap_type(struct boot_loader_state *state,
10181022
* available
10191023
*/
10201024
if (upgrade_valid && reset_addr > PM_CPUNET_B0N_ADDRESS) {
1021-
struct image_header *hdr = (struct image_header *)secondary_fa->fa_off;
1025+
uint32_t fw_size = hdr->ih_img_size;
10221026
uint32_t vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
10231027
uint32_t *net_core_fw_addr = (uint32_t *)(vtable_addr);
1024-
uint32_t fw_size = hdr->ih_img_size;
1028+
#if defined(CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY)
1029+
size_t mock_size;
1030+
uint32_t *flash_sim_addr = flash_simulator_get_memory(NULL, &mock_size);
1031+
/* Direct copy from flash to mocked flash in SRAM. */
1032+
rc = flash_area_read(fap_secondary_slot, hdr->ih_hdr_size , (void *)(flash_sim_addr),
1033+
fw_size);
1034+
if (rc != 0) {
1035+
BOOT_LOG_INF("Error whilst copying image from Flash to SRAM(flash_sim): %d",
1036+
rc);
1037+
return BOOT_SWAP_TYPE_FAIL;
1038+
}
1039+
net_core_fw_addr = flash_sim_addr;
1040+
#endif
10251041
BOOT_LOG_INF("Starting network core update");
10261042
rc = pcd_network_core_update(net_core_fw_addr, fw_size);
10271043

10281044
if (rc != 0) {
10291045
swap_type = BOOT_SWAP_TYPE_FAIL;
10301046
} else {
10311047
BOOT_LOG_INF("Done updating network core");
1048+
10321049
#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
10331050
/* swap_erase_trailer_sectors is undefined if upgrade only
10341051
* method is used. There is no need to erase sectors, because
10351052
* the image cannot be reverted.
10361053
*/
1037-
rc = swap_erase_trailer_sectors(state,
1038-
secondary_fa);
1054+
rc = swap_erase_trailer_sectors(state, fap_secondary_slot);
1055+
if (rc != 0) {
1056+
BOOT_LOG_DBG("Unable to erase swap trailer info");
1057+
}
1058+
#elif defined(MCUBOOT_OVERWRITE_ONLY)
1059+
size_t last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
1060+
rc = boot_erase_region(fap_secondary_slot,
1061+
boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1062+
last_sector),
1063+
boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1064+
last_sector));
1065+
if (rc != 0) {
1066+
/* This will cause the network update to happen again
1067+
* on the next reboot
1068+
*/
1069+
BOOT_LOG_DBG("Unable to erase trailer info");
1070+
}
10391071
#endif
10401072
swap_type = BOOT_SWAP_TYPE_NONE;
10411073
}

0 commit comments

Comments
 (0)