1.10.1.2 Using Version Numbers in the Anti-Rollback Example Code

The second key function that shows how to use the version number is the Anti-Rollback function IsUpdateRequired() located in the file boot_demo.c. This function shows one way of examining the current execution application image and the downloaded image to determine if the FW programmed execution image partition needs to be updated. At first this seems like a simple function shown below would be adequate.

if (BOOT_VersionNumberGet(IMAGE_1) > BOOT_VersionNumberGet(IMAGE_2))
{
        BOOT_ImageCopy(IMAGE_0, IMAGE_1);
}

However, the update function needs to check for not only version numbers, the code also needs to check if a version number is available as well as verifying the validity of each image. The flow of Anti-Rollback code is shown below. Before any downloaded code is updated, its integrity is checked and its version number is recorded.

Figure 1-23.  Anti Rollback Flow Chart

Within this code, the first checks are to see if the images are valid. This is performed by the code shown below. Both of these calls will verify the integrity of the selected image and return a pass/fail result.

downloadImageValid = BOOT_ImageVerify(DOWNLOADED_IMAGE);
executionImageValid = BOOT_ImageVerify(EXECUTION_IMAGE);

Next the code will look at the headers of the application images and verify if the images have a version number header and if so, what the version numbers are. These two calls are shown below.

executionVersionPresent = BOOT_VersionNumberGet(EXECUTION_IMAGE, &executionVersion);
downloadVersionPresent = BOOT_VersionNumberGet(DOWNLOADED_IMAGE, &downloadVersion);

Once we have these checks in place, the final check before updating the first is to compare the downloadedImage version number to the executionImage version number. If the downloadedImage version number is greater, then the application image will be copied over to the execution image location using the call BOOT_ImageCopy(EXECUTION_IMAGE, DOWNLOADED_IMAGE);