1# Picotool Property Defines 2# All INHERITED, so can be defined globally, or per target 3# 4# The picotool functions all set the specified target properties, 5# and therefore if the property should be set for multiple targets 6# then it can be set manually with `set_property` or other CMake 7# functions to set properties for a given scope. 8define_property(TARGET 9 PROPERTY PICOTOOL_OTP_FILE 10 INHERITED 11 BRIEF_DOCS "OTP File to write" 12 FULL_DOCS "OTP File to write" 13) 14define_property(TARGET 15 PROPERTY PICOTOOL_SIGN_OUTPUT 16 INHERITED 17 BRIEF_DOCS "Sign output" 18 FULL_DOCS "Sign output" 19) 20define_property(TARGET 21 PROPERTY PICOTOOL_SIGFILE 22 INHERITED 23 BRIEF_DOCS "Private key for signing" 24 FULL_DOCS "Private key for signing" 25) 26define_property(TARGET 27 PROPERTY PICOTOOL_HASH_OUTPUT 28 INHERITED 29 BRIEF_DOCS "Hash output" 30 FULL_DOCS "Hash output" 31) 32define_property(TARGET 33 PROPERTY PICOTOOL_EMBED_PT 34 INHERITED 35 BRIEF_DOCS "Partition table to embed in output" 36 FULL_DOCS "Partition table to embed in output" 37) 38define_property(TARGET 39 PROPERTY PICOTOOL_AESFILE 40 INHERITED 41 BRIEF_DOCS "AES key for encrypting" 42 FULL_DOCS "AES key for encrypting" 43) 44define_property(TARGET 45 PROPERTY PICOTOOL_ENC_SIGFILE 46 INHERITED 47 BRIEF_DOCS "Private key for signing encrypted binaries" 48 FULL_DOCS "Private key for signing encrypted binaries" 49) 50define_property(TARGET 51 PROPERTY PICOTOOL_UF2_PACKAGE_ADDR 52 INHERITED 53 BRIEF_DOCS "Address to package UF2 at" 54 FULL_DOCS "Address to package UF2 at" 55) 56define_property(TARGET 57 PROPERTY PICOTOOL_UF2_FAMILY 58 INHERITED 59 BRIEF_DOCS "UF2 family to use" 60 FULL_DOCS "UF2 family to use" 61) 62define_property(TARGET 63 PROPERTY PICOTOOL_EXTRA_PROCESS_ARGS 64 INHERITED 65 BRIEF_DOCS "Extra arguments to pass to processing" 66 FULL_DOCS "Extra arguments to pass to processing" 67) 68define_property(TARGET 69 PROPERTY PICOTOOL_EXTRA_UF2_ARGS 70 INHERITED 71 BRIEF_DOCS "Extra arguments to pass to uf2 conversion" 72 FULL_DOCS "Extra arguments to pass to uf2 conversion" 73) 74 75# Check pioasm is installed, or build it if not installed 76function(pico_init_pioasm) 77 # Assemble the version string from components instead of using PICO_SDK_VERSION_STRING, because the version string 78 # potentially has a PRE_RELEASE_ID suffix, which will trip up the find_package call. 79 set(pioasm_VERSION_REQUIRED "${PICO_SDK_VERSION_MAJOR}.${PICO_SDK_VERSION_MINOR}.${PICO_SDK_VERSION_REVISION}") 80 if (NOT TARGET pioasm AND NOT DEFINED pioasm_FOUND) 81 set(pioasm_INSTALL_DIR ${CMAKE_BINARY_DIR}/pioasm-install) 82 if (NOT pioasm_DIR AND EXISTS ${pioasm_INSTALL_DIR}/pioasm) 83 set(pioasm_DIR ${pioasm_INSTALL_DIR}/pioasm) 84 endif() 85 # Find package - will find installed pioasm, either at pioasm_DIR or system 86 find_package(pioasm ${pioasm_VERSION_REQUIRED} QUIET CONFIG NO_CMAKE_FIND_ROOT_PATH) 87 88 if (NOT pioasm_FOUND) 89 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools) 90 # todo CMAKE_CURRENT_FUNCTION_LIST_DIR ... what version? 91 find_package(pioasm MODULE REQUIRED) 92 endif() 93 endif() 94 95 if (TARGET pioasm) 96 set(pioasm_FOUND 1 PARENT_SCOPE) 97 else() 98 message("No pioasm found") 99 endif() 100endfunction() 101 102# Check picotool is installed, or download and build it if not installed 103function(pico_init_picotool) 104 set(picotool_VERSION_REQUIRED 2.0.0) 105 if (NOT TARGET picotool AND NOT DEFINED picotool_FOUND) 106 # Build path of local install dir 107 if (DEFINED ENV{PICOTOOL_FETCH_FROM_GIT_PATH} AND (NOT PICOTOOL_FETCH_FROM_GIT_PATH)) 108 set(PICOTOOL_FETCH_FROM_GIT_PATH $ENV{PICOTOOL_FETCH_FROM_GIT_PATH}) 109 message("Using PICOTOOL_FETCH_FROM_GIT_PATH from environment ('${PICOTOOL_FETCH_FROM_GIT_PATH}')") 110 endif () 111 include(FetchContent) 112 if (PICOTOOL_FETCH_FROM_GIT_PATH) 113 get_filename_component(picotool_INSTALL_DIR "${PICOTOOL_FETCH_FROM_GIT_PATH}" ABSOLUTE) 114 else() 115 set(picotool_INSTALL_DIR ${FETCHCONTENT_BASE_DIR}) 116 endif () 117 118 if (NOT PICOTOOL_FORCE_FETCH_FROM_GIT AND NOT ENV{PICOTOOL_FORCE_FETCH_FROM_GIT}) 119 if (NOT picotool_DIR AND EXISTS ${picotool_INSTALL_DIR}/picotool) 120 set(picotool_DIR ${picotool_INSTALL_DIR}/picotool) 121 endif() 122 # Find package - will find installed picotool, either at picotool_DIR or system 123 find_package(picotool ${picotool_VERSION_REQUIRED} QUIET CONFIG NO_CMAKE_FIND_ROOT_PATH) 124 if (NOT picotool_FOUND AND picotool_CONSIDERED_VERSIONS) 125 message(FATAL_ERROR 126 "Incompatible picotool installation found: " 127 "Requires version ${picotool_VERSION_REQUIRED}, " 128 "you have version ${picotool_CONSIDERED_VERSIONS}\n" 129 "Update your installation, or set PICOTOOL_FORCE_FETCH_FROM_GIT " 130 "to download and build the correct version" 131 ) 132 endif() 133 else() 134 message("Forcing fetch of picotool from git") 135 endif() 136 if (NOT picotool_FOUND) 137 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools) 138 find_package(picotool MODULE) 139 endif() 140 endif() 141 142 if (TARGET picotool) 143 set(picotool_FOUND 1 PARENT_SCOPE) 144 else() 145 message("No picotool found") 146 endif() 147endfunction() 148 149# Generate pio header and include it in the build 150# PICO_CMAKE_CONFIG: PICO_DEFAULT_PIOASM_OUTPUT_FORMAT, Default output format used by pioasm when using pico_generate_pio_header, type=string, default=c-sdk, group=build 151function(pico_generate_pio_header TARGET PIO) 152 pico_init_pioasm() 153 cmake_parse_arguments(pico_generate_pio_header "" "OUTPUT_FORMAT;OUTPUT_DIR" "" ${ARGN} ) 154 155 if (pico_generate_pio_header_OUTPUT_FORMAT) 156 set(OUTPUT_FORMAT "${pico_generate_pio_header_OUTPUT_FORMAT}") 157 elseif(DEFINED PICO_DEFAULT_PIOASM_OUTPUT_FORMAT) 158 set(OUTPUT_FORMAT "${PICO_DEFAULT_PIOASM_OUTPUT_FORMAT}") 159 else() 160 set(OUTPUT_FORMAT "c-sdk") 161 endif() 162 163 if (pico_generate_pio_header_OUTPUT_DIR) 164 file(MAKE_DIRECTORY ${pico_generate_pio_header_OUTPUT_DIR}) 165 get_filename_component(HEADER_DIR ${pico_generate_pio_header_OUTPUT_DIR} ABSOLUTE) 166 else() 167 set(HEADER_DIR "${CMAKE_CURRENT_BINARY_DIR}") 168 endif() 169 get_filename_component(PIO_NAME ${PIO} NAME) 170 set(HEADER "${HEADER_DIR}/${PIO_NAME}.h") 171 #message("Will generate ${HEADER}") 172 get_filename_component(HEADER_GEN_TARGET ${PIO} NAME_WE) 173 set(HEADER_GEN_TARGET "${TARGET}_${HEADER_GEN_TARGET}_pio_h") 174 175 add_custom_target(${HEADER_GEN_TARGET} DEPENDS ${HEADER}) 176 177 if (PICO_PIO_VERSION) 178 set(VERSION_STRING "${PICO_PIO_VERSION}") 179 else() 180 set(VERSION_STRING "0") 181 endif() 182 add_custom_command(OUTPUT ${HEADER} 183 DEPENDS ${PIO} 184 COMMAND pioasm -o ${OUTPUT_FORMAT} -v ${VERSION_STRING} ${PIO} ${HEADER} 185 VERBATIM) 186 add_dependencies(${TARGET} ${HEADER_GEN_TARGET}) 187 get_target_property(target_type ${TARGET} TYPE) 188 if ("INTERFACE_LIBRARY" STREQUAL "${target_type}") 189 target_include_directories(${TARGET} INTERFACE ${HEADER_DIR}) 190 else() 191 target_include_directories(${TARGET} PUBLIC ${HEADER_DIR}) 192 endif() 193endfunction() 194 195# pico_package_uf2_output(TARGET PACKADDR) 196# Package a UF2 output to be written to the PACKADDR address. This can be 197# used with a no_flash binary to write the UF2 to flash when dragging & 198# dropping, and it will be copied to SRAM by the bootrom before execution. 199# This sets PICOTOOL_UF2_PACKAGE_ADDR to PACKADDR. 200function(pico_package_uf2_output TARGET PACKADDR) 201 set_target_properties(${TARGET} PROPERTIES 202 PICOTOOL_UF2_PACKAGE_ADDR ${PACKADDR} 203 ) 204endfunction() 205 206# pico_set_otp_key_output_file(TARGET OTPFILE) 207# Output the public key hash and other necessary rows to an otp JSON file. 208# This sets PICOTOOL_OTP_FILE to OTPFILE. 209function(pico_set_otp_key_output_file TARGET OTPFILE) 210 set_target_properties(${TARGET} PROPERTIES 211 PICOTOOL_OTP_FILE ${OTPFILE} 212 ) 213endfunction() 214 215# pico_load_map_clear_sram(TARGET) 216# Adds an entry to the load map to instruct the bootrom to clear all of SRAM 217# before loading the binary. This appends the `--clear` argument 218# to PICOTOOL_EXTRA_PROCESS_ARGS. 219function(pico_load_map_clear_sram TARGET) 220 # get and set, to inherit list 221 get_target_property(extra_args ${TARGET} PICOTOOL_EXTRA_PROCESS_ARGS) 222 if (extra_args) 223 set_target_properties(${TARGET} PROPERTIES PICOTOOL_EXTRA_PROCESS_ARGS ${extra_args}) 224 endif() 225 # append --clear to list 226 set_property(TARGET ${TARGET} APPEND PROPERTY 227 PICOTOOL_EXTRA_PROCESS_ARGS "--clear" 228 ) 229endfunction() 230 231# pico_set_binary_version(<TARGET> [MAJOR <version>] [MINOR <version>] [ROLLBACK <version>] [ROLLBACK_ROWS <rows...>]) 232# Adds a version item to the metadata block, with the given major, minor and 233# rollback version, along with the rollback rows. These are appended as arguments 234# to PICOTOOL_EXTRA_PROCESS_ARGS if setting the rollback version, or set as compile 235# definitions if only setting the major/minor versions. 236function(pico_set_binary_version TARGET) 237 set(oneValueArgs MAJOR MINOR ROLLBACK) 238 set(multiValueArgs ROWS) 239 cmake_parse_arguments(PARSE_ARGV 1 SV "" "${oneValueArgs}" "${multiValueArgs}") 240 # get and set, to inherit list 241 get_target_property(extra_args ${TARGET} PICOTOOL_EXTRA_PROCESS_ARGS) 242 if (extra_args) 243 set_target_properties(${TARGET} PROPERTIES PICOTOOL_EXTRA_PROCESS_ARGS ${extra_args}) 244 endif() 245 if (SV_ROLLBACK) 246 if (SV_MAJOR) 247 # append major version 248 set_property(TARGET ${TARGET} APPEND PROPERTY 249 PICOTOOL_EXTRA_PROCESS_ARGS "--major" "${SV_MAJOR}" 250 ) 251 endif() 252 if (SV_MINOR) 253 # append minor version 254 set_property(TARGET ${TARGET} APPEND PROPERTY 255 PICOTOOL_EXTRA_PROCESS_ARGS "--minor" "${SV_MINOR}" 256 ) 257 endif() 258 # append rollback version 259 set_property(TARGET ${TARGET} APPEND PROPERTY 260 PICOTOOL_EXTRA_PROCESS_ARGS "--rollback" "${SV_ROLLBACK}" 261 ) 262 if (SV_ROWS) 263 # append rollback rows 264 foreach(row IN LISTS SV_ROWS) 265 set_property(TARGET ${TARGET} APPEND PROPERTY 266 PICOTOOL_EXTRA_PROCESS_ARGS "${row}" 267 ) 268 endforeach() 269 endif() 270 else() 271 if (SV_MAJOR) 272 # set major version 273 target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_VERSION_MAJOR=${SV_MAJOR}) 274 endif() 275 if (SV_MINOR) 276 # append minor version 277 target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_VERSION_MINOR=${SV_MINOR}) 278 endif() 279 endif() 280endfunction() 281 282# pico_set_uf2_family(TARGET FAMILY) 283# Set the UF2 family to use when creating the UF2. 284# This sets PICOTOOL_UF2_FAMILY to FAMILY. 285function(pico_set_uf2_family TARGET FAMILY) 286 set_target_properties(${TARGET} PROPERTIES 287 PICOTOOL_UF2_FAMILY ${FAMILY} 288 ) 289endfunction() 290 291# pico_sign_binary(TARGET [SIGFILE]) 292# Sign the target binary with the given PEM signature. This sets 293# PICOTOOL_SIGN_OUTPUT to true, PICOTOOL_SIGFILE to SIGFILE (if specified), 294# and PICOTOOL_OTP_FILE to ${TARGET}.otp.json (if not already set). To 295# specify a common SIGFILE for multiple targets, the SIGFILE property can be 296# set for a given scope, and then the SIGFILE argument is optional. 297function(pico_sign_binary TARGET) 298 # Enforce signing through target properties 299 set_target_properties(${TARGET} PROPERTIES 300 PICOTOOL_SIGN_OUTPUT true 301 ) 302 if (ARGC EQUAL 2) 303 set_target_properties(${TARGET} PROPERTIES 304 PICOTOOL_SIGFILE ${ARGV1} 305 ) 306 else() 307 get_target_property(sig_file ${TARGET} PICOTOOL_SIGFILE) 308 if (NOT sig_file) 309 message(FATAL_ERROR "Signature file not set for ${TARGET}") 310 endif() 311 endif() 312 get_target_property(otp_file ${TARGET} PICOTOOL_OTP_FILE) 313 if (NOT otp_file) 314 set_target_properties(${TARGET} PROPERTIES 315 PICOTOOL_OTP_FILE "${TARGET}.otp.json" 316 ) 317 endif() 318endfunction() 319 320# pico_hash_binary(TARGET) 321# Hash the target binary. This sets PICOTOOL_HASH_OUTPUT to true. 322function(pico_hash_binary TARGET) 323 # Enforce hashing through target properties 324 set_target_properties(${TARGET} PROPERTIES 325 PICOTOOL_HASH_OUTPUT true 326 ) 327endfunction() 328 329# pico_embed_pt_in_binary(TARGET PTFILE) 330# Create the specified partition table from JSON, and embed it in the 331# block loop. This sets PICOTOOL_EMBED_PT to PTFILE. 332function(pico_embed_pt_in_binary TARGET PTFILE) 333 set_target_properties(${TARGET} PROPERTIES 334 PICOTOOL_EMBED_PT ${PTFILE} 335 ) 336endfunction() 337 338# pico_encrypt_binary(TARGET AESFILE [SIGFILE]) 339# Encrypt the target binary with the given AES key (should be a binary 340# file containing 32 bytes of a random key), and sign the encrypted binary. 341# This sets PICOTOOL_AESFILE to AESFILE, and PICOTOOL_ENC_SIGFILE to SIGFILE 342# if present, else PICOTOOL_SIGFILE. 343function(pico_encrypt_binary TARGET AESFILE) 344 set_target_properties(${TARGET} PROPERTIES 345 PICOTOOL_AESFILE ${AESFILE} 346 ) 347 if (ARGC EQUAL 3) 348 set_target_properties(${TARGET} PROPERTIES 349 PICOTOOL_ENC_SIGFILE ${ARGV2} 350 ) 351 else() 352 get_target_property(enc_sig_file ${TARGET} PICOTOOL_ENC_SIGFILE) 353 if (NOT enc_sig_file) 354 get_target_property(sig_file ${TARGET} PICOTOOL_SIGFILE) 355 if (NOT sig_file) 356 message(FATAL_ERROR "Signature file not set for ${TARGET}") 357 else() 358 set_target_properties(${TARGET} PROPERTIES 359 PICOTOOL_ENC_SIGFILE ${sig_file} 360 ) 361 endif() 362 endif() 363 endif() 364endfunction() 365 366# pico_add_uf2_output(TARGET) 367# Add a UF2 output using picotool - must be called after 368# all required properties have been set 369function(pico_add_uf2_output TARGET) 370 pico_init_picotool() 371 get_target_property(${TARGET}_archive_directory ${TARGET} ARCHIVE_OUTPUT_DIRECTORY) 372 if (${TARGET}_archive_directory) 373 get_filename_component(output_path "${${TARGET}_archive_directory}" 374 REALPATH BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}") 375 file(MAKE_DIRECTORY "${output_path}") 376 set(output_path "${output_path}/") 377 else() 378 set(output_path "") 379 endif() 380 381 get_target_property(${TARGET}_uf2_package_addr ${TARGET} PICOTOOL_UF2_PACKAGE_ADDR) 382 if (${TARGET}_uf2_package_addr) 383 set(uf2_package_args "-o;${${TARGET}_uf2_package_addr}") 384 endif() 385 386 get_target_property(extra_uf2_args ${TARGET} PICOTOOL_EXTRA_UF2_ARGS) 387 if (1) # TODO: A2 only (Errata RP2350-E9) 388 if (NOT extra_uf2_args) 389 set(extra_uf2_args "--abs-block") 390 elseif(NOT "--abs-block" IN_LIST extra_uf2_args) 391 list(APPEND extra_uf2_args "--abs-block") 392 endif() 393 else() 394 if (NOT extra_uf2_args) 395 set(extra_uf2_args "") 396 endif() 397 endif() 398 399 if (picotool_FOUND) 400 get_target_property(picotool_family ${TARGET} PICOTOOL_UF2_FAMILY) 401 if (NOT picotool_family) 402 if (PICOTOOL_DEFAULT_FAMILY) 403 set(picotool_family ${PICOTOOL_DEFAULT_FAMILY}) 404 else() 405 set(picotool_family ${PICO_PLATFORM}) 406 endif() 407 endif() 408 add_custom_command(TARGET ${TARGET} POST_BUILD 409 COMMAND picotool 410 ARGS uf2 convert 411 --quiet 412 ${uf2_package_args} 413 $<TARGET_FILE:${TARGET}> 414 ${output_path}$<IF:$<BOOL:$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>>,$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>,$<TARGET_PROPERTY:${TARGET},NAME>>.uf2 415 --family ${picotool_family} 416 ${extra_uf2_args} 417 COMMAND_EXPAND_LISTS 418 VERBATIM) 419 endif() 420endfunction() 421 422# Run picotool post-processing on the binary - must be called after 423# all required properties have been set 424function(picotool_postprocess_binary TARGET) 425 # Read target properties 426 get_target_property(picotool_sign_output ${TARGET} PICOTOOL_SIGN_OUTPUT) 427 if (picotool_sign_output) 428 list(APPEND picotool_args "--sign") 429 get_target_property(picotool_sigfile ${TARGET} PICOTOOL_SIGFILE) 430 endif() 431 432 get_target_property(picotool_hash_output ${TARGET} PICOTOOL_HASH_OUTPUT) 433 if (picotool_hash_output) 434 list(APPEND picotool_args "--hash") 435 endif() 436 437 get_target_property(otp_file ${TARGET} PICOTOOL_OTP_FILE) 438 if (NOT otp_file) 439 set(otp_file "") 440 endif() 441 get_target_property(uf2_package_addr ${TARGET} PICOTOOL_UF2_PACKAGE_ADDR) 442 443 # Embed PT properties 444 get_target_property(picotool_embed_pt ${TARGET} PICOTOOL_EMBED_PT) 445 446 # Encryption properties 447 get_target_property(picotool_aesfile ${TARGET} PICOTOOL_AESFILE) 448 get_target_property(picotool_enc_sigfile ${TARGET} PICOTOOL_ENC_SIGFILE) 449 450 # Extra args 451 get_target_property(extra_process_args ${TARGET} PICOTOOL_EXTRA_PROCESS_ARGS) 452 if (extra_process_args) 453 list(APPEND picotool_args ${extra_process_args}) 454 endif() 455 456 pico_init_picotool() 457 if (picotool_FOUND) 458 # Embed PT 459 if (picotool_embed_pt) 460 add_custom_command(TARGET ${TARGET} POST_BUILD 461 DEPENDS ${picotool_embed_pt} 462 COMMAND picotool partition create --quiet ${picotool_embed_pt} $<TARGET_FILE:${TARGET}> $<TARGET_FILE:${TARGET}> 463 VERBATIM) 464 endif() 465 # Signing/hashing/load maps for packaging 466 if ( 467 picotool_sign_output OR 468 picotool_hash_output OR 469 uf2_package_addr OR 470 extra_process_args 471 ) 472 add_custom_command(TARGET ${TARGET} POST_BUILD 473 DEPENDS ${picotool_sigfile} 474 COMMAND picotool 475 ARGS seal 476 --quiet 477 $<TARGET_FILE:${TARGET}> $<TARGET_FILE:${TARGET}> 478 ${picotool_sigfile} ${otp_file} 479 ${picotool_args} 480 COMMAND_EXPAND_LISTS 481 VERBATIM) 482 endif() 483 # Encryption 484 if (picotool_aesfile) 485 add_custom_command(TARGET ${TARGET} POST_BUILD 486 DEPENDS ${picotool_enc_sigfile} ${picotool_aesfile} 487 COMMAND picotool encrypt --quiet --hash --sign $<TARGET_FILE:${TARGET}> $<TARGET_FILE:${TARGET}> ${picotool_aesfile} ${picotool_enc_sigfile} 488 VERBATIM) 489 if (ARGC EQUAL 2) 490 set(${ARGV1} TRUE PARENT_SCOPE) 491 endif() 492 endif() 493 endif() 494endfunction() 495