blob: 7fe9f10d8585e7ccc6f24864bb95afa8479f2e42 [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 \
Fabio Utzig263d4392018-06-05 10:37:35 -0300132 --slot-size 0x60000
Marti Bolivarbf909a12017-11-13 19:43:46 -0500133 $(BUILD_DIR_HELLO1)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600134 signed-hello1.bin
135
136clean_hello1: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500137 rm -rf $(BUILD_DIR_HELLO1)
David Brownada28e12017-07-05 13:36:37 -0600138
139# Build and sign "hello2".
140# This is the same signing command as above, except that it adds the
141# "--pad" argument. This will also add the trailer that indicates
142# this image is intended to be an upgrade. It should be flashed into
143# slot1 instead of slot0.
144hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500145 (mkdir -p $(BUILD_DIR_HELLO2) && \
146 cd $(BUILD_DIR_HELLO2) && \
147 cmake -DFROM_WHO=hello2 \
148 -G"Unix Makefiles" \
149 -DBOARD=$(BOARD) \
150 $(SOURCE_DIRECTORY)/hello-world && \
151 make -j$(nproc))
David Brownada28e12017-07-05 13:36:37 -0600152 $(IMGTOOL) sign \
153 --key $(SIGNING_KEY) \
154 --header-size $(BOOT_HEADER_LEN) \
155 --align $(FLASH_ALIGNMENT) \
156 --version 1.2 \
Fabio Utzig263d4392018-06-05 10:37:35 -0300157 --slot-size 0x60000 \
158 --pad \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500159 $(BUILD_DIR_HELLO2)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600160 signed-hello2.bin
161
162clean_hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500163 rm -rf $(BUILD_DIR_HELLO2)
David Brownada28e12017-07-05 13:36:37 -0600164
David Brown1d3f67d2017-09-14 15:10:24 -0600165# These flash_* targets use pyocd to flash the images. The addresses
166# are hardcoded at this time.
167
David Brownada28e12017-07-05 13:36:37 -0600168flash_boot:
169 $(PYOCD_FLASHTOOL) -ce -a 0 mcuboot.bin
170
171flash_hello1:
172 $(PYOCD_FLASHTOOL) -a 0x20000 signed-hello1.bin
173
174flash_hello2:
175 $(PYOCD_FLASHTOOL) -a 0x80000 signed-hello2.bin
176
David Brown84a67af2017-07-26 00:36:02 -0600177flash_full:
178 $(PYOCD_FLASHTOOL) -ce -a 0 full.bin
179
David Brown1d3f67d2017-09-14 15:10:24 -0600180# These test- targets reinvoke make with the configuration set to test
181# various configurations. This will generally be followed by using
182# the above flash targets.
183
184# Test a good image, with a good upgrade, using RSA signatures.
185# flash_boot: Unable to find bootable image
186# flash_hello1: hello1 runs
187# flash_hello2: hello2 runs
188# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500189test-good-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600190 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400191 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500192 all
David Brown1d3f67d2017-09-14 15:10:24 -0600193
194# Test a good image, with a good upgrade, using ECDSA signatures.
195# flash_boot: Unable to find bootable image
196# flash_hello1: hello1 runs
197# flash_hello2: hello2 runs
198# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500199test-good-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600200 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400201 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500202 SIGNING_KEY=../../root-ec-p256.pem \
203 all
David Brown1d3f67d2017-09-14 15:10:24 -0600204
205# Test (with RSA) that overwrite-only works. This should boot,
206# upgrade correctly, but not revert once the upgrade has been done.
207# flash_boot: Unable to find bootable image
208# flash_hello1: hello1 runs
209# flash_hello2: hello2 runs
210# reset: hello2 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500211test-overwrite: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600212 $(MAKE) \
Marti Bolivara8798432018-04-12 13:12:06 -0400213 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-upgrade-only.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500214 all
David Brown1d3f67d2017-09-14 15:10:24 -0600215
216# Test that when configured for RSA, a wrong signature in the upgrade
217# image will fail to upgrade.
218# flash_boot: Unable to find bootable image
219# flash_hello1: hello1 runs
220# flash_hello2: hello1 runs
221# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500222test-bad-rsa-upgrade: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600223 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400224 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500225 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600226 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400227 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500228 SIGNING_KEY=../../root-ec-p256.pem \
229 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600230
231# Test that when configured for ECDSA, a wrong signature in the upgrade
232# image will fail to upgrade.
233# flash_boot: Unable to find bootable image
234# flash_hello1: hello1 runs
235# flash_hello2: hello1 runs
236# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500237test-bad-ecdsa-upgrade: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600238 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400239 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500240 SIGNING_KEY=../../root-ec-p256.pem \
241 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600242 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400243 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500244 SIGNING_KEY=../../root-rsa-2048.pem \
245 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600246
247# Test that when configured to not validate slot0, we still boot, but
248# don't upgrade.
249# flash_boot: tries to boot and resets
250# flash_hello1: hello1 runs
251# flash_hello2: hello1 runs
252# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500253test-no-bootcheck: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600254 $(MAKE) \
Marti Bolivar15c9b6f2018-04-12 13:08:39 -0400255 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-skip-slot0-validate.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500256 SIGNING_KEY=../../root-ec-p256.pem \
257 all
David Brown1d3f67d2017-09-14 15:10:24 -0600258
259# Test a good image, with a wrong-signature upgrade, using RSA signatures.
260# flash_boot: Unable to find bootable image
261# flash_hello1: hello1 runs
262# flash_hello2: hello1 runs
263# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500264test-wrong-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600265 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400266 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500267 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600268 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400269 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500270 SIGNING_KEY=bad-keys/bad-rsa-2048.pem \
271 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600272
273# Test a good image, with a wrong-signature upgrade, using ECDSA signatures.
274# flash_boot: Unable to find bootable image
275# flash_hello1: hello1 runs
276# flash_hello2: hello1 runs
277# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500278test-wrong-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600279 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400280 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500281 SIGNING_KEY=../../root-ec-p256.pem \
282 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600283 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400284 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500285 SIGNING_KEY=bad-keys/bad-ec-p256.pem \
286 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600287
David Brownada28e12017-07-05 13:36:37 -0600288check:
289 @if [ -z "$$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi
Marti Bolivar71354102017-08-01 15:09:44 -0400290 @if [ -z "$(BOARD)" ]; then echo "You must specify BOARD=<board>"; false; fi