blob: 1477bbf6167cb921df8eab75e1ba6428c6024c87 [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#
42# "make flash_hello1" will then flash the first application into
43# "slot0". This should boot into this app, print a small message, and
44# give the zephyr console.
45#
46# "make flash_hello2" will flash hello2 into the second slot. The
47# 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
David Brownada28e12017-07-05 13:36:37 -060078PYOCD_FLASHTOOL = pyocd-flashtool
79
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) \
David Browndbc57272017-07-17 15:38:54 -060095 -p signed-hello1.bin \
96 -s signed-hello2.bin \
97 -o full.bin
98
David Brownada28e12017-07-05 13:36:37 -060099clean: clean_boot clean_hello1 clean_hello2
100 @rm -f signed-hello1.bin
101 @rm -f signed-hello2.bin
102 @rm -f mcuboot.bin
103
104boot: check
105 @rm -f mcuboot.bin
Marti Bolivarbf909a12017-11-13 19:43:46 -0500106 (mkdir -p $(BUILD_DIR_BOOT) && \
107 cd $(BUILD_DIR_BOOT) && \
Marti Bolivara8798432018-04-12 13:12:06 -0400108 cmake -DOVERLAY_CONFIG=$(BOOTLOADER_OVERLAY_CONFIG) \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500109 -G"Unix Makefiles" \
110 -DBOARD=$(BOARD) \
111 $(SOURCE_DIRECTORY)/../../boot/zephyr && \
112 make -j$(nproc))
113 cp $(BUILD_DIR_BOOT)/zephyr/zephyr.bin mcuboot.bin
David Brownada28e12017-07-05 13:36:37 -0600114
115clean_boot: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500116 rm -rf $(BUILD_DIR_BOOT)
David Brownada28e12017-07-05 13:36:37 -0600117
118# Build and sign "hello1".
119hello1: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500120 (mkdir -p $(BUILD_DIR_HELLO1) && \
121 cd $(BUILD_DIR_HELLO1) && \
122 cmake -DFROM_WHO=hello1 \
123 -G"Unix Makefiles" \
124 -DBOARD=$(BOARD) \
125 $(SOURCE_DIRECTORY)/hello-world && \
126 make -j$(nproc))
David Brownada28e12017-07-05 13:36:37 -0600127 $(IMGTOOL) sign \
128 --key $(SIGNING_KEY) \
129 --header-size $(BOOT_HEADER_LEN) \
130 --align $(FLASH_ALIGNMENT) \
131 --version 1.2 \
132 --included-header \
Fabio Utzig263d4392018-06-05 10:37:35 -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
144# slot1 instead of slot0.
145hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500146 (mkdir -p $(BUILD_DIR_HELLO2) && \
147 cd $(BUILD_DIR_HELLO2) && \
148 cmake -DFROM_WHO=hello2 \
149 -G"Unix Makefiles" \
150 -DBOARD=$(BOARD) \
151 $(SOURCE_DIRECTORY)/hello-world && \
152 make -j$(nproc))
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 \
158 --included-header \
Fabio Utzig263d4392018-06-05 10:37:35 -0300159 --slot-size 0x60000 \
160 --pad \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500161 $(BUILD_DIR_HELLO2)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600162 signed-hello2.bin
163
164clean_hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500165 rm -rf $(BUILD_DIR_HELLO2)
David Brownada28e12017-07-05 13:36:37 -0600166
David Brown1d3f67d2017-09-14 15:10:24 -0600167# These flash_* targets use pyocd to flash the images. The addresses
168# are hardcoded at this time.
169
David Brownada28e12017-07-05 13:36:37 -0600170flash_boot:
171 $(PYOCD_FLASHTOOL) -ce -a 0 mcuboot.bin
172
173flash_hello1:
174 $(PYOCD_FLASHTOOL) -a 0x20000 signed-hello1.bin
175
176flash_hello2:
177 $(PYOCD_FLASHTOOL) -a 0x80000 signed-hello2.bin
178
David Brown84a67af2017-07-26 00:36:02 -0600179flash_full:
180 $(PYOCD_FLASHTOOL) -ce -a 0 full.bin
181
David Brown1d3f67d2017-09-14 15:10:24 -0600182# These test- targets reinvoke make with the configuration set to test
183# various configurations. This will generally be followed by using
184# the above flash targets.
185
186# Test a good image, with a good upgrade, using RSA signatures.
187# flash_boot: Unable to find bootable image
188# flash_hello1: hello1 runs
189# flash_hello2: hello2 runs
190# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500191test-good-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600192 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400193 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500194 all
David Brown1d3f67d2017-09-14 15:10:24 -0600195
196# Test a good image, with a good upgrade, using ECDSA signatures.
197# flash_boot: Unable to find bootable image
198# flash_hello1: hello1 runs
199# flash_hello2: hello2 runs
200# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500201test-good-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600202 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400203 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500204 SIGNING_KEY=../../root-ec-p256.pem \
205 all
David Brown1d3f67d2017-09-14 15:10:24 -0600206
207# Test (with RSA) that overwrite-only works. This should boot,
208# upgrade correctly, but not revert once the upgrade has been done.
209# flash_boot: Unable to find bootable image
210# flash_hello1: hello1 runs
211# flash_hello2: hello2 runs
212# reset: hello2 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500213test-overwrite: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600214 $(MAKE) \
Marti Bolivara8798432018-04-12 13:12:06 -0400215 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-upgrade-only.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500216 all
David Brown1d3f67d2017-09-14 15:10:24 -0600217
218# Test that when configured for RSA, a wrong signature in the upgrade
219# image will fail to upgrade.
220# flash_boot: Unable to find bootable image
221# flash_hello1: hello1 runs
222# flash_hello2: hello1 runs
223# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500224test-bad-rsa-upgrade: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600225 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400226 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500227 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600228 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400229 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500230 SIGNING_KEY=../../root-ec-p256.pem \
231 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600232
233# Test that when configured for ECDSA, a wrong signature in the upgrade
234# image will fail to upgrade.
235# flash_boot: Unable to find bootable image
236# flash_hello1: hello1 runs
237# flash_hello2: hello1 runs
238# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500239test-bad-ecdsa-upgrade: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600240 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400241 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500242 SIGNING_KEY=../../root-ec-p256.pem \
243 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600244 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400245 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500246 SIGNING_KEY=../../root-rsa-2048.pem \
247 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600248
249# Test that when configured to not validate slot0, we still boot, but
250# don't upgrade.
251# flash_boot: tries to boot and resets
252# flash_hello1: hello1 runs
253# flash_hello2: hello1 runs
254# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500255test-no-bootcheck: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600256 $(MAKE) \
Marti Bolivar15c9b6f2018-04-12 13:08:39 -0400257 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-skip-slot0-validate.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500258 SIGNING_KEY=../../root-ec-p256.pem \
259 all
David Brown1d3f67d2017-09-14 15:10:24 -0600260
261# Test a good image, with a wrong-signature upgrade, using RSA signatures.
262# flash_boot: Unable to find bootable image
263# flash_hello1: hello1 runs
264# flash_hello2: hello1 runs
265# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500266test-wrong-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600267 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400268 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500269 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600270 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400271 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500272 SIGNING_KEY=bad-keys/bad-rsa-2048.pem \
273 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600274
275# Test a good image, with a wrong-signature upgrade, using ECDSA signatures.
276# flash_boot: Unable to find bootable image
277# flash_hello1: hello1 runs
278# flash_hello2: hello1 runs
279# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500280test-wrong-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600281 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400282 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500283 SIGNING_KEY=../../root-ec-p256.pem \
284 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600285 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400286 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500287 SIGNING_KEY=bad-keys/bad-ec-p256.pem \
288 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600289
David Brownada28e12017-07-05 13:36:37 -0600290check:
291 @if [ -z "$$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi
Marti Bolivar71354102017-08-01 15:09:44 -0400292 @if [ -z "$(BOARD)" ]; then echo "You must specify BOARD=<board>"; false; fi