From 08401acbbaa8d86f219eb5accb4701fd520c3af9 Mon Sep 17 00:00:00 2001 From: Alice Ziuziakowska Date: Wed, 24 Jun 2026 14:43:32 +0100 Subject: [PATCH] sw: cmake: Build OpenSBI in `FW_JUMP` mode and build U-Boot and tests separately Signed-off-by: Alice Ziuziakowska --- doc/ref/dev_guide.md | 2 +- sw/cmake/opensbi.cmake | 84 ++++++++++---------------- sw/cmake/uboot.cmake | 26 ++++---- sw/device/tests/opensbi/CMakeLists.txt | 10 +-- sw/device/tests/opensbi/test_payload.S | 2 + 5 files changed, 52 insertions(+), 72 deletions(-) diff --git a/doc/ref/dev_guide.md b/doc/ref/dev_guide.md index 6bf5197e7..b18bf6d1e 100644 --- a/doc/ref/dev_guide.md +++ b/doc/ref/dev_guide.md @@ -149,7 +149,7 @@ ctest --test-dir build/sw -R fpga_genesys2 To run CHERI Linux on the Genesys 2 board, run: ```sh -util/fpga_runner.py run -e build/sw/opensbi_with_uboot/opensbi_with_uboot_fw_payload.elf -f build/sw/linux/linux_image 0x90000000 -f build/sw/rootfs_uboot_image 0xa0000000 +util/fpga_runner.py run -e build/sw/opensbi/opensbi_fw_jump.elf -e build/sw/uboot/u-boot -f build/sw/linux/linux_image 0x90000000 -f build/sw/rootfs_uboot_image 0xa0000000 ``` ### Standalone UART diff --git a/sw/cmake/opensbi.cmake b/sw/cmake/opensbi.cmake index 65239b517..c878f0974 100644 --- a/sw/cmake/opensbi.cmake +++ b/sw/cmake/opensbi.cmake @@ -2,17 +2,8 @@ # Licensed under the Apache License, Version 2.0, see LICENSE for details. # SPDX-License-Identifier: Apache-2.0 -# Function that builds OpenSBI with a specific payload for Mocha. -function(mocha_opensbi_with_payload) - set(options TEST) - set(one_value_args TARGET) - cmake_parse_arguments(arg "${options}" "${one_value_args}" "" ${ARGN}) - - # find the target payload file. - set(PAYLOAD "$.bin") - # name of the target OpenSBI build. - set(NAME opensbi_with_${arg_TARGET}) - +# Function that builds OpenSBI for Mocha. +function(mocha_opensbi OPENSBI_NAME) # OpenSBI repository and tag to use. set(OPENSBI_REPOSITORY https://github.com/lowrisc/opensbi) set(OPENSBI_TAG mocha-devel) @@ -30,11 +21,9 @@ function(mocha_opensbi_with_payload) PLATFORM=generic # Use the Mocha defconfig file. PLATFORM_DEFCONFIG=mocha_defconfig - # Build a 'payload' firmware. - FW_PAYLOAD=y - FW_JUMP=n - # Build with the given payload. - FW_PAYLOAD_PATH=${PAYLOAD} + # Build a 'jump' firmware. + FW_JUMP=y + FW_PAYLOAD=n # Disable position-independent code and link to DRAM base, # as loading position-independent executables is not supported by # our Verilator ELF loader. @@ -47,16 +36,16 @@ function(mocha_opensbi_with_payload) # Built firmware binary to copy into the root of the external project directory. # This consists of OpenSBI + the provided payload for the next stage. set(FIRMWARE - build/platform/generic/firmware/fw_payload.elf + build/platform/generic/firmware/fw_jump.elf ) # install command - copy the firmware binaries to the root of the external project directory. set(INSTALL_COMMAND - cp ${FIRMWARE} /${NAME}_fw_payload.elf + cp ${FIRMWARE} /${OPENSBI_NAME}_fw_jump.elf ) ExternalProject_Add( - ${NAME} PREFIX ${NAME} + ${OPENSBI_NAME} PREFIX ${OPENSBI_NAME} GIT_REPOSITORY ${OPENSBI_REPOSITORY} GIT_TAG ${OPENSBI_TAG} # OpenSBI builds in its own 'build' sub-directory. @@ -66,8 +55,6 @@ function(mocha_opensbi_with_payload) CONFIGURE_COMMAND "" # no configure step needed, do nothing here. BUILD_COMMAND ${BUILD_COMMAND} INSTALL_COMMAND ${INSTALL_COMMAND} - # depend on the given payload target. - DEPENDS ${PAYLOAD_TARGET} # suppress output from stdout. LOG_DOWNLOAD true LOG_UPDATE true @@ -79,46 +66,39 @@ function(mocha_opensbi_with_payload) LOG_OUTPUT_ON_FAILURE true ) - add_dependencies(${NAME} ${arg_TARGET}) - - if(${arg_TEST}) - install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/${NAME}/${NAME}_fw_payload.elf - DESTINATION . - COMPONENT ${NAME} - ) - else() - install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/${NAME}/${NAME}_fw_payload.elf - DESTINATION . - COMPONENT boot - ) - endif() + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${OPENSBI_NAME}/${OPENSBI_NAME}_fw_jump.elf + DESTINATION . + COMPONENT boot + ) endfunction() -# Like mocha_opensbi_with_payload, but also adds a test for -# Verilator and FPGA environments. -# TODO: Integrate this with the build system better. -function(mocha_opensbi_with_payload_test PAYLOAD_TARGET) - # name of the target OpenSBI build. - set(NAME opensbi_with_${PAYLOAD_TARGET}) +function(mocha_opensbi_test OPENSBI_NAME TARGET_NAME) - mocha_opensbi_with_payload(TARGET ${PAYLOAD_TARGET} TEST) + add_dependencies(${TARGET_NAME} ${OPENSBI_NAME}) + set(TEST_NAME opensbi_with_${TARGET_NAME}) add_test( - NAME ${NAME}_sim_verilator - COMMAND ${PROJECT_SOURCE_DIR}/../util/verilator_runner.sh -r $_scrambled.vmem -E ${NAME}/${NAME}_fw_payload.elf + NAME ${TEST_NAME}_sim_verilator + COMMAND ${PROJECT_SOURCE_DIR}/../util/verilator_runner.sh + -r $_scrambled.vmem + -E ${OPENSBI_NAME}/${OPENSBI_NAME}_fw_jump.elf + -E $ ) add_test( - NAME ${NAME}_fpga_genesys2 - COMMAND ${PROJECT_SOURCE_DIR}/../util/fpga_runner.py test -e ${NAME}/${NAME}_fw_payload.elf + NAME ${TEST_NAME}_fpga_genesys2 + COMMAND ${PROJECT_SOURCE_DIR}/../util/fpga_runner.py test + -e ${OPENSBI_NAME}/${OPENSBI_NAME}_fw_jump.elf + -e $ ) - set_property(TEST ${NAME}_sim_verilator PROPERTY TIMEOUT 7200) - set_property(TEST ${NAME}_sim_verilator PROPERTY LABELS opensbi verilator slow) - set_property(TEST ${NAME}_fpga_genesys2 PROPERTY TIMEOUT 60) - set_property(TEST ${NAME}_fpga_genesys2 PROPERTY LABELS opensbi fpga) + set_property(TEST ${TEST_NAME}_sim_verilator PROPERTY TIMEOUT 7200) + set_property(TEST ${TEST_NAME}_sim_verilator PROPERTY LABELS opensbi verilator slow) + set_property(TEST ${TEST_NAME}_fpga_genesys2 PROPERTY TIMEOUT 60) + set_property(TEST ${TEST_NAME}_fpga_genesys2 PROPERTY LABELS opensbi fpga) endfunction() -mocha_opensbi_with_payload_test(opensbi_test_payload) +mocha_opensbi(opensbi) + +mocha_opensbi_test(opensbi opensbi_test_payload) diff --git a/sw/cmake/uboot.cmake b/sw/cmake/uboot.cmake index d11e14742..394b5ffe2 100644 --- a/sw/cmake/uboot.cmake +++ b/sw/cmake/uboot.cmake @@ -3,8 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 # Function that builds U-Boot, creating a target with the given output name. -function(mocha_uboot OUTPUT_NAME) - set(UBOOT_BUILD_NAME ${OUTPUT_NAME}_build) +function(mocha_uboot UBOOT_NAME) # U-Boot repository and tag to use. set(UBOOT_REPOSITORY https://github.com/lowrisc/u-boot) set(UBOOT_TAG mocha-devel) @@ -22,10 +21,15 @@ function(mocha_uboot OUTPUT_NAME) "CC=clang -target riscv64-unknown-elf" "LD=ld.lld" ) + + # install command - copy the built U-Boot binary to the root of the external project directory. + set(INSTALL_COMMAND + cp u-boot + ) ExternalProject_Add( - ${UBOOT_BUILD_NAME} - PREFIX ${UBOOT_BUILD_NAME} + ${UBOOT_NAME} + PREFIX ${UBOOT_NAME} GIT_REPOSITORY ${UBOOT_REPOSITORY} GIT_TAG ${UBOOT_TAG} GIT_SHALLOW true @@ -35,7 +39,7 @@ function(mocha_uboot OUTPUT_NAME) BUILD_JOB_SERVER_AWARE true CONFIGURE_COMMAND ${CONFIGURE_COMMAND} BUILD_COMMAND ${BUILD_COMMAND} - INSTALL_COMMAND "" + INSTALL_COMMAND ${INSTALL_COMMAND} # suppress output from stdout. LOG_DOWNLOAD true LOG_UPDATE true @@ -47,13 +51,11 @@ function(mocha_uboot OUTPUT_NAME) LOG_OUTPUT_ON_FAILURE true ) - add_executable(${OUTPUT_NAME} IMPORTED GLOBAL) - set_target_properties(${OUTPUT_NAME} PROPERTIES - IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${UBOOT_BUILD_NAME}/src/${UBOOT_BUILD_NAME}/u-boot - ) - add_dependencies(${OUTPUT_NAME} ${UBOOT_BUILD_NAME}) + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/${UBOOT_NAME}/u-boot + DESTINATION . + COMPONENT boot + ) endfunction() mocha_uboot(uboot) - -mocha_opensbi_with_payload(TARGET uboot) diff --git a/sw/device/tests/opensbi/CMakeLists.txt b/sw/device/tests/opensbi/CMakeLists.txt index 4674c7c5f..e994aaedf 100644 --- a/sw/device/tests/opensbi/CMakeLists.txt +++ b/sw/device/tests/opensbi/CMakeLists.txt @@ -12,11 +12,7 @@ target_compile_options(${NAME} PUBLIC ${VANILLA_FLAGS}) # so that S-mode software can map itself into virtual memory # using just a few large pages. This is a requirement for Linux too, # see https://docs.kernel.org/arch/riscv/boot.html#kernel-location. -target_link_options(${NAME} PUBLIC "-Wl,--Ttext=0x80200000") -# Objcopy to a binary, to be used as the OpenSBI payload. -add_custom_command( - TARGET ${NAME} POST_BUILD - COMMAND ${CMAKE_OBJCOPY} -O binary "$" - "$.bin" - VERBATIM +target_link_options(${NAME} PUBLIC + "-Wl,--Ttext=0x80200000" + "-Tmocha_dram.ld" "-L${LDS_DIR}" ) diff --git a/sw/device/tests/opensbi/test_payload.S b/sw/device/tests/opensbi/test_payload.S index 938120a18..cf5a0c1f7 100644 --- a/sw/device/tests/opensbi/test_payload.S +++ b/sw/device/tests/opensbi/test_payload.S @@ -15,7 +15,9 @@ .section .text +.globl _init_vector .globl _start +_init_vector: _start: /* Perform an SBI call to print test_status_string. */ li a7, SBI_DEBUG_CONSOLE