cmake_minimum_required(VERSION 3.22)
project(bootloader)

set(SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/)
set(COMMON ${SRC_ROOT}/common)
set(CRYPTO ${SRC_ROOT}/cyclone_crypto)
set(BOOT ${SRC_ROOT}/cyclone_boot)
set(THIRD_PARTY ${CMAKE_SOURCE_DIR}/../../../../../third_party)

# 2nd-stage bootloader version (published in the info block at flash base + 0x400)
set(BOOT_VERSION "1.0.0" CACHE STRING "2nd-stage bootloader firmware version")
string(REPLACE "." ";" BOOT_VERSION_PARTS ${BOOT_VERSION})
list(GET BOOT_VERSION_PARTS 0 BOOT_MAJOR_VERSION)
list(GET BOOT_VERSION_PARTS 1 BOOT_MINOR_VERSION)
list(GET BOOT_VERSION_PARTS 2 BOOT_REV_NUMBER)

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Include toolchain file
# include("gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
message("Build type: " ${CMAKE_BUILD_TYPE})

add_executable(bootloader)

target_sources(bootloader PRIVATE
        ${COMMON}/cpu_endian.c
        ${COMMON}/os_port_none.c
        ${COMMON}/date_time.c
        ${COMMON}/str.c

        ${BOOT}/core/crc32.c
        ${BOOT}/core/mailbox.c
        ${BOOT}/drivers/mcu_core/stm32f7xx_mcu_driver.c
        ${BOOT}/drivers/flash/internal/stm32f7xx_flash_driver.c
        ${BOOT}/drivers/flash/external/mx25l512_flash_driver.c
        ${BOOT}/core/verify.c
        ${BOOT}/modules/image/image.c
        ${BOOT}/modules/image/image_process.c
        ${BOOT}/modules/image/image_utils.c
        ${BOOT}/modules/memory/memory.c
        ${BOOT}/modules/security/cipher.c
        ${BOOT}/modules/security/verify_runtime.c
        ${BOOT}/modules/security/verify_integrity.c
        ${BOOT}/modules/security/verify_sign.c
        ${BOOT}/bootloader/second_stage/boot.c
        ${BOOT}/bootloader/second_stage/boot_common.c
        ${BOOT}/bootloader/second_stage/boot_verify.c

        ${CRYPTO}/hash/sha1.c
        ${CRYPTO}/hash/sha224.c
        ${CRYPTO}/hash/sha256.c
        ${CRYPTO}/hash/sha384.c
        ${CRYPTO}/hash/sha512.c
        ${CRYPTO}/cipher/aes.c
        ${CRYPTO}/cipher_modes/cbc.c
        ${CRYPTO}/encoding/oid.c
        ${CRYPTO}/encoding/asn1.c
        ${CRYPTO}/encoding/base64.c
        ${CRYPTO}/mpi/mpi.c
        ${CRYPTO}/mpi/mpi_misc.c
        ${CRYPTO}/ecc/ec_curves.c
        ${CRYPTO}/ecc/ecdsa.c
        ${CRYPTO}/ecc/ec.c
        ${CRYPTO}/ecc/ec_misc.c
        ${CRYPTO}/pkc/rsa.c
        ${CRYPTO}/pkc/rsa_misc.c
        ${CRYPTO}/pkix/pem_common.c
        ${CRYPTO}/pkix/pem_key_import.c
        ${CRYPTO}/pkix/pem_decrypt.c
        ${CRYPTO}/pkix/x509_common.c
        ${CRYPTO}/pkix/x509_key_parse.c
        ${CRYPTO}/pkix/pkcs8_key_parse.c

        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery.c
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery_eeprom.c
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery_lcd.c
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery_qspi.c
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery_sd.c
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery_sdram.c
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery/stm32f769i_discovery_ts.c
        ${THIRD_PARTY}/st/boards/components/ft6x06/ft6x06.c
        ${THIRD_PARTY}/st/boards/components/otm8009a/otm8009a.c
        ${THIRD_PARTY}/st/boards/components/wm8994/wm8994.c

        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_adc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_adc_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_can.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_cec.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_cortex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_crc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_cryp.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_cryp_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dac.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dac_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dcmi.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dcmi_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dfsdm.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dma.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dma_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dma2d.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_dsi.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_eth.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_flash.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_flash_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_gpio.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_hash.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_hash_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_hcd.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_i2c.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_i2c_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_i2s.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_irda.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_iwdg.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_lptim.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_ltdc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_ltdc_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_nand.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_nor.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_pcd.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_pcd_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_pwr.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_pwr_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_qspi.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_rcc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_rcc_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_rng.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_rtc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_rtc_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_sai.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_sai_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_sd.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_sdram.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_smartcard.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_spdifrx.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_spi.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_sram.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_tim.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_tim_ex.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_uart.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_usart.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_hal_wwdg.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_ll_fmc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_ll_sdmmc.c
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/src/stm32f7xx_ll_usb.c

        ${CMAKE_SOURCE_DIR}/cmake/startup_stm32f769xx.S
        ${CMAKE_SOURCE_DIR}/cmake/syscalls.c
        ${CMAKE_SOURCE_DIR}/src/system_stm32f7xx.c
        ${CMAKE_SOURCE_DIR}/src/stm32f7xx_it.c
        ${CMAKE_SOURCE_DIR}/src/main.c
        ${CMAKE_SOURCE_DIR}/src/board.c
        ${CMAKE_SOURCE_DIR}/src/debug.c
)

target_include_directories(bootloader PRIVATE
        ${CMAKE_SOURCE_DIR}/src
        ${THIRD_PARTY}/cmsis/include
        ${THIRD_PARTY}/st/devices/stm32f7xx
        ${THIRD_PARTY}/st/drivers/stm32f7xx_hal_driver/inc
        ${THIRD_PARTY}/st/boards/stm32f769i_discovery

        ${COMMON}
        ${BOOT}
        ${CRYPTO}
        ${BOOT}/modules
        ${BOOT}/bootloader
)

target_compile_definitions(bootloader PRIVATE
    STM32F769xx
    USE_HAL_DRIVER
    USE_STM32F769I_DISCO

    BOOT_VERSION_STRING="${BOOT_VERSION}"
    BOOT_MAJOR_VERSION=${BOOT_MAJOR_VERSION}
    BOOT_MINOR_VERSION=${BOOT_MINOR_VERSION}
    BOOT_REV_NUMBER=${BOOT_REV_NUMBER}
    $<$<CONFIG:Debug>:DEBUG>
)

# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(bootloader PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)

# Convert .elf to .bin
add_custom_command(TARGET bootloader POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O binary
        ${CMAKE_CURRENT_BINARY_DIR}/bootloader.elf
        ${CMAKE_CURRENT_BINARY_DIR}/bootloader.bin
        COMMENT "Generating binary file from ELF"
)

# Copy the generated .bin file to ../
add_custom_command(TARGET bootloader POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
            ${CMAKE_CURRENT_BINARY_DIR}/bootloader.bin
            ${CMAKE_CURRENT_SOURCE_DIR}/../
    COMMENT "Copying bootloader.bin to parent directory"
)

# Refresh the footprint report on a Release build. A Debug build leaves the committed
# numbers alone, so a report always describes the configuration customers ship.
if (CMAKE_BUILD_TYPE STREQUAL "Release")
	add_custom_command(TARGET bootloader POST_BUILD
		COMMAND python3 ${CMAKE_SOURCE_DIR}/footprint.py
		--map "${CMAKE_CURRENT_BINARY_DIR}/bootloader.map"
		--ld "${CMAKE_SOURCE_DIR}/cmake/stm32f769_flash.ld"
		--elf "${CMAKE_CURRENT_BINARY_DIR}/bootloader.elf"
		--output-summary "${CMAKE_SOURCE_DIR}/reports/footprint_summary.json"
		--build-type Release
		--folders ${SRC_ROOT}/cyclone_boot ${SRC_ROOT}/cyclone_crypto ${SRC_ROOT}/cyclone_tcp ${SRC_ROOT}/common
	)
endif ()
