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.

  • 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?

  • 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.

Reply
  • 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.

Children
  • 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

  • 1. I have used GPREGRET in the application to signal to the bootloader to commence loading a new image.

    2. I am struggling to understand how to configure FDS so that my application and the bootloader can share a common area of memory.  I can get them to use separate areas. However I need to exchange information on the location and size of the image to load. The bootloader uses FDS to define an area to store information on the banks, and my application uses FDS to define an area to store information on the image to load. Somehow one needs to access the information of the other.

    3. The simple option works fine.

  • Oh, I think I misunderstood you in your previous reply, regarding the shared memory.

    Usually the way this works, is that this is part of the init packet (and then later the bootloader settings, which holds information about the application image. 

    I am actually not sure what the best way to do this without an init packet, which I guess that you don't have. I guess I would look into just using the first flash page in the bank to store the size of the application (or elsewhere), and this information will be present when the bootloader is told to start changing the application. Use the first page to store your application information (size, CRC, and so on), and then you store the actual application image from the second page and onwards.

    Best regards,

    Edvin

Related