Skip to content

Commit

Permalink
Merge pull request #160 from MRNIU/boot
Browse files Browse the repository at this point in the history
Boot
  • Loading branch information
MRNIU authored Jun 15, 2023
2 parents f67a831 + 52d2d5f commit ab59c10
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
87 changes: 76 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,64 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
include(config)
include(ExternalProject)

# opensbi
add_custom_target(opensbi)
add_custom_command(TARGET opensbi
POST_BUILD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/3rd/opensbi
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/3rd/opensbi
COMMAND
make
CROSS_COMPILE=riscv64-linux-gnu-
FW_JUMP=y FW_JUMP_ADDR=0x80200000
PLATFORM_RISCV_XLEN=64
PLATFORM=generic
O=${CMAKE_BINARY_DIR}/3rd/opensbi
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_BINARY_DIR}/3rd/opensbi/platform/generic/firmware/fw_jump.elf
${CMAKE_BINARY_DIR}/firmware/fw_jump.elf
COMMENT "build opensbi..."
)

# gnu-efi
add_custom_target(gnuefi)
add_custom_command(TARGET gnuefi
POST_BUILD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/3rd/gnu-efi
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/3rd/gnu-efi
COMMAND
make
ARCH=${ARCH}
OBJDIR=${CMAKE_BINARY_DIR}/3rd/gnu-efi
COMMENT "build gnu-efi..."
)

add_custom_target(kernel_pre)
if (ARCH STREQUAL "riscv64")
add_custom_command(TARGET kernel_pre
DEPENDS opensbi
COMMAND make opensbi
)

elseif (ARCH STREQUAL "x86_64")
add_custom_command(TARGET kernel_pre
DEPENDS gnuefi
COMMAND make gnuefi
)

elseif (ARCH STREQUAL "aarch64")
add_custom_command(TARGET kernel_pre
DEPENDS gnuefi
COMMAND make ARCH=${ARCH} gnuefi
)

endif ()


# 内核本体
ExternalProject_Add(
kernel
DEPENDS kernel_pre
BUILD_ALWAYS 1
PREFIX ${CMAKE_BINARY_DIR}
SOURCE_DIR ${CMAKE_SOURCE_DIR}/src
Expand Down Expand Up @@ -130,10 +185,6 @@ add_custom_command(TARGET image
# 固件
if (ARCH STREQUAL "riscv64")
set(FIRMWARE fw_jump.elf)
file(
COPY ${CMAKE_SOURCE_DIR}/3rd/opensbi/build/platform/generic/firmware/${FIRMWARE}
DESTINATION ${CMAKE_BINARY_DIR}/firmware
)
set(QEMU_FIRMWARE -bios ${CMAKE_BINARY_DIR}/firmware/${FIRMWARE})
elseif (ARCH STREQUAL "x86_64")
set(FIRMWARE OVMF_${ARCH}.fd)
Expand Down Expand Up @@ -162,21 +213,35 @@ if (ARCH STREQUAL "riscv64")
set(QEMU_MACHINE -machine virt)
set(QEMU_DEVICES)
set(QEMU_IMAGE -kernel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${KernelName})
add_custom_target(run DEPENDS kernel)
add_custom_command(
TARGET run
COMMENT "Run SimpleKernel in qemu."
COMMAND qemu-system-${ARCH} ${QEMU_FLAGS} ${QEMU_MACHINE} ${QEMU_DEVICES} ${QEMU_FIRMWARE} ${QEMU_IMAGE} ${QEMU_DEBUG}
)

elseif (ARCH STREQUAL "x86_64")
set(QEMU_MACHINE -m 128M)
set(QEMU_DEVICES -net none)
set(QEMU_IMAGE -hda fat:rw:${CMAKE_BINARY_DIR}/image/)
list(APPEND QEMU_FLAGS)
add_custom_target(run DEPENDS image_uefi)
add_custom_command(
TARGET run
COMMENT "Run SimpleKernel in qemu."
COMMAND qemu-system-${ARCH} ${QEMU_FLAGS} ${QEMU_MACHINE} ${QEMU_DEVICES} ${QEMU_FIRMWARE} ${QEMU_IMAGE} ${QEMU_DEBUG}
)

elseif (ARCH STREQUAL "aarch64")
set(QEMU_MACHINE -machine virt -cpu cortex-a72)
set(QEMU_DEVICES -net none)
# set(QEMU_IMAGE -hda fat:rw:${CMAKE_BINARY_DIR}/image/)
set(QEMU_IMAGE -drive format=raw,file=${CMAKE_BINARY_DIR}/kernel.img)
endif ()
add_custom_target(run DEPENDS image_uefi)
add_custom_command(
TARGET run
COMMENT "Run SimpleKernel in qemu."
COMMAND qemu-system-${ARCH} ${QEMU_FLAGS} ${QEMU_MACHINE} ${QEMU_DEVICES} ${QEMU_FIRMWARE} ${QEMU_IMAGE} ${QEMU_DEBUG}
)

add_custom_target(run DEPENDS image_uefi)
add_custom_command(
TARGET run
COMMENT "Run SimpleKernel in qemu."
COMMAND qemu-system-${ARCH} ${QEMU_FLAGS} ${QEMU_MACHINE} ${QEMU_DEVICES} ${QEMU_FIRMWARE} ${QEMU_IMAGE} ${QEMU_DEBUG}
)
endif ()
5 changes: 2 additions & 3 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ set -e
# 输出实际执行内容
#set -x

#ARCH=riscv64
ARCH=x86_64
ARCH=riscv64
#ARCH=x86_64
#ARCH=aarch64

# 重新编译
mkdir -p ./build_${ARCH}/
rm -rf ./build_${ARCH}/*
cd ./build_${ARCH}
cmake -DARCH=${ARCH} ..
make run
21 changes: 15 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ set(ignoreMe "${CMAKE_FIND_ROOT_PATH_MODE_LIBRARY}${CMAKE_FIND_ROOT_PATH_MODE_PA
include(header_files)
# 引入添加汇编文件函数
include(find_asm_files)
# 代码优化级别
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}")

# 通用选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffreestanding -nostdlib -fexceptions -nostartfiles -fPIC -no-pie -Wall -Wextra -MMD")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffreestanding -nostdlib -fexceptions -nostartfiles -fPIC -no-pie -Wall -Wextra -MMD")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}")

# 输出相关信息
message("CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
Expand All @@ -36,27 +45,27 @@ if (ARCH STREQUAL "riscv64")
elseif (ARCH STREQUAL "x86_64")
# 将各个子对象链接为内核文件
add_executable(${KernelName}
${CMAKE_SOURCE_DIR}/../3rd/gnu-efi/bin/${ARCH}/gnuefi/reloc_${ARCH}.o
${CMAKE_BINARY_DIR}/../3rd/gnu-efi/gnuefi/reloc_${ARCH}.o
# ${CMAKE_SOURCE_DIR}/../3rd/posix-uefi/bin/crt0_${ARCH}.o
$<TARGET_OBJECTS:arch>
$<TARGET_OBJECTS:kernel>
)
link_libraries(${KernelName} PRIVATE
${CMAKE_SOURCE_DIR}/../3rd/gnu-efi/bin/${ARCH}/lib/libefi.a
${CMAKE_SOURCE_DIR}/../3rd/gnu-efi/bin/${ARCH}/gnuefi/libgnuefi.a
${CMAKE_BINARY_DIR}/../3rd/gnu-efi/lib/libefi.a
${CMAKE_BINARY_DIR}/../3rd/gnu-efi/gnuefi/libgnuefi.a
# ${CMAKE_SOURCE_DIR}/../3rd/posix-uefi/bin/libuefi_${ARCH}.a
)
elseif (ARCH STREQUAL "aarch64")
# 将各个子对象链接为内核文件
add_executable(${KernelName}
${CMAKE_SOURCE_DIR}/../3rd/gnu-efi/bin/${ARCH}/gnuefi/reloc_${ARCH}.o
${CMAKE_BINARY_DIR}/../3rd/gnu-efi/gnuefi/reloc_${ARCH}.o
# ${CMAKE_SOURCE_DIR}/../3rd/posix-uefi/bin/crt0_${ARCH}.o
$<TARGET_OBJECTS:arch>
$<TARGET_OBJECTS:kernel>
)
link_libraries(${KernelName} PRIVATE
${CMAKE_SOURCE_DIR}/../3rd/gnu-efi/bin/${ARCH}/lib/libefi.a
${CMAKE_SOURCE_DIR}/../3rd/gnu-efi/bin/${ARCH}/gnuefi/libgnuefi.a
${CMAKE_BINARY_DIR}/../3rd/gnu-efi/lib/libefi.a
${CMAKE_BINARY_DIR}/../3rd/gnu-efi/gnuefi/libgnuefi.a
# ${CMAKE_SOURCE_DIR}/../3rd/posix-uefi/bin/libuefi_${ARCH}.a
)
endif ()
Expand Down

0 comments on commit ab59c10

Please sign in to comment.