DFU using USB file system (not serial emulation)?

My project has a nrf5340+external flash, and an external USB-C connector. The device exposes a USB file system (FAT) which is hosted on the external flash partition, and appears as an external disk when connected to a host. This all works well and is used to update config, image files etc for the device to use.

I would like to be able to do application / network firmware updates by simply copying the new image (hex or bin) to this file system and rebooting. Has anyone done this? I'm guessing it involves using MCUBoot bootloader and copying the image into the secondary image slot somehow? I also want to put the secondard image slot on my external flash as won't have space on the internal flash - even better would be if MCUBoot could take the image directly from the file in the FS....

by the way, to be clear, I do NOT want to use the 'DFU over USB serial emulation' method, as this requires the user to install the specific tool to the host machine, which may not be available for their host or may not be allowed by IT policy (or by their level of expertise...). 

thanks for any pointers!

  • Hi Brian, 

    As far as I know you only need to call boot_write_img_confirmed() in the application at its first boot after the swap then it should be permanent. 

    You can find the call in the \nrf\samples\bluetooth\mesh\dfu\target sample. Where we call dfu_target_image_confirm() after bluetooth initialized. 

    From what I can see in the code swap type should be set to 

    swap_type = BOOT_SWAP_TYPE_PERM
    in boot_write_img_confirmed-> boot_set_next()

    Could you confirm you call dfu_target_image_confirm() in the application after the first boot after swapping ? 
    Please make sure you give the application enough time to write to flash before reset. 
  • As far as I know you only need to call boot_write_img_confirmed() in the application at its first boot after the swap then it should be permanent. 

    Yes, that was my understanding. My code calls 

    ret = boot_write_img_confirmed();
    exactly as per the sample. However the swap_type remains as BOOT_SWAP_TYPE_TEST - as far as I can see the bootutil code for boot_set_next() just updates the image_ok flag to SET...
    (inspecting the bootutil code on the mcu github)
    Where do you see the swap_type being updated?
  • Please make sure you give the application enough time to write to flash before reset. 

    yes, I give it at least 10s and this appears ot be ok as mcuboot sees image_ok as 0x1 after the reboot.

  • Hi Brian, 
    I was looking at this line https://github.com/mcu-tools/mcuboot/blob/main/boot/bootutil/src/bootutil_public.c#L534

    But the magic of the slot was not BOOT_MAGIC_UNSET ? I assume it's BOOT_MAGIC_GOOD?

    As far as I know at the first boot, the new image should already be in the primary slot and the old image should be in the secondary (test swap). 


    There are 3 scenarios where action (new swap) need to be taken as in here
    In your case if magic_primary_slot=BOOT_MAGIC_GOOD and image_ok_primary_slot= BOOT_FLAG_SET then I don't see why it would do a revert. 

  • Hi,

    Yes, thats what confuses me. At boot mcuboot tells me what its found:

    *** Booting nRF Connect SDK 3758bcbfa5cd ***
    I: Starting bootloader
    I: Primary image: magic=good, swap_type=0x2, copy_done=0x1, image_ok=0x1
    I: Secondary image: magic=good, swap_type=0x2, copy_done=0x3, image_ok=0x3
    I: Boot source: none
    I: Image index: 0, Swap type: test
    I: Starting swap using move algorithm.
    I: Bootloader chainload address offset: 0xe000

    In your case if magic_primary_slot=BOOT_MAGIC_GOOD and image_ok_primary_slot= BOOT_FLAG_SET then I don't see why it would do a revert. 

    And as you can see it has that, so it SHOULD NOT REVERT : and yet it does.

    Work around (but not a good long term solution) : I destroy the secondary slot by running a fake dfu_target process on it with just 1 buffer of 0's (which essentially makes the image invalid) -> mcuboot is forced to stay with my new primary). 

    Not very satisfactory, as it means I have no rollback later....but at least it stops it flip flopping at every boot!

Related