blob: 4a0a5c7a0e029cdf934539c4fa53ee385e16d4eb [file] [log] [blame]
Tamas Ban581034a2017-12-19 19:54:37 +00001#------------------------------------------------------------------------------
Raef Coles55e5e6b2021-01-19 11:41:08 +00002# Copyright (c) 2020-2021, 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 Coles6d47fb72020-12-16 09:26:48 +000011add_library(mcuboot_config INTERFACE)
12
Raef Coles8efad882020-07-10 09:46:00 +010013set(MCUBOOT_KEY_ENC "${MCUBOOT_PATH}/enc-rsa2048-pub.pem" CACHE FILEPATH "Path to key with which to encrypt binary")
14
Raef Coles6d47fb72020-12-16 09:26:48 +000015add_subdirectory("${MCUBOOT_PATH}/boot/bootutil" bootutil)
16
17target_include_directories(bootutil
18 PUBLIC
19 include
Michel Jaouenfd7164f2021-04-29 09:37:49 +020020 config
Raef Coles6d47fb72020-12-16 09:26:48 +000021)
22
23target_link_libraries(bootutil
24 PUBLIC
25 mcuboot_config
26 platform_bl2
27 bl2_mbedcrypto
28)
29
Mark Horvath8576e382021-03-12 10:24:55 +010030target_compile_definitions(bootutil
31 PRIVATE
32 $<$<BOOL:${DEFAULT_MCUBOOT_FLASH_MAP}>:DEFAULT_MCUBOOT_FLASH_MAP>
33)
34
Raef Coles6d47fb72020-12-16 09:26:48 +000035target_include_directories(mcuboot_config
36 INTERFACE
37 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}> # for mcuboot_config.h only
38)
39
Raef Coles8efad882020-07-10 09:46:00 +010040target_include_directories(bl2
41 PUBLIC
42 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Raef Coles8efad882020-07-10 09:46:00 +010043)
44
45target_sources(bl2
46 PRIVATE
47 ${CMAKE_CURRENT_SOURCE_DIR}/bl2_main.c
48 ${CMAKE_CURRENT_SOURCE_DIR}/keys.c
49 ${CMAKE_CURRENT_SOURCE_DIR}/flash_map_extended.c
50 ${CMAKE_CURRENT_SOURCE_DIR}/flash_map_legacy.c
Raef Coles6d47fb72020-12-16 09:26:48 +000051)
52
53target_link_libraries(bl2
54 PUBLIC
55 bootutil
Raef Coles8efad882020-07-10 09:46:00 +010056)
57
58set(MCUBOOT_ALLOWED_LOG_LEVELS OFF ERROR WARNING INFO DEBUG)
59list(FIND MCUBOOT_ALLOWED_LOG_LEVELS ${MCUBOOT_LOG_LEVEL} LOG_LEVEL_ID)
60
61configure_file(include/mcuboot_config/mcuboot_config.h.in
62 ${CMAKE_CURRENT_BINARY_DIR}/mcuboot_config/mcuboot_config.h
63 @ONLY)
64
65############################### IMAGE SIGNING ##################################
66
67find_package(Python3)
68
69set(FLASH_AREA_NUM 0)
Sherry Zhangae25f052021-05-13 14:49:48 +080070set(IMAGE_TYPE "S_IMAGE")
Raef Coles8efad882020-07-10 09:46:00 +010071if (MCUBOOT_IMAGE_NUMBER GREATER 1)
72 configure_file(signing_layout.c.in signing_layout_s.c @ONLY)
73 add_library(signing_layout_s OBJECT ${CMAKE_CURRENT_BINARY_DIR}/signing_layout_s.c)
David Vinczec3e313a2020-01-06 17:31:11 +010074else()
Raef Coles8efad882020-07-10 09:46:00 +010075 # Imgtool script requires the s_ns sufix. Since only one sigining layout is
76 # used in this mode the signing_layout_s target's source file is renamed.
77 configure_file(signing_layout.c.in signing_layout_s_ns.c @ONLY)
78 add_library(signing_layout_s OBJECT ${CMAKE_CURRENT_BINARY_DIR}/signing_layout_s_ns.c)
David Vinczec3e313a2020-01-06 17:31:11 +010079endif()
80
Raef Coles8efad882020-07-10 09:46:00 +010081target_compile_options(signing_layout_s
82 PRIVATE
83 $<$<C_COMPILER_ID:GNU>:-E\;-xc>
84 $<$<C_COMPILER_ID:ARMClang>:-E\;-xc>
85 $<$<C_COMPILER_ID:IAR>:--preprocess=ns\;$<TARGET_OBJECTS:signing_layout_s>>
86)
87target_compile_definitions(signing_layout_s
88 PRIVATE
89 $<$<BOOL:${BL2}>:BL2>
90 $<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
Sherry Zhangae25f052021-05-13 14:49:48 +080091 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},DIRECT_XIP>:IMAGE_ROM_FIXED>
Raef Coles8efad882020-07-10 09:46:00 +010092)
93target_link_libraries(signing_layout_s
94 PRIVATE
95 platform_bl2
96)
97
98if(NS)
99 add_custom_target(tfm_s_ns_bin
100 SOURCES tfm_s_ns.bin
101 )
102 add_custom_command(OUTPUT tfm_s_ns.bin
103 DEPENDS $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
104 DEPENDS $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
105 DEPENDS tfm_s_bin tfm_ns_bin
106 DEPENDS signing_layout_s
107
108 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/assemble.py
109 --layout $<TARGET_OBJECTS:signing_layout_s>
110 -s $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
111 -n $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
112 -o tfm_s_ns.bin
113 COMMAND ${CMAKE_COMMAND} -E copy tfm_s_ns.bin $<TARGET_FILE_DIR:bl2>
114 )
Tamas Ban581034a2017-12-19 19:54:37 +0000115endif()
116
Raef Coles8efad882020-07-10 09:46:00 +0100117add_custom_target(tfm_s_signed_bin
118 SOURCES tfm_s_signed.bin
119)
120add_custom_command(OUTPUT tfm_s_signed.bin
121 DEPENDS $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
122 DEPENDS tfm_s_bin signing_layout_s
123 WORKING_DIRECTORY ${MCUBOOT_PATH}/scripts
Tamas Band0f4e1d2019-07-11 09:39:03 +0100124
Raef Coles8efad882020-07-10 09:46:00 +0100125 #Sign secure binary image with provided secret key
126 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
127 -v ${MCUBOOT_IMAGE_VERSION_S}
128 --layout $<TARGET_OBJECTS:signing_layout_s>
129 -k ${MCUBOOT_KEY_S}
130 --public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
131 --align 1
132 --pad
133 --pad-header
134 -H 0x400
135 -s ${MCUBOOT_SECURITY_COUNTER_S}
Raef Coles55e5e6b2021-01-19 11:41:08 +0000136 -d \"\(1,${MCUBOOT_NS_IMAGE_MIN_VER}\)\"
Raef Coles8efad882020-07-10 09:46:00 +0100137 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
138 $<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
139 $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
140 ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin
141 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin $<TARGET_FILE_DIR:bl2>
142)
143
Mark Horvath531fce82021-01-15 16:10:22 +0100144set(FLASH_AREA_NUM 1)
Sherry Zhangae25f052021-05-13 14:49:48 +0800145set(IMAGE_TYPE "NS_IMAGE")
Mark Horvath531fce82021-01-15 16:10:22 +0100146configure_file(signing_layout.c.in signing_layout_ns.c @ONLY)
147
148add_library(signing_layout_ns OBJECT ${CMAKE_CURRENT_BINARY_DIR}/signing_layout_ns.c)
149target_compile_options(signing_layout_ns
150 PRIVATE
151 $<$<C_COMPILER_ID:GNU>:-E\;-xc>
152 $<$<C_COMPILER_ID:ARMClang>:-E\;-xc>
153 $<$<C_COMPILER_ID:IAR>:--preprocess=ns\;$<TARGET_OBJECTS:signing_layout_ns>>
154)
155target_compile_definitions(signing_layout_ns
156 PRIVATE
157 $<$<BOOL:${BL2}>:BL2>
158 $<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
Sherry Zhangae25f052021-05-13 14:49:48 +0800159 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},DIRECT_XIP>:IMAGE_ROM_FIXED>
Mark Horvath531fce82021-01-15 16:10:22 +0100160)
161target_link_libraries(signing_layout_ns
162 PRIVATE
163 platform_bl2
164)
165
Raef Coles8efad882020-07-10 09:46:00 +0100166if(NS)
Raef Coles8efad882020-07-10 09:46:00 +0100167 add_custom_target(tfm_ns_signed_bin
168 SOURCES tfm_ns_signed.bin
169 )
170 add_custom_command(OUTPUT tfm_ns_signed.bin
171 DEPENDS $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
172 DEPENDS tfm_ns_bin signing_layout_ns
173 WORKING_DIRECTORY ${MCUBOOT_PATH}/scripts
174
175 #Sign non-secure binary image with provided secret key
176 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
177 -v ${MCUBOOT_IMAGE_VERSION_NS}
178 --layout $<TARGET_OBJECTS:signing_layout_ns>
179 -k ${MCUBOOT_KEY_NS}
180 --public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
181 --align 1
182 --pad
183 --pad-header
184 -H 0x400
185 -s ${MCUBOOT_SECURITY_COUNTER_NS}
Raef Coles55e5e6b2021-01-19 11:41:08 +0000186 -d \"\(0, ${MCUBOOT_S_IMAGE_MIN_VER}\)\"
Raef Coles8efad882020-07-10 09:46:00 +0100187 $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
188 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
189 $<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
190 ${CMAKE_CURRENT_BINARY_DIR}/tfm_ns_signed.bin
191 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_ns_signed.bin $<TARGET_FILE_DIR:bl2>
192 )
Tamas Band0f4e1d2019-07-11 09:39:03 +0100193endif()
194
Raef Coles8efad882020-07-10 09:46:00 +0100195if(NS)
196 add_custom_target(tfm_s_ns_signed_bin
Raef Coles8efad882020-07-10 09:46:00 +0100197 SOURCES tfm_s_ns_signed.bin
198 )
199 if (MCUBOOT_IMAGE_NUMBER GREATER 1)
200 add_custom_command(OUTPUT tfm_s_ns_signed.bin
201 DEPENDS tfm_s_signed_bin $<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
202 DEPENDS tfm_ns_signed_bin $<TARGET_FILE_DIR:tfm_ns>/tfm_ns.bin
203 DEPENDS signing_layout_s
204
205 # Create concatenated binary image from the two independently signed
206 # binary file. This only uses the local assemble.py script (not from
207 # upstream mcuboot) because that script is geared towards zephyr
208 # support
209 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/assemble.py
210 --layout $<TARGET_OBJECTS:signing_layout_s>
211 -s $<TARGET_FILE_DIR:bl2>/tfm_s_signed.bin
212 -n $<TARGET_FILE_DIR:bl2>/tfm_ns_signed.bin
213 -o tfm_s_ns_signed.bin
214 COMMAND ${CMAKE_COMMAND} -E copy tfm_s_ns_signed.bin $<TARGET_FILE_DIR:bl2>
215 )
216 else()
217 add_custom_command(OUTPUT tfm_s_ns_signed.bin
Sherry Zhang4c9b0ed2021-04-20 18:29:19 +0800218 WORKING_DIRECTORY ${MCUBOOT_PATH}/scripts
Raef Coles8efad882020-07-10 09:46:00 +0100219 DEPENDS tfm_s_ns_bin tfm_s_ns.bin
220 DEPENDS signing_layout_s
221
Mark Horvathb9ac0d52020-09-09 10:48:22 +0200222 # Use the non-secure key to sign the combined image if FORWARD_PROT_MSG is set.
223 # In such a configuration there is a subsystem with higher privileges controlling the
224 # the boot process and current implementation requires to use the non-secure key here.
Raef Coles8efad882020-07-10 09:46:00 +0100225 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
226 -v ${MCUBOOT_IMAGE_VERSION_S}
227 --layout $<TARGET_OBJECTS:signing_layout_s>
Mark Horvathb9ac0d52020-09-09 10:48:22 +0200228 -k $<IF:$<BOOL:${FORWARD_PROT_MSG}>,${MCUBOOT_KEY_NS},${MCUBOOT_KEY_S}>
Raef Coles8efad882020-07-10 09:46:00 +0100229 --public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
230 --align 1
231 --pad
232 --pad-header
233 -H 0x400
234 -s ${MCUBOOT_SECURITY_COUNTER_S}
Raef Coles8efad882020-07-10 09:46:00 +0100235 $<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
236 $<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
Sherry Zhang4c9b0ed2021-04-20 18:29:19 +0800237 ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_ns.bin
Raef Coles8efad882020-07-10 09:46:00 +0100238 ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_ns_signed.bin
239 COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_ns_signed.bin $<TARGET_FILE_DIR:bl2>
240 )
241 endif()
Marc Moreno Berenguea1f296f2018-01-25 15:21:22 +0000242endif()
243
Raef Coles8efad882020-07-10 09:46:00 +0100244add_custom_target(signed_images
245 ALL
Mark Horvathf0565142020-11-19 18:52:44 +0100246 DEPENDS $<IF:$<BOOL:${NS}>,tfm_s_ns_signed_bin,tfm_s_signed_bin>
Raef Coles8efad882020-07-10 09:46:00 +0100247)