NRF5340 network core DFU with external flash

HI,

I'm trying to configure the MCUboot / Netboot with an external QSPI flash. I can do it for the APP core with MCUboot, however I get lost how it work with the NET core / Netboot. I can do this with storing update in internal flash but not external flash.

Looking into the "bootloader\mcuboot\boot\bootuil\src\loader.c", I found something strange, in this function:

static int
boot_validated_swap_type(struct boot_loader_state *state,
                         struct boot_status *bs)
{
    int swap_type;
    fih_int fih_rc = FIH_FAILURE;
    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.
     */

    if (hdr->ih_magic == IMAGE_MAGIC) {
        vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
        vtable = (uint32_t *)(vtable_addr);
        reset_addr = vtable[1];
#ifdef PM_S1_ADDRESS
        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 */
    }

It try to get the image's "ih_magic" and "reset_addr" from "hdr" which eventually go to "state.imsg[0][1].area" (to determine if the update image is for APP or NET). When storing the update in internal flash, this is correctly pointed. But after I change it to external storage, this area points to the external storage's physical address, just like this: (I store the update in MX25R64, address 0x00)

So this function just get the ih_magic from the INTERNAL FLASH by using my external flash address (0x00), of course it will never get the "reset_addr" correctly, and always think my update image is APP core.

Is there anything I missed? Or any fix for this function?

Parents
  • I'm not sure which code you want because that loader.c is come from the NCS. Anyway, I amend the "peripheral_uart" sample project to use the external flash as FW storage, this is base on the 5340DK board.

    www.dropbox.com/.../peripheral_uart.zip

    I add some lines in main.c, so it will write a simple header/trailer to the on board flash, so the bootloader should "think" the FW image is valid (but I didn't include the whole image to it so the SHA verification should failed, this is good because if so, it will try to write to the internal flash, which will corrupt my APP). The header/trailer is comes from the "net_core_app_update.bin" as you can see from the image's vector table.

    You can set a breakpoint in the "boot_validated_swap_type" of loader.c, you should able to see that it want to get the "reset_addr" from the image, to determine if this APP or NET, but failed, because it even cannot get the correct IMAGE_MAGIC due to the incorrect pointer, as I mentioned in this post.

    PS, I'm using the NCS v1.6.1.

  • Hi Nordic guy,

    Please find attachment.

    7457.peripheral_uart.zip

    Hope to get the feedback soon.

Reply Children
No Data
Related