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()

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

# The whole HAL; the linker drops what the bootloader does not use
file(GLOB HAL_SOURCES ${THIRD_PARTY}/st/drivers/stm32u3xx_hal_driver/src/*.c)
list(FILTER HAL_SOURCES EXCLUDE REGEX "(_template|_hal_hcd)\\.c$")

add_executable(bootloader)

target_sources(bootloader PRIVATE
        ${COMMON}/cpu_endian.c
        ${COMMON}/os_port_none.c
        ${COMMON}/date_time.c
        ${BOOT}/core/crc32.c
        ${BOOT}/core/mailbox.c
        ${BOOT}/drivers/mcu_core/stm32u3xx_mcu_driver.c
        ${BOOT}/drivers/flash/internal/stm32u3xx_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_fallback.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/stm32u3xx_nucleo/stm32u3xx_nucleo.c

        ${HAL_SOURCES}

        ${CMAKE_SOURCE_DIR}/cmake/startup_stm32u385xx.s
        ${CMAKE_SOURCE_DIR}/cmake/syscalls.c
        ${CMAKE_SOURCE_DIR}/src/system_stm32u3xx.c
        ${CMAKE_SOURCE_DIR}/src/stm32u3xx_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/stm32u3xx
        ${THIRD_PARTY}/st/drivers/stm32u3xx_hal_driver/inc
        ${THIRD_PARTY}/st/boards/stm32u3xx_nucleo
        ${COMMON}
        ${CRYPTO}
        ${BOOT}
        ${BOOT}/modules
        ${BOOT}/bootloader
)

target_compile_definitions(bootloader PRIVATE
    STM32U385xx
    USE_HAL_DRIVER
    __USE_C99_MATH

    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/stm32u385_bl_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 ()
