# FOTA Stack Source Code

The RSL CMSIS-Pack includes a prebuilt version of the FOTA Stack available in the form of a software component, as shown in Performing Your First FOTA Update. The fota.bin and libfota.a files are located under <cmsis_pack_root>\ONSemiconductor\<device>\<version>\lib\Release. The source code to generate these files is also available in the CMSIS-Pack, under <cmsis_pack_root>\ONSemiconductor\<device>\<version>\firmware\source\lib\fota.

If you would like to customize and rebuild these files, follow these steps:

| 1. | Import the source code project. Navigate to File > Import > General > Existing Projects into Workspace, set the Select root directory: option with the path to the source code <cmsis_pack_root>\ONSemiconductor\<device>\<version>\firmware\source\lib\fota, mark the checkbox Copy projects into workspace, and click Finish. The project appears in the left side of Project Explorer, as shown in the "Copying the Project" figure.

Figure: Copying the Project

| 2. | Modify the source code. For example, change the FOTA stack version number in app.conf.h.

#define FOTA_VER_MAJOR 1

#define FOTA_VER_MINOR 1 //0

#define FOTA_VER_REVISION 0

| 3. | Build the project. After building successfully, libfota.a and fota.bin are generated under the Debug or Release folder, as shown in the "Building the Project" figure.

Figure: Building the Project

| 4. | To use your customized files, replace the libfota.a and the fota.bin in your in your CMSIS-Pack installation (<cmsis_pack_root>\ONSemiconductor\<device>\<version>\lib\Release).

| 5. | After replacing these files, it is necessary to refresh the RTE folder of existing projects in your workspace, so that the IDE uses the newly generated files. The simplest way to do this is to delete the fota.bin and libfota.a files under RTE > Device > <device>, and right-click the project name and choose Refresh to refresh your sample project (for example, you can do this in ble_peripheral_server_fota).

| 6. | Rebuild the existing project so that a new .fota image is generated based on your modified libfota.a and fota.bin files.

| 7. | Run BLE Explorer to perform a FOTA update. The tool detects the updated version number of the stack FOTA stack version: FOTA <higher_version_number>, in comparison to the one installed on the device. (In this example, we are using FOTA stack version: FOTA 1.1.0, for when version 1.0.0 is installed.) This means a full update is required (FOTA Stack + application). This is performed in two steps: first, the FOTA stack is updated; next, the device resets and the user application is updated.

If you would like to create the secure FOTA library for secure applications, follow these steps:

1. Make sure you have the FOTA application in your workspace.
2. Right-click the FOTA project name and choose 3 Release_Secure under Build configurations > Set Active.
3. You can find the defined symbols under Preprocessor in Settings for the Secure FOTA:
   - CFG_SECURE_FOTA
   - BL_APPLICATION_BASE=0x10D800
   - BL_DOWNLOAD_BASE=0x144000

   - BL_SECURE_STORAGE_SIZE=0x2C00

     BL_APPLICATION_BASE is the start address of FOTA, BL_DOWNLOAD_BASE is the start address of the download area, and SECURE_STORAGE_SIZE is the size of the secure storage area.

4. You can find the option for the secure bootloader size in Project > Properties > C/C++ Build > Settings > Build Steps > Post-build steps.

   - Release_Secure Bootloader_size 0xD800

     0xD800 means the secure bootloader size.

5. Build the project. After building successfully, libfota.a and fota.bin are generated under the Release_Secure folder.
6. To use the secure FOTA files, replace the libfota.a and the fota.bin files in your CMSIS-Pack installation <cmsis_pack_root>\ONSemiconductor\<device>\<version>\lib\Release_Secure).