Boot swap - technique to reprogram bootloader itself

Bootloader is a small utility program which can be added on to flash microcontrollers. It helps us to download code to the microcontroller whenever we need without using a flash programmer hardware and without taking out the chip from the target board/device.

But what if we have found some bug in the bootloader itself and want to reprogram the bootloader ?
Some options are
  1. Use a flash programmer
  2. Using ISP (for those boards having support)
But what about embedded systems in which controller is not accessable and the only option is reprogramming itself  using the bootloader ?

This is possible if
  1. Microcontroller has capability to execute code from RAM.
  2. Microcontrollerhas hardware capability to swap the boot blocks(Process called as boot swapping).
Brief note about bootloader
Layout of a simple bootloader
In a simple bootloader example shown above, bootloader is present in the region 0 to 0x1000. So this region will not be changed during reprogramming.
  1. A flash programmer/ISP etc is used to download Bootloader (or Bootloader + initial application) code to the microcontroller.
  2. Normaly bootloader passess conltorl goes to the application.
  3. Whenever there is a command to reprogram, program flow control goes to the bootloader.
  4. Bootloader starts receiving the new Application code from the remote unit using one of the communication medium(CAN, LIN, SPI, RS232, USB etc).
  5. Bootloader verifies whether the data is completely written without any errors, and finilizes the reprogramming/reflashing.
Bootloader with option for reprogramming the bootloader itself

I will explain this with the help of the microcontroller v850 series microcontroller from Renesas.
Flash Layout of v850 Series controllers - Reprogramming the entire flash Memory (Boot Swap)
v850 controller flash memory ROM has the following features
  1. Flash memory is divided in to smaller blocks, which can be individually erased as shown in the figure above.
  2. Block 0 can be swapped with Block 2 and Block 1 can be swapped with Block 3.
  3. This will help in reprogramming the entire flash memorey(including bootloader) without any risk of dumbing the chip.
Updating Bootloaders
 Now the software can be designed in two ways
A) Always the entire flash memory is reflashed.
In this cas there will not be seperate bootloader/application. So download is done in two stages. Also it is ensured that the code for downloading and the dependencies are present in the lower two bocks. Steps involved will be.
  1. Starts after receiving the command for downloading.
  2. Actual execution of the code which reprogramms will be copied to the RAM and executin will jump to the code in RAM. For this purpose, code is compiled such that it can run from any location(Position independent code - PIC).
  3. Downloads and writes the lower two blocks(0 & 1) of code in the blocks (1 & 2).
  4. Verifies whether two blocks are written without any error using CRC/checksum.
  5. Swaps blocks 0 and 1 with bocks 2 and 3. At this stage even if something goes wrong, the old code in bocks 0 and 1 can download the new code again. (Stage 1 complete)
  6. New code starts and continues downloading the rest of the code.
  7. Download is verified and finalised.
B) Simple architecture is followed, special code with bootloader(Bootswapper) will be downloaded to update the bootloader.

Updation of bootloader using bootswapper

In this case bootloader will work same as that of the simple bootloader. But if we want to update the bootlaoder, then we can download a special code (say Bootswapper). This special code will contain the code for the new boot loader which is offsetted to some other adderss. After complete download, bootloader will pass the control to the application(here the special code - bootswapper). Now this special code will execute from RAM and copy the address offseted new bootloader to the bock 2 and 3. After that it will swap the blocks to have the new bootloader in the blocks 0 and 1.

No comments :

Post a Comment