blob: 8749d378560915a8ea1fa9d89a9dc2c713f8f7d7 [file] [log] [blame]
David Brownada28e12017-07-05 13:36:37 -06001###########################################################################
2# Sample multi-part application Makefile
3#
4# Copyright (c) 2017 Linaro Limited
Marti Bolivarbf909a12017-11-13 19:43:46 -05005# Copyright (c) 2017 Open Source Foundries Limited
David Brownada28e12017-07-05 13:36:37 -06006#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18###########################################################################
19
20# This is an example Makefile to demonstrate how to use mcuboot to
21# deploy and upgrade images. The image building should work on any
22# supported target, however flashing will likely require changes to
23# the flash addresses, depending on the partition layout of the device
24# in question.
25#
26# running
Marti Bolivar71354102017-08-01 15:09:44 -040027#
David Brownada28e12017-07-05 13:36:37 -060028# make BOARD=frdm_k64f all
29#
30# should generate three "*.bin" files in this directory:
31#
32# mcuboot.bin: The bootloader itself
33# signed-hello1.bin: A signed sample.
34# signed-hello2.bin: An upgrade image, signed and marked for
35# upgrade.
36#
37# "make flash_boot" should flash the bootloader into the flash,
38# erasing the rest of the device. If you examine the device at this
39# time, you should see a message about the bootloader not being able
40# to find a bootable image.
41#
David Vincze2d736ad2019-02-18 11:50:22 +010042# "make flash_hello1" will then flash the first application into the
43# "primary slot". This should boot into this app, print a small message, and
David Brownada28e12017-07-05 13:36:37 -060044# give the zephyr console.
45#
David Vincze2d736ad2019-02-18 11:50:22 +010046# "make flash_hello2" will flash hello2 into the "secondary slot". The
David Brownada28e12017-07-05 13:36:37 -060047# reset should upgrade and run the new image. Resetting again should
48# then revert back to the first app, since we did not mark this image
49# as good.
50
Marti Bolivara4818a52018-04-12 13:02:38 -040051# Extra .conf fragments to merge into the MCUboot .config, as a
52# semicolon-separated list (i.e., a CMake list).
53BOOTLOADER_OVERLAY_CONFIG ?=
54
Marti Bolivarbf909a12017-11-13 19:43:46 -050055BOARD ?= frdm_k64f
56
David Brownada28e12017-07-05 13:36:37 -060057.PHONY: check boot hello1 clean_boot clean_hello1 \
58 hello2 clean_hello2 flash_boot flash_hello1 flash_hello2
59
60# For signing, use the default RSA demo key, to match the default in
61# the mcuboot Makefile.
David Brown1d3f67d2017-09-14 15:10:24 -060062SIGNING_KEY ?= ../../root-rsa-2048.pem
David Brownada28e12017-07-05 13:36:37 -060063
64# The header size should match that in hello1/prj.conf
65# CONFIG_TEXT_SECTION_OFFSET. This value needs to be a power of two
66# that is at least as large as the size of the vector table. The
67# value given here of 0x200 should be sufficient for any supported
68# devices, but it can be made smaller, as long as this value matches
69# that used to build the app.
70BOOT_HEADER_LEN = 0x200
71
72# For upgrades, the signing tool needs to know the device alignment.
73# This requirement will be going away soon.
74FLASH_ALIGNMENT = 8
75
76IMGTOOL = ../../scripts/imgtool.py
David Browndbc57272017-07-17 15:38:54 -060077ASSEMBLE = ../../scripts/assemble.py
Maureen Helm0e0c4882019-02-18 17:20:00 -060078PYOCD = pyocd
David Brownada28e12017-07-05 13:36:37 -060079
Marti Bolivarbf909a12017-11-13 19:43:46 -050080SOURCE_DIRECTORY := $(CURDIR)
81BUILD_DIRECTORY := $(CURDIR)/build/$(BOARD)
82BUILD_DIR_BOOT := $(BUILD_DIRECTORY)/mcuboot
83BUILD_DIR_HELLO1 := $(BUILD_DIRECTORY)/hello1
84BUILD_DIR_HELLO2 := $(BUILD_DIRECTORY)/hello2
85
David Brownada28e12017-07-05 13:36:37 -060086help:
87 @echo "make <target> BOARD=<board>"
David Browndbc57272017-07-17 15:38:54 -060088 @echo "<target>: all, boot, hello1, full.bin"
Marti Bolivarbf909a12017-11-13 19:43:46 -050089 @echo "<board>: frdm_k64f only for now"
David Brownada28e12017-07-05 13:36:37 -060090
91all: boot hello1 hello2
92
David Browndbc57272017-07-17 15:38:54 -060093full.bin: boot hello1 hello2
Marti Bolivarbf909a12017-11-13 19:43:46 -050094 $(ASSEMBLE) -b $(BUILD_DIR_BOOT) \
Torsten Rasmussen33fbef52020-06-03 20:21:13 +020095 -z $(ZEPHYR_BASE) \
David Browndbc57272017-07-17 15:38:54 -060096 -p signed-hello1.bin \
97 -s signed-hello2.bin \
98 -o full.bin
99
David Brownada28e12017-07-05 13:36:37 -0600100clean: clean_boot clean_hello1 clean_hello2
101 @rm -f signed-hello1.bin
102 @rm -f signed-hello2.bin
103 @rm -f mcuboot.bin
104
105boot: check
106 @rm -f mcuboot.bin
Marti Bolivarbf909a12017-11-13 19:43:46 -0500107 (mkdir -p $(BUILD_DIR_BOOT) && \
108 cd $(BUILD_DIR_BOOT) && \
Marti Bolivara8798432018-04-12 13:12:06 -0400109 cmake -DOVERLAY_CONFIG=$(BOOTLOADER_OVERLAY_CONFIG) \
David Brown358ca1a2019-06-25 10:13:20 -0600110 -G"Ninja" \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500111 -DBOARD=$(BOARD) \
112 $(SOURCE_DIRECTORY)/../../boot/zephyr && \
David Brown358ca1a2019-06-25 10:13:20 -0600113 ninja)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500114 cp $(BUILD_DIR_BOOT)/zephyr/zephyr.bin mcuboot.bin
David Brownada28e12017-07-05 13:36:37 -0600115
116clean_boot: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500117 rm -rf $(BUILD_DIR_BOOT)
David Brownada28e12017-07-05 13:36:37 -0600118
119# Build and sign "hello1".
120hello1: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500121 (mkdir -p $(BUILD_DIR_HELLO1) && \
122 cd $(BUILD_DIR_HELLO1) && \
123 cmake -DFROM_WHO=hello1 \
David Brown358ca1a2019-06-25 10:13:20 -0600124 -G"Ninja" \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500125 -DBOARD=$(BOARD) \
126 $(SOURCE_DIRECTORY)/hello-world && \
David Brown358ca1a2019-06-25 10:13:20 -0600127 ninja)
David Brownada28e12017-07-05 13:36:37 -0600128 $(IMGTOOL) sign \
129 --key $(SIGNING_KEY) \
130 --header-size $(BOOT_HEADER_LEN) \
131 --align $(FLASH_ALIGNMENT) \
132 --version 1.2 \
Fabio Utzigffffbad2018-07-10 09:34:25 -0300133 --slot-size 0x60000 \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500134 $(BUILD_DIR_HELLO1)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600135 signed-hello1.bin
136
137clean_hello1: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500138 rm -rf $(BUILD_DIR_HELLO1)
David Brownada28e12017-07-05 13:36:37 -0600139
140# Build and sign "hello2".
141# This is the same signing command as above, except that it adds the
142# "--pad" argument. This will also add the trailer that indicates
143# this image is intended to be an upgrade. It should be flashed into
David Vincze2d736ad2019-02-18 11:50:22 +0100144# the secondary slot instead of the primary slot.
David Brownada28e12017-07-05 13:36:37 -0600145hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500146 (mkdir -p $(BUILD_DIR_HELLO2) && \
147 cd $(BUILD_DIR_HELLO2) && \
148 cmake -DFROM_WHO=hello2 \
David Brown358ca1a2019-06-25 10:13:20 -0600149 -G"Ninja" \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500150 -DBOARD=$(BOARD) \
151 $(SOURCE_DIRECTORY)/hello-world && \
David Brown358ca1a2019-06-25 10:13:20 -0600152 ninja)
David Brownada28e12017-07-05 13:36:37 -0600153 $(IMGTOOL) sign \
154 --key $(SIGNING_KEY) \
155 --header-size $(BOOT_HEADER_LEN) \
156 --align $(FLASH_ALIGNMENT) \
157 --version 1.2 \
Fabio Utzig263d4392018-06-05 10:37:35 -0300158 --slot-size 0x60000 \
159 --pad \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500160 $(BUILD_DIR_HELLO2)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600161 signed-hello2.bin
162
163clean_hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500164 rm -rf $(BUILD_DIR_HELLO2)
David Brownada28e12017-07-05 13:36:37 -0600165
David Brown1d3f67d2017-09-14 15:10:24 -0600166# These flash_* targets use pyocd to flash the images. The addresses
167# are hardcoded at this time.
168
David Brownada28e12017-07-05 13:36:37 -0600169flash_boot:
Maureen Helm0e0c4882019-02-18 17:20:00 -0600170 $(PYOCD) flash -e chip -a 0 mcuboot.bin
David Brownada28e12017-07-05 13:36:37 -0600171
172flash_hello1:
Maureen Helm0e0c4882019-02-18 17:20:00 -0600173 $(PYOCD) flash -a 0x20000 signed-hello1.bin
David Brownada28e12017-07-05 13:36:37 -0600174
175flash_hello2:
Maureen Helm0e0c4882019-02-18 17:20:00 -0600176 $(PYOCD) flash -a 0x80000 signed-hello2.bin
David Brownada28e12017-07-05 13:36:37 -0600177
David Brown84a67af2017-07-26 00:36:02 -0600178flash_full:
Maureen Helm0e0c4882019-02-18 17:20:00 -0600179 $(PYOCD) flash -e chip -a 0 full.bin
David Brown84a67af2017-07-26 00:36:02 -0600180
David Brown1d3f67d2017-09-14 15:10:24 -0600181# These test- targets reinvoke make with the configuration set to test
182# various configurations. This will generally be followed by using
183# the above flash targets.
184
185# Test a good image, with a good upgrade, using RSA signatures.
186# flash_boot: Unable to find bootable image
187# flash_hello1: hello1 runs
188# flash_hello2: hello2 runs
189# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500190test-good-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600191 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400192 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500193 all
David Brown1d3f67d2017-09-14 15:10:24 -0600194
195# Test a good image, with a good upgrade, using ECDSA signatures.
196# flash_boot: Unable to find bootable image
197# flash_hello1: hello1 runs
198# flash_hello2: hello2 runs
199# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500200test-good-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600201 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400202 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500203 SIGNING_KEY=../../root-ec-p256.pem \
204 all
David Brown1d3f67d2017-09-14 15:10:24 -0600205
206# Test (with RSA) that overwrite-only works. This should boot,
207# upgrade correctly, but not revert once the upgrade has been done.
208# flash_boot: Unable to find bootable image
209# flash_hello1: hello1 runs
210# flash_hello2: hello2 runs
211# reset: hello2 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500212test-overwrite: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600213 $(MAKE) \
Marti Bolivara8798432018-04-12 13:12:06 -0400214 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-upgrade-only.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500215 all
David Brown1d3f67d2017-09-14 15:10:24 -0600216
217# Test that when configured for RSA, a wrong signature in the upgrade
218# image will fail to upgrade.
219# flash_boot: Unable to find bootable image
220# flash_hello1: hello1 runs
221# flash_hello2: hello1 runs
222# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500223test-bad-rsa-upgrade: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600224 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400225 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500226 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600227 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400228 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500229 SIGNING_KEY=../../root-ec-p256.pem \
230 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600231
232# Test that when configured for ECDSA, a wrong signature in the upgrade
233# image will fail to upgrade.
234# flash_boot: Unable to find bootable image
235# flash_hello1: hello1 runs
236# flash_hello2: hello1 runs
237# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500238test-bad-ecdsa-upgrade: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600239 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400240 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500241 SIGNING_KEY=../../root-ec-p256.pem \
242 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600243 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400244 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500245 SIGNING_KEY=../../root-rsa-2048.pem \
246 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600247
David Vincze2d736ad2019-02-18 11:50:22 +0100248# Test that when configured to not validate the primary slot, we still boot, but
David Brown1d3f67d2017-09-14 15:10:24 -0600249# don't upgrade.
250# flash_boot: tries to boot and resets
251# flash_hello1: hello1 runs
252# flash_hello2: hello1 runs
253# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500254test-no-bootcheck: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600255 $(MAKE) \
David Vincze2d736ad2019-02-18 11:50:22 +0100256 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-skip-primary-slot-validate.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500257 SIGNING_KEY=../../root-ec-p256.pem \
258 all
David Brown1d3f67d2017-09-14 15:10:24 -0600259
260# Test a good image, with a wrong-signature upgrade, using RSA signatures.
261# flash_boot: Unable to find bootable image
262# flash_hello1: hello1 runs
263# flash_hello2: hello1 runs
264# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500265test-wrong-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600266 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400267 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500268 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600269 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400270 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500271 SIGNING_KEY=bad-keys/bad-rsa-2048.pem \
272 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600273
274# Test a good image, with a wrong-signature upgrade, using ECDSA signatures.
275# flash_boot: Unable to find bootable image
276# flash_hello1: hello1 runs
277# flash_hello2: hello1 runs
278# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500279test-wrong-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600280 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400281 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500282 SIGNING_KEY=../../root-ec-p256.pem \
283 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600284 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400285 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500286 SIGNING_KEY=bad-keys/bad-ec-p256.pem \
287 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600288
David Brownada28e12017-07-05 13:36:37 -0600289check:
290 @if [ -z "$$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi
Marti Bolivar71354102017-08-01 15:09:44 -0400291 @if [ -z "$(BOARD)" ]; then echo "You must specify BOARD=<board>"; false; fi