reactos/boot/freeldr/freeldr/uefi.cmake
Justin Miller ff3dadf89d
[FREELDR] Enable UEFI boot for x86 and amd64 (#5267)
Co-authored-by: Stanislav Motylkov <x86corez@gmail.com>
Co-authored-by: Hermès BÉLUSCA - MAÏTO <hermes.belusca-maito@reactos.org>

- Allow to boot NT kernel on UEFI systems with our 2 primary supported architectures
- Implement remaining code needed to pass execution to x86 and amd64 kernels

CORE-11954
2023-10-11 12:45:08 -07:00

140 lines
3.9 KiB
CMake

##
## PROJECT: FreeLoader
## LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
## PURPOSE: Build definitions for UEFI
## COPYRIGHT: Copyright 2023 Justin Miller <justinmiller100@gmail.com>
##
include_directories(BEFORE
${REACTOS_SOURCE_DIR}/boot/environ/include/efi
${REACTOS_SOURCE_DIR}/boot/freeldr/freeldr
${REACTOS_SOURCE_DIR}/boot/freeldr/freeldr/include
${REACTOS_SOURCE_DIR}/boot/freeldr/freeldr/include/arch/uefi)
list(APPEND UEFILDR_ARC_SOURCE
${FREELDR_ARC_SOURCE}
arch/uefi/stubs.c
arch/uefi/ueficon.c
arch/uefi/uefidisk.c
arch/uefi/uefihw.c
arch/uefi/uefimem.c
arch/uefi/uefisetup.c
arch/uefi/uefiutil.c
arch/uefi/uefivid.c
arch/vgafont.c)
if(ARCH STREQUAL "i386")
list(APPEND UEFILDR_ARC_SOURCE
arch/i386/i386idt.c)
list(APPEND UEFILDR_COMMON_ASM_SOURCE
arch/uefi/i386/uefiasm.S
arch/i386/i386trap.S)
elseif(ARCH STREQUAL "amd64")
list(APPEND UEFILDR_COMMON_ASM_SOURCE
arch/uefi/amd64/uefiasm.S)
elseif(ARCH STREQUAL "arm")
list(APPEND UEFILDR_ARC_SOURCE
arch/arm/macharm.c
arch/arm/debug.c)
#TBD
elseif(ARCH STREQUAL "arm64")
#TBD
else()
#TBD
endif()
add_asm_files(uefifreeldr_common_asm ${FREELDR_COMMON_ASM_SOURCE} ${UEFILDR_COMMON_ASM_SOURCE})
add_library(uefifreeldr_common
${uefifreeldr_common_asm}
${UEFILDR_ARC_SOURCE}
${FREELDR_BOOTLIB_SOURCE}
${FREELDR_BOOTMGR_SOURCE}
${FREELDR_NTLDR_SOURCE})
target_compile_definitions(uefifreeldr_common PRIVATE UEFIBOOT)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
# Prevent using SSE (no support in freeldr)
target_compile_options(uefifreeldr_common PUBLIC -mno-sse)
endif()
set(PCH_SOURCE
${UEFILDR_ARC_SOURCE}
${FREELDR_BOOTLIB_SOURCE}
${FREELDR_BOOTMGR_SOURCE}
${FREELDR_NTLDR_SOURCE})
add_pch(uefifreeldr_common include/arch/uefi/uefildr.h PCH_SOURCE)
add_dependencies(uefifreeldr_common bugcodes asm xdk)
## GCC builds need this extra thing for some reason...
if(ARCH STREQUAL "i386" AND NOT MSVC)
target_link_libraries(uefifreeldr_common mini_hal)
endif()
spec2def(uefildr.exe freeldr.spec)
list(APPEND UEFILDR_BASE_SOURCE
include/arch/uefi/uefildr.h
arch/uefi/uefildr.c
${FREELDR_BASE_SOURCE})
if(ARCH STREQUAL "i386")
# Must be included together with disk/scsiport.c
list(APPEND UEFILDR_BASE_SOURCE
${CMAKE_CURRENT_BINARY_DIR}/uefildr.def)
endif()
add_executable(uefildr ${UEFILDR_BASE_SOURCE})
set_target_properties(uefildr PROPERTIES SUFFIX ".efi")
target_compile_definitions(uefildr PRIVATE UEFIBOOT)
# On AMD64 we only map 1GB with freeloader, tell UEFI to keep us low!
if(ARCH STREQUAL "amd64")
set_image_base(uefildr 0x10000)
endif()
if(MSVC)
if(NOT ARCH STREQUAL "arm")
target_link_options(uefildr PRIVATE /DYNAMICBASE:NO)
endif()
target_link_options(uefildr PRIVATE /NXCOMPAT:NO /ignore:4078 /ignore:4254 /DRIVER)
# We don't need hotpatching
remove_target_compile_option(uefildr "/hotpatch")
else()
target_link_options(uefildr PRIVATE -Wl,--exclude-all-symbols,--file-alignment,0x200,--section-alignment,0x200)
# Strip everything, including rossym data
add_custom_command(TARGET uefildr
POST_BUILD
COMMAND ${CMAKE_STRIP} --remove-section=.rossym $<TARGET_FILE:uefildr>
COMMAND ${CMAKE_STRIP} --strip-all $<TARGET_FILE:uefildr>)
endif()
if(MSVC)
set_subsystem(uefildr EFI_APPLICATION)
else()
set_subsystem(uefildr 10)
endif()
set_entrypoint(uefildr EfiEntry)
if(ARCH STREQUAL "i386")
target_link_libraries(uefildr mini_hal)
endif()
target_link_libraries(uefildr uefifreeldr_common cportlib blcmlib blrtl libcntpr)
# dynamic analysis switches
if(STACK_PROTECTOR)
target_sources(uefildr PRIVATE $<TARGET_OBJECTS:gcc_ssp_nt>)
endif()
if(RUNTIME_CHECKS)
target_link_libraries(uefildr runtmchk)
endif()
add_dependencies(uefildr xdk)