Hi
I have attached a "real-world" application that uses the swap feature, runs on the FRDM-K64F and loads new code over the UART.
In the attached file "uTaskerSerialBoot_SREC_Swap.zip" there are two versions of the application (each as a .bin and a s19 file). One has the version number V2 and the other the version number V3 so that it is easily seen that the swap has been done between 2 different software versions.
To use:
1. Load either xxxxV2 or xxxxV3.bin to start with an initial application. Assuming that the swap mechanism hasn't been used before (or after a full chip erase) it will be seen that it is uninitialised. The virtual COM port uses 57'600Baud and the following screen is displayed at reset.
Swap uninitialised
uTasker Serial Loader V1.2
============================
[Swap size 0x00080000]
bc = blank check
dc = delete code
ld = start load
sw = swap memory
If the blank check is performed (this is checking the alternative flash block) it will initially result in it being empty. If it is not, command a delete to erase it.
2. Now load the alternative software version as SREC file using the load command. I recommed TeraTerm as terminal emulator since one can drag and drop the file onto it to do a transfer.
First command "ld" and then copy the file. It will look like this (each dot is a programmed s-rec line), whereby the SREC is being copied to the alterative flash block:
> ld
Please start S-REC download: ...............................................................................................................................
...................................................................................................................................................................................
...................................................................................................................................................................................
.................................
Terminated - swap if required
After the complete code has been programmed the Flash block is not swapped automatically - with the "bc" command it can be seeen that the flash is no longer empty.
In this state the original software runs after a reset, although the new/alternative SW is in the alternative block waiting to be activated.
3. Command the swap using "sw" and then confirming it.
> sw
Swap memory [y/n] ? >
Swap OK - resetting...
Swap using 1
uTasker Serial Loader V1.3
============================
[Swap size 0x00080000]
bc = blank check
dc = delete code
ld = start load
sw = swap memory
After the swap has completed (takes a few hundred ms) "Swap OK" is indicated to confirm that all went well and then a reset is executed after a short delay.
It is seen that the new block is now used (a reset is always required to finalise the swap).
The flash swap block being used is shown as block 1.
4. Either new S-recs can be programmed using the same technique (for a guide to the operation see http://www.utasker.com/docs/uTasker/uTaskerSerialLoader.PDF ) or else it is possible to switch between the two programed software versions using the "sw" command again. For example, when I do it in this state:
> sw
Swap memory [y/n] ? >
Swap OK - resetting...
Swap using 0
uTasker Serial Loader V1.2
============================
[Swap size 0x00080000]
bc = blank check
dc = delete code
ld = start load
sw = swap memory
Now the block 0 is being used again, where the original software version is.
This can be repeated to quickly switch back and forward between the two available software versions.
One extra bit of information is that the mechanism also requires a sector in both blocks for use for a state value - the final sector in each block is thus reserved for this so the application size is max. 508k (half the flash = 512k - one sector of 4k).
This is thus a "real" application that allows the UART to be used to load the new code and also controls the swap mechanism. I used the uTasker SREC loader as base but the same is possible by including the swap part in the uTasker application and then doing the update via USB-CDC or Ethernet.
The question is however whether that is already the end of the story, and in my opinion is is certainly not!
Although the swap machanism (in its version V1.4 I believe, so I don't know whether it is recommended on older chips) tolerates power loss or some other failure during the swap process (in that case it should continue to completion after the next power up) this in itself is not a guarantee of a robust solution - it guarantees only a small part of it and in fact also not necessarily the most important part of it either.
Consider the following possibilies:
1. I command a swap 'before' I have loaded the new code. Now the board doesn't work and has to be sent back to the manufacturer for programming!
- solution is to not allow the swap until it is know that there is new code present.
2. I load a new software version that has an error in 'its' loading mechanism. Now I swap and the new version operates and I delete the original version becasue I want to upload a new one again. Now I can't continue - the board has to be sent back to the manufacturer for recovery.
- solution is to ensure that only fully verified code is loaded (best with some authentication to ensure that no one loads unverified code)
3. I load code but something goes wrong - eg. power loss during loading - but I don't check carefully and still command a swap.Now I have lost control and cannot even restore the previous version (which is stil in memory) since the new code is needed to command a swap back again. Again 'bricked'.
- solution is to ensure that the integrity of the alternative application can be verified
It becomes quite clear that a small boot loader sometimes is quite useful to ensure that recovery is always possible. The advantage of the swap block method is not really in the uploading - it is no more simple or robust than a small boot loader, but it does have that nice feature that when there are two good software versions loaded (that don't cause control to be lost) one can very easily and quickly switch between the two. Also the two software versions are pure-standalone versions that don't need special linking to operate together with a boot loader.
To ensure the advantage a little more is needed in the software that is prepared for loading and in its operation:
1. It needs to be authenticated - the swap must not operate if the software can't authenticate the new software because the risk is simply too high. The binary loaded must include some form of authentication (key) that can be checked.
2. The swap should also not be possible if the code in the alternative memory is not know to be correct. If this can't be done with the authentication key the binary should also include details about its size and a CRC so that the initial application can verify that its integrity in flash is OK.
Therefore I will look at adding authentication and integrity checking (maybe MD5?) in the applications and then I think that the technique should find some good uses.
Regards
Mark