Loading Image in app and passing to bootloader

I am using SDK16 and attempting to load images for loading.I have questions and would welcome pointers

1. I need to use the application to load a new image into memory ready for loading. What is preferred method to pass information to the bootloader so that it will load the image on next reboot? I have adapted open bootloader to remove all the transports.

2. When I am developing an application and use an IDE to load a new image, how can the bootloader be informed there is a valid app so that it runs the app?

Parents
  • Hello,

    I have adapted open bootloader to remove all the transports.

    What bootloader, exactly did you start with?

    IT's been a while since I looked into this, but there are ways to make the bootloader use images already stored in flash. You need to store it in flash as if it was uploaded by the bootloader itself, but I am not sure about what you mean by "Open bootloader" Did you disable the security? Or just the transport layers?

    Best regards,

    Edvin

  • I started from an example in SDK16 called open_bootloader and disabled all the transports as these are not used in the bootleader, rather the applications places a binary copy of the image in flash using a file sent in ascii format, either hex or srec, and coverts on the fly. This provides information on load address, store address, and size, and could also manage merged hex.

    My questions are:

    1. How does my application pass information to the bootloader that there is an image in flash to be loaded. Is there a preferred method. If not then I can use a common area of flash.

    2. If I have a bootloader, how do I provide information to the bootloader that there is a valid application to run if I load the application using the flash tools provided by an IDE.The problem is that I can't use the app to signal the bootloader as the app is never run.

  • I see. This example bootloader is intended for the nRF52840 Dongle, which has a USB bootloader where the security is taken out, because it should work with the nRF52840 dongle, which doesn't have a bootloader. Hence, the default way to update the application running on it is via nRF Connect for Desktop -> Programmer. Hence it should accept .hex files that are not signed, but part of the magic here happens in nRF Connect for Desktop, which is not open source. Hence, I recommend that you start with the secure_bootloader example.

    So the way this normally works, is to use the ble_app_buttonless_dfu example, and implement this into your custom application. In this example the application only tells the bootloader that it will receive a new image, and that the bootloader should restart in DFU mode. So your task will be to make the application handle the transfer part, and when this is done, and the new image is stored in flash, tell the bootloader to start working on it. You must decide whether you want to implement the "init packet" which contains the signature of the application and the size of it, which is usually used to check that 1: The image is valid, signed with the proper key, and 2: whether it will fit in a second slot or not. In your case, it needs to fit in a second slot, because it is the running application that handles the transfer. 

    To tell the bootloader to enter DFU mode, it uses the GPREGRET register, which is retained during the reset, and checked in the bootloader's:

    nrf_bootloader_init() -> dfu_enter_check() which checks the GPREGRET register. 

    You can also see in the nrf_bootloader_init() that it checks bank 1 whether a new application is present. So if everything is in transferred and stored correctly, then you may be able to just reset, and check if the bootloader picks it up.

    Some other useful functions: on_data_obj_execute_request_sched() and on_data_obj_execute_request() in nrf_dfu_req_handler.c, which are the functions triggered when either the init packet or the entire image is transferred, and the bootloader should take action.

    Best regards,

    Edvin

  • Thank you for the hints. I think I understand how I can use common MBR memory to pass the paramaters to flash the image that is loaded and use GPREGRET to indicate to the bootloader to start the process.

    My remaining question was how an app loaded using IDE can indicate to the bootlloader there is a valid app and so the bootloader runs the app. I am assuming it has to write to bank0?

  • What I tried to say was that you use the GPREGRET to tell the bootloader to do DFU stuff instead of just starting the old application (which is the default behavior). When you do so, the bootloader will start investigating the image present in bank1. If it is valid, it will perform a swap, so that whatever was in bank1 will be moved to bank0, and then it will start the new app which then will be present in bank0. 

    BR,
    Edvin

  • I need to clarify my question. When I am developing code I develop in the IDE and then use the IDE to load directly to flash - I do not use the DFU to load the new image. In this case the bootloader does not find a valid image in bank 0 or bank 1 and so sits in the bootloader.

    I can change the code so that I poke 1 into bank 0 to fool the bootloader and so run the new image, but I wondered if there was a preferred method using a flag.

  • I have a further question. My image contains several separate code sections, normally this is the app and then a section to write to the various registers in upper memory. The hex (srec) file contains the separate address sections where to load. How is the bootloader expected to deal with loading these separate sections?

Reply Children
  • Malcolm said:
    the bootloader does not find a valid image in bank 0 or bank 1 and so sits in the bootloader.

    This is because it is lacking something called the "bootloader settings", which is a chunk in flash containing some information about the bootloader. When you perform a DFU operation, these settings are generated by the bootloader and stored in the settings page. If you flash the application manually, you need to generate these bootloader settings, and flash it together with the application (for simplicity, I recommend flashing using nrfjprog while dealing with bootloaders. Write a .bat file with the commands you want to run, and run it from your command prompt.)

    So after you build your application, you can run something like this:

    nrfutil settings generate ... settings.hex
    nrfjprog --program <path to application hex file> --verify --sectorerase
    nrfjprog --program <path to settings.hex> --verify --sectorerase
    nrfjprog --program <path to bootloader.hex> --verify --sectorerase
    nrfjprog --reset

    you can copy this text into a file called e.g. test_application_and_bootloader.bat, and run it from a cmd line.

    Malcolm said:
    The hex (srec) file contains the separate address sections where to load. How is the bootloader expected to deal with loading these separate sections?

    Does your application.hex actually program these addresses, or are they indirectly written by the application?

    I guess that it is not part of your application hex file, but the application writes to a certain area in the upper flash during runtime, which would also be the case if you are using e.g. the peer manager to store bonding data for Bluetooth connections. 

    In that case, the bootloader will set aside an area in the top of the flash, right before the bootloader itself. The size of this area is determined by a define in the bootloader's sdk_config.h called NRF_DFU_APP_DATA_AREA_SIZE. This is the size (in bytes) that the bootloader will never touch, either for validating the application, or while performing a DFU. By default this is set to 3 flash pages (each flash page is of size 4096 bytes).

    If you are using FDS in addition to some custom data on different flash pages, make sure that they are all in the top of the flash (the FDS pages will automatically be closest to the bootloader), and that the NRF_DFU_APP_DATA_AREA_SIZE is up to date.

    Best regards,

    Edvin

  • My application already does 90% of the work and so I have decided to add code to erase the old image and flash the new image. This is easily done by running the required code from RAM so that it is not erased. This works.

    The hex file contains a value to be written at 10001018 (UICR_PARAM_PAGE_ADDR) and the start address for the application. My question is where to write the start address so that soft device jumps to the correct location to run the application as I do not have a bootloader.

    This approach works if I flash the same image as the start address does not change, but will fail if i change the code in the image.

  • Malcolm said:
    This is easily done by running the required code from RAM so that it is not erased. This works.

    You should consider what would happen if you loose power during this operation. At least if this is an end product, it is not unlikely that someone are to power off the device, not knowing the consequences. 

    Malcolm said:
    The hex file contains a value to be written at 10001018 (UICR_PARAM_PAGE_ADDR) and the start address for the application

    You can't update the UICR on the nRF52840 during runtime. That is, you can write to it if it is 0xFFFFFFFF, but if it is not 0xFFFFFFFF, you can't write to it without erasing it, and you can't erase it from the application on the nRF52840. So if you need to update this register on every DFU, then you can't do that. 

    But why do you need to update the value at UICR_PARAM_PAGE_ADDR? Wouldn't it be the same every time?

    Best regards,

    Edvin

  • I accept your point about the bootloader and so I am returning to determine if I can fix issues so the bootlloader will cooperate with my app:

    1. How do I initialise access to memory used by the bootloader within my app to pass information on the image to flash (i.e write to bank 1 with parameters)?

    2. How do I ensure my app creates a separate area in flash for FDS storage so it does not use the same area as bootloader?

    3. When the bootloader starts an app (nrf_bootloader_app_start) how do I initialise NRF_LOG to use RTT. Before the bootlloader was used this used to work, but now I only see bootloader messages?

  • 1: Not sure exactly what you mean? Do you mean how to tell the bootloader to start the DFU process, moving the new application to the correct place? If so, you can look at the ble_app_buttonless_dfu example, which does this. It writes a value to the GPREGRET register in ble_dfu.c:

        err_code = sd_power_gpregret_set(0, BOOTLOADER_DFU_START);
        VERIFY_SUCCESS(err_code);

    This register is retained during resets, and the bootloader checks this register on startup to decide whether to enter DFU mode or not.

    Malcolm said:
    2. How do I ensure my app creates a separate area in flash for FDS storage so it does not use the same area as bootloader?

    In your bootloader's sdk_config.h, you will find a definition:

    NRF_DFU_APP_DATA_AREA_SIZE

    Which is the number of bytes closest to the bootloader (the top, right beneath the bootloader) that the bootloader will never touch. This is the location where FDS is also being placed. By default, this has the value 12288, which is 3 flash pages, but you can adjust this to fit your application.

    Malcolm said:
    3. When the bootloader starts an app (nrf_bootloader_app_start) how do I initialise NRF_LOG to use RTT. Before the bootlloader was used this used to work, but now I only see bootloader messages?

    The way that RTT logging works is that the messages are written to an area in ram. When you have two instances (bootloader and application) using RTT, then these will end up in different places. Unfortunately, the way that the RTT terminals work is that it looks for a special word in RAM where it will find the logs. It usually looks until it finds it, and starts printing the logs from there. So if you have two places in RAM where this appears, then you need to either search for another address where this is set. You can specify what addresses to search in, so if you find the first place in one RTT log instance, you can search for only the ram addresses after that.

    Or a simpler option is probably to disable the RTT logging from the bootloader when you want to monitor the logs from the application.

    Best regards,

    Edvin

Related