blob: dd73702e4a499f9ec153578443c83a3fa1605c0d [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
honglin leng37e42232022-08-17 11:23:04 +080056SLOT_SIZE ?= 0x60000
57BOOT_ADDR ?= 0x0
58IMG0_ADDR ?= 0x20000
59IMG1_ADDR ?= 0x80000
Marti Bolivarbf909a12017-11-13 19:43:46 -050060
David Brownada28e12017-07-05 13:36:37 -060061.PHONY: check boot hello1 clean_boot clean_hello1 \
62 hello2 clean_hello2 flash_boot flash_hello1 flash_hello2
63
64# For signing, use the default RSA demo key, to match the default in
65# the mcuboot Makefile.
David Brown1d3f67d2017-09-14 15:10:24 -060066SIGNING_KEY ?= ../../root-rsa-2048.pem
David Brownada28e12017-07-05 13:36:37 -060067
68# The header size should match that in hello1/prj.conf
69# CONFIG_TEXT_SECTION_OFFSET. This value needs to be a power of two
70# that is at least as large as the size of the vector table. The
71# value given here of 0x200 should be sufficient for any supported
72# devices, but it can be made smaller, as long as this value matches
73# that used to build the app.
74BOOT_HEADER_LEN = 0x200
75
76# For upgrades, the signing tool needs to know the device alignment.
77# This requirement will be going away soon.
78FLASH_ALIGNMENT = 8
79
80IMGTOOL = ../../scripts/imgtool.py
David Browndbc57272017-07-17 15:38:54 -060081ASSEMBLE = ../../scripts/assemble.py
Maureen Helm0e0c4882019-02-18 17:20:00 -060082PYOCD = pyocd
David Brownada28e12017-07-05 13:36:37 -060083
Marti Bolivarbf909a12017-11-13 19:43:46 -050084SOURCE_DIRECTORY := $(CURDIR)
85BUILD_DIRECTORY := $(CURDIR)/build/$(BOARD)
86BUILD_DIR_BOOT := $(BUILD_DIRECTORY)/mcuboot
87BUILD_DIR_HELLO1 := $(BUILD_DIRECTORY)/hello1
88BUILD_DIR_HELLO2 := $(BUILD_DIRECTORY)/hello2
89
David Brownada28e12017-07-05 13:36:37 -060090help:
91 @echo "make <target> BOARD=<board>"
David Browndbc57272017-07-17 15:38:54 -060092 @echo "<target>: all, boot, hello1, full.bin"
Marti Bolivarbf909a12017-11-13 19:43:46 -050093 @echo "<board>: frdm_k64f only for now"
David Brownada28e12017-07-05 13:36:37 -060094
95all: boot hello1 hello2
96
David Browndbc57272017-07-17 15:38:54 -060097full.bin: boot hello1 hello2
Marti Bolivarbf909a12017-11-13 19:43:46 -050098 $(ASSEMBLE) -b $(BUILD_DIR_BOOT) \
Torsten Rasmussen33fbef52020-06-03 20:21:13 +020099 -z $(ZEPHYR_BASE) \
David Browndbc57272017-07-17 15:38:54 -0600100 -p signed-hello1.bin \
101 -s signed-hello2.bin \
102 -o full.bin
103
David Brownada28e12017-07-05 13:36:37 -0600104clean: clean_boot clean_hello1 clean_hello2
105 @rm -f signed-hello1.bin
106 @rm -f signed-hello2.bin
107 @rm -f mcuboot.bin
108
109boot: check
110 @rm -f mcuboot.bin
Marti Bolivarbf909a12017-11-13 19:43:46 -0500111 (mkdir -p $(BUILD_DIR_BOOT) && \
112 cd $(BUILD_DIR_BOOT) && \
Marti Bolivara8798432018-04-12 13:12:06 -0400113 cmake -DOVERLAY_CONFIG=$(BOOTLOADER_OVERLAY_CONFIG) \
David Brown358ca1a2019-06-25 10:13:20 -0600114 -G"Ninja" \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500115 -DBOARD=$(BOARD) \
116 $(SOURCE_DIRECTORY)/../../boot/zephyr && \
David Brown358ca1a2019-06-25 10:13:20 -0600117 ninja)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500118 cp $(BUILD_DIR_BOOT)/zephyr/zephyr.bin mcuboot.bin
David Brownada28e12017-07-05 13:36:37 -0600119
120clean_boot: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500121 rm -rf $(BUILD_DIR_BOOT)
David Brownada28e12017-07-05 13:36:37 -0600122
123# Build and sign "hello1".
124hello1: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500125 (mkdir -p $(BUILD_DIR_HELLO1) && \
126 cd $(BUILD_DIR_HELLO1) && \
127 cmake -DFROM_WHO=hello1 \
David Brown358ca1a2019-06-25 10:13:20 -0600128 -G"Ninja" \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500129 -DBOARD=$(BOARD) \
130 $(SOURCE_DIRECTORY)/hello-world && \
David Brown358ca1a2019-06-25 10:13:20 -0600131 ninja)
David Brownada28e12017-07-05 13:36:37 -0600132 $(IMGTOOL) sign \
133 --key $(SIGNING_KEY) \
134 --header-size $(BOOT_HEADER_LEN) \
135 --align $(FLASH_ALIGNMENT) \
136 --version 1.2 \
honglin leng37e42232022-08-17 11:23:04 +0800137 --slot-size $(SLOT_SIZE) \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500138 $(BUILD_DIR_HELLO1)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600139 signed-hello1.bin
140
141clean_hello1: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500142 rm -rf $(BUILD_DIR_HELLO1)
David Brownada28e12017-07-05 13:36:37 -0600143
144# Build and sign "hello2".
145# This is the same signing command as above, except that it adds the
146# "--pad" argument. This will also add the trailer that indicates
147# this image is intended to be an upgrade. It should be flashed into
David Vincze2d736ad2019-02-18 11:50:22 +0100148# the secondary slot instead of the primary slot.
David Brownada28e12017-07-05 13:36:37 -0600149hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500150 (mkdir -p $(BUILD_DIR_HELLO2) && \
151 cd $(BUILD_DIR_HELLO2) && \
152 cmake -DFROM_WHO=hello2 \
David Brown358ca1a2019-06-25 10:13:20 -0600153 -G"Ninja" \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500154 -DBOARD=$(BOARD) \
155 $(SOURCE_DIRECTORY)/hello-world && \
David Brown358ca1a2019-06-25 10:13:20 -0600156 ninja)
David Brownada28e12017-07-05 13:36:37 -0600157 $(IMGTOOL) sign \
158 --key $(SIGNING_KEY) \
159 --header-size $(BOOT_HEADER_LEN) \
160 --align $(FLASH_ALIGNMENT) \
161 --version 1.2 \
honglin leng37e42232022-08-17 11:23:04 +0800162 --slot-size $(SLOT_SIZE) \
Fabio Utzig263d4392018-06-05 10:37:35 -0300163 --pad \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500164 $(BUILD_DIR_HELLO2)/zephyr/zephyr.bin \
David Brownada28e12017-07-05 13:36:37 -0600165 signed-hello2.bin
166
167clean_hello2: check
Marti Bolivarbf909a12017-11-13 19:43:46 -0500168 rm -rf $(BUILD_DIR_HELLO2)
David Brownada28e12017-07-05 13:36:37 -0600169
David Brown1d3f67d2017-09-14 15:10:24 -0600170# These flash_* targets use pyocd to flash the images. The addresses
171# are hardcoded at this time.
172
David Brownada28e12017-07-05 13:36:37 -0600173flash_boot:
honglin leng37e42232022-08-17 11:23:04 +0800174 $(PYOCD) flash -e chip -a $(BOOT_ADDR) mcuboot.bin
David Brownada28e12017-07-05 13:36:37 -0600175
176flash_hello1:
honglin leng37e42232022-08-17 11:23:04 +0800177 $(PYOCD) flash -a $(IMG0_ADDR) signed-hello1.bin
David Brownada28e12017-07-05 13:36:37 -0600178
179flash_hello2:
honglin leng37e42232022-08-17 11:23:04 +0800180 $(PYOCD) flash -a $(IMG1_ADDR) signed-hello2.bin
David Brownada28e12017-07-05 13:36:37 -0600181
David Brown84a67af2017-07-26 00:36:02 -0600182flash_full:
honglin leng37e42232022-08-17 11:23:04 +0800183 $(PYOCD) flash -e chip -a $(BOOT_ADDR) full.bin
David Brown84a67af2017-07-26 00:36:02 -0600184
David Brown1d3f67d2017-09-14 15:10:24 -0600185# These test- targets reinvoke make with the configuration set to test
186# various configurations. This will generally be followed by using
187# the above flash targets.
188
189# Test a good image, with a good upgrade, using RSA signatures.
190# flash_boot: Unable to find bootable image
191# flash_hello1: hello1 runs
192# flash_hello2: hello2 runs
193# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500194test-good-rsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600195 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400196 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500197 all
David Brown1d3f67d2017-09-14 15:10:24 -0600198
199# Test a good image, with a good upgrade, using ECDSA signatures.
200# flash_boot: Unable to find bootable image
201# flash_hello1: hello1 runs
202# flash_hello2: hello2 runs
203# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500204test-good-ecdsa: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600205 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400206 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500207 SIGNING_KEY=../../root-ec-p256.pem \
208 all
David Brown1d3f67d2017-09-14 15:10:24 -0600209
210# Test (with RSA) that overwrite-only works. This should boot,
211# upgrade correctly, but not revert once the upgrade has been done.
212# flash_boot: Unable to find bootable image
213# flash_hello1: hello1 runs
214# flash_hello2: hello2 runs
215# reset: hello2 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500216test-overwrite: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600217 $(MAKE) \
Marti Bolivara8798432018-04-12 13:12:06 -0400218 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-upgrade-only.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500219 all
David Brown1d3f67d2017-09-14 15:10:24 -0600220
221# Test that when configured for RSA, a wrong signature in the upgrade
222# image will fail to upgrade.
223# flash_boot: Unable to find bootable image
224# flash_hello1: hello1 runs
225# flash_hello2: hello1 runs
226# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500227test-bad-rsa-upgrade: clean
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 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600231 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400232 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500233 SIGNING_KEY=../../root-ec-p256.pem \
234 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600235
236# Test that when configured for ECDSA, a wrong signature in the upgrade
237# image will fail to upgrade.
238# flash_boot: Unable to find bootable image
239# flash_hello1: hello1 runs
240# flash_hello2: hello1 runs
241# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500242test-bad-ecdsa-upgrade: clean
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-ec-p256.pem \
246 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600247 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400248 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500249 SIGNING_KEY=../../root-rsa-2048.pem \
250 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600251
David Vincze2d736ad2019-02-18 11:50:22 +0100252# Test that when configured to not validate the primary slot, we still boot, but
David Brown1d3f67d2017-09-14 15:10:24 -0600253# don't upgrade.
254# flash_boot: tries to boot and resets
255# flash_hello1: hello1 runs
256# flash_hello2: hello1 runs
257# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500258test-no-bootcheck: clean
David Brown1d3f67d2017-09-14 15:10:24 -0600259 $(MAKE) \
David Vincze2d736ad2019-02-18 11:50:22 +0100260 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-skip-primary-slot-validate.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500261 SIGNING_KEY=../../root-ec-p256.pem \
262 all
David Brown1d3f67d2017-09-14 15:10:24 -0600263
264# Test a good image, with a wrong-signature upgrade, using RSA signatures.
265# flash_boot: Unable to find bootable image
266# flash_hello1: hello1 runs
267# flash_hello2: hello1 runs
268# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500269test-wrong-rsa: clean
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 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600273 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400274 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-rsa.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500275 SIGNING_KEY=bad-keys/bad-rsa-2048.pem \
276 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600277
278# Test a good image, with a wrong-signature upgrade, using ECDSA signatures.
279# flash_boot: Unable to find bootable image
280# flash_hello1: hello1 runs
281# flash_hello2: hello1 runs
282# reset: hello1 runs
Marti Bolivarbf909a12017-11-13 19:43:46 -0500283test-wrong-ecdsa: clean
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=../../root-ec-p256.pem \
287 boot hello1
David Brown1d3f67d2017-09-14 15:10:24 -0600288 $(MAKE) \
Marti Bolivara4818a52018-04-12 13:02:38 -0400289 BOOTLOADER_OVERLAY_CONFIG=$(PWD)/overlay-ecdsa-p256.conf \
Marti Bolivarbf909a12017-11-13 19:43:46 -0500290 SIGNING_KEY=bad-keys/bad-ec-p256.pem \
291 hello2
David Brown1d3f67d2017-09-14 15:10:24 -0600292
David Brownada28e12017-07-05 13:36:37 -0600293check:
294 @if [ -z "$$ZEPHYR_BASE" ]; then echo "Zephyr environment not set up"; false; fi
Marti Bolivar71354102017-08-01 15:09:44 -0400295 @if [ -z "$(BOARD)" ]; then echo "You must specify BOARD=<board>"; false; fi