|
| 1 | +if(CONFIG_BOOTLOADER_MCUBOOT) |
| 2 | + # Build a second bootloader image |
| 3 | + |
| 4 | + set(MCUBOOT_BASE ${CMAKE_CURRENT_LIST_DIR}/..) |
| 5 | + |
| 6 | + zephyr_add_executable(mcuboot require_build) |
| 7 | + |
| 8 | + if (${require_build}) |
| 9 | + add_subdirectory(${MCUBOOT_BASE}/boot/zephyr ${CMAKE_BINARY_DIR}/mcuboot) |
| 10 | + endif() # ${require_build} |
| 11 | + |
| 12 | + set(to_sign_hex ${KERNEL_HEX_NAME}) |
| 13 | + |
| 14 | + # TODO: Assert that the bootloader and image use the same key. |
| 15 | + |
| 16 | + set(signed_image_hex ${PROJECT_BINARY_DIR}/signed.hex) |
| 17 | + set(signed_image_bin ${PROJECT_BINARY_DIR}/signed.bin) |
| 18 | + set(to_sign_bin ${PROJECT_BINARY_DIR}/to_sign.bin) |
| 19 | + set(update_hex ${PROJECT_BINARY_DIR}/update.hex) |
| 20 | + set(update_bin ${PROJECT_BINARY_DIR}/update.bin) |
| 21 | + |
| 22 | + get_property(app_binary_dir GLOBAL PROPERTY PROJECT_BINARY_DIR) |
| 23 | + set(merged_hex_file |
| 24 | + ${app_binary_dir}/mcuboot_primary_app.hex) |
| 25 | + set(merged_hex_file_depends |
| 26 | + mcuboot_primary_app_hex$<SEMICOLON>${PROJECT_BINARY_DIR}/mcuboot_primary_app.hex) |
| 27 | + set(sign_merged |
| 28 | + $<TARGET_EXISTS:partition_manager>) |
| 29 | + set(to_sign_hex |
| 30 | + $<IF:${sign_merged},${merged_hex_file},${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}>) |
| 31 | + set(sign_depends |
| 32 | + $<IF:${sign_merged},${merged_hex_file_depends},zephyr_final>) |
| 33 | + set(sign_cmd |
| 34 | + ${PYTHON_EXECUTABLE} |
| 35 | + ${MCUBOOT_BASE}/scripts/imgtool.py |
| 36 | + sign |
| 37 | + --key ${MCUBOOT_BASE}/${CONFIG_BOOT_SIGNATURE_KEY_FILE} |
| 38 | + --header-size $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PAD_SIZE> |
| 39 | + --align ${DT_FLASH_WRITE_BLOCK_SIZE} |
| 40 | + --version ${CONFIG_MCUBOOT_IMAGE_VERSION} |
| 41 | + --slot-size $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PRIMARY_SIZE> |
| 42 | + --pad-header |
| 43 | + ) |
| 44 | + |
| 45 | + add_custom_command( |
| 46 | + OUTPUT |
| 47 | + ${signed_image_hex} |
| 48 | + ${update_hex} |
| 49 | + ${update_bin} |
| 50 | + COMMAND |
| 51 | + ${sign_cmd} |
| 52 | + ${to_sign_hex} |
| 53 | + ${signed_image_hex} |
| 54 | + COMMAND |
| 55 | + ${CMAKE_OBJCOPY} |
| 56 | + --input-target=ihex |
| 57 | + --output-target=binary |
| 58 | + ${to_sign_hex} |
| 59 | + ${to_sign_bin} |
| 60 | + COMMAND |
| 61 | + ${sign_cmd} |
| 62 | + ${to_sign_bin} |
| 63 | + ${update_bin} |
| 64 | + COMMAND |
| 65 | + ${sign_cmd} |
| 66 | + --pad # This argument is needed for MCUboot to apply the test swap. |
| 67 | + ${to_sign_hex} |
| 68 | + ${update_hex} |
| 69 | + COMMAND |
| 70 | + ${CMAKE_OBJCOPY} |
| 71 | + --input-target=ihex |
| 72 | + --output-target=ihex |
| 73 | + --change-address $<TARGET_PROPERTY:partition_manager,PM_MCUBOOT_PRIMARY_SIZE> |
| 74 | + ${update_hex} |
| 75 | + ${PROJECT_BINARY_DIR}/moved_update.hex |
| 76 | + DEPENDS |
| 77 | + ${sign_depends} |
| 78 | + ) |
| 79 | + add_custom_target(mcuboot_sign_target DEPENDS ${signed_image_hex}) |
| 80 | + |
| 81 | + set_property(GLOBAL PROPERTY |
| 82 | + mcuboot_primary_app_PM_HEX_FILE |
| 83 | + ${signed_image_hex} |
| 84 | + ) |
| 85 | + set_property(GLOBAL PROPERTY |
| 86 | + mcuboot_primary_app_PM_TARGET |
| 87 | + mcuboot_sign_target |
| 88 | + ) |
| 89 | +endif() |
0 commit comments