|
| 1 | +# Firmware upgrade |
| 2 | + |
| 3 | +This section documents the complete firmware upgrade procedure, enabling secure boot |
| 4 | +for an existing embedded application. |
| 5 | + |
| 6 | +The steps to follow to complete a firmware upgrade with wolfBoot are: |
| 7 | + - Compile the firmware with the correct entry point |
| 8 | + - Sign the firmware |
| 9 | + - Transfer the image using a secure connection, and store it to the secondary firmware slot |
| 10 | + - Trigger the image swap |
| 11 | + - Reboot to let the bootloader begin the image swap |
| 12 | + |
| 13 | + |
| 14 | +An application can be linked to wolfBoot `bootutil` library, which exports the API to trigger |
| 15 | +the upgrade at the next reboot, and some helper functions to access the flash partition for |
| 16 | +erase/write through the target specific [HAL](HAL.md). |
| 17 | + |
| 18 | +## Upgrade mode |
| 19 | + |
| 20 | +wolfBoot can be compiled to provide two different types of upgrade: |
| 21 | + - Overwrite-only mode |
| 22 | + - Swap mode |
| 23 | + |
| 24 | +### Overwrite-only mode |
| 25 | + |
| 26 | +If wolfBoot is compiled with the `SWAP=0` command line option, no swap partition is required. |
| 27 | +In this case, every time that a firmware upgrade is triggered, there is no way to revert the upgrade. |
| 28 | + |
| 29 | +After the received firmware image is marked as 'pending' from the application, wolfBoot will verify its |
| 30 | +authenticity and integrity, and overwrite the boot partition by storing the new image. |
| 31 | + |
| 32 | +No firmware image rollback option is present in this configuration. |
| 33 | + |
| 34 | +### Swap mode |
| 35 | + |
| 36 | +Using the default configuration, wolfBoot offers the possibility to initiate, confirm or rollback an upgrade. |
| 37 | + |
| 38 | +This mode requires an additional partition (swap) that can be used as a temporary storage. The swap partition |
| 39 | +is also able to track the status of the flash operations, so that in case of power failure it can restart |
| 40 | +the interrupted upgrade operation. |
| 41 | + |
| 42 | +The application can trigger temporary or permanent upgrades. |
| 43 | + |
| 44 | + |
| 45 | +## Building a new firmware image |
| 46 | + |
| 47 | +Firmware images are position-dependent, and can only boot from the origin of the **boot** partition in the FLASH. |
| 48 | +This design choice means that the chosen firmware is always stored in the **boot** partition, and the bootloader |
| 49 | +is responsible for pre-validating an upgrade image and copy it to the correct address. |
| 50 | + |
| 51 | +All the firmware images must therefore have their entry point set to the address corresponding to the beginning |
| 52 | +of the **boot** partition, plus an offset of 256 Bytes to account for the firmware header. |
| 53 | + |
| 54 | +Once the firmware is compiled and linked, it must be signed using the `ed25519_sign` tool. The tool produces |
| 55 | +a signed image that can be transferred to the target using a secure connection, using the same key corresponding |
| 56 | +to the public key currently used for verification. |
| 57 | + |
| 58 | +The tool also appends the TLV at the end of the image, containing the signatures and the SHA256 hash of the firmware. |
| 59 | + |
| 60 | +## Embedded application |
| 61 | + |
| 62 | +At any given time, an application or OS running on a wolfBoot system can receive an updated version of itself, |
| 63 | +and store the updated image in the second partition in the FLASH memory. |
| 64 | + |
| 65 | +In order to communicate with the bootloader, the application can be linked with the `bootutil` library, by |
| 66 | +including the source file [lib/bootutil/bootutil\_misc.c](../lib/bootutil/bootutil_misc.c) and the back-end of |
| 67 | +the [Hardware Abstraction Layer](docs/HAL.md) in use by wolfBoot for the target. |
| 68 | + |
| 69 | +If the running application lacks support for FLASH IAP operations, *bootutil* can also be used to easily access |
| 70 | +the flash for erasing/writing. |
| 71 | + |
| 72 | +### Storing the update image |
| 73 | + |
| 74 | +The application can access the flash area using the following API: |
| 75 | + |
| 76 | + |
| 77 | +#### Flash access |
| 78 | + |
| 79 | +The following functions are declared in `include/flash.h`: |
| 80 | + |
| 81 | +`int flash_area_open(uint8_t id, const struct flash_area **area)` |
| 82 | + |
| 83 | +Initialize a `flash_area` object associated to the partition associated to identifier |
| 84 | +`id`. |
| 85 | + |
| 86 | +Possible identifiers are: |
| 87 | + - `1` for the **boot** partition |
| 88 | + - `2` for the **upgrade** partition |
| 89 | + - `3` for the **swap** partition, if present. |
| 90 | + |
| 91 | +This function must be called before using any other `flash_area_*` function. |
| 92 | +Returns 0 on success. |
| 93 | + |
| 94 | +`void flash_area_close(const struct flash_area *area)` |
| 95 | +Stop using a flash area associated to the structure passed as argument. |
| 96 | + |
| 97 | + |
| 98 | +`int flash_area_read(const struct flash_area *, uint32_t off, void *dst, uint32_t len)` |
| 99 | +`int flash_area_write(const struct flash_area *, uint32_t off, const void *src, uint32_t len)` |
| 100 | +`int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len)` |
| 101 | + |
| 102 | +Read/Write/Erase a portion of the flash area, starting at offset `off` relative to the beginning |
| 103 | +of the partition. |
| 104 | + |
| 105 | +#### Triggering an upgrade |
| 106 | + |
| 107 | +The following function is exported by the bootutil library and declared in `include/bootutil.h`: |
| 108 | + |
| 109 | +`int boot_set_pending(int permanent);` |
| 110 | +Trigger a firmware upgrade after the next reboot. If the permanent flag is not active and wolfBoot |
| 111 | +is compiled in "swap" mode, the change will require to be confirmed via `boot_set_confirmed()` after |
| 112 | +a successful boot, otherwise the original image will be rolled back after another reboot. |
| 113 | + |
| 114 | +`int boot_set_confirmed(void);` |
| 115 | +Confirm a previously triggered temporary upgrade. Only available when wolfBoot is compiled in swap mode. |
| 116 | + |
| 117 | +To boot into the image stored in the upgrade partition, it is sufficient to call: |
| 118 | + |
| 119 | +`boot_set_pending(1);` |
| 120 | + |
| 121 | +and reboot the system to initiate the upgrade. |
| 122 | + |
0 commit comments