Skip to content

Commit

Permalink
[nrf noup] boot: nrf53-specific customizations
Browse files Browse the repository at this point in the history
- Add network core bootloader implementation

  Enables network core updates of nrf53 using MCUBoot by identifying
  images through their start addresses. Also implements the control and
  transfer using the PCD module.

- Add support for multi image DFU using partition manager.

- Add check for netcore addr if NSIB is enabled so netcore updates works

- boot: zephyr: move thingy53_nrf5340_cpuapp.conf downstream

  Moved the board configuration for Thingy:53 Application Core to the
  nRF Connect SDK MCUboot downstream repository. The configuration file
  contains references to the Kconfig modules that are only available in
  the nRF Connect SDK. The current configuration is set up to work in the
  nRF Connect SDK environment and cannot be used upstream.

- pm: enable ram flash partition using common flag

  This patch makes mcuboot_primary_1 ram-flash partition
  selectable using CONFIG_NRF53_MCUBOOT_PRIMARY_1_RAM_FLASH
  property. This is needed since CONFIG_NRF53_MULTI_IMAGE_UPDATE
  become not only configuration which requires that partition.

- MCUBoot configures USB CDC by its own. There is no need for
  BOARD_SERIAL_BACKEND_CDC_ACM option to configure anything which is
  later overwritten anyway.

  Jira: NCSDK-18596

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
Signed-off-by: Emil Obalski <emil.obalski@nordicsemi.no>
Signed-off-by: Håkon Øye Amundsen <haakon.amundsen@nordicsemi.no>
Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Kamil Piszczek <Kamil.Piszczek@nordicsemi.no>
Signed-off-by: Ole Sæther <ole.saether@nordicsemi.no>
Signed-off-by: Sigvart Hovland <sigvart.hovland@nordicsemi.no>
Signed-off-by: Simon Iversen <simon.iversen@nordicsemi.no>
Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
Signed-off-by: Trond Einar Snekvik <Trond.Einar.Snekvik@nordicsemi.no>
Signed-off-by: Mateusz Kapala <mateusz.kapala@nordicsemi.no>
(cherry picked from commit 625c84e)
(cherry picked from commit 6fec3ff)
(cherry picked from commit 3883cb9)
(cherry picked from commit b96bed8)
Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
  • Loading branch information
sigvartmh authored and tejlmand committed May 3, 2023
1 parent 6804ab2 commit 4302a91
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 26 deletions.
96 changes: 70 additions & 26 deletions boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
#include "bootutil/boot_hooks.h"
#include "bootutil/mcuboot_status.h"

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
#include <dfu/pcd.h>
#endif

#ifdef MCUBOOT_ENC_IMAGES
#include "bootutil/enc_key.h"
#endif
Expand Down Expand Up @@ -912,42 +916,52 @@ boot_validated_swap_type(struct boot_loader_state *state,
{
int swap_type;
FIH_DECLARE(fih_rc, FIH_FAILURE);
#ifdef PM_S1_ADDRESS
bool upgrade_valid = false;

#if defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP)
const struct flash_area *secondary_fa =
BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
struct image_header *hdr = (struct image_header *)secondary_fa->fa_off;
uint32_t vtable_addr = 0;
uint32_t *vtable = 0;
uint32_t reset_addr = 0;
/* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
* B1 slot S0 or S1) share the same secondary slot, we need to check
* whether the update candidate in the secondary slot is intended for
* image 0 or image 1 primary by looking at the address of the reset
* vector. Note that there are good reasons for not using img_num from
* the swap info.
*/
const struct flash_area *secondary_fa =
BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
struct image_header *hdr =
(struct image_header *)secondary_fa->fa_off;

if (hdr->ih_magic == IMAGE_MAGIC) {
const struct flash_area *primary_fa;
uint32_t vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
uint32_t *vtable = (uint32_t *)(vtable_addr);
uint32_t reset_addr = vtable[1];
int rc = flash_area_open(
flash_area_id_from_multi_image_slot(
BOOT_CURR_IMG(state),
BOOT_PRIMARY_SLOT),
&primary_fa);

if (rc != 0) {
return BOOT_SWAP_TYPE_FAIL;
}
/* Get start and end of primary slot for current image */
if (reset_addr < primary_fa->fa_off ||
reset_addr > (primary_fa->fa_off + primary_fa->fa_size)) {
/* The image in the secondary slot is not intended for this image
*/
return BOOT_SWAP_TYPE_NONE;
}
}
vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
vtable = (uint32_t *)(vtable_addr);
reset_addr = vtable[1];
#ifdef PM_S1_ADDRESS
#ifdef PM_CPUNET_B0N_ADDRESS
if(reset_addr < PM_CPUNET_B0N_ADDRESS)
#endif
{
const struct flash_area *primary_fa;
int rc = flash_area_open(flash_area_id_from_multi_image_slot(
BOOT_CURR_IMG(state),
BOOT_PRIMARY_SLOT),
&primary_fa);

if (rc != 0) {
return BOOT_SWAP_TYPE_FAIL;
}
/* Get start and end of primary slot for current image */
if (reset_addr < primary_fa->fa_off ||
reset_addr > (primary_fa->fa_off + primary_fa->fa_size)) {
/* The image in the secondary slot is not intended for this image
*/
return BOOT_SWAP_TYPE_NONE;
}
}
#endif /* PM_S1_ADDRESS */
}
#endif /* PM_S1_ADDRESS || CONFIG_SOC_NRF5340_CPUAPP */

swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
if (BOOT_IS_UPGRADE(swap_type)) {
Expand All @@ -961,7 +975,37 @@ boot_validated_swap_type(struct boot_loader_state *state,
} else {
swap_type = BOOT_SWAP_TYPE_FAIL;
}
} else {
upgrade_valid = true;
}

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
/* If the update is valid, and it targets the network core: perform the
* update and indicate to the caller of this function that no update is
* available
*/
if (upgrade_valid && reset_addr > PM_CPUNET_B0N_ADDRESS) {
uint32_t fw_size = hdr->ih_img_size;

BOOT_LOG_INF("Starting network core update");
int rc = pcd_network_core_update(vtable, fw_size);

if (rc != 0) {
swap_type = BOOT_SWAP_TYPE_FAIL;
} else {
BOOT_LOG_INF("Done updating network core");
#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
/* swap_erase_trailer_sectors is undefined if upgrade only
* method is used. There is no need to erase sectors, because
* the image cannot be reverted.
*/
rc = swap_erase_trailer_sectors(state,
secondary_fa);
#endif
swap_type = BOOT_SWAP_TYPE_NONE;
}
}
#endif /* CONFIG_SOC_NRF5340_CPUAPP */
}

return swap_type;
Expand Down
73 changes: 73 additions & 0 deletions boot/zephyr/boards/thingy53_nrf5340_cpuapp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
CONFIG_SIZE_OPTIMIZATIONS=y

CONFIG_SYSTEM_CLOCK_NO_WAIT=y
CONFIG_PM=n

CONFIG_MAIN_STACK_SIZE=10240
CONFIG_MBEDTLS_CFG_FILE="mcuboot-mbedtls-cfg.h"

CONFIG_BOOT_MAX_IMG_SECTORS=2048
CONFIG_BOOT_SIGNATURE_TYPE_RSA=y

# Flash
CONFIG_FLASH=y
CONFIG_BOOT_ERASE_PROGRESSIVELY=y
CONFIG_SOC_FLASH_NRF_EMULATE_ONE_BYTE_WRITE_ACCESS=y
CONFIG_FPROTECT=y

# Serial
CONFIG_SERIAL=y
CONFIG_UART_LINE_CTRL=y

# MCUBoot serial
CONFIG_GPIO=y
CONFIG_MCUBOOT_SERIAL=y
CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD=y
CONFIG_BOOT_SERIAL_CDC_ACM=y

# Required by QSPI
CONFIG_NORDIC_QSPI_NOR=y
CONFIG_NORDIC_QSPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_NORDIC_QSPI_NOR_STACK_WRITE_BUFFER_SIZE=16

# Required by USB and QSPI
CONFIG_MULTITHREADING=y

# USB
CONFIG_BOARD_SERIAL_BACKEND_CDC_ACM=n
CONFIG_USB_DEVICE_REMOTE_WAKEUP=n
CONFIG_USB_DEVICE_MANUFACTURER="Nordic Semiconductor ASA"
CONFIG_USB_DEVICE_PRODUCT="Bootloader Thingy:53"
CONFIG_USB_DEVICE_VID=0x1915
CONFIG_USB_DEVICE_PID=0x5300
CONFIG_USB_CDC_ACM=y

# Decrease memory footprint
CONFIG_CBPRINTF_NANO=y
CONFIG_TIMESLICING=n
CONFIG_BOOT_BANNER=n
CONFIG_CONSOLE=n
CONFIG_CONSOLE_HANDLER=n
CONFIG_UART_CONSOLE=n
CONFIG_USE_SEGGER_RTT=n
CONFIG_LOG=n
CONFIG_ERRNO=n
CONFIG_PRINTK=n
CONFIG_RESET_ON_FATAL_ERROR=n
CONFIG_SPI=n
CONFIG_I2C=n
CONFIG_UART_NRFX=n

# The following configurations are required to support simultaneous multi image update
CONFIG_PCD_APP=y
CONFIG_UPDATEABLE_IMAGE_NUMBER=2
CONFIG_BOOT_UPGRADE_ONLY=y
# The network core cannot access external flash directly. The flash simulator must be used to
# provide a memory region that is used to forward the new firmware to the network core.
CONFIG_FLASH_SIMULATOR=y
CONFIG_FLASH_SIMULATOR_DOUBLE_WRITES=y
CONFIG_FLASH_SIMULATOR_STATS=n

# Enable custom command to erase settings partition.
CONFIG_ENABLE_MGMT_PERUSER=y
CONFIG_BOOT_MGMT_CUSTOM_STORAGE_ERASE=y
23 changes: 23 additions & 0 deletions boot/zephyr/include/sysflash/sysflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

#elif (MCUBOOT_IMAGE_NUMBER == 2)

/* If B0 is present then two bootloaders are present, and we must use
* a single secondary slot for both primary slots.
*/
#ifdef PM_B0_ADDRESS

extern uint32_t _image_1_primary_slot_id[];

#define FLASH_AREA_IMAGE_PRIMARY(x) \
Expand All @@ -31,6 +36,24 @@ extern uint32_t _image_1_primary_slot_id[];
(x == 1) ? \
PM_MCUBOOT_SECONDARY_ID: \
255 )
#else

#define FLASH_AREA_IMAGE_PRIMARY(x) \
((x == 0) ? \
PM_MCUBOOT_PRIMARY_ID : \
(x == 1) ? \
PM_MCUBOOT_PRIMARY_1_ID : \
255 )

#define FLASH_AREA_IMAGE_SECONDARY(x) \
((x == 0) ? \
PM_MCUBOOT_SECONDARY_ID: \
(x == 1) ? \
PM_MCUBOOT_SECONDARY_1_ID: \
255 )

#endif /* PM_B0_ADDRESS */

#endif
#define FLASH_AREA_IMAGE_SCRATCH PM_MCUBOOT_SCRATCH_ID

Expand Down
7 changes: 7 additions & 0 deletions boot/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const struct boot_uart_funcs boot_funcs = {
#include <zephyr/drivers/hwinfo.h>
#endif

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
#include <dfu/pcd.h>
#endif

/* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
* replaced by CONFIG_LOG_MODE_MINIMAL.
*/
Expand Down Expand Up @@ -681,6 +685,9 @@ void main(void)
;
}

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
pcd_lock_ram();
#endif
#endif /* USE_PARTITION_MANAGER && CONFIG_FPROTECT */

ZEPHYR_BOOT_LOG_STOP();
Expand Down
13 changes: 13 additions & 0 deletions boot/zephyr/pm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,16 @@ mcuboot_pad:
#ifdef CONFIG_FPROTECT
align: {start: CONFIG_FPROTECT_BLOCK_SIZE}
#endif

#if (CONFIG_NRF53_MCUBOOT_PRIMARY_1_RAM_FLASH)
mcuboot_primary_1:
region: ram_flash
size: CONFIG_NRF53_RAM_FLASH_SIZE
#endif /* CONFIG_NRF53_MULTI_IMAGE_UPDATE */

#if (CONFIG_NRF53_MULTI_IMAGE_UPDATE)
mcuboot_secondary_1:
region: external_flash
size: CONFIG_NRF53_RAM_FLASH_SIZE

#endif /* CONFIG_NRF53_MULTI_IMAGE_UPDATE */

0 comments on commit 4302a91

Please sign in to comment.