|
| 1 | +# wolfBoot demo application for MPLABx IDE |
| 2 | + |
| 3 | +Instruction to compile and test under Windows or Linux OS. |
| 4 | + |
| 5 | + |
| 6 | +## Target platform |
| 7 | + |
| 8 | +This example application has been configured to work on SAM E51 Curiosity Nano |
| 9 | +evaluation board. |
| 10 | + |
| 11 | +wolfBoot is configured in `DUAL_BANK` mode, meaning that it will use the |
| 12 | +bank-swapping feature available on SAM E51 to update the firmware. This operation |
| 13 | +is much faster than physically swapping the content of two partitions one sector |
| 14 | +at a time. |
| 15 | + |
| 16 | + |
| 17 | +wolfBoot is stored and executed at the beginning of the flash (0x00000000), while the signed |
| 18 | +application image starts at address 0x0000 8000. |
| 19 | + |
| 20 | + |
| 21 | +## Before starting |
| 22 | + |
| 23 | +- Read the instructions in [Targets.md](/docs/Targets.md) before starting. If your |
| 24 | +version of xc32 does not have a license for optimizers, wolfBoot binary image |
| 25 | +will normally not fit in the first 32B of the flash. If this is the case, you may |
| 26 | +want to consider compiling wolfBoot separately, by providing a .config file and |
| 27 | +running `make`. |
| 28 | + |
| 29 | +- The key provided in the bootloader project (in the test/keystore.c source file) |
| 30 | +is for demo purposes only. This is a 'test' key that should not be used in |
| 31 | +production, because in fact, it's very well known as it's distributed with the |
| 32 | +wolfBoot sources. Please read the instructions [to generate or import your own |
| 33 | +keys](/docs/Signing.md) if you intend to secure your target device. The |
| 34 | +`test/keystore.c` source file generates warnings about this when included in the |
| 35 | +build. Replace with your automatically generated `keystore.c` when you run `keygen`. |
| 36 | + |
| 37 | + |
| 38 | +## Instructions |
| 39 | + |
| 40 | +### Creating the keys |
| 41 | + |
| 42 | +This step is required to compile the bootloader with a valid key. |
| 43 | + |
| 44 | +Enter the `tools/keytools` directory and run make. This will create the `keygen` and `sign` tools. |
| 45 | +On Windows, you can use the .sln file provided to build the keytools using Visual Studio. |
| 46 | + |
| 47 | +Check [docs/Signing.md](/docs/Signing.md) for the detailed instructions on compiling the tools and |
| 48 | +creating keys using the `keygen` tool. |
| 49 | + |
| 50 | +When used with the `-g` option, the `keygen` tool will generate a keypair. The |
| 51 | +file `wolfboot_signing_private_key.der` in the root of the repository contains |
| 52 | +the private key that will be used to sign valid firmware images. |
| 53 | + |
| 54 | +The file `src/keystore.c` now contains the public key that the bootloader |
| 55 | +incorporates, to use it later to verify the image. |
| 56 | + |
| 57 | +After generating your own private + public keys, remember to replace the file |
| 58 | +`IDE/MPLAB/test/keystore.c` with the newly created one, `src/keystore.c`: |
| 59 | + |
| 60 | + |
| 61 | +``` |
| 62 | +cp src/keystore.c IDE/MPLAB/test |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +### Compiling and linking the images |
| 67 | + |
| 68 | +Now both projects (wolfboot and test-app) can be compiled and linked. |
| 69 | +The resulting images will be placed in the output directory `dist/default/production/` |
| 70 | +under the main directory of each of the two projects. |
| 71 | + |
| 72 | +The only requirement for the application to be staged by wolfboot is the modified |
| 73 | +entry point when setting the origin of the space in ROM. |
| 74 | + |
| 75 | +With the example configuration (ecc384+sha256), the manifest header size is 512B |
| 76 | +(0x200), and the main BOOT partition starts at 32 KB (0x8000). |
| 77 | + |
| 78 | +The entry point for the application firmware must take the total offset into |
| 79 | +account, by setting `ROM_ORIGIN` to 0x8200. Change this parameter accordingly if |
| 80 | +you plan to modify the geometry of your partitions in FLASH. |
| 81 | + |
| 82 | +The FLASH memory configuration for this demo is located in include/MPLAB/target.h. |
| 83 | + |
| 84 | +The file can be changed manually to set a new partitions geometry, or a new target.h |
| 85 | +could be created in the include/ directory by running `make`, which will be based |
| 86 | +on the chosen configuration via the `.config` file. If a custom `target.h` is created |
| 87 | +by `make`, the demo version in `include/MPLAB` can be removed. |
| 88 | + |
| 89 | +The wolfBoot project should build with no modifications to the project, because it |
| 90 | +does not use any .c files from the manufacturer's BSP. |
| 91 | + |
| 92 | +On the other hand, the application uses the Harmony libraries to access the UART |
| 93 | +USB and other peripherals. To generate the necessary files, once you open the |
| 94 | +project in MPLAB X IDE, click on the MCC icon on top, then click on "Generate" to |
| 95 | +download the latest Microchip libraries and device support in your local repository. |
| 96 | + |
| 97 | + |
| 98 | +### Converting to .bin format |
| 99 | + |
| 100 | +MPLAB ide produces executables of the bootloader and the test firmware images in |
| 101 | +both `.elf` and `.hex` formats. |
| 102 | + |
| 103 | +Using the `objcopy` tool from the gcc toolchain, we can convert the images to the |
| 104 | +binary format. |
| 105 | + |
| 106 | +To convert the wolfBoot image from `.hex` to `.bin` run: |
| 107 | + |
| 108 | + |
| 109 | +``` |
| 110 | +arm-none-eabi-objcopy -I ihex -O binary IDE/MPLAB/bootloader/wolfboot-same51.dualbank.X/dist/default/production/wolfboot-same51.dualbank.X.production.hex mplab_wolfboot.bin |
| 111 | +
|
| 112 | +``` |
| 113 | + |
| 114 | +To convert the application the same way: |
| 115 | + |
| 116 | +``` |
| 117 | +arm-none-eabi-objcopy -I ihex -O binary IDE/MPLAB/test-app/test-usb-updater.same51.X/dist/default/production/test-usb-updater.same51.X.production.hex mplab_app.bin |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +### Signing the application image |
| 122 | + |
| 123 | +The test application (`mplab_app.bin`) must be now tagged with a version number |
| 124 | +and signed. This is done by the `sign` tool. |
| 125 | + |
| 126 | +The tool requires the location of the private key and one numeric argument, stored |
| 127 | +as the version tag for the signed image. Running it with version "1": |
| 128 | + |
| 129 | +``` |
| 130 | +tools/keytools/sign --ecc384 mplab_app.bin wolfboot_signing_private_key.der 1 |
| 131 | +``` |
| 132 | + |
| 133 | +will create a new file `mplab_app_v1_signed.bin`, that can be uploaded into the |
| 134 | +flash memory at address 0x8000. The first 512 B in this binary file are |
| 135 | +reserved by wolfBoot for the manifest header. |
| 136 | + |
| 137 | + |
| 138 | +### Uploading the binary images to the target |
| 139 | + |
| 140 | +Follow the instructions in [docs/Targets.md](/docs/Targets.md) to upload the binary |
| 141 | +images (`mplab_wolfboot.bin` and `mplab_app_v1_signed.bin`) to the target. |
| 142 | + |
| 143 | +### Verify that the system is up and running |
| 144 | + |
| 145 | +When the application starts, it will send a few bytes through the USB UART |
| 146 | +interface associated. |
| 147 | + |
| 148 | +This data can be parsed with a small tool used in wolfBoot's automated tests, |
| 149 | +`test-expect-version`. |
| 150 | + |
| 151 | +- Compile the tool via: |
| 152 | + |
| 153 | +``` |
| 154 | +make -C tools/test-expect-version |
| 155 | +``` |
| 156 | + |
| 157 | +- Run the program: |
| 158 | + |
| 159 | +``` |
| 160 | +tools/test-expect-version/test-expect-version |
| 161 | +``` |
| 162 | + |
| 163 | +- Reset the target. |
| 164 | + |
| 165 | +The tool will display the version of the current firmware running. |
| 166 | +This data is sent by the application itself when it boots. |
| 167 | + |
| 168 | +### Update the firmware via test-update-server (Linux and MacOS only) |
| 169 | + |
| 170 | +A demo update server via USB UART is provided in the directory |
| 171 | +`tools/test-update-server`. |
| 172 | + |
| 173 | +To test the update mechanism, first compile the tool via: |
| 174 | + |
| 175 | +``` |
| 176 | +make -C tools/test-update-server |
| 177 | +``` |
| 178 | + |
| 179 | +To provide a signed update package, sign and tag a different version of the |
| 180 | +application, e.g. for version "8" run: |
| 181 | + |
| 182 | + |
| 183 | +``` |
| 184 | +tools/keytools/sign --ecc384 mplab_app.bin wolfboot_signing_private_key.der 8 |
| 185 | +``` |
| 186 | + |
| 187 | +Run the update server: |
| 188 | + |
| 189 | +``` |
| 190 | +tools/test-update-server/server mplab_app_v8_signed.bin |
| 191 | +``` |
| 192 | + |
| 193 | +Reset the target. The application will initiate an update request, to which |
| 194 | +the server will reply and will send the signed image through the USB UART. |
| 195 | + |
| 196 | +The server should display the progress of the update on screen. |
| 197 | + |
| 198 | +At the end of the update, verify that wolfboot has swapped the mapping of the |
| 199 | +two FLASH banks, cloned itself so it sits at the beginning of both banks, and |
| 200 | +correctly verified and staged the new firmware, by checking via |
| 201 | +`test-expect-version` that the current firmware running has version '8'. |
| 202 | + |
| 203 | + |
| 204 | + |
0 commit comments