1# SPDX-License-Identifier: Apache-2.0
2
3cmake_minimum_required(VERSION 3.20.0)
4
5# Skip compiler checking
6set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
7
8project(second_stage_bootloader)
9enable_language(ASM)
10
11set(rp2_common_dir ${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/src/rp2_common)
12set(rp2040_dir ${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/src/rp2040)
13set(common_dir ${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/src/common)
14set(boot_stage_dir ${rp2_common_dir}/boot_stage2)
15
16add_executable(boot_stage2)
17
18if(${FLASH_TYPE} STREQUAL W25Q080)
19  set(flash_type_file boot2_w25q080.S)
20elseif(${FLASH_TYPE} STREQUAL GENERIC_03H)
21  set(flash_type_file boot2_generic_03h.S)
22elseif(${FLASH_TYPE} STREQUAL IS25LP080)
23  set(flash_type_file boot2_is25lp080.S)
24elseif(${FLASH_TYPE} STREQUAL W25X10CL)
25  set(flash_type_file boot2_w25x10cl.S)
26elseif(${FLASH_TYPE} STREQUAL AT25SF128A)
27  set(flash_type_file boot2_at25sf128a.S)
28else()
29  message(FATAL_ERROR "No flash type selected")
30endif()
31
32target_sources(boot_stage2 PRIVATE ${boot_stage_dir}/${flash_type_file})
33
34target_include_directories(boot_stage2 PUBLIC
35  ..
36  ${boot_stage_dir}/asminclude
37  ${rp2_common_dir}/pico_platform/include
38  ${rp2040_dir}/hardware_regs/include
39  ${common_dir}/pico_base/include
40  ${ZEPHYR_BASE}/include
41  )
42
43target_link_options(boot_stage2 PRIVATE
44  "-nostartfiles"
45  "--specs=nosys.specs"
46  "LINKER:--script=${boot_stage_dir}/boot_stage2.ld"
47  )
48
49# The second stage bootloader is compiled without kconfig definitions.
50# Therefore, in order to use toolchain.h, it needs to define CONFIG_ARM.
51target_compile_definitions(boot_stage2 PRIVATE -DCONFIG_ARM=1)
52
53# Generates a binary file from the compiled bootloader
54add_custom_command(TARGET boot_stage2
55  POST_BUILD
56  BYPRODUCTS boot_stage2.bin
57  COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:boot_stage2> boot_stage2.bin
58  )
59
60# Checksums the binary, pads it, and generates an assembly file
61add_custom_command(TARGET boot_stage2
62  POST_BUILD
63  BYPRODUCTS ${RP2_BOOTLOADER_BYPRODUCT}
64  COMMAND ${PYTHON_EXECUTABLE} ${boot_stage_dir}/pad_checksum
65  -s 0xffffffff boot_stage2.bin ${RP2_BOOTLOADER_BYPRODUCT}
66  )
67