blob: c8dd0a38539bfcf1a974694b7d5ae8f05064cd1d [file] [log] [blame]
Tamas Ban581034a2017-12-19 19:54:37 +00001#------------------------------------------------------------------------------
Raef Coles8efad882020-07-10 09:46:00 +01002# Copyright (c) 2020, Arm Limited. All rights reserved.
Tamas Ban581034a2017-12-19 19:54:37 +00003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#------------------------------------------------------------------------------
7
Raef Coles69817322020-10-19 14:14:14 +01008cmake_minimum_required(VERSION 3.15)
Raef Coles8efad882020-07-10 09:46:00 +01009cmake_policy(SET CMP0079 NEW)
Tamas Ban581034a2017-12-19 19:54:37 +000010
Raef Coles8efad882020-07-10 09:46:00 +010011set(MCUBOOT_KEY_ENC "${MCUBOOT_PATH}/enc-rsa2048-pub.pem" CACHE FILEPATH "Path to key with which to encrypt binary")
12
13target_include_directories(bl2
14 PUBLIC
15 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
16 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # for mcuboot_config.h only
17 $<BUILD_INTERFACE:${MCUBOOT_PATH}/boot/bootutil/include>
18 $<BUILD_INTERFACE:${MCUBOOT_PATH}/boot>
19)
20
21target_sources(bl2
22 PRIVATE
23 ${CMAKE_CURRENT_SOURCE_DIR}/bl2_main.c
24 ${CMAKE_CURRENT_SOURCE_DIR}/keys.c
25 ${CMAKE_CURRENT_SOURCE_DIR}/flash_map_extended.c
26 ${CMAKE_CURRENT_SOURCE_DIR}/flash_map_legacy.c
27 ${MCUBOOT_PATH}/boot/bootutil/src/loader.c
28 ${MCUBOOT_PATH}/boot/bootutil/src/bootutil_misc.c
29 ${MCUBOOT_PATH}/boot/bootutil/src/image_validate.c
30 ${MCUBOOT_PATH}/boot/bootutil/src/image_rsa.c
31 ${MCUBOOT_PATH}/boot/bootutil/src/tlv.c
32 ${MCUBOOT_PATH}/boot/bootutil/src/boot_record.c
Balint Matyi69e2d2e2020-07-08 10:53:54 +010033 ${MCUBOOT_PATH}/boot/bootutil/src/swap_scratch.c
34 ${MCUBOOT_PATH}/boot/bootutil/src/swap_move.c
35 ${MCUBOOT_PATH}/boot/bootutil/src/swap_misc.c
36 ${MCUBOOT_PATH}/boot/bootutil/src/encrypted.c
Tamas Ban1bfc9da2020-07-09 13:55:38 +010037 ${MCUBOOT_PATH}/boot/bootutil/src/fault_injection_hardening.c
38 ${MCUBOOT_PATH}/boot/bootutil/src/fault_injection_hardening_delay_rng_mbedtls.c
Raef Coles8efad882020-07-10 09:46:00 +010039)
40
41set(MCUBOOT_ALLOWED_LOG_LEVELS OFF ERROR WARNING INFO DEBUG)
42list(FIND MCUBOOT_ALLOWED_LOG_LEVELS ${MCUBOOT_LOG_LEVEL} LOG_LEVEL_ID)
43
44configure_file(include/mcuboot_config/mcuboot_config.h.in
45 ${CMAKE_CURRENT_BINARY_DIR}/mcuboot_config/mcuboot_config.h
46 @ONLY)
47
48############################### IMAGE SIGNING ##################################
49
50find_package(Python3)
51
52set(FLASH_AREA_NUM 0)
53if (MCUBOOT_IMAGE_NUMBER GREATER 1)
54 configure_file(signing_layout.c.in signing_layout_s.c @ONLY)
55 add_library(signing_layout_s OBJECT ${CMAKE_CURRENT_BINARY_DIR}/signing_layout_s.c)
David Vinczec3e313a2020-01-06 17:31:11 +010056else()
Raef Coles8efad882020-07-10 09:46:00 +010057 # Imgtool script requires the s_ns sufix. Since only one sigining layout is
58 # used in this mode the signing_layout_s target's source file is renamed.
59 configure_file(signing_layout.c.in signing_layout_s_ns.c @ONLY)
60 add_library(signing_layout_s OBJECT ${CMAKE_CURRENT_BINARY_DIR}/signing_layout_s_ns.c)
David Vinczec3e313a2020-01-06 17:31:11 +010061endif()
62
Raef Coles8efad882020-07-10 09:46:00 +010063target_compile_options(signing_layout_s
64 PRIVATE
65 $<$<C_COMPILER_ID:GNU>:-E\;-xc>
66 $<$<C_COMPILER_ID:ARMClang>:-E\;-xc>
67 $<$<C_COMPILER_ID:IAR>:--preprocess=ns\;$<TARGET_OBJECTS:signing_layout_s>>
68)
69target_compile_definitions(signing_layout_s
70 PRIVATE
71 $<$<BOOL:${BL2}>:BL2>
72 $<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
73)
74target_link_libraries(signing_layout_s
75 PRIVATE
76 platform_bl2
77)
78
79if(NS)
80 add_custom_target(tfm_s_ns_bin
81 SOURCES tfm_s_ns.bin
82 )
83 add_custom_command(OUTPUT tfm_s_ns.bin
84 DEPENDS $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
85 DEPENDS $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
86 DEPENDS tfm_s_bin tfm_ns_bin
87 DEPENDS signing_layout_s
88
89 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/assemble.py
90 --layout $<TARGET_OBJECTS:signing_layout_s>
91 -s $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
92 -n $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
93 -o tfm_s_ns.bin
94 COMMAND ${CMAKE_COMMAND} -E copy tfm_s_ns.bin $<TARGET_FILE_DIR:bl2>
95 )
Tamas Ban581034a2017-12-19 19:54:37 +000096endif()
97
Raef Coles8efad882020-07-10 09:46:00 +010098add_custom_target(tfm_s_signed_bin
99 SOURCES tfm_s_signed.bin
100)
101add_custom_command(OUTPUT tfm_s_signed.bin
102 DEPENDS $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
103 DEPENDS tfm_s_bin signing_layout_s
104 WORKING_DIRECTORY ${MCUBOOT_PATH}/scripts
Tamas Band0f4e1d2019-07-11 09:39:03 +0100105
Raef Coles8efad882020-07-10 09:46:00 +0100106 #Sign secure binary image with provided secret key
107 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
108 -v ${MCUBOOT_IMAGE_VERSION_S}
109 --layout $<TARGET_OBJECTS:signing_layout_s>
110 -k ${MCUBOOT_KEY_S}
111 --public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
112 --align 1
113 --pad
114 --pad-header
115 -H 0x400
116 -s ${MCUBOOT_SECURITY_COUNTER_S}
117 -d \"\(0,${MCUBOOT_S_IMAGE_MIN_VER}\)\"
118 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
119 $<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
120 $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
121 ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin
122 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin $<TARGET_FILE_DIR:bl2>
123)
124
125if(NS)
126 set(FLASH_AREA_NUM 1)
127 configure_file(signing_layout.c.in signing_layout_ns.c @ONLY)
128
129 add_library(signing_layout_ns OBJECT ${CMAKE_CURRENT_BINARY_DIR}/signing_layout_ns.c)
130 target_compile_options(signing_layout_ns
131 PRIVATE
132 $<$<C_COMPILER_ID:GNU>:-E\;-xc>
133 $<$<C_COMPILER_ID:ARMClang>:-E\;-xc>
134 $<$<C_COMPILER_ID:IAR>:--preprocess=ns\;$<TARGET_OBJECTS:signing_layout_ns>>
135 )
136 target_compile_definitions(signing_layout_ns
137 PRIVATE
138 $<$<BOOL:${BL2}>:BL2>
139 $<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
140 )
141 target_link_libraries(signing_layout_ns
142 PRIVATE
143 platform_bl2
144 )
145
146 add_custom_target(tfm_ns_signed_bin
147 SOURCES tfm_ns_signed.bin
148 )
149 add_custom_command(OUTPUT tfm_ns_signed.bin
150 DEPENDS $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
151 DEPENDS tfm_ns_bin signing_layout_ns
152 WORKING_DIRECTORY ${MCUBOOT_PATH}/scripts
153
154 #Sign non-secure binary image with provided secret key
155 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
156 -v ${MCUBOOT_IMAGE_VERSION_NS}
157 --layout $<TARGET_OBJECTS:signing_layout_ns>
158 -k ${MCUBOOT_KEY_NS}
159 --public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
160 --align 1
161 --pad
162 --pad-header
163 -H 0x400
164 -s ${MCUBOOT_SECURITY_COUNTER_NS}
165 -d \"\(1, ${MCUBOOT_NS_IMAGE_MIN_VER}\)\"
166 $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
167 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
168 $<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
169 ${CMAKE_CURRENT_BINARY_DIR}/tfm_ns_signed.bin
170 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_ns_signed.bin $<TARGET_FILE_DIR:bl2>
171 )
Tamas Band0f4e1d2019-07-11 09:39:03 +0100172endif()
173
Raef Coles8efad882020-07-10 09:46:00 +0100174if(NS)
175 add_custom_target(tfm_s_ns_signed_bin
Raef Coles8efad882020-07-10 09:46:00 +0100176 SOURCES tfm_s_ns_signed.bin
177 )
178 if (MCUBOOT_IMAGE_NUMBER GREATER 1)
179 add_custom_command(OUTPUT tfm_s_ns_signed.bin
180 DEPENDS tfm_s_signed_bin $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
181 DEPENDS tfm_ns_signed_bin $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
182 DEPENDS signing_layout_s
183
184 # Create concatenated binary image from the two independently signed
185 # binary file. This only uses the local assemble.py script (not from
186 # upstream mcuboot) because that script is geared towards zephyr
187 # support
188 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/assemble.py
189 --layout $<TARGET_OBJECTS:signing_layout_s>
190 -s $<TARGET_FILE_DIR:bl2>/tfm_s_signed.bin
191 -n $<TARGET_FILE_DIR:bl2>/tfm_ns_signed.bin
192 -o tfm_s_ns_signed.bin
193 COMMAND ${CMAKE_COMMAND} -E copy tfm_s_ns_signed.bin $<TARGET_FILE_DIR:bl2>
194 )
195 else()
196 add_custom_command(OUTPUT tfm_s_ns_signed.bin
197 DEPENDS tfm_s_ns_bin tfm_s_ns.bin
198 DEPENDS signing_layout_s
199
Mark Horvathb9ac0d52020-09-09 10:48:22 +0200200 # Use the non-secure key to sign the combined image if FORWARD_PROT_MSG is set.
201 # In such a configuration there is a subsystem with higher privileges controlling the
202 # the boot process and current implementation requires to use the non-secure key here.
Raef Coles8efad882020-07-10 09:46:00 +0100203 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
204 -v ${MCUBOOT_IMAGE_VERSION_S}
205 --layout $<TARGET_OBJECTS:signing_layout_s>
Mark Horvathb9ac0d52020-09-09 10:48:22 +0200206 -k $<IF:$<BOOL:${FORWARD_PROT_MSG}>,${MCUBOOT_KEY_NS},${MCUBOOT_KEY_S}>
Raef Coles8efad882020-07-10 09:46:00 +0100207 --public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
208 --align 1
209 --pad
210 --pad-header
211 -H 0x400
212 -s ${MCUBOOT_SECURITY_COUNTER_S}
213 -d \"\(0, ${MCUBOOT_S_IMAGE_MIN_VER}\)\"
214 -d \"\(1, ${MCUBOOT_NS_IMAGE_MIN_VER}\)\"
215 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
216 $<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
217 tfm_s_ns.bin
218 ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_ns_signed.bin
219 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_ns_signed.bin $<TARGET_FILE_DIR:bl2>
220 )
221 endif()
Marc Moreno Berenguea1f296f2018-01-25 15:21:22 +0000222endif()
223
Raef Coles8efad882020-07-10 09:46:00 +0100224add_custom_target(signed_images
225 ALL
Mark Horvathf0565142020-11-19 18:52:44 +0100226 DEPENDS $<IF:$<BOOL:${NS}>,tfm_s_ns_signed_bin,tfm_s_signed_bin>
Raef Coles8efad882020-07-10 09:46:00 +0100227)