1# SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
2#
3# SPDX-License-Identifier: Apache-2.0
4
5cmake_minimum_required(VERSION 3.13)
6
7project(hal)
8
9set(esp_hal_dir ${ESP_HAL_PATH})
10set(src_dir ${CMAKE_CURRENT_LIST_DIR}/src)
11set(include_dirs
12    ${CMAKE_CURRENT_LIST_DIR}/include
13    ${CMAKE_CURRENT_LIST_DIR}/include/${MCUBOOT_TARGET}
14    )
15
16list(APPEND include_dirs
17    ${esp_hal_dir}/components/bootloader_support/include
18    ${esp_hal_dir}/components/bootloader_support/private_include
19    ${esp_hal_dir}/components/bootloader_support/bootloader_flash/include
20    ${esp_hal_dir}/components/spi_flash/include
21    ${esp_hal_dir}/components/spi_flash/include/spi_flash
22    ${esp_hal_dir}/components/esp_app_format/include
23    ${esp_hal_dir}/components/newlib/platform_include
24    ${esp_hal_dir}/components/esp_common/include
25    ${esp_hal_dir}/components/${MCUBOOT_ARCH}/include
26    ${esp_hal_dir}/components/esp_rom/include
27    ${esp_hal_dir}/components/esp_rom/include/${MCUBOOT_TARGET}
28    ${esp_hal_dir}/components/esp_rom/${MCUBOOT_TARGET}
29    ${esp_hal_dir}/components/soc/include
30    ${esp_hal_dir}/components/soc/${MCUBOOT_TARGET}
31    ${esp_hal_dir}/components/soc/${MCUBOOT_TARGET}/include
32    ${esp_hal_dir}/components/efuse/include
33    ${esp_hal_dir}/components/efuse/${MCUBOOT_TARGET}/include
34    ${esp_hal_dir}/components/efuse/private_include
35    ${esp_hal_dir}/components/efuse/${MCUBOOT_TARGET}/private_include
36    ${esp_hal_dir}/components/esp_hw_support/include
37    ${esp_hal_dir}/components/esp_hw_support/include/soc
38    ${esp_hal_dir}/components/esp_hw_support/include/soc/${MCUBOOT_TARGET}
39    ${esp_hal_dir}/components/esp_hw_support/port/include
40    ${esp_hal_dir}/components/esp_hw_support/include/esp_private
41    ${esp_hal_dir}/components/esp_hw_support/port/${MCUBOOT_TARGET}
42    ${esp_hal_dir}/components/hal/${MCUBOOT_TARGET}/include
43    ${esp_hal_dir}/components/hal/include
44    ${esp_hal_dir}/components/hal/platform_port/include
45    ${esp_hal_dir}/components/esp_system/include
46    ${esp_hal_dir}/components/log/include
47    )
48
49if("${MCUBOOT_ARCH}" STREQUAL "xtensa")
50    list(APPEND include_dirs
51        ${esp_hal_dir}/components/${MCUBOOT_ARCH}/${MCUBOOT_TARGET}/include
52        ${esp_hal_dir}/components/${MCUBOOT_ARCH}/include
53        )
54endif()
55
56set(hal_srcs
57    ${esp_hal_dir}/components/bootloader_support/src/${MCUBOOT_TARGET}/bootloader_${MCUBOOT_TARGET}.c
58    ${esp_hal_dir}/components/bootloader_support/src/bootloader_init.c
59    ${esp_hal_dir}/components/bootloader_support/src/bootloader_common.c
60    ${esp_hal_dir}/components/bootloader_support/src/bootloader_common_loader.c
61    ${esp_hal_dir}/components/bootloader_support/src/bootloader_console.c
62    ${esp_hal_dir}/components/bootloader_support/src/bootloader_console_loader.c
63    ${esp_hal_dir}/components/bootloader_support/bootloader_flash/src/bootloader_flash.c
64    ${esp_hal_dir}/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_${MCUBOOT_TARGET}.c
65    ${esp_hal_dir}/components/bootloader_support/src/bootloader_clock_init.c
66    ${esp_hal_dir}/components/bootloader_support/src/bootloader_clock_loader.c
67    ${esp_hal_dir}/components/bootloader_support/src/bootloader_efuse.c
68    ${esp_hal_dir}/components/bootloader_support/src/bootloader_panic.c
69    ${esp_hal_dir}/components/bootloader_support/src/bootloader_mem.c
70    ${esp_hal_dir}/components/bootloader_support/src/bootloader_random.c
71    ${esp_hal_dir}/components/bootloader_support/src/bootloader_random_${MCUBOOT_TARGET}.c
72    ${esp_hal_dir}/components/bootloader_support/src/bootloader_utility.c
73    ${esp_hal_dir}/components/bootloader_support/src/esp_image_format.c
74    ${esp_hal_dir}/components/bootloader_support/src/${MCUBOOT_TARGET}/bootloader_soc.c
75    ${esp_hal_dir}/components/bootloader_support/src/${MCUBOOT_TARGET}/bootloader_sha.c
76    ${esp_hal_dir}/components/hal/mpu_hal.c
77    ${esp_hal_dir}/components/hal/efuse_hal.c
78    ${esp_hal_dir}/components/hal/mmu_hal.c
79    ${esp_hal_dir}/components/hal/wdt_hal_iram.c
80    ${esp_hal_dir}/components/hal/${MCUBOOT_TARGET}/efuse_hal.c
81    ${esp_hal_dir}/components/soc/${MCUBOOT_TARGET}/uart_periph.c
82    ${esp_hal_dir}/components/soc/${MCUBOOT_TARGET}/gpio_periph.c
83    ${esp_hal_dir}/components/esp_hw_support/port/${MCUBOOT_TARGET}/rtc_time.c
84    ${esp_hal_dir}/components/esp_hw_support/port/${MCUBOOT_TARGET}/rtc_clk.c
85    ${esp_hal_dir}/components/esp_hw_support/port/${MCUBOOT_TARGET}/rtc_clk_init.c
86    ${esp_hal_dir}/components/esp_rom/patches/esp_rom_uart.c
87    ${esp_hal_dir}/components/esp_rom/patches/esp_rom_sys.c
88    ${esp_hal_dir}/components/esp_rom/patches/esp_rom_spiflash.c
89    ${esp_hal_dir}/components/efuse/${MCUBOOT_TARGET}/esp_efuse_table.c
90    ${esp_hal_dir}/components/efuse/src/esp_efuse_fields.c
91    ${esp_hal_dir}/components/efuse/${MCUBOOT_TARGET}/esp_efuse_fields.c
92    ${esp_hal_dir}/components/efuse/src/esp_efuse_api.c
93    ${esp_hal_dir}/components/efuse/src/esp_efuse_utility.c
94    ${esp_hal_dir}/components/efuse/${MCUBOOT_TARGET}/esp_efuse_utility.c
95    ${esp_hal_dir}/components/log/log_noos.c
96    ${src_dir}/bootloader_banner.c
97    ${src_dir}/bootloader_wdt.c
98    )
99
100if(DEFINED CONFIG_SECURE_BOOT_V2_ENABLED)
101    list(APPEND hal_srcs
102        ${src_dir}/secure_boot.c
103        ${esp_hal_dir}/components/bootloader_support/src/secure_boot_v2/secure_boot_signatures_bootloader.c
104        ${esp_hal_dir}/components/bootloader_support/src/${MCUBOOT_TARGET}/secure_boot_secure_features.c
105        )
106    list(APPEND include_dirs
107        ${esp_hal_dir}/components/bootloader_support/src/secure_boot_v2
108        )
109endif()
110
111if(DEFINED CONFIG_SECURE_FLASH_ENC_ENABLED)
112    list(APPEND hal_srcs
113        ${src_dir}/flash_encrypt.c
114        ${esp_hal_dir}/components/bootloader_support/src/${MCUBOOT_TARGET}/flash_encryption_secure_features.c
115        )
116    set_source_files_properties(
117        ${src_dir}/flash_encrypt.c
118        PROPERTIES COMPILE_FLAGS
119        "-Wno-unused-variable"
120        )
121endif()
122
123if("${MCUBOOT_ARCH}" STREQUAL "xtensa")
124    list(APPEND hal_srcs
125        ${esp_hal_dir}/components/esp_rom/patches/esp_rom_longjmp.S
126        )
127endif()
128
129set(CFLAGS
130    "-nostdlib"
131    "-Wno-frame-address"
132    "-Wall"
133    "-Wextra"
134    "-W"
135    "-Wwrite-strings"
136    "-Wlogical-op"
137    "-Wshadow"
138    "-ffunction-sections"
139    "-fdata-sections"
140    "-fstrict-volatile-bitfields"
141    "-Werror=all"
142    "-Wno-error=unused-function"
143    "-Wno-error=unused-but-set-variable"
144    "-Wno-error=unused-variable"
145    "-Wno-error=deprecated-declarations"
146    "-Wno-unused-parameter"
147    "-Wno-sign-compare"
148    "-ggdb"
149    "-Os"
150    "-D_GNU_SOURCE"
151    "-std=gnu17"
152    "-Wno-old-style-declaration"
153    "-Wno-implicit-int"
154    )
155
156set(LDFLAGS
157    "-Wno-frame-address"
158    "-Wl,--cref"
159    "-Wl,--Map=${APP_NAME}.map"
160    "-fno-rtti"
161    "-fno-lto"
162    "-Wl,--gc-sections"
163    "-Wl,--undefined=uxTopUsedPriority"
164    "-lm"
165    "-lgcc"
166    "-lgcov"
167    )
168
169if("${MCUBOOT_ARCH}" STREQUAL "xtensa")
170    list(APPEND CFLAGS
171        "-mlongcalls"
172        )
173    list(APPEND LDFLAGS
174        "-mlongcalls"
175        )
176endif()
177
178set(LINKER_SCRIPTS
179    -T${esp_hal_dir}/components/esp_rom/${MCUBOOT_TARGET}/ld/${MCUBOOT_TARGET}.rom.ld
180    -T${esp_hal_dir}/components/esp_rom/${MCUBOOT_TARGET}/ld/${MCUBOOT_TARGET}.rom.libgcc.ld
181    -T${esp_hal_dir}/components/esp_rom/${MCUBOOT_TARGET}/ld/${MCUBOOT_TARGET}.rom.api.ld
182    -T${esp_hal_dir}/components/soc/${MCUBOOT_TARGET}/ld/${MCUBOOT_TARGET}.peripherals.ld
183    )
184
185include(${CMAKE_CURRENT_LIST_DIR}/include/${MCUBOOT_TARGET}/${MCUBOOT_TARGET}.cmake)
186
187add_library(hal STATIC ${hal_srcs} ${include_dirs})
188
189# Wrap for overriding the print banner function from bootloader_support
190add_definitions(-DIDF_VER=0)
191target_link_libraries(
192    hal
193    INTERFACE
194    "-Wl,--wrap=bootloader_print_banner")
195
196target_include_directories(
197    hal
198    PUBLIC
199    ${include_dirs}
200    )
201
202target_compile_options(
203    hal
204    PUBLIC
205    ${CFLAGS}
206    )
207
208target_link_libraries(
209    hal
210    PUBLIC
211    ${LDFLAGS}
212    ${LINKER_SCRIPTS}
213    )
214