-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMakeLists.txt
159 lines (143 loc) · 7.05 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright (c) 2024 Jakub Zimnol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
cmake_minimum_required(VERSION 3.13)
########################################
# CMake options
########################################
option(PFB_WITH_BOOTLOADER_LOGS "Enables logging messages from the bootloader using stdio" ON)
option(PFB_REDIRECT_BOOTLOADER_LOGS_TO_UART "Redirects bootloader's logs from USB to UART" OFF)
option(PFB_WITH_IMAGE_ENCRYPTION "Enables image encryption using AES ECB algorithm" ON)
option(PFB_AES_KEY "AES key used for image encryption and decryption")
option(PFB_WITH_SHA256_HASHING "Enables image SHA256 appending and checking" ON)
########################################
# Check and set AES key
########################################
if (PFB_WITH_IMAGE_ENCRYPTION)
if (NOT PFB_AES_KEY)
set(PFB_AES_KEY "default")
message(WARNING "AES key has been set to: \"${PFB_AES_KEY}\"")
endif ()
message(STATUS "AES key: ${PFB_AES_KEY}")
string(REGEX MATCH "^[0-9a-zA-Z]+" aes_key_match ${PFB_AES_KEY})
string(LENGTH ${PFB_AES_KEY} aes_key_length)
if ((NOT ${PFB_AES_KEY} STREQUAL ${aes_key_match}) OR (${aes_key_length} GREATER 32))
message(FATAL_ERROR
"AES key must be 32 bytes long and contain only characters from the set [0-9a-zA-Z].")
endif ()
math(EXPR num_zeros_needed "32 - ${aes_key_length}")
string(REPEAT "x" ${num_zeros_needed} XS)
string(CONCAT PFB_AES_KEY ${PFB_AES_KEY} ${XS})
message(STATUS "AES key padded to 32 characters: ${PFB_AES_KEY}")
set(PFB_AES_KEY_GLOBAL ${PFB_AES_KEY} PARENT_SCOPE)
endif()
################################################################################
# Define the pico_fota_bootloader_lib library
################################################################################
add_library(pico_fota_bootloader_lib STATIC
src/pico_fota_bootloader.c)
target_include_directories(pico_fota_bootloader_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(pico_fota_bootloader_lib PUBLIC
hardware_watchdog
pico_stdlib
hardware_flash)
target_link_libraries(pico_fota_bootloader_lib PRIVATE
pico_mbedtls)
target_link_options(pico_fota_bootloader_lib PRIVATE
"-T${CMAKE_CURRENT_SOURCE_DIR}/linker_common/linker_definitions.ld")
if (PFB_WITH_IMAGE_ENCRYPTION)
target_compile_definitions(pico_fota_bootloader_lib PRIVATE PFB_WITH_IMAGE_ENCRYPTION)
target_compile_definitions(pico_fota_bootloader_lib PRIVATE PFB_AES_KEY=\"${PFB_AES_KEY}\")
endif ()
if (PFB_WITH_SHA256_HASHING)
target_compile_definitions(pico_fota_bootloader_lib PRIVATE PFB_WITH_SHA256_HASHING)
endif ()
set(BOOTLOADER_DIR_GLOBAL ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
########################################
# Manage application binary
########################################
function(pfb_compile_with_bootloader Target)
target_link_options(${Target} PRIVATE "-L${BOOTLOADER_DIR_GLOBAL}/linker_common")
pico_set_linker_script(${Target} ${BOOTLOADER_DIR_GLOBAL}/linker_common/application.ld)
if (PFB_WITH_SHA256_HASHING OR PFB_WITH_IMAGE_ENCRYPTION)
find_package(Python COMPONENTS Interpreter REQUIRED)
if (NOT Python_Interpreter_FOUND)
message(FATAL_ERROR
"Python interpreter not found and is required for SHA256 appending and AES image encryption")
endif ()
endif ()
add_custom_command(
TARGET ${Target}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${Target}>
$<TARGET_PROPERTY:${Target},NAME>_fota_image.bin)
if (PFB_WITH_SHA256_HASHING)
add_custom_command(
TARGET ${Target}
POST_BUILD
COMMAND ${Python_EXECUTABLE} ${BOOTLOADER_DIR_GLOBAL}/scripts/sha256_append.py
--target-file "$<TARGET_PROPERTY:${Target},NAME>_fota_image.bin"
COMMENT "Appending encrypted FOTA file with SHA256...")
endif ()
if (PFB_WITH_IMAGE_ENCRYPTION)
add_custom_command(
TARGET ${Target}
POST_BUILD
COMMAND ${Python_EXECUTABLE} ${BOOTLOADER_DIR_GLOBAL}/scripts/aes_encrypt.py
--target-file "$<TARGET_PROPERTY:${Target},NAME>_fota_image.bin"
--aes-key ${PFB_AES_KEY_GLOBAL}
COMMENT "Encrypting FOTA image using AES...")
endif ()
endfunction()
################################################################################
# Create bootloader binary
################################################################################
add_executable(pico_fota_bootloader
bootloader.c)
target_link_libraries(pico_fota_bootloader
hardware_structs
hardware_resets
pico_fota_bootloader_lib
cmsis_core)
function (target_compile_link_options Name Option)
target_compile_options(${Name} PRIVATE ${Option})
target_link_options(${Name} PRIVATE ${Option})
endfunction ()
target_compile_link_options(pico_fota_bootloader "-Os")
target_compile_link_options(pico_fota_bootloader "-ffunction-sections")
target_compile_link_options(pico_fota_bootloader "-fdata-sections")
target_compile_link_options(pico_fota_bootloader "-L${CMAKE_CURRENT_SOURCE_DIR}/linker_common")
target_link_options(pico_fota_bootloader PRIVATE "LINKER:--gc-sections")
pico_set_linker_script(pico_fota_bootloader ${CMAKE_CURRENT_SOURCE_DIR}/linker_common/bootloader.ld)
pico_add_extra_outputs(pico_fota_bootloader)
########################################
# Manage bootloader's logs
########################################
if (PFB_WITH_BOOTLOADER_LOGS)
target_compile_definitions(pico_fota_bootloader PRIVATE PFB_WITH_BOOTLOADER_LOGS)
if (PFB_REDIRECT_BOOTLOADER_LOGS_TO_UART)
pico_enable_stdio_usb(pico_fota_bootloader 0)
pico_enable_stdio_uart(pico_fota_bootloader 1)
else ()
pico_enable_stdio_usb(pico_fota_bootloader 1)
pico_enable_stdio_uart(pico_fota_bootloader 0)
endif ()
endif ()