David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 1 | ########################################################################### |
| 2 | # Sample multi-part application Makefile |
| 3 | # |
| 4 | # Copyright (c) 2017 Linaro Limited |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | ########################################################################### |
| 18 | |
| 19 | # This is an example Makefile to demonstrate how to use mcuboot to |
| 20 | # deploy and upgrade images. The image building should work on any |
| 21 | # supported target, however flashing will likely require changes to |
| 22 | # the flash addresses, depending on the partition layout of the device |
| 23 | # in question. |
| 24 | # |
| 25 | # running |
Marti Bolivar | 7135410 | 2017-08-01 15:09:44 -0400 | [diff] [blame] | 26 | # |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 27 | # make BOARD=frdm_k64f all |
| 28 | # |
| 29 | # should generate three "*.bin" files in this directory: |
| 30 | # |
| 31 | # mcuboot.bin: The bootloader itself |
| 32 | # signed-hello1.bin: A signed sample. |
| 33 | # signed-hello2.bin: An upgrade image, signed and marked for |
| 34 | # upgrade. |
| 35 | # |
| 36 | # "make flash_boot" should flash the bootloader into the flash, |
| 37 | # erasing the rest of the device. If you examine the device at this |
| 38 | # time, you should see a message about the bootloader not being able |
| 39 | # to find a bootable image. |
| 40 | # |
| 41 | # "make flash_hello1" will then flash the first application into |
| 42 | # "slot0". This should boot into this app, print a small message, and |
| 43 | # give the zephyr console. |
| 44 | # |
| 45 | # "make flash_hello2" will flash hello2 into the second slot. The |
| 46 | # reset should upgrade and run the new image. Resetting again should |
| 47 | # then revert back to the first app, since we did not mark this image |
| 48 | # as good. |
| 49 | |
| 50 | BOARD ?= |
| 51 | |
David Brown | 1d3f67d | 2017-09-14 15:10:24 -0600 | [diff] [blame] | 52 | # We can override the Zephyr configuration for the bootloader by |
| 53 | # setting this. |
| 54 | BOOTLOADER_CONFIG ?= |
| 55 | |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 56 | .PHONY: check boot hello1 clean_boot clean_hello1 \ |
| 57 | hello2 clean_hello2 flash_boot flash_hello1 flash_hello2 |
| 58 | |
| 59 | # For signing, use the default RSA demo key, to match the default in |
| 60 | # the mcuboot Makefile. |
David Brown | 1d3f67d | 2017-09-14 15:10:24 -0600 | [diff] [blame] | 61 | SIGNING_KEY ?= ../../root-rsa-2048.pem |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 62 | |
| 63 | # The header size should match that in hello1/prj.conf |
| 64 | # CONFIG_TEXT_SECTION_OFFSET. This value needs to be a power of two |
| 65 | # that is at least as large as the size of the vector table. The |
| 66 | # value given here of 0x200 should be sufficient for any supported |
| 67 | # devices, but it can be made smaller, as long as this value matches |
| 68 | # that used to build the app. |
| 69 | BOOT_HEADER_LEN = 0x200 |
| 70 | |
| 71 | # For upgrades, the signing tool needs to know the device alignment. |
| 72 | # This requirement will be going away soon. |
| 73 | FLASH_ALIGNMENT = 8 |
| 74 | |
| 75 | IMGTOOL = ../../scripts/imgtool.py |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 76 | ASSEMBLE = ../../scripts/assemble.py |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 77 | PYOCD_FLASHTOOL = pyocd-flashtool |
| 78 | |
| 79 | help: |
| 80 | @echo "make <target> BOARD=<board>" |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 81 | @echo "<target>: all, boot, hello1, full.bin" |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 82 | @echo "<board>: frdm_k64f, 96b_carbon, etc." |
| 83 | |
| 84 | all: boot hello1 hello2 |
| 85 | |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 86 | full.bin: boot hello1 hello2 |
| 87 | $(ASSEMBLE) -b ../../outdir/$(BOARD) \ |
| 88 | -p signed-hello1.bin \ |
| 89 | -s signed-hello2.bin \ |
| 90 | -o full.bin |
| 91 | |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 92 | clean: clean_boot clean_hello1 clean_hello2 |
| 93 | @rm -f signed-hello1.bin |
| 94 | @rm -f signed-hello2.bin |
| 95 | @rm -f mcuboot.bin |
| 96 | |
| 97 | boot: check |
| 98 | @rm -f mcuboot.bin |
David Brown | 1d3f67d | 2017-09-14 15:10:24 -0600 | [diff] [blame] | 99 | $(MAKE) -C ../.. BOARD=$(BOARD) -j$(nproc) $(BOOTLOADER_CONFIG) |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 100 | cp ../../outdir/$(BOARD)/zephyr.bin mcuboot.bin |
| 101 | |
| 102 | clean_boot: check |
| 103 | rm -rf ../../outdir/$(BOARD) |
| 104 | |
| 105 | # Build and sign "hello1". |
| 106 | hello1: check |
Marti Bolivar | b9f5a68 | 2017-08-01 15:41:05 -0400 | [diff] [blame] | 107 | $(MAKE) -C hello-world FROM_WHO="hello1" O=outdir/hello1/$(BOARD) BOARD=$(BOARD) -j$(nproc) |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 108 | $(IMGTOOL) sign \ |
| 109 | --key $(SIGNING_KEY) \ |
| 110 | --header-size $(BOOT_HEADER_LEN) \ |
| 111 | --align $(FLASH_ALIGNMENT) \ |
| 112 | --version 1.2 \ |
| 113 | --included-header \ |
Marti Bolivar | b9f5a68 | 2017-08-01 15:41:05 -0400 | [diff] [blame] | 114 | hello-world/outdir/hello1/$(BOARD)/zephyr.bin \ |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 115 | signed-hello1.bin |
| 116 | |
| 117 | clean_hello1: check |
Marti Bolivar | b9f5a68 | 2017-08-01 15:41:05 -0400 | [diff] [blame] | 118 | rm -rf hello-world/outdir/hello1/$(BOARD) |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 119 | |
| 120 | # Build and sign "hello2". |
| 121 | # This is the same signing command as above, except that it adds the |
| 122 | # "--pad" argument. This will also add the trailer that indicates |
| 123 | # this image is intended to be an upgrade. It should be flashed into |
| 124 | # slot1 instead of slot0. |
| 125 | hello2: check |
Marti Bolivar | b9f5a68 | 2017-08-01 15:41:05 -0400 | [diff] [blame] | 126 | $(MAKE) -C hello-world FROM_WHO="hello2" O=outdir/hello2/$(BOARD) BOARD=$(BOARD) -j$(nproc) |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 127 | $(IMGTOOL) sign \ |
| 128 | --key $(SIGNING_KEY) \ |
| 129 | --header-size $(BOOT_HEADER_LEN) \ |
| 130 | --align $(FLASH_ALIGNMENT) \ |
| 131 | --version 1.2 \ |
| 132 | --included-header \ |
| 133 | --pad 0x60000 \ |
Marti Bolivar | b9f5a68 | 2017-08-01 15:41:05 -0400 | [diff] [blame] | 134 | hello-world/outdir/hello2/$(BOARD)/zephyr.bin \ |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 135 | signed-hello2.bin |
| 136 | |
| 137 | clean_hello2: check |
Marti Bolivar | b9f5a68 | 2017-08-01 15:41:05 -0400 | [diff] [blame] | 138 | rm -rf hello-world/outdir/hello2/$(BOARD) |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 139 | |
David Brown | 1d3f67d | 2017-09-14 15:10:24 -0600 | [diff] [blame] | 140 | # These flash_* targets use pyocd to flash the images. The addresses |
| 141 | # are hardcoded at this time. |
| 142 | |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 143 | flash_boot: |
| 144 | $(PYOCD_FLASHTOOL) -ce -a 0 mcuboot.bin |
| 145 | |
| 146 | flash_hello1: |
| 147 | $(PYOCD_FLASHTOOL) -a 0x20000 signed-hello1.bin |
| 148 | |
| 149 | flash_hello2: |
| 150 | $(PYOCD_FLASHTOOL) -a 0x80000 signed-hello2.bin |
| 151 | |
David Brown | 84a67af | 2017-07-26 00:36:02 -0600 | [diff] [blame] | 152 | flash_full: |
| 153 | $(PYOCD_FLASHTOOL) -ce -a 0 full.bin |
| 154 | |
David Brown | 1d3f67d | 2017-09-14 15:10:24 -0600 | [diff] [blame] | 155 | # These test- targets reinvoke make with the configuration set to test |
| 156 | # various configurations. This will generally be followed by using |
| 157 | # the above flash targets. |
| 158 | |
| 159 | # Test a good image, with a good upgrade, using RSA signatures. |
| 160 | # flash_boot: Unable to find bootable image |
| 161 | # flash_hello1: hello1 runs |
| 162 | # flash_hello2: hello2 runs |
| 163 | # reset: hello1 runs |
| 164 | test-good-rsa: |
| 165 | $(MAKE) \ |
| 166 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 167 | clean |
| 168 | $(MAKE) \ |
| 169 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 170 | all |
| 171 | |
| 172 | # Test a good image, with a good upgrade, using ECDSA signatures. |
| 173 | # flash_boot: Unable to find bootable image |
| 174 | # flash_hello1: hello1 runs |
| 175 | # flash_hello2: hello2 runs |
| 176 | # reset: hello1 runs |
| 177 | test-good-ecdsa: |
| 178 | $(MAKE) \ |
| 179 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 180 | SIGNING_KEY=../../root-ec-p256.pem \ |
| 181 | clean |
| 182 | $(MAKE) \ |
| 183 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 184 | SIGNING_KEY=../../root-ec-p256.pem \ |
| 185 | all |
| 186 | |
| 187 | # Test (with RSA) that overwrite-only works. This should boot, |
| 188 | # upgrade correctly, but not revert once the upgrade has been done. |
| 189 | # flash_boot: Unable to find bootable image |
| 190 | # flash_hello1: hello1 runs |
| 191 | # flash_hello2: hello2 runs |
| 192 | # reset: hello2 runs |
| 193 | test-overwrite: |
| 194 | $(MAKE) \ |
| 195 | BOOTLOADER_CONFIG="CONF_UPGRADE_ONLY=YES" \ |
| 196 | clean |
| 197 | $(MAKE) \ |
| 198 | BOOTLOADER_CONFIG="CONF_UPGRADE_ONLY=YES" \ |
| 199 | all |
| 200 | |
| 201 | # Test that when configured for RSA, a wrong signature in the upgrade |
| 202 | # image will fail to upgrade. |
| 203 | # flash_boot: Unable to find bootable image |
| 204 | # flash_hello1: hello1 runs |
| 205 | # flash_hello2: hello1 runs |
| 206 | # reset: hello1 runs |
| 207 | test-bad-rsa-upgrade: |
| 208 | $(MAKE) \ |
| 209 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 210 | clean |
| 211 | $(MAKE) \ |
| 212 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 213 | boot hello1 |
| 214 | $(MAKE) \ |
| 215 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 216 | SIGNING_KEY=../../root-ec-p256.pem \ |
| 217 | hello2 |
| 218 | |
| 219 | # Test that when configured for ECDSA, a wrong signature in the upgrade |
| 220 | # image will fail to upgrade. |
| 221 | # flash_boot: Unable to find bootable image |
| 222 | # flash_hello1: hello1 runs |
| 223 | # flash_hello2: hello1 runs |
| 224 | # reset: hello1 runs |
| 225 | test-bad-ecdsa-upgrade: |
| 226 | $(MAKE) \ |
| 227 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 228 | clean |
| 229 | $(MAKE) \ |
| 230 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 231 | SIGNING_KEY=../../root-ec-p256.pem \ |
| 232 | boot hello1 |
| 233 | $(MAKE) \ |
| 234 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 235 | SIGNING_KEY=../../root-rsa-2048.pem \ |
| 236 | hello2 |
| 237 | |
| 238 | # Test that when configured to not validate slot0, we still boot, but |
| 239 | # don't upgrade. |
| 240 | # flash_boot: tries to boot and resets |
| 241 | # flash_hello1: hello1 runs |
| 242 | # flash_hello2: hello1 runs |
| 243 | # reset: hello1 runs |
| 244 | test-no-bootcheck: |
| 245 | $(MAKE) \ |
| 246 | BOOTLOADER_CONFIG="CONF_VALIDATE_SLOT0=NO" \ |
| 247 | clean |
| 248 | $(MAKE) \ |
| 249 | BOOTLOADER_CONFIG="CONF_VALIDATE_SLOT0=NO" \ |
| 250 | SIGNING_KEY=../../root-ec-p256.pem \ |
| 251 | all |
| 252 | |
| 253 | # Test a good image, with a wrong-signature upgrade, using RSA signatures. |
| 254 | # flash_boot: Unable to find bootable image |
| 255 | # flash_hello1: hello1 runs |
| 256 | # flash_hello2: hello1 runs |
| 257 | # reset: hello1 runs |
| 258 | test-wrong-rsa: |
| 259 | $(MAKE) \ |
| 260 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 261 | clean |
| 262 | $(MAKE) \ |
| 263 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 264 | boot hello1 |
| 265 | $(MAKE) \ |
| 266 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=RSA" \ |
| 267 | SIGNING_KEY=bad-keys/bad-rsa-2048.pem \ |
| 268 | hello2 |
| 269 | |
| 270 | # Test a good image, with a wrong-signature upgrade, using ECDSA signatures. |
| 271 | # flash_boot: Unable to find bootable image |
| 272 | # flash_hello1: hello1 runs |
| 273 | # flash_hello2: hello1 runs |
| 274 | # reset: hello1 runs |
| 275 | test-wrong-ecdsa: |
| 276 | $(MAKE) \ |
| 277 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 278 | clean |
| 279 | $(MAKE) \ |
| 280 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 281 | SIGNING_KEY=../../root-ec-p256.pem \ |
| 282 | boot hello1 |
| 283 | $(MAKE) \ |
| 284 | BOOTLOADER_CONFIG="CONF_SIGNATURE_TYPE=ECDSA_P256" \ |
| 285 | SIGNING_KEY=bad-keys/bad-ec-p256.pem \ |
| 286 | hello2 |
| 287 | |
David Brown | ada28e1 | 2017-07-05 13:36:37 -0600 | [diff] [blame] | 288 | check: |
| 289 | @if [ -z "$$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi |
Marti Bolivar | 7135410 | 2017-08-01 15:09:44 -0400 | [diff] [blame] | 290 | @if [ -z "$(BOARD)" ]; then echo "You must specify BOARD=<board>"; false; fi |