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 ${rp2040_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  ${rp2040_dir}/pico_platform/include
38  ${rp2040_dir}/hardware_regs/include
39  ${common_dir}/pico_base_headers/include
40  ${rp2_common_dir}/pico_platform_compiler/include
41  ${rp2_common_dir}/pico_platform_sections/include
42  ${rp2_common_dir}/pico_platform_panic/include
43  ${ZEPHYR_BASE}/include
44  )
45
46target_link_options(boot_stage2 PRIVATE
47  "-nostartfiles"
48  "--specs=nosys.specs"
49  "LINKER:--script=${boot_stage_dir}/boot_stage2.ld"
50  )
51
52# The second stage bootloader is compiled without kconfig definitions.
53# Therefore, in order to use toolchain.h, it needs to define CONFIG_ARM.
54target_compile_definitions(boot_stage2 PRIVATE -DCONFIG_ARM=1)
55
56# Generates a binary file from the compiled bootloader
57add_custom_command(TARGET boot_stage2
58  POST_BUILD
59  BYPRODUCTS boot_stage2.bin
60  COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:boot_stage2> boot_stage2.bin
61  )
62
63# Checksums the binary, pads it, and generates an assembly file
64add_custom_command(TARGET boot_stage2
65  POST_BUILD
66  BYPRODUCTS ${RP2_BOOTLOADER_BYPRODUCT}
67  COMMAND ${PYTHON_EXECUTABLE} ${boot_stage_dir}/pad_checksum
68  -s 0xffffffff boot_stage2.bin ${RP2_BOOTLOADER_BYPRODUCT}
69  )
70