cmake_minimum_required(VERSION 3.22)
project(bootloader C CXX ASM)

set(SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/)
set(COMMON ${SRC_ROOT}/common)
set(CRYPTO ${SRC_ROOT}/cyclone_crypto)
set(UPDATE ${SRC_ROOT}/cyclone_boot)
set(TCP ${SRC_ROOT}/cyclone_tcp)
set(THIRD_PARTY ${CMAKE_SOURCE_DIR}/../../../../../third_party)
set(HAL_DRIVERS ${THIRD_PARTY}/st/drivers/stm32f4xx_hal_driver/src/)
set(BSP ${THIRD_PARTY}/st/boards/stm32f4xx_nucleo_144/)
set(TFTP ${CMAKE_SOURCE_DIR}/../../../../common/update_protocols/tftp)

# Bootloader version shown in the trace
set(BOOT_VERSION "1.0.0" CACHE STRING "Standalone bootloader firmware version")

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(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

add_executable(bootloader
        ${COMMON}/cpu_endian.c
        ${COMMON}/os_port_none.c
        ${COMMON}/date_time.c
        ${COMMON}/str.c
        ${COMMON}/path.c
        ${UPDATE}/drivers/mcu_core/stm32f4xx_mcu_driver.c
        ${UPDATE}/drivers/flash/internal/stm32f4xx_flash_driver.c
        ${UPDATE}/core/crc32.c
        ${UPDATE}/core/mailbox.c
        ${UPDATE}/modules/image/image.c
        ${UPDATE}/modules/image/image_process.c
        ${UPDATE}/modules/image/image_utils.c
        ${UPDATE}/modules/memory/memory.c
        ${UPDATE}/modules/memory/memory_ex.c
        ${UPDATE}/core/verify.c
        ${UPDATE}/modules/security/verify_auth.c
        ${UPDATE}/modules/security/verify_sign.c
        ${UPDATE}/modules/security/verify_runtime.c
        ${UPDATE}/modules/security/verify_integrity.c
        ${UPDATE}/modules/security/cipher.c
        ${UPDATE}/update/update.c
        ${UPDATE}/update/update_misc.c
        ${UPDATE}/bootloader/standalone/boot.c
        ${UPDATE}/bootloader/standalone/boot_trigger.c
        ${UPDATE}/bootloader/standalone/boot_recovery.c
        ${UPDATE}/bootloader/
        ${UPDATE}/modules
        ${CRYPTO}/hardware/stm32f4xx/stm32f4xx_crypto.c
        ${CRYPTO}/hardware/stm32f4xx/stm32f4xx_crypto_trng.c
        ${CRYPTO}/hash/md5.c
        ${CRYPTO}/hash/sha1.c
        ${CRYPTO}/hash/sha224.c
        ${CRYPTO}/hash/sha256.c
        ${CRYPTO}/hash/sha384.c
        ${CRYPTO}/hash/sha512.c
        ${CRYPTO}/mac/hmac.c
        ${CRYPTO}/cipher/rc4.c
        ${CRYPTO}/cipher/idea.c
        ${CRYPTO}/cipher/des.c
        ${CRYPTO}/cipher/des3.c
        ${CRYPTO}/cipher/aes.c
        ${CRYPTO}/cipher/camellia.c
        ${CRYPTO}/cipher/seed.c
        ${CRYPTO}/cipher/aria.c
        ${CRYPTO}/cipher_modes/cbc.c
        ${CRYPTO}/aead/ccm.c
        ${CRYPTO}/aead/gcm.c
        ${CRYPTO}/cipher/chacha.c
        ${CRYPTO}/mac/poly1305.c
        ${CRYPTO}/aead/chacha20_poly1305.c
        ${CRYPTO}/pkc/dh.c
        ${CRYPTO}/pkc/rsa.c
        ${CRYPTO}/pkc/dsa.c
        ${CRYPTO}/ecc/ec.c
        ${CRYPTO}/ecc/ec_curves.c
        ${CRYPTO}/ecc/ecdh.c
        ${CRYPTO}/ecc/ecdsa.c
        ${CRYPTO}/ecc/eddsa.c
        ${CRYPTO}/ecc/curve25519.c
        ${CRYPTO}/ecc/curve448.c
        ${CRYPTO}/ecc/x25519.c
        ${CRYPTO}/ecc/x448.c
        ${CRYPTO}/ecc/ed25519.c
        ${CRYPTO}/ecc/ed448.c
        ${CRYPTO}/mpi/mpi.c
        ${CRYPTO}/mpi/mpi_misc.c
        ${CRYPTO}/encoding/base64.c
        ${CRYPTO}/encoding/asn1.c
        ${CRYPTO}/encoding/oid.c
        ${CRYPTO}/pkix/pem_import.c
        ${CRYPTO}/pkix/pem_key_import.c
        ${CRYPTO}/pkix/pem_decrypt.c
        ${CRYPTO}/pkix/pem_common.c
        ${CRYPTO}/pkix/pkcs5_decrypt.c
        ${CRYPTO}/pkix/pkcs5_common.c
        ${CRYPTO}/pkix/pkcs8_key_parse.c
        ${CRYPTO}/pkix/x509_key_parse.c
        ${CRYPTO}/pkix/x509_cert_parse.c
        ${CRYPTO}/pkix/x509_cert_validate.c
        ${CRYPTO}/pkix/x509_cert_ext_parse.c
        ${CRYPTO}/pkix/x509_common.c
        ${CRYPTO}/pkix/x509_sign_parse.c
        ${CRYPTO}/pkix/x509_sign_verify.c
        ${CRYPTO}/kdf/hkdf.c
        ${CRYPTO}/ecc/ec_misc.c
        ${CRYPTO}/pkc/rsa_misc.c
        ${TCP}/core/net.c
        ${TCP}/core/net_mem.c
        ${TCP}/core/net_misc.c
        ${TCP}/core/nic.c
        ${TCP}/core/ethernet.c
        ${TCP}/core/ethernet_misc.c
        ${TCP}/ipv4/arp.c
        ${TCP}/ipv4/arp_cache.c
        ${TCP}/ipv4/ipv4.c
        ${TCP}/ipv4/ipv4_frag.c
        ${TCP}/ipv4/ipv4_multicast.c
        ${TCP}/ipv4/ipv4_misc.c
        ${TCP}/ipv4/icmp.c
        ${TCP}/core/ip.c
        ${TCP}/core/udp.c
        ${TCP}/core/socket.c
        ${TCP}/core/socket_misc.c
        ${TCP}/drivers/mac/stm32f4xx_eth_driver.c
        ${TCP}/drivers/phy/lan8742_driver.c
        ${TCP}/tftp/tftp_server.c
        ${TCP}/tftp/tftp_server_misc.c
        ${BSP}/stm32f4xx_nucleo_144.c
        ${HAL_DRIVERS}/stm32f4xx_hal.c
        ${HAL_DRIVERS}/stm32f4xx_hal_adc.c
        ${HAL_DRIVERS}/stm32f4xx_hal_adc_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_can.c
        ${HAL_DRIVERS}/stm32f4xx_hal_cec.c
        ${HAL_DRIVERS}/stm32f4xx_hal_cortex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_crc.c
        ${HAL_DRIVERS}/stm32f4xx_hal_cryp.c
        ${HAL_DRIVERS}/stm32f4xx_hal_cryp_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dac.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dac_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dcmi.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dcmi_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dfsdm.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dma.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dma_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dma2d.c
        ${HAL_DRIVERS}/stm32f4xx_hal_dsi.c
        ${HAL_DRIVERS}/stm32f4xx_hal_eth.c
        ${HAL_DRIVERS}/stm32f4xx_hal_flash.c
        ${HAL_DRIVERS}/stm32f4xx_hal_flash_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_flash_ramfunc.c
        ${HAL_DRIVERS}/stm32f4xx_hal_fmpi2c.c
        ${HAL_DRIVERS}/stm32f4xx_hal_fmpi2c_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_gpio.c
        ${HAL_DRIVERS}/stm32f4xx_hal_hash.c
        ${HAL_DRIVERS}/stm32f4xx_hal_hash_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_hcd.c
        ${HAL_DRIVERS}/stm32f4xx_hal_i2c.c
        ${HAL_DRIVERS}/stm32f4xx_hal_i2c_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_i2s.c
        ${HAL_DRIVERS}/stm32f4xx_hal_i2s_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_irda.c
        ${HAL_DRIVERS}/stm32f4xx_hal_iwdg.c
        ${HAL_DRIVERS}/stm32f4xx_hal_lptim.c
        ${HAL_DRIVERS}/stm32f4xx_hal_ltdc.c
        ${HAL_DRIVERS}/stm32f4xx_hal_ltdc_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_nand.c
        ${HAL_DRIVERS}/stm32f4xx_hal_nor.c
        ${HAL_DRIVERS}/stm32f4xx_hal_pccard.c
        ${HAL_DRIVERS}/stm32f4xx_hal_pcd.c
        ${HAL_DRIVERS}/stm32f4xx_hal_pcd_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_pwr.c
        ${HAL_DRIVERS}/stm32f4xx_hal_pwr_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_qspi.c
        ${HAL_DRIVERS}/stm32f4xx_hal_rcc.c
        ${HAL_DRIVERS}/stm32f4xx_hal_rcc_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_rng.c
        ${HAL_DRIVERS}/stm32f4xx_hal_rtc.c
        ${HAL_DRIVERS}/stm32f4xx_hal_rtc_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_sai.c
        ${HAL_DRIVERS}/stm32f4xx_hal_sai_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_sd.c
        ${HAL_DRIVERS}/stm32f4xx_hal_sdram.c
        ${HAL_DRIVERS}/stm32f4xx_hal_smartcard.c
        ${HAL_DRIVERS}/stm32f4xx_hal_spdifrx.c
        ${HAL_DRIVERS}/stm32f4xx_hal_spi.c
        ${HAL_DRIVERS}/stm32f4xx_hal_sram.c
        ${HAL_DRIVERS}/stm32f4xx_hal_tim.c
        ${HAL_DRIVERS}/stm32f4xx_hal_tim_ex.c
        ${HAL_DRIVERS}/stm32f4xx_hal_uart.c
        ${HAL_DRIVERS}/stm32f4xx_hal_usart.c
        ${HAL_DRIVERS}/stm32f4xx_hal_wwdg.c
        ${HAL_DRIVERS}/stm32f4xx_ll_fmc.c
        ${HAL_DRIVERS}/stm32f4xx_ll_fsmc.c
        ${HAL_DRIVERS}/stm32f4xx_ll_sdmmc.c
        ${HAL_DRIVERS}/stm32f4xx_ll_usb.c
        ${CMAKE_SOURCE_DIR}/cmake/startup_stm32f439xx.s
        ${CMAKE_SOURCE_DIR}/cmake/syscalls.c
        ${CMAKE_SOURCE_DIR}/src/system_stm32f4xx.c
        ${CMAKE_SOURCE_DIR}/src/stm32f4xx_it.c
        ${CMAKE_SOURCE_DIR}/src/main.c
        ${CMAKE_SOURCE_DIR}/src/debug.c
        ${CMAKE_SOURCE_DIR}/src/board.c
        ${TFTP}/tftp_receiver.c
        ${TFTP}/tftp_transport.c
)

target_include_directories(bootloader PUBLIC
        ${CMAKE_SOURCE_DIR}/src
        ${TFTP}
        ${COMMON}
        ${CRYPTO}
        ${TCP}
        ${UPDATE}
        ${UPDATE}/bootloader
        ${UPDATE}/modules
        ${THIRD_PARTY}/cmsis/include
        ${THIRD_PARTY}/st/devices/stm32f4xx
        ${THIRD_PARTY}/st/drivers/stm32f4xx_hal_driver/inc
        ${BSP}
)

target_compile_definitions(bootloader PUBLIC
        STM32F4xxxI
        STM32F439xx
        USE_HAL_DRIVER
        USE_STM32F4XX_NUCLEO_144
        _WINSOCK_H
        BOOT_VERSION_STRING="${BOOT_VERSION}"
        $<$<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}/tftp_server_bl.map"
		--ld "${CMAKE_SOURCE_DIR}/cmake/STM32F439XX_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 ()
