How to create custom MCUBOOT for DFU

Hello!

I am creating a DFU application and want to customize the bootloader code.

I used this example: https://devzone.nordicsemi.com/guides/nrf-connect-sdk-guides/b/software/posts/ncs-dfu

How can you include a custom MCUBOOT when doing this example?

Parents Reply Children
  • Hi Markus, 

    I haven't looked deep into the code of smp_svr. So I can't point you to the exact place of the flash writing when receiving new image. 

    But as far as I know the flash handling is in fs_mgmt.c in \zephyr\subsys\mgmt\mcumgr\lib\cmd\fs_mgmt\src. And the image management is in \zephyr\subsys\mgmt\mcumgr\lib\cmd\img_mgmt. 

    Also, pay attention to the smp.c file, it's when the SMP request is handled. 

  • Hi again!

    We have looked into the files.

    It seems like img_mgmt.c is an appropriate file to modify. There is a function at line 392 called img_mgmt_upload(struct mgmt_ctxt *ctxt). Also check Line 522 ("/* Write the image data to flash. */")

    We believe this could be a good location to include decompress. What do you think? Or should it be "deeper" in the data sending.

  • Hi Markus, 


    I have to admit that I haven't tried to modify the img_mgmt library myself. But it looks to me that it's where the flash write actually happened. So the function img_mgmt_impl_write_image_data() will write the data it get from cbor_read_object() to flash. The decompression can happens here. I'm not sure how you do decompress but what the code here is doing is to write to flash chunk by chunk, until the /* If this is the last chunk */ . 
    So if your decompression would work with each chunk then you can do it on the fly, or you will do it when it's the last chunk and the whole image has been received. It depends on how you will do decompression. 

  • Hi, we decided upon implementing decompression chunk by chunk, so in the img_mgmt_impl_write_image_data() function. A problem now occurrs here in the upload function of img_mgmt.c:

     

    It seems like the img_mgmt_impl_upload_inspect does not accept the compressed file.

  • Hi Markus, 

    Please inspect which condition is failed inside img_mgmt_impl_upload_inspect(). 
    I'm not so sure how you created the chunks of compressed file but it need to reflect how the "normal" image is sent. 
    Especially in the first chunk, it must include the image header, and the hash SHA256 of the image. 

    As far as I can see, to be able to achieve what you planning to do, a deep study of the img_mgt library is needed. And I would suggest to go step by step as follow : 

    - First please try to study a normal DFU update using a very small image maybe just 1-2 pages in flash size. This make it easy to study a normal successful DFU update. 

    - After that you can try uploading slightly modified version of the same image. You can do for example XOR the image with a key. Then when you "decompress" it you XOR with the same key. This way, you modified the image but the size of the image/chunk is not changed. To be able to do this you must be able to handle image header and the hash correctly. In addition at this step you would be able to manipulate how the image is written to flash. 

    - The last step would be to do actual compression and sending smaller chunk  size or smaller number of chunks. Then when you write the image to flash you can do compression and manipulate the size of the image you are writing on flash. At this moment the offset in the image you sent and the offset in flash that you write the image would be different and you would need to find a solution to accommodate that.  

    Again, I haven't done this myself, so this is only a suggestion. And please be aware that img_mgmt is not our code it's contributed to Zephyr by mynewt. 

Related