From a959011faa6cd49c7c2db430902f224a9a24094a Mon Sep 17 00:00:00 2001 From: Jacob Alexander Date: Mon, 2 Mar 2015 01:58:53 -0800 Subject: [PATCH] Adding dfu-suffix signing support to build system - If dfu-suffix is not found, a warning is given and the binary is not signed - Unsigned binaries are still ok with the latest version of dfu-util --- Lib/CMake/FindDFUSuffix.cmake | 40 +++++++++++++++++++++++++++++++++++ Lib/CMake/build.cmake | 23 +++++++++++++++----- 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 Lib/CMake/FindDFUSuffix.cmake diff --git a/Lib/CMake/FindDFUSuffix.cmake b/Lib/CMake/FindDFUSuffix.cmake new file mode 100644 index 0000000..9aaa8f2 --- /dev/null +++ b/Lib/CMake/FindDFUSuffix.cmake @@ -0,0 +1,40 @@ +# The module defines the following variables: +# DFU_SUFFIX_EXECUTABLE - path to ctags command line client +# DFU_SUFFIX_FOUND - true if the command line client was found +# DFU_SUFFIX_VERSION_STRING - the version of dfu-suffix found (since CMake 2.8.8) +# Example usage: +# find_package( DFUSuffix ) +# if( DFU_SUFFIX_FOUND ) +# message("ctags found: ${DFU_SUFFIX_EXECUTABLE}") +# endif() + +find_program ( DFU_SUFFIX_EXECUTABLE + NAMES dfu-suffix + DOC "dfu-suffix executable" +) +mark_as_advanced ( DFU_SUFFIX_EXECUTABLE ) + +if ( DFU_SUFFIX_EXECUTABLE ) + execute_process ( COMMAND ${DFU_SUFFIX_EXECUTABLE} --version + OUTPUT_VARIABLE dfu_suffix_version + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + if ( dfu_suffix_version MATCHES "^dfu-suffix \\(dfu-util\\)" ) + string ( REPLACE "\n" "" DFU_SUFFIX_VERSION_STRING ${dfu_suffix_version} ) + string ( REPLACE "dfu-suffix (dfu-util) " "" DFU_SUFFIX_VERSION_STRING ${DFU_SUFFIX_VERSION_STRING} ) + string ( REGEX REPLACE "Copyright .*$" "" DFU_SUFFIX_VERSION_STRING ${DFU_SUFFIX_VERSION_STRING} ) + endif () + unset ( dfu_suffix_version ) +endif () + +# Handle the QUIETLY and REQUIRED arguments and set DFU_SUFFIX_FOUND to TRUE if +# all listed variables are TRUE + +include ( FindPackageHandleStandardArgs ) +find_package_handle_standard_args ( DFU_SUFFIX + REQUIRED_VARS DFU_SUFFIX_EXECUTABLE + VERSION_VAR DFU_SUFFIX_VERSION_STRING +) + diff --git a/Lib/CMake/build.cmake b/Lib/CMake/build.cmake index 676e557..b3209bf 100644 --- a/Lib/CMake/build.cmake +++ b/Lib/CMake/build.cmake @@ -1,6 +1,6 @@ ###| CMAKE Kiibohd Controller Source Configurator |### # -# Written by Jacob Alexander in 2011-2014 for the Kiibohd Controller +# Written by Jacob Alexander in 2011-2015 for the Kiibohd Controller # # Released into the Public Domain # @@ -46,12 +46,25 @@ endif () #| Convert the .ELF into a .bin to load onto the McHCK +#| Then sign using dfu-suffix (requries dfu-util) if ( DEFINED DFU ) + # dfu-suffix is required to sign the dfu binary + find_package ( DFUSuffix ) + set( TARGET_BIN ${TARGET}.dfu.bin ) - add_custom_command( TARGET ${TARGET_ELF} POST_BUILD - COMMAND ${OBJ_COPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN} - COMMENT "Creating dfu binary file: ${TARGET_BIN}" - ) + if ( DFU_SUFFIX_FOUND ) + add_custom_command( TARGET ${TARGET_ELF} POST_BUILD + COMMAND ${OBJ_COPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN} + COMMAND ${DFU_SUFFIX_EXECUTABLE} --add ${TARGET_BIN} --vid ${BOOT_VENDOR_ID} --pid ${BOOT_PRODUCT_ID} 1> /dev/null + COMMENT "Create and sign dfu bin file: ${TARGET_BIN}" + ) + else () + message ( WARNING "DFU Binary has not been signed, requires dfu-suffix..." ) + add_custom_command( TARGET ${TARGET_ELF} POST_BUILD + COMMAND ${OBJ_COPY} ${BIN_FLAGS} ${TARGET_ELF} ${TARGET_BIN} + COMMENT "Creating dfu binary file: ${TARGET_BIN}" + ) + endif () endif ()