From 109c24f38956af4fa81a2fd0f255108dba3e1cbd Mon Sep 17 00:00:00 2001 From: belliash Date: Sat, 17 Sep 2022 00:14:09 +0200 Subject: [PATCH] Initial support for xtldr modules and bootloader protocol --- CMakeLists.txt | 2 +- sdk/xtdk/xtuefi.h | 5 -- xtldr/CMakeLists.txt | 5 +- xtldr/includes/bldefs.h | 39 +++++++++++++++ xtldr/includes/xtbl.h | 20 +++++--- xtldr/modules/CMakeLists.txt | 1 + xtldr/modules/blproto.c | 78 ++++++++++++++++++++++++++++++ xtldr/modules/dummy/CMakeLists.txt | 26 ++++++++++ xtldr/modules/dummy/dummy.c | 48 ++++++++++++++++++ xtldr/xtldr.c | 51 +++++++++++++++++++ xtoskrnl/CMakeLists.txt | 2 +- 11 files changed, 262 insertions(+), 15 deletions(-) create mode 100644 xtldr/includes/bldefs.h create mode 100644 xtldr/modules/CMakeLists.txt create mode 100644 xtldr/modules/blproto.c create mode 100644 xtldr/modules/dummy/CMakeLists.txt create mode 100644 xtldr/modules/dummy/dummy.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 81a094a..14e90d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ add_compiler_flags(-D__RELFILE__="&__FILE__[__FILE__[0] == '.' ? sizeof \\\"${_P # Set the virtual disk image size (in MiB) set_disk_image_size(128) -# Build subprojects +# Build all subprojects add_subdirectory(bootdata) add_subdirectory(xtldr) add_subdirectory(xtoskrnl) diff --git a/sdk/xtdk/xtuefi.h b/sdk/xtdk/xtuefi.h index c492ab0..afe6048 100644 --- a/sdk/xtdk/xtuefi.h +++ b/sdk/xtdk/xtuefi.h @@ -234,11 +234,6 @@ #define EFI_UART_IO_PROTOCOL_GUID {0xBB25CF6F, 0xF1D4, 0x11D2, {0x9A, 0x0C, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0xFD}} #define EFI_UNICODE_COLLATION_PROTOCOL_GUID {0x1D85CD7F, 0xF43D, 0x11D2, {0x9A, 0x0C, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D}} -/* EFI XT protocols GUIDs */ -#define EFI_XT_ELF_IMAGE_PROTOCOL_GUID {0x9B539887, 0x5854, 0x424C, {0x42, 0xFA, 0x00, 0x90, 0xC9, 0x04, 0x66, 0xA4}} -#define EFI_XT_HIVE_IMAGE_PROTOCOL_GUID {0x5A3688A2, 0x5854, 0x424C, {0xBC, 0xDF, 0x00, 0x8F, 0xAA, 0x52, 0x63, 0xD4}} -#define EFI_XT_PECOFF_IMAGE_PROTOCOL_GUID {0x85DAFF92, 0x5854, 0x424C, {0xB1, 0xBF, 0x00, 0x79, 0x39, 0xEA, 0x6E, 0xFD}} - /* Basic UEFI types */ typedef PVOID EFI_EVENT, *PEFI_EVENT; typedef PVOID EFI_HANDLE, *PEFI_HANDLE; diff --git a/xtldr/CMakeLists.txt b/xtldr/CMakeLists.txt index b747b18..0b3847d 100644 --- a/xtldr/CMakeLists.txt +++ b/xtldr/CMakeLists.txt @@ -1,3 +1,6 @@ +# Build XTLDR modules +add_subdirectory(modules) + # XT Boot Loader PROJECT(XTLDR) @@ -18,7 +21,7 @@ list(APPEND XTLDR_SOURCE add_executable(xtldr ${XTLDR_SOURCE}) # Add linker libraries -target_link_libraries(xtldr xtoslib) +target_link_libraries(xtldr libxtos) # Set proper binary name and install target if(ARCH STREQUAL "i686") diff --git a/xtldr/includes/bldefs.h b/xtldr/includes/bldefs.h new file mode 100644 index 0000000..655eca4 --- /dev/null +++ b/xtldr/includes/bldefs.h @@ -0,0 +1,39 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/includes/bldefs.h + * DESCRIPTION: Definitions for the XT boot loader + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_BLDEFS_H +#define __XTLDR_BLDEFS_H + +#include + + +/* EFI XT boot devices */ +#define XT_BOOT_DEVICE_UNKNOWN 0x00 +#define XT_BOOT_DEVICE_CDROM 0x01 +#define XT_BOOT_DEVICE_FLOPPY 0x02 +#define XT_BOOT_DEVICE_HARDDISK 0x03 +#define XT_BOOT_DEVICE_RAMDISK 0x04 + +/* EFI XT protocols GUIDs */ +#define XT_BOOT_LOADER_PROTOCOL_GUID {0x21FF49E8, 0x5854, 0x424C, {0xE9, 0x11, 0x00, 0x86, 0xE1, 0x2C, 0xD9, 0xC1}} +#define XT_ELF_IMAGE_PROTOCOL_GUID {0x9B539887, 0x5854, 0x424C, {0x42, 0xFA, 0x00, 0x90, 0xC9, 0x04, 0x66, 0xA4}} +#define XT_HIVE_IMAGE_PROTOCOL_GUID {0x5A3688A2, 0x5854, 0x424C, {0xBC, 0xDF, 0x00, 0x8F, 0xAA, 0x52, 0x63, 0xD4}} +#define XT_PECOFF_IMAGE_PROTOCOL_GUID {0x85DAFF92, 0x5854, 0x424C, {0xB1, 0xBF, 0x00, 0x79, 0x39, 0xEA, 0x6E, 0xFD}} + +/* EFI XT routines forward references */ +typedef VOID (*PBL_DBG_PRINT)(IN PUINT16 Format, IN ...); +typedef VOID (*PBL_EFI_PRINT)(IN PUINT16 Format, IN ...); + +/* EFI XT Boot Loader Protocol */ +typedef struct _XT_BOOT_LOADER_PROTOCOL +{ + PBL_DBG_PRINT DbgPrint; + PBL_EFI_PRINT EfiPrint; +} XT_BOOT_LOADER_PROTOCOL, *PXT_BOOT_LOADER_PROTOCOL; + +#endif /* __XTLDR_BLDEFS_H */ diff --git a/xtldr/includes/xtbl.h b/xtldr/includes/xtbl.h index 0dcc3e7..3646123 100644 --- a/xtldr/includes/xtbl.h +++ b/xtldr/includes/xtbl.h @@ -10,19 +10,14 @@ #define __XTLDR_XTBL_H #include +#include -#define XT_BOOT_DEVICE_UNKNOWN 0x00 -#define XT_BOOT_DEVICE_CDROM 0x01 -#define XT_BOOT_DEVICE_FLOPPY 0x02 -#define XT_BOOT_DEVICE_HARDDISK 0x03 -#define XT_BOOT_DEVICE_RAMDISK 0x04 - /* EFI Image Handle */ EXTERN EFI_HANDLE EfiImageHandle; /* EFI System Table */ -EXTERN EFI_SYSTEM_TABLE *EfiSystemTable; +EXTERN PEFI_SYSTEM_TABLE EfiSystemTable; /* Serial port configuration */ EXTERN CPPORT EfiSerialPort; @@ -65,6 +60,17 @@ BlGetVolumeDevicePath(IN PUCHAR SystemPath, OUT PEFI_DEVICE_PATH_PROTOCOL *DevicePath, OUT PUCHAR *Path); +EFI_STATUS +BlGetXtLoaderProtocol(EFI_HANDLE ImageHandle, + PEFI_SYSTEM_TABLE SystemTable, + PXT_BOOT_LOADER_PROTOCOL *LoaderProtocol); + +EFI_STATUS +BlLoadEfiModules(); + +EFI_STATUS +BlpRegisterXtLoaderProtocol(); + EFI_STATUS BlStartXtLoader(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable); diff --git a/xtldr/modules/CMakeLists.txt b/xtldr/modules/CMakeLists.txt new file mode 100644 index 0000000..c65186e --- /dev/null +++ b/xtldr/modules/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(dummy) diff --git a/xtldr/modules/blproto.c b/xtldr/modules/blproto.c new file mode 100644 index 0000000..34f6dde --- /dev/null +++ b/xtldr/modules/blproto.c @@ -0,0 +1,78 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/blproto.c + * DESCRIPTION: EFI XTLDR protocol API + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * This routine locates and opens the XT boot loader protocol. + * + * @param ImageHandle + * Firmware-allocated handle that identifies the image. + * + * @param SystemTable + * Provides the EFI system table. + * + * @param LoaderProtocol + * Supplies the address where a pointer to the bootloader protocol is returned. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +EFI_STATUS +BlGetXtLoaderProtocol(EFI_HANDLE ImageHandle, + PEFI_SYSTEM_TABLE SystemTable, + PXT_BOOT_LOADER_PROTOCOL *LoaderProtocol) +{ + EFI_GUID Guid = XT_BOOT_LOADER_PROTOCOL_GUID; + PEFI_HANDLE Handles = NULL; + EFI_STATUS Status; + UINT_PTR Count; + UINT Index; + + /* Try to locate the handles */ + Status = SystemTable->BootServices->LocateHandleBuffer(ByProtocol, &Guid, NULL, &Count, &Handles); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to get handles */ + return Status; + } + + /* Check if any handles returned */ + if(Count > 0) + { + /* Iterate through all given handles */ + for(Index = 0; Index < Count; Index++) + { + /* Try to open protocol */ + Status = SystemTable->BootServices->OpenProtocol(Handles[Index], &Guid, (PVOID *)&LoaderProtocol, + ImageHandle, NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); + + /* Check if successfully opened the loader protocol */ + if(Status == STATUS_EFI_SUCCESS) + { + /* Protocol found */ + break; + } + } + } + + /* Free handles */ + SystemTable->BootServices->FreePool(Handles); + + /* Make sure the loaded protocol has been found */ + if(LoaderProtocol == NULL) + { + /* Protocol not found */ + return STATUS_EFI_NOT_FOUND; + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} diff --git a/xtldr/modules/dummy/CMakeLists.txt b/xtldr/modules/dummy/CMakeLists.txt new file mode 100644 index 0000000..d2efef7 --- /dev/null +++ b/xtldr/modules/dummy/CMakeLists.txt @@ -0,0 +1,26 @@ +# XT Boot Loader +PROJECT(XTLDR_DUMMY) + +# Specify include directories +include_directories( + ${EXECTOS_SOURCE_DIR}/sdk/xtdk + ${XTLDR_SOURCE_DIR}/includes) + +# Specify list of source code files +list(APPEND XTLDR_DUMMY_SOURCE + ${XTLDR_SOURCE_DIR}/modules/blproto.c + ${XTLDR_DUMMY_SOURCE_DIR}/dummy.c) + +# Link bootloader executable +add_executable(dummy ${XTLDR_DUMMY_SOURCE}) + +# Add linker libraries +target_link_libraries(dummy libxtos) + +# Set proper binary name and install target +set_target_properties(dummy PROPERTIES SUFFIX .efi) +set_install_target(dummy efi/boot/xtldr) + +# Set loader entrypoint, imagebase address, ordinals and subsystem +set_entrypoint(dummy "BlXtLoaderModuleMain") +set_subsystem(dummy efi_boot_service_driver) diff --git a/xtldr/modules/dummy/dummy.c b/xtldr/modules/dummy/dummy.c new file mode 100644 index 0000000..071bb5d --- /dev/null +++ b/xtldr/modules/dummy/dummy.c @@ -0,0 +1,48 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/dummy/dummy.c + * DESCRIPTION: Boot loader dummy module + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/* EFI Image Handle */ +EFI_HANDLE EfiImageHandle; + +/* EFI System Table */ +PEFI_SYSTEM_TABLE EfiSystemTable; + +/* EFI XT Loader Protocol */ +PXT_BOOT_LOADER_PROTOCOL EfiXtLdrProtocol; + +/** + * This routine is the entry point of the XT EFI boot loader module. + * + * @param ImageHandle + * Firmware-allocated handle that identifies the image. + * + * @param SystemTable + * Provides the EFI system table. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +EFI_STATUS +BlXtLoaderModuleMain(EFI_HANDLE ImageHandle, + PEFI_SYSTEM_TABLE SystemTable) +{ + /* Set the system table and image handle */ + EfiImageHandle = ImageHandle; + EfiSystemTable = SystemTable; + + /* Open the XTLDR protocol */ + BlGetXtLoaderProtocol(ImageHandle, SystemTable, &EfiXtLdrProtocol); + + /* Print message and return success */ + EfiXtLdrProtocol->EfiPrint(L"XTLDR dummy module initialized\n"); + return STATUS_EFI_SUCCESS; +} diff --git a/xtldr/xtldr.c b/xtldr/xtldr.c index bf262a2..84100c6 100644 --- a/xtldr/xtldr.c +++ b/xtldr/xtldr.c @@ -18,6 +18,43 @@ PEFI_SYSTEM_TABLE EfiSystemTable; /* Serial port configuration */ CPPORT EfiSerialPort; +/** + * This routine loads XTLDR EFI modules. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +EFI_STATUS +BlLoadEfiModules() +{ + EFI_STATUS Status; + + return STATUS_EFI_SUCCESS; +} + +/** + * This routine registers XTLDR protocol for further usage by modules. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +EFI_STATUS +BlRegisterXtLoaderProtocol() +{ + EFI_GUID Guid = XT_BOOT_LOADER_PROTOCOL_GUID; + XT_BOOT_LOADER_PROTOCOL LoaderProtocol; + EFI_HANDLE Handle; + + /* Set all routines available via loader protocol */ + LoaderProtocol.DbgPrint = BlDbgPrint; + LoaderProtocol.EfiPrint = BlEfiPrint; + + /* Register loader protocol */ + return EfiSystemTable->BootServices->InstallProtocolInterface(&Handle, &Guid, EFI_NATIVE_INTERFACE, &LoaderProtocol); +} + /** * This routine is the entry point of the XT EFI boot loader. * @@ -60,6 +97,20 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, BlDbgPrint(L"WARNING: Failed to disable watchdog timer\n"); } + /* Register loader protocol */ + Status = BlRegisterXtLoaderProtocol(); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to register loader protocol */ + BlDbgPrint(L"ERROR: Failed to register XTLDR loader protocol\n"); + } + + Status = BlLoadEfiModules(); + if(Status != STATUS_EFI_SUCCESS) + { + BlDbgPrint(L"ERROR: Failed to load XTLDR modules\n"); + } + /* Discover and enumerate EFI block devices */ BlEnumerateEfiBlockDevices(); diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 845b5df..2ddaf6b 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -18,7 +18,7 @@ list(APPEND XTOSKRNL_SOURCE set_specfile(xtoskrnl.spec) # Link static XTOS library -add_library(xtoslib ${XTOSKRNL_SOURCE}) +add_library(libxtos ${XTOSKRNL_SOURCE}) # Link kernel executable add_executable(xtoskrnl