1idf_build_get_property(target IDF_TARGET)
2
3set(include_dirs "include" "include/${target}")
4
5set(private_required_comp "")
6
7set(sources "")
8
9if(target STREQUAL "linux")
10    list(APPEND sources "${target}/esp_rom_sys.c"
11                        "${target}/esp_rom_crc.c"
12                        "${target}/esp_rom_md5.c"
13                        "${target}/esp_rom_efuse.c")
14    list(APPEND include_dirs "${IDF_PATH}/tools/mocks/soc/include")
15else()
16    list(APPEND include_dirs "${target}")
17    list(APPEND sources "patches/esp_rom_crc.c"
18                        "patches/esp_rom_sys.c"
19                        "patches/esp_rom_uart.c"
20                        "patches/esp_rom_spiflash.c"
21                        "patches/esp_rom_efuse.c"
22                        "patches/esp_rom_gpio.c")
23
24
25    # Override regi2c implementation in ROM
26    if(CONFIG_ESP_ROM_HAS_REGI2C_BUG OR CONFIG_ESP_ROM_WITHOUT_REGI2C)
27        if(target STREQUAL "esp32c6")
28            list(APPEND sources "patches/esp_rom_hp_regi2c_${target}.c")
29        else()
30            list(APPEND sources "patches/esp_rom_regi2c_${target}.c")
31        endif()
32    endif()
33
34    if(CONFIG_HEAP_TLSF_USE_ROM_IMPL AND (CONFIG_ESP_ROM_TLSF_CHECK_PATCH OR CONFIG_HEAP_TLSF_CHECK_PATCH))
35        # This file shall be included in the build if TLSF in ROM is activated
36        list(APPEND sources "patches/esp_rom_tlsf.c")
37    endif()
38
39    list(APPEND private_required_comp soc hal)
40endif()
41
42if(CONFIG_IDF_TARGET_ARCH_XTENSA)
43    list(APPEND sources "patches/esp_rom_longjmp.S")
44endif()
45
46if(CONFIG_SOC_SYSTIMER_SUPPORTED)
47    list(APPEND sources "patches/esp_rom_systimer.c")
48endif()
49
50if(CONFIG_HAL_WDT_USE_ROM_IMPL)
51    list(APPEND sources "patches/esp_rom_wdt.c")
52endif()
53
54if(CONFIG_ESP_ROM_HAS_FLASH_COUNT_PAGES_BUG OR CONFIG_ESP_ROM_HAS_CACHE_WRITEBACK_BUG)
55    list(APPEND sources "patches/esp_rom_cache_esp32s2_esp32s3.c")
56endif()
57
58if(CONFIG_ESP_ROM_HAS_CACHE_WRITEBACK_BUG)
59    list(APPEND sources "patches/esp_rom_cache_writeback_esp32s3.S")
60endif()
61
62idf_component_register(SRCS ${sources}
63                       INCLUDE_DIRS ${include_dirs}
64                       PRIV_REQUIRES ${private_required_comp}
65                       LDFRAGMENTS linker.lf)
66
67set(ld_folder "ld")
68
69# Append a target linker script at the target-specific path,
70# only the 'name' part is different for each script
71function(rom_linker_script name)
72    target_linker_script(${COMPONENT_LIB} INTERFACE "${target}/${ld_folder}/${target}.rom.${name}.ld")
73endfunction()
74
75if(target STREQUAL "linux")
76    # We need to disable some warnings due to the ROM code's printf implementation
77    if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND ${CMAKE_CXX_COMPILER_VERSION} GREATER "7.0.0")
78        target_compile_options(${COMPONENT_LIB} PRIVATE -Wimplicit-fallthrough=0 -Wno-shift-count-overflow)
79    endif()
80    if(CMAKE_C_COMPILER_ID MATCHES "Clang")  # Clang or AppleClang
81        target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-integer-overflow -Wno-shift-count-overflow)
82    endif()
83else()
84    target_linker_script(${COMPONENT_LIB} INTERFACE "${target}/${ld_folder}/${target}.rom.ld")
85    rom_linker_script("api")
86    if(NOT CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
87        if(target STREQUAL "esp32s3" OR target STREQUAL "esp32c3")
88            rom_linker_script("bt_funcs")
89        endif()
90    endif()
91
92    if(CONFIG_COMPILER_FLOAT_LIB_FROM_GCCLIB)
93        rom_linker_script("libgcc")
94    else()
95        rom_linker_script("rvfp")
96    endif()
97endif()
98
99idf_build_get_property(time_t_size TIME_T_SIZE)
100
101if(BOOTLOADER_BUILD)
102    if(target STREQUAL "esp32")
103        rom_linker_script("newlib-funcs")
104        if(NOT CONFIG_SPI_FLASH_ROM_DRIVER_PATCH)
105            rom_linker_script("spiflash")
106        endif()
107        if(CONFIG_ESP32_REV_MIN_FULL GREATER_EQUAL 300)
108            rom_linker_script("eco3")
109        endif()
110
111    elseif(target STREQUAL "esp32s2")
112        rom_linker_script("newlib-funcs")
113        rom_linker_script("spiflash")
114
115    elseif(target STREQUAL "esp32s3")
116        rom_linker_script("newlib")
117
118    elseif(target STREQUAL "esp32c3")
119        rom_linker_script("newlib")
120
121    elseif(target STREQUAL "esp32c2")
122        rom_linker_script("newlib")
123
124    elseif(target STREQUAL "esp32c6")
125        rom_linker_script("newlib")
126        # The linking of the bootloader needs to use the rom_i2c_writeReg_Mask in esp32c6.rom.phy.ld
127        rom_linker_script("phy")
128        if(CONFIG_HAL_WDT_USE_ROM_IMPL)
129            rom_linker_script("wdt")
130        endif()
131        rom_linker_script("version")
132
133    elseif(target STREQUAL "esp32h2")
134        rom_linker_script("newlib")
135        if(CONFIG_HAL_WDT_USE_ROM_IMPL)
136            rom_linker_script("wdt")
137        endif()
138    endif()
139
140else() # Regular app build
141    if(target STREQUAL "esp32")
142        rom_linker_script("newlib-data")
143        rom_linker_script("syscalls")
144
145        if(NOT CONFIG_SPIRAM_CACHE_WORKAROUND)
146            # ESP32 only: these ROM functions may only be used if PSRAM cache workaround is disabled.
147            # Otherwise we need to link to a multilib version of libc compiled with PSRAM workaround.
148            rom_linker_script("newlib-funcs")
149
150            if(time_t_size EQUAL 4)
151                # The ROM functions listed in this linker script depend on sizeof(time_t).
152                # Since ROM for ESP32 was compiled for 32-bit time_t, only link these functions
153                # if the toolchain is also using 32-bit time_t.
154                rom_linker_script("newlib-time")
155
156                if(CONFIG_NEWLIB_NANO_FORMAT)
157                    # nano formatting functions in ROM are also built for 32-bit time_t.
158                    rom_linker_script("newlib-nano")
159                endif()
160            endif()
161        endif()
162
163        if(NOT CONFIG_SPI_FLASH_ROM_DRIVER_PATCH)
164            rom_linker_script("spiflash")
165        endif()
166
167        if(CONFIG_ESP32_REV_MIN_FULL GREATER_EQUAL 300)
168            rom_linker_script("eco3")
169        endif()
170
171    elseif(target STREQUAL "esp32s2")
172        rom_linker_script("newlib-funcs")
173        rom_linker_script("newlib-data")
174        rom_linker_script("spiflash")
175
176        if(time_t_size EQUAL 4)
177            # The ROM functions listed in this linker script depend on sizeof(time_t).
178            # Since ROM for ESP32-S2 was compiled for 32-bit time_t, only link these functions
179            # if the toolchain is also using 32-bit time_t.
180            rom_linker_script("newlib-time")
181
182            if(CONFIG_NEWLIB_NANO_FORMAT)
183                # nano formatting functions in ROM are also built for 32-bit time_t.
184                rom_linker_script("newlib-nano")
185            endif()
186        endif()
187
188    elseif(target STREQUAL "esp32s3")
189        rom_linker_script("newlib")
190        rom_linker_script("version")
191
192        if(CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
193            if(NOT CONFIG_BT_CTRL_BLE_MASTER)
194                rom_linker_script("ble_master")
195            endif()
196            if(NOT CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT AND NOT CONFIG_BT_BLE_50_FEATURES_SUPPORTED)
197                rom_linker_script("ble_50")
198            endif()
199            if(CONFIG_BT_BLE_CCA_MODE_NONE)
200                rom_linker_script("ble_cca")
201            endif()
202            if(NOT CONFIG_BT_NIMBLE_SECURITY_ENABLE AND NOT CONFIG_BT_BLE_SMP_ENABLE)
203                rom_linker_script("ble_smp")
204            endif()
205            if(NOT CONFIG_BT_CTRL_DTM_ENABLE)
206                rom_linker_script("ble_dtm")
207            endif()
208            if(NOT CONFIG_BT_CTRL_BLE_TEST)
209                rom_linker_script("ble_test")
210            endif()
211            if(NOT CONFIG_BT_CTRL_BLE_SCAN)
212                rom_linker_script("ble_scan")
213            endif()
214        endif()
215
216        if(time_t_size EQUAL 4)
217            # The ROM functions listed in this linker script depend on sizeof(time_t).
218            # Since ROM for ESP32-S3 was compiled for 32-bit time_t, only link these functions
219            # if the toolchain is also using 32-bit time_t.
220            rom_linker_script("newlib-time")
221
222            if(CONFIG_NEWLIB_NANO_FORMAT)
223                # nano formatting functions in ROM are also built for 32-bit time_t.
224                rom_linker_script("newlib-nano")
225            endif()
226        endif()
227
228    elseif(target STREQUAL "esp32c3")
229        rom_linker_script("newlib")
230        rom_linker_script("version")
231
232        if(CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
233            if(NOT CONFIG_BT_CTRL_BLE_MASTER)
234                rom_linker_script("ble_master")
235            endif()
236            if(NOT CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT AND NOT CONFIG_BT_BLE_50_FEATURES_SUPPORTED)
237                rom_linker_script("ble_50")
238            endif()
239            if(CONFIG_BT_BLE_CCA_MODE_NONE)
240                rom_linker_script("ble_cca")
241            endif()
242            if(NOT CONFIG_BT_NIMBLE_SECURITY_ENABLE AND NOT CONFIG_BT_BLE_SMP_ENABLE)
243                rom_linker_script("ble_smp")
244            endif()
245            if(NOT CONFIG_BT_CTRL_DTM_ENABLE)
246                rom_linker_script("ble_dtm")
247            endif()
248            if(NOT CONFIG_BT_CTRL_BLE_TEST)
249                rom_linker_script("ble_test")
250            endif()
251            if(NOT CONFIG_BT_CTRL_BLE_SCAN)
252                rom_linker_script("ble_scan")
253            endif()
254        endif()
255
256        if(time_t_size EQUAL 4)
257            # The ROM functions listed in this linker script depend on sizeof(time_t).
258            # Since ROM for ESP32-C3 was compiled for 32-bit time_t, only link these functions
259            # if the toolchain is also using 32-bit time_t.
260            rom_linker_script("newlib-time")
261
262            if(CONFIG_NEWLIB_NANO_FORMAT)
263                # nano formatting functions in ROM are also built for 32-bit time_t.
264                rom_linker_script("newlib-nano")
265            endif()
266        endif()
267
268        if(CONFIG_ESP32C3_REV_MIN_FULL GREATER_EQUAL 3)
269            rom_linker_script("eco3")
270            if(NOT CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
271                rom_linker_script("eco3_bt_funcs")
272            endif()
273        endif()
274
275        if(CONFIG_ESP32C3_REV_MIN_FULL GREATER_EQUAL 101)
276            rom_linker_script("eco7")
277            if(NOT CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
278                rom_linker_script("eco7_bt_funcs")
279            endif()
280        endif()
281
282    elseif(target STREQUAL "esp32c2")
283        rom_linker_script("newlib")
284        rom_linker_script("version")
285
286        if(CONFIG_MBEDTLS_USE_CRYPTO_ROM_IMPL)
287            rom_linker_script("mbedtls")
288            # For ESP32C2(ECO4), mbedTLS in ROM has been updated to v3.6.0-LTS
289            if(CONFIG_ESP32C2_REV_MIN_FULL GREATER_EQUAL 200)
290                rom_linker_script("mbedtls.eco4")
291            endif()
292        endif()
293
294        if(CONFIG_NEWLIB_NANO_FORMAT)
295            # nano formatting functions in ROM are also built for 64-bit time_t.
296            rom_linker_script("newlib-nano")
297        endif()
298
299        if(CONFIG_ESP32C2_REV_MIN_FULL GREATER_EQUAL 200)
300            rom_linker_script("eco4")
301        endif()
302
303        if(NOT CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY)
304            if(CONFIG_ESP32C2_REV_MIN_FULL GREATER_EQUAL 200)
305                rom_linker_script("ble-eco4")
306            else()
307                rom_linker_script("ble")
308            endif()
309        endif()
310
311    elseif(target STREQUAL "esp32c6")
312        rom_linker_script("newlib")
313        rom_linker_script("version")
314
315        # esp32c6.rom.api.ld has been split to several lds by components.
316        rom_linker_script("phy")
317        rom_linker_script("coexist")
318        rom_linker_script("net80211")
319        rom_linker_script("pp")
320
321        if(CONFIG_SPI_FLASH_ROM_IMPL)
322            rom_linker_script("spiflash")
323        endif()
324
325        if(CONFIG_HAL_WDT_USE_ROM_IMPL)
326            rom_linker_script("wdt")
327        endif()
328
329        if(NOT CONFIG_NEWLIB_NANO_FORMAT)
330            # Normal(Non-nano) formatting functions in ROM are also built for 64-bit time_t.
331            rom_linker_script("newlib-normal")
332        endif()
333
334    elseif(target STREQUAL "esp32h2")
335        rom_linker_script("newlib")
336        rom_linker_script("version")
337
338        if(CONFIG_SPI_FLASH_ROM_IMPL)
339            rom_linker_script("spiflash")
340        endif()
341
342        if(CONFIG_HAL_WDT_USE_ROM_IMPL)
343            rom_linker_script("wdt")
344        endif()
345
346        if(CONFIG_NEWLIB_NANO_FORMAT AND NOT CONFIG_ESP_ROM_HAS_NEWLIB_NANO_PRINTF_FLOAT_BUG)
347            # nano formatting functions in ROM are also built for 64-bit time_t.
348            rom_linker_script("newlib-nano")
349        endif()
350
351    endif()
352
353    if(CONFIG_HEAP_TLSF_USE_ROM_IMPL)
354        # After registering the component, set the tlsf_set_rom_patches symbol as undefined
355        # to force the linker to integrate the whole `esp_rom_tlsf.c` object file inside the
356        # final binary. This is necessary because tlsf_set_rom_patches is a constructor, thus,
357        # there as no explicit reference/call to it in IDF.
358        if((CONFIG_ESP_ROM_TLSF_CHECK_PATCH OR CONFIG_HEAP_TLSF_CHECK_PATCH))
359            target_link_libraries(${COMPONENT_LIB} PRIVATE "-u tlsf_set_rom_patches")
360        endif()
361
362        rom_linker_script("heap")
363    endif()
364
365    if(CONFIG_IDF_TARGET_ARCH_XTENSA)
366        target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=longjmp")
367    endif()
368endif()
369
370if(target STREQUAL "esp32s2")
371    target_sources(${COMPONENT_LIB} PRIVATE "esp32s2/usb_patches.c")
372endif()
373