diff --git a/xtldr/arch/amd64/memory.cc b/xtldr/arch/amd64/memory.cc index faa4f96..911705a 100644 --- a/xtldr/arch/amd64/memory.cc +++ b/xtldr/arch/amd64/memory.cc @@ -7,7 +7,7 @@ * Aiken Harris */ -#include +#include /** diff --git a/xtldr/arch/i686/memory.cc b/xtldr/arch/i686/memory.cc index 6e46dc2..0a7b4a4 100644 --- a/xtldr/arch/i686/memory.cc +++ b/xtldr/arch/i686/memory.cc @@ -7,7 +7,7 @@ * Aiken Harris */ -#include +#include /** diff --git a/xtldr/bootutil.cc b/xtldr/bootutil.cc index 7f5925e..5100ce3 100644 --- a/xtldr/bootutil.cc +++ b/xtldr/bootutil.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Aiken Harris */ -#include +#include /** diff --git a/xtldr/config.cc b/xtldr/config.cc index f25baed..4f82d7a 100644 --- a/xtldr/config.cc +++ b/xtldr/config.cc @@ -7,7 +7,7 @@ * Aiken Harris */ -#include +#include /** diff --git a/xtldr/console.cc b/xtldr/console.cc index 049d0c2..b5ef268 100644 --- a/xtldr/console.cc +++ b/xtldr/console.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/debug.cc b/xtldr/debug.cc index b935986..d48bb4e 100644 --- a/xtldr/debug.cc +++ b/xtldr/debug.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/efiutils.cc b/xtldr/efiutils.cc index 15e2fda..38755de 100644 --- a/xtldr/efiutils.cc +++ b/xtldr/efiutils.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/globals.cc b/xtldr/globals.cc index 218497c..82aefa7 100644 --- a/xtldr/globals.cc +++ b/xtldr/globals.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /* XT Boot Loader registered boot protocol list */ diff --git a/xtldr/hardware.cc b/xtldr/hardware.cc index 580c30e..23294de 100644 --- a/xtldr/hardware.cc +++ b/xtldr/hardware.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/includes/globals.h b/xtldr/includes/globals.hh similarity index 89% rename from xtldr/includes/globals.h rename to xtldr/includes/globals.hh index a142ca5..d40360a 100644 --- a/xtldr/includes/globals.h +++ b/xtldr/includes/globals.hh @@ -1,13 +1,13 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/includes/globals.h + * FILE: xtldr/includes/globals.hh * DESCRIPTION: XTLDR global variables * DEVELOPERS: Rafal Kupiec */ -#ifndef __XTLDR_GLOBALS_H -#define __XTLDR_GLOBALS_H +#ifndef __XTLDR_GLOBALS_HH +#define __XTLDR_GLOBALS_HH #include @@ -48,4 +48,4 @@ EXTERN EFI_HANDLE EfiImageHandle; /* EFI System Table */ EXTERN PEFI_SYSTEM_TABLE EfiSystemTable; -#endif /* __XTLDR_GLOBALS_H */ +#endif /* __XTLDR_GLOBALS_HH */ diff --git a/xtldr/includes/xtldr.h b/xtldr/includes/xtldr.hh similarity index 98% rename from xtldr/includes/xtldr.h rename to xtldr/includes/xtldr.hh index 390d3e5..7e38d66 100644 --- a/xtldr/includes/xtldr.h +++ b/xtldr/includes/xtldr.hh @@ -1,18 +1,18 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/includes/xtldr.h + * FILE: xtldr/includes/xtldr.hh * DESCRIPTION: Top level header for XTLDR * DEVELOPERS: Rafal Kupiec */ -#ifndef __XTLDR_XTLDR_H -#define __XTLDR_XTLDR_H +#ifndef __XTLDR_XTLDR_HH +#define __XTLDR_XTLDR_HH #include #include -#include +#include /* XTLDR routines forward references */ @@ -579,4 +579,4 @@ XTCDECL VOID BlpUpdateConfiguration(IN PLIST_ENTRY NewConfig); -#endif /* __XTLDR_XTLDR_H */ +#endif /* __XTLDR_XTLDR_HH */ diff --git a/xtldr/library/modproto.cc b/xtldr/library/modproto.cc index 11bdd4e..3a0f3f2 100644 --- a/xtldr/library/modproto.cc +++ b/xtldr/library/modproto.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/memory.cc b/xtldr/memory.cc index 35c46a0..d9fdeb3 100644 --- a/xtldr/memory.cc +++ b/xtldr/memory.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/modules/acpi/CMakeLists.txt b/xtldr/modules/acpi/CMakeLists.txt index d94f9d0..d726689 100644 --- a/xtldr/modules/acpi/CMakeLists.txt +++ b/xtldr/modules/acpi/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( # Specify list of source code files list(APPEND XTLDR_ACPI_SOURCE - ${XTLDR_ACPI_SOURCE_DIR}/acpi.c - ${XTLDR_ACPI_SOURCE_DIR}/globals.c) + ${XTLDR_ACPI_SOURCE_DIR}/acpi.cc + ${XTLDR_ACPI_SOURCE_DIR}/data.cc) # Link module executable add_executable(acpi ${XTLDR_ACPI_SOURCE}) diff --git a/xtldr/modules/acpi/acpi.c b/xtldr/modules/acpi/acpi.cc similarity index 81% rename from xtldr/modules/acpi/acpi.c rename to xtldr/modules/acpi/acpi.cc index f6c5175..07d78c6 100644 --- a/xtldr/modules/acpi/acpi.c +++ b/xtldr/modules/acpi/acpi.cc @@ -1,20 +1,21 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/acpi/acpi.c + * FILE: xtldr/modules/acpi/acpi.cc * DESCRIPTION: XTLDR ACPI Support Module * DEVELOPERS: Rafal Kupiec */ -#include +#include -/* Dummy module information */ +/* ACPI module information */ MODULE_AUTHOR(L"Rafal Kupiec "); MODULE_DESCRIPTION(L"ACPI support"); MODULE_LICENSE(L"GPLv3"); MODULE_VERSION(L"0.1"); + /** * Attempts to get XSDP. If it is not found or checksum mismatch, it will try to get RSDP instead. * @@ -27,12 +28,12 @@ MODULE_VERSION(L"0.1"); */ XTCDECL EFI_STATUS -AcGetAcpiDescriptionPointer(OUT PVOID *AcpiTable) +Acpi::GetAcpiDescriptionPointer(OUT PVOID *AcpiTable) { PVOID Rsdp; /* Try to get XSDP (ACPI 2.0) from system configuration tables */ - if(AcGetXsdpTable(&Rsdp) == STATUS_EFI_SUCCESS) + if(GetXsdpTable(&Rsdp) == STATUS_EFI_SUCCESS) { /* XSDP found, return success */ *AcpiTable = Rsdp; @@ -40,7 +41,7 @@ AcGetAcpiDescriptionPointer(OUT PVOID *AcpiTable) } /* Try to get RSDP (ACPI 1.0) from system configuration tables */ - if(AcGetRsdpTable(&Rsdp) == STATUS_EFI_SUCCESS) + if(GetRsdpTable(&Rsdp) == STATUS_EFI_SUCCESS) { /* RSDP found, return success */ *AcpiTable = Rsdp; @@ -69,9 +70,9 @@ AcGetAcpiDescriptionPointer(OUT PVOID *AcpiTable) */ XTCDECL EFI_STATUS -AcGetAcpiTable(IN CONST UINT Signature, - IN PVOID PreviousTable, - OUT PVOID *AcpiTable) +Acpi::GetAcpiTable(IN CONST UINT Signature, + IN PVOID PreviousTable, + OUT PVOID *AcpiTable) { PACPI_DESCRIPTION_HEADER TableHeader; SIZE_T RsdtIndex, TableIndex; @@ -85,7 +86,7 @@ AcGetAcpiTable(IN CONST UINT Signature, *AcpiTable = NULLPTR; /* Get Root System Description Table Pointer */ - Status = AcGetAcpiDescriptionPointer((PVOID)&Rsdp); + Status = GetAcpiDescriptionPointer((PVOID*)&Rsdp); if(Status != STATUS_EFI_SUCCESS) { /* ACPI tables not found, return error */ @@ -159,7 +160,7 @@ AcGetAcpiTable(IN CONST UINT Signature, if(TableHeader->Signature != ACPI_FADT_SIGNATURE || TableHeader->Revision > 2) { /* Validate table checksum */ - if(!AcpValidateAcpiTable(TableHeader, TableHeader->Length)) + if(!ValidateAcpiTable(TableHeader, TableHeader->Length)) { /* Checksum mismatch, return error */ return STATUS_EFI_CRC_ERROR; @@ -183,7 +184,7 @@ AcGetAcpiTable(IN CONST UINT Signature, */ XTCDECL EFI_STATUS -AcGetApicBase(OUT PVOID *ApicBase) +Acpi::GetApicBase(OUT PVOID *ApicBase) { CPUID_REGISTERS CpuRegisters; @@ -220,7 +221,7 @@ AcGetApicBase(OUT PVOID *ApicBase) */ XTCDECL EFI_STATUS -AcGetRsdpTable(OUT PVOID *AcpiTable) +Acpi::GetRsdpTable(OUT PVOID *AcpiTable) { EFI_GUID AcpiGuid = EFI_CONFIG_TABLE_ACPI_TABLE_GUID; EFI_STATUS Status; @@ -228,7 +229,7 @@ AcGetRsdpTable(OUT PVOID *AcpiTable) /* Get RSDP (ACPI 1.0) table from system configuration tables */ Status = XtLdrProtocol->Util.GetConfigurationTable(&AcpiGuid, &RsdpTable); - if(Status != STATUS_EFI_SUCCESS || !AcpValidateAcpiTable(RsdpTable, 20)) + if(Status != STATUS_EFI_SUCCESS || !ValidateAcpiTable(RsdpTable, 20)) { /* RSDP not found or checksum mismatch */ *AcpiTable = NULLPTR; @@ -252,15 +253,15 @@ AcGetRsdpTable(OUT PVOID *AcpiTable) */ XTCDECL EFI_STATUS -AcGetSMBiosTable(OUT PVOID *SmBiosTable) +Acpi::GetSMBiosTable(OUT PVOID *SmBiosTable) { EFI_GUID SmBiosGuid = EFI_CONFIG_TABLE_SMBIOS_TABLE_GUID; PSMBIOS_TABLE_HEADER SmBios; EFI_STATUS Status; /* Get SMBIOS table from system configuration tables */ - Status = XtLdrProtocol->Util.GetConfigurationTable(&SmBiosGuid, (PVOID)&SmBios); - if(Status != STATUS_EFI_SUCCESS || !AcpValidateAcpiTable(SmBios, SmBios->Length)) + Status = XtLdrProtocol->Util.GetConfigurationTable(&SmBiosGuid, (PVOID*)&SmBios); + if(Status != STATUS_EFI_SUCCESS || !ValidateAcpiTable(SmBios, SmBios->Length)) { /* SMBIOS not found or checksum mismatch */ *SmBiosTable = NULLPTR; @@ -284,15 +285,15 @@ AcGetSMBiosTable(OUT PVOID *SmBiosTable) */ XTCDECL EFI_STATUS -AcGetSMBios3Table(OUT PVOID *SmBiosTable) +Acpi::GetSMBios3Table(OUT PVOID *SmBiosTable) { EFI_GUID SmBios3Guid = EFI_CONFIG_TABLE_SMBIOS3_TABLE_GUID; PSMBIOS3_TABLE_HEADER SmBios; EFI_STATUS Status; /* Get SMBIOS3 table from system configuration tables */ - Status = XtLdrProtocol->Util.GetConfigurationTable(&SmBios3Guid, (PVOID)&SmBios); - if(Status != STATUS_EFI_SUCCESS || !AcpValidateAcpiTable(SmBios, SmBios->Length)) + Status = XtLdrProtocol->Util.GetConfigurationTable(&SmBios3Guid, (PVOID*)&SmBios); + if(Status != STATUS_EFI_SUCCESS || !ValidateAcpiTable(SmBios, SmBios->Length)) { /* SMBIOS3 not found or checksum mismatch */ *SmBiosTable = NULLPTR; @@ -316,7 +317,7 @@ AcGetSMBios3Table(OUT PVOID *SmBiosTable) */ XTCDECL EFI_STATUS -AcGetXsdpTable(OUT PVOID *AcpiTable) +Acpi::GetXsdpTable(OUT PVOID *AcpiTable) { EFI_GUID AcpiGuid = EFI_CONFIG_TABLE_ACPI20_TABLE_GUID; EFI_STATUS Status; @@ -324,7 +325,7 @@ AcGetXsdpTable(OUT PVOID *AcpiTable) /* Get XSDP (ACPI 2.0) from system configuration tables */ Status = XtLdrProtocol->Util.GetConfigurationTable(&AcpiGuid, &XsdpTable); - if(Status != STATUS_EFI_SUCCESS || !AcpValidateAcpiTable(XsdpTable, 36)) + if(Status != STATUS_EFI_SUCCESS || !ValidateAcpiTable(XsdpTable, 36)) { /* XSDP not found or checksum mismatch */ *AcpiTable = NULLPTR; @@ -336,6 +337,48 @@ AcGetXsdpTable(OUT PVOID *AcpiTable) return STATUS_EFI_SUCCESS; } +/** + * Initializes ACPI module by opening XTLDR protocol and installing ACPI protocol. + * + * @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 + */ +XTCDECL +EFI_STATUS +Acpi::InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable) +{ + EFI_GUID Guid = XT_ACPI_PROTOCOL_GUID; + EFI_STATUS Status; + + /* Open the XTLDR protocol */ + Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open the protocol, return error */ + return STATUS_EFI_PROTOCOL_ERROR; + } + + /* Set routines available via ACPI protocol */ + AcpiProtocol.GetAcpiDescriptionPointer = GetAcpiDescriptionPointer; + AcpiProtocol.GetAcpiTable = GetAcpiTable; + AcpiProtocol.GetApicBase = GetApicBase; + AcpiProtocol.GetRsdpTable = GetRsdpTable; + AcpiProtocol.GetSMBiosTable = GetSMBiosTable; + AcpiProtocol.GetSMBios3Table = GetSMBios3Table; + AcpiProtocol.GetXsdpTable = GetXsdpTable; + + /* Install ACPI protocol */ + return XtLdrProtocol->Protocol.Install(&AcpiProtocol, &Guid); +} + /** * Validates given ACPI table by calculating its checksum. * @@ -351,15 +394,15 @@ AcGetXsdpTable(OUT PVOID *AcpiTable) */ XTCDECL BOOLEAN -AcpValidateAcpiTable(IN PVOID Buffer, - IN UINT_PTR Size) +Acpi::ValidateAcpiTable(IN PVOID Buffer, + IN UINT_PTR Size) { PUCHAR Pointer; UCHAR Sum; /* Initialize variables */ Sum = 0; - Pointer = Buffer; + Pointer = (PUCHAR)Buffer; /* Calculate checksum of given table */ while(Size != 0) @@ -391,26 +434,6 @@ EFI_STATUS XtLdrModuleMain(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { - EFI_GUID Guid = XT_ACPI_PROTOCOL_GUID; - EFI_STATUS Status; - - /* Open the XTLDR protocol */ - Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open the protocol, return error */ - return STATUS_EFI_PROTOCOL_ERROR; - } - - /* Set routines available via ACPI protocol */ - AcpAcpiProtocol.GetAcpiDescriptionPointer = AcGetAcpiDescriptionPointer; - AcpAcpiProtocol.GetAcpiTable = AcGetAcpiTable; - AcpAcpiProtocol.GetApicBase = AcGetApicBase; - AcpAcpiProtocol.GetRsdpTable = AcGetRsdpTable; - AcpAcpiProtocol.GetSMBiosTable = AcGetSMBiosTable; - AcpAcpiProtocol.GetSMBios3Table = AcGetSMBios3Table; - AcpAcpiProtocol.GetXsdpTable = AcGetXsdpTable; - - /* Install ACPI protocol */ - return XtLdrProtocol->Protocol.Install(&AcpAcpiProtocol, &Guid); + /* Initialize ACPI module */ + return Acpi::InitializeModule(ImageHandle, SystemTable); } diff --git a/xtldr/modules/acpi/globals.c b/xtldr/modules/acpi/data.cc similarity index 51% rename from xtldr/modules/acpi/globals.c rename to xtldr/modules/acpi/data.cc index abfd269..f5fef75 100644 --- a/xtldr/modules/acpi/globals.c +++ b/xtldr/modules/acpi/data.cc @@ -1,16 +1,16 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/acpi/globals.c - * DESCRIPTION: ACPI module global variables + * FILE: xtldr/modules/acpi/data.cc + * DESCRIPTION: ACPI module global and static data * DEVELOPERS: Rafal Kupiec */ -#include +#include /* ACPI Protocol */ -XTBL_ACPI_PROTOCOL AcpAcpiProtocol; +XTBL_ACPI_PROTOCOL Acpi::AcpiProtocol; /* XTLDR protocol handler */ -PXTBL_LOADER_PROTOCOL XtLdrProtocol; +PXTBL_LOADER_PROTOCOL Acpi::XtLdrProtocol; diff --git a/xtldr/modules/acpi/includes/acpi.h b/xtldr/modules/acpi/includes/acpi.h deleted file mode 100644 index f7b8ee0..0000000 --- a/xtldr/modules/acpi/includes/acpi.h +++ /dev/null @@ -1,57 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/acpi/includes/acpi.h - * DESCRIPTION: XTLDR ACPI module header file - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_ACPI_ACPI_H -#define __XTLDR_ACPI_ACPI_H - -#include -#include - - -/* ACPI module routines forward references */ -XTCDECL -EFI_STATUS -AcGetAcpiDescriptionPointer(OUT PVOID *AcpiTable); - -XTCDECL -EFI_STATUS -AcGetAcpiTable(IN CONST UINT Signature, - IN PVOID PreviousTable, - OUT PVOID *AcpiTable); - -XTCDECL -EFI_STATUS -AcGetApicBase(OUT PVOID *ApicBase); - -XTCDECL -EFI_STATUS -AcGetRsdpTable(OUT PVOID *AcpiTable); - -XTCDECL -EFI_STATUS -AcGetSMBiosTable(OUT PVOID *SmBiosTable); - -XTCDECL -EFI_STATUS -AcGetSMBios3Table(OUT PVOID *SmBiosTable); - -XTCDECL -EFI_STATUS -AcGetXsdpTable(OUT PVOID *AcpiTable); - -XTCDECL -BOOLEAN -AcpValidateAcpiTable(IN PVOID Buffer, - IN UINT_PTR Size); - -XTCDECL -EFI_STATUS -XtLdrModuleMain(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); - -#endif/* __XTLDR_ACPI_ACPI_H */ diff --git a/xtldr/modules/acpi/includes/acpi.hh b/xtldr/modules/acpi/includes/acpi.hh new file mode 100644 index 0000000..80a2e6f --- /dev/null +++ b/xtldr/modules/acpi/includes/acpi.hh @@ -0,0 +1,40 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/acpi/includes/acpi.hh + * DESCRIPTION: XTLDR ACPI module header file + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_ACPI_ACPI_HH +#define __XTLDR_ACPI_ACPI_HH + +#include + + +/* ACPI module for XTLDR */ +class Acpi +{ + private: + STATIC XTBL_ACPI_PROTOCOL AcpiProtocol; + STATIC PXTBL_LOADER_PROTOCOL XtLdrProtocol; + + public: + STATIC XTCDECL EFI_STATUS GetAcpiDescriptionPointer(OUT PVOID *AcpiTable); + STATIC XTCDECL EFI_STATUS GetAcpiTable(IN CONST UINT Signature, + IN PVOID PreviousTable, + OUT PVOID *AcpiTable); + STATIC XTCDECL EFI_STATUS GetApicBase(OUT PVOID *ApicBase); + STATIC XTCDECL EFI_STATUS GetRsdpTable(OUT PVOID *AcpiTable); + STATIC XTCDECL EFI_STATUS GetSMBiosTable(OUT PVOID *SmBiosTable); + STATIC XTCDECL EFI_STATUS GetSMBios3Table(OUT PVOID *SmBiosTable); + STATIC XTCDECL EFI_STATUS GetXsdpTable(OUT PVOID *AcpiTable); + STATIC XTCDECL EFI_STATUS InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); + + private: + STATIC XTCDECL BOOLEAN ValidateAcpiTable(IN PVOID Buffer, + IN UINT_PTR Size); +}; + +#endif /* __XTLDR_ACPI_ACPI_HH */ diff --git a/xtldr/modules/acpi/includes/globals.h b/xtldr/modules/acpi/includes/globals.h deleted file mode 100644 index 417b26d..0000000 --- a/xtldr/modules/acpi/includes/globals.h +++ /dev/null @@ -1,21 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/acpi/includes/globals.h - * DESCRIPTION: ACPI Module global variables - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_ACPI_GLOBALS_H -#define __XTLDR_ACPI_GLOBALS_H - -#include - - -/* ACPI Protocol */ -EXTERN XTBL_ACPI_PROTOCOL AcpAcpiProtocol; - -/* XTLDR protocol handler */ -EXTERN PXTBL_LOADER_PROTOCOL XtLdrProtocol; - -#endif/* __XTLDR_ACPI_GLOBALS_H */ diff --git a/xtldr/modules/beep/CMakeLists.txt b/xtldr/modules/beep/CMakeLists.txt index 1c132bd..5cd4140 100644 --- a/xtldr/modules/beep/CMakeLists.txt +++ b/xtldr/modules/beep/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( # Specify list of source code files list(APPEND XTLDR_BEEP_SOURCE - ${XTLDR_BEEP_SOURCE_DIR}/beep.c - ${XTLDR_BEEP_SOURCE_DIR}/globals.c) + ${XTLDR_BEEP_SOURCE_DIR}/beep.cc + ${XTLDR_BEEP_SOURCE_DIR}/data.cc) # Link module executable add_executable(beep ${XTLDR_BEEP_SOURCE}) diff --git a/xtldr/modules/beep/beep.c b/xtldr/modules/beep/beep.cc similarity index 81% rename from xtldr/modules/beep/beep.c rename to xtldr/modules/beep/beep.cc index b4efe65..5938c48 100644 --- a/xtldr/modules/beep/beep.c +++ b/xtldr/modules/beep/beep.cc @@ -1,12 +1,12 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/beep/beep.c + * FILE: xtldr/modules/beep/beep.cc * DESCRIPTION: XTLDR Beep Module * DEVELOPERS: Rafal Kupiec */ -#include +#include /* Beep module information */ @@ -15,6 +15,7 @@ MODULE_DESCRIPTION(L"Plays a GRUB compatible tune via PC speaker"); MODULE_LICENSE(L"GPLv3"); MODULE_VERSION(L"0.1"); + /** * Disables the PC speaker. * @@ -24,7 +25,7 @@ MODULE_VERSION(L"0.1"); */ XTCDECL VOID -BpDisableToneBeep() +Beep::DisableToneBeep() { UCHAR Status; @@ -45,7 +46,7 @@ BpDisableToneBeep() */ XTCDECL VOID -BpEnableToneBeep(IN UINT Pitch) +Beep::EnableToneBeep(IN UINT Pitch) { UINT Counter; UCHAR Status; @@ -72,6 +73,43 @@ BpEnableToneBeep(IN UINT Pitch) HlWritePort8(0x61, Status | 0x03); } +/** + * Initializes BEEP module by opening XTLDR protocol and playing the tune. + * + * @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 + */ +XTCDECL +EFI_STATUS +Beep::InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable) +{ + EFI_STATUS Status; + PWCHAR Tune; + + /* Open the XTLDR protocol */ + Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open the protocol, return error */ + return STATUS_EFI_PROTOCOL_ERROR; + } + + /* Play the tune set in the configuration */ + XtLdrProtocol->Config.GetValue(L"TUNE", &Tune); + PlayTune(Tune); + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + /** * This routine plays a tune. * @@ -84,7 +122,7 @@ BpEnableToneBeep(IN UINT Pitch) */ XTCDECL VOID -BpPlayTune(IN PWCHAR Arguments) +Beep::PlayTune(IN PWCHAR Arguments) { LONG Pitch, Duration, Tempo; PWCHAR Argument, LastArgument; @@ -104,28 +142,28 @@ BpPlayTune(IN PWCHAR Arguments) if(Tempo < 0) { /* Set the tempo */ - Tempo = BpWideStringToNumber(Argument); + Tempo = WideStringToNumber(Argument); } else if(Pitch < 0) { /* Set the pitch */ - Pitch = BpWideStringToNumber(Argument); + Pitch = WideStringToNumber(Argument); } else { /* Set the duration */ - Duration = BpWideStringToNumber(Argument); + Duration = WideStringToNumber(Argument); /* Check pitch value */ if(Pitch > 0) { /* Emit the beep tone */ - BpEnableToneBeep(Pitch); + EnableToneBeep(Pitch); } else { /* Stop emitting beep tone */ - BpDisableToneBeep(); + DisableToneBeep(); } /* Wait for duration time */ @@ -141,7 +179,7 @@ BpPlayTune(IN PWCHAR Arguments) } /* Stop emitting beep tone */ - BpDisableToneBeep(); + DisableToneBeep(); } /** @@ -156,7 +194,7 @@ BpPlayTune(IN PWCHAR Arguments) */ XTCDECL UINT -BpWideStringToNumber(IN PWCHAR String) +Beep::WideStringToNumber(IN PWCHAR String) { ULONG Number = 0; @@ -195,21 +233,6 @@ EFI_STATUS XtLdrModuleMain(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { - EFI_STATUS Status; - PWCHAR Tune; - - /* Open the XTLDR protocol */ - Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open the protocol, return error */ - return STATUS_EFI_PROTOCOL_ERROR; - } - - /* Play the tune set in the configuration */ - XtLdrProtocol->Config.GetValue(L"TUNE", &Tune); - BpPlayTune(Tune); - - /* Return success */ - return STATUS_EFI_SUCCESS; + /* Initialize BEEP module */ + return Beep::InitializeModule(ImageHandle, SystemTable); } diff --git a/xtldr/modules/beep/globals.c b/xtldr/modules/beep/data.cc similarity index 54% rename from xtldr/modules/beep/globals.c rename to xtldr/modules/beep/data.cc index 1c58f21..92e95f5 100644 --- a/xtldr/modules/beep/globals.c +++ b/xtldr/modules/beep/data.cc @@ -1,13 +1,13 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/beep/globals.c - * DESCRIPTION: Beep module global variables + * FILE: xtldr/modules/beep/data.cc + * DESCRIPTION: BEEP module global and static data * DEVELOPERS: Rafal Kupiec */ -#include +#include /* XTLDR protocol handler */ -PXTBL_LOADER_PROTOCOL XtLdrProtocol; +PXTBL_LOADER_PROTOCOL Beep::XtLdrProtocol; diff --git a/xtldr/modules/beep/includes/beep.h b/xtldr/modules/beep/includes/beep.h deleted file mode 100644 index db0af8f..0000000 --- a/xtldr/modules/beep/includes/beep.h +++ /dev/null @@ -1,38 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/beep/includes/beep.h - * DESCRIPTION: XTLDR Beep Module header file - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_BEEP_BEEP_H -#define __XTLDR_BEEP_BEEP_H - -#include -#include - - -/* Beep module routines forward references */ -XTCDECL -VOID -BpDisableToneBeep(); - -XTCDECL -VOID -BpEnableToneBeep(IN UINT Pitch); - -XTCDECL -VOID -BpPlayTune(IN PWCHAR Arguments); - -XTCDECL -UINT -BpWideStringToNumber(IN PWCHAR String); - -XTCDECL -EFI_STATUS -XtLdrModuleMain(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); - -#endif/* __XTLDR_BEEP_BEEP_H */ diff --git a/xtldr/modules/beep/includes/beep.hh b/xtldr/modules/beep/includes/beep.hh new file mode 100644 index 0000000..9b613d1 --- /dev/null +++ b/xtldr/modules/beep/includes/beep.hh @@ -0,0 +1,32 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/beep/includes/beep.hh + * DESCRIPTION: XTLDR Beep Module header file + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_BEEP_BEEP_HH +#define __XTLDR_BEEP_BEEP_HH + +#include + + +/* BEEP module for XTLDR */ +class Beep +{ + private: + STATIC PXTBL_LOADER_PROTOCOL XtLdrProtocol; + + public: + STATIC XTCDECL EFI_STATUS InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); + STATIC XTCDECL VOID PlayTune(IN PWCHAR Arguments); + + private: + STATIC XTCDECL VOID DisableToneBeep(); + STATIC XTCDECL VOID EnableToneBeep(IN UINT Pitch); + STATIC XTCDECL UINT WideStringToNumber(IN PWCHAR String); +}; + +#endif /* __XTLDR_BEEP_BEEP_HH */ diff --git a/xtldr/modules/beep/includes/globals.h b/xtldr/modules/beep/includes/globals.h deleted file mode 100644 index abd4894..0000000 --- a/xtldr/modules/beep/includes/globals.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/beep/includes/globals.h - * DESCRIPTION: XTLDR Beep Module global variables - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_BEEP_GLOBALS_H -#define __XTLDR_BEEP_GLOBALS_H - -#include - - -/* XTLDR protocol handler */ -EXTERN PXTBL_LOADER_PROTOCOL XtLdrProtocol; - -#endif/* __XTLDR_BEEP_GLOBALS_H */ diff --git a/xtldr/modules/chainldr/CMakeLists.txt b/xtldr/modules/chainldr/CMakeLists.txt index 5fcb8de..63cbdb9 100644 --- a/xtldr/modules/chainldr/CMakeLists.txt +++ b/xtldr/modules/chainldr/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( # Specify list of source code files list(APPEND XTLDR_CHAINLDR_SOURCE - ${XTLDR_CHAINLDR_SOURCE_DIR}/chainldr.c - ${XTLDR_CHAINLDR_SOURCE_DIR}/globals.c) + ${XTLDR_CHAINLDR_SOURCE_DIR}/chainldr.cc + ${XTLDR_CHAINLDR_SOURCE_DIR}/data.cc) # Link module executable add_executable(chainldr ${XTLDR_CHAINLDR_SOURCE}) diff --git a/xtldr/modules/chainldr/chainldr.c b/xtldr/modules/chainldr/chainldr.cc similarity index 85% rename from xtldr/modules/chainldr/chainldr.c rename to xtldr/modules/chainldr/chainldr.cc index 3d28dbd..17da6cf 100644 --- a/xtldr/modules/chainldr/chainldr.c +++ b/xtldr/modules/chainldr/chainldr.cc @@ -1,12 +1,12 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/chainldr/chainldr.c + * FILE: xtldr/modules/chainldr/chainldr.cc * DESCRIPTION: XTLDR Chain Loader * DEVELOPERS: Rafal Kupiec */ -#include +#include /* ChainLoader module information */ @@ -15,6 +15,7 @@ MODULE_DESCRIPTION(L"XTLDR Chain Loader"); MODULE_LICENSE(L"GPLv3"); MODULE_VERSION(L"0.1"); + /** * Chainloads another boot loader. * @@ -27,7 +28,7 @@ MODULE_VERSION(L"0.1"); */ XTCDECL EFI_STATUS -ChBootSystem(IN PXTBL_BOOT_PARAMETERS Parameters) +ChainLoader::BootSystem(IN PXTBL_BOOT_PARAMETERS Parameters) { EFI_GUID LIPGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID; EFI_MEMMAP_DEVICE_PATH MemoryDevicePath[2]; @@ -66,14 +67,14 @@ ChBootSystem(IN PXTBL_BOOT_PARAMETERS Parameters) XtLdrProtocol->Debug.Print(L"ERROR: Unable to open system boot directory (Status Code: 0x%zX)\n", Status); /* Close volume and return error code */ - XtLdrProtocol->Disk.CloseVolume(DiskHandle); + XtLdrProtocol->Disk.CloseVolume(&DiskHandle); return Status; } /* Read EFI image file from disk and close both directory and EFI volume */ Status = XtLdrProtocol->Disk.ReadFile(BootDir, Parameters->KernelFile, &LoaderData, &LoaderSize); BootDir->Close(BootDir); - XtLdrProtocol->Disk.CloseVolume(DiskHandle); + XtLdrProtocol->Disk.CloseVolume(&DiskHandle); /* Setup device path for EFI image */ MemoryDevicePath[0].Header.Length[0] = sizeof(EFI_MEMMAP_DEVICE_PATH); @@ -123,6 +124,45 @@ ChBootSystem(IN PXTBL_BOOT_PARAMETERS Parameters) return XtLdrProtocol->Util.StartEfiImage(LoaderHandle); } +/** + * Initializes CHAINLDR module by opening XTLDR protocol and installing CHAINLOADER protocol. + * + * @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 + */ +XTCDECL +EFI_STATUS +ChainLoader::InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable) +{ + EFI_GUID Guid = XT_CHAIN_BOOT_PROTOCOL_GUID; + EFI_STATUS Status; + + /* Open the XTLDR protocol */ + Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open the protocol, return error */ + return STATUS_EFI_PROTOCOL_ERROR; + } + + /* Set routines available via ChainLoader boot protocol */ + BootProtocol.BootSystem = BootSystem; + + /* Register XTOS boot protocol */ + XtLdrProtocol->Boot.RegisterProtocol(L"CHAINLOADER", &Guid); + + /* Install XTOS protocol */ + return XtLdrProtocol->Protocol.Install(&BootProtocol, &Guid); +} + /** * This routine is the entry point of the XT EFI boot loader module. * @@ -141,23 +181,6 @@ EFI_STATUS XtLdrModuleMain(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { - EFI_GUID Guid = XT_CHAIN_BOOT_PROTOCOL_GUID; - EFI_STATUS Status; - - /* Open the XTLDR protocol */ - Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open the protocol, return error */ - return STATUS_EFI_PROTOCOL_ERROR; - } - - /* Set routines available via ChainLoader boot protocol */ - ChpBootProtocol.BootSystem = ChBootSystem; - - /* Register XTOS boot protocol */ - XtLdrProtocol->Boot.RegisterProtocol(L"CHAINLOADER", &Guid); - - /* Install XTOS protocol */ - return XtLdrProtocol->Protocol.Install(&ChpBootProtocol, &Guid); + /* Initialize CHAINLDR module */ + return ChainLoader::InitializeModule(ImageHandle, SystemTable); } diff --git a/xtldr/modules/chainldr/globals.c b/xtldr/modules/chainldr/data.cc similarity index 50% rename from xtldr/modules/chainldr/globals.c rename to xtldr/modules/chainldr/data.cc index 7380ca3..7b89e80 100644 --- a/xtldr/modules/chainldr/globals.c +++ b/xtldr/modules/chainldr/data.cc @@ -1,16 +1,16 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/chainldr/globals.c - * DESCRIPTION: XTLDR Chain Loader global variables + * FILE: xtldr/modules/chainldr/data.cc + * DESCRIPTION: CHAINLDR module global and static data * DEVELOPERS: Rafal Kupiec */ -#include +#include /* ChainLoader Boot Protocol */ -XTBL_BOOT_PROTOCOL ChpBootProtocol; +XTBL_BOOT_PROTOCOL ChainLoader::BootProtocol; /* XTLDR protocol handler */ -PXTBL_LOADER_PROTOCOL XtLdrProtocol; +PXTBL_LOADER_PROTOCOL ChainLoader::XtLdrProtocol; diff --git a/xtldr/modules/chainldr/includes/chainldr.h b/xtldr/modules/chainldr/includes/chainldr.h deleted file mode 100644 index 9fdf0c5..0000000 --- a/xtldr/modules/chainldr/includes/chainldr.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/chainldr/includes/chainldr.h - * DESCRIPTION: XTLDR Chain Loader header file - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_CHAINLDR_CHAINLDR_H -#define __XTLDR_CHAINLDR_CHAINLDR_H - -#include -#include - - -/* ChainLoader module routines forward references */ -XTCDECL -EFI_STATUS -ChBootSystem(IN PXTBL_BOOT_PARAMETERS Parameters); - -XTCDECL -EFI_STATUS -XtLdrModuleMain(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); - -#endif/* __XTLDR_CHAINLDR_CHAINLDR_H */ diff --git a/xtldr/modules/chainldr/includes/chainldr.hh b/xtldr/modules/chainldr/includes/chainldr.hh new file mode 100644 index 0000000..b0e18b0 --- /dev/null +++ b/xtldr/modules/chainldr/includes/chainldr.hh @@ -0,0 +1,28 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/chainldr/includes/chainldr.hh + * DESCRIPTION: XTLDR Chain Loader header file + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_CHAINLDR_CHAINLDR_HH +#define __XTLDR_CHAINLDR_CHAINLDR_HH + +#include + + +/* CHAINLDR module for XTLDR */ +class ChainLoader +{ + private: + STATIC XTBL_BOOT_PROTOCOL BootProtocol; + STATIC PXTBL_LOADER_PROTOCOL XtLdrProtocol; + + public: + STATIC XTCDECL EFI_STATUS BootSystem(IN PXTBL_BOOT_PARAMETERS Parameters); + STATIC XTCDECL EFI_STATUS InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); +}; + +#endif /* __XTLDR_CHAINLDR_CHAINLDR_HH */ diff --git a/xtldr/modules/chainldr/includes/globals.h b/xtldr/modules/chainldr/includes/globals.h deleted file mode 100644 index 89f379b..0000000 --- a/xtldr/modules/chainldr/includes/globals.h +++ /dev/null @@ -1,21 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/chainldr/includes/globals.h - * DESCRIPTION: XTLDR Chain Loader global variables - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_CHAINLDR_GLOBALS_H -#define __XTLDR_CHAINLDR_GLOBALS_H - -#include - - -/* ChainLoader Boot Protocol */ -EXTERN XTBL_BOOT_PROTOCOL ChpBootProtocol; - -/* XTLDR protocol handler */ -EXTERN PXTBL_LOADER_PROTOCOL XtLdrProtocol; - -#endif/* __XTLDR_CHAINLDR_GLOBALS_H */ diff --git a/xtldr/modules/dummy/CMakeLists.txt b/xtldr/modules/dummy/CMakeLists.txt index 2583b64..8b0e3a1 100644 --- a/xtldr/modules/dummy/CMakeLists.txt +++ b/xtldr/modules/dummy/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( # Specify list of source code files list(APPEND XTLDR_DUMMY_SOURCE - ${XTLDR_DUMMY_SOURCE_DIR}/dummy.c - ${XTLDR_DUMMY_SOURCE_DIR}/globals.c) + ${XTLDR_DUMMY_SOURCE_DIR}/dummy.cc + ${XTLDR_DUMMY_SOURCE_DIR}/data.cc) # Link module executable add_executable(dummy ${XTLDR_DUMMY_SOURCE}) diff --git a/xtldr/modules/dummy/globals.c b/xtldr/modules/dummy/data.cc similarity index 51% rename from xtldr/modules/dummy/globals.c rename to xtldr/modules/dummy/data.cc index 071747c..457a937 100644 --- a/xtldr/modules/dummy/globals.c +++ b/xtldr/modules/dummy/data.cc @@ -1,16 +1,16 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/dummy/globals.c - * DESCRIPTION: Dummy XTLDR module global variables + * FILE: xtldr/modules/dummy/data.cc + * DESCRIPTION: Dummy XTLDR module global and static data * DEVELOPERS: Rafal Kupiec */ -#include +#include -/* XTLDR protocol handler */ -PXTBL_LOADER_PROTOCOL XtLdrProtocol; - /* Dummy Boot Protocol handler */ -XTBL_BOOT_PROTOCOL BlpDummyProtocol; +XTBL_BOOT_PROTOCOL Dummy::DummyProtocol; + +/* XTLDR protocol handler */ +PXTBL_LOADER_PROTOCOL Dummy::XtLdrProtocol; diff --git a/xtldr/modules/dummy/dummy.c b/xtldr/modules/dummy/dummy.cc similarity index 67% rename from xtldr/modules/dummy/dummy.c rename to xtldr/modules/dummy/dummy.cc index fdd0ee1..52a8e83 100644 --- a/xtldr/modules/dummy/dummy.c +++ b/xtldr/modules/dummy/dummy.cc @@ -1,12 +1,12 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/dummy/dummy.c + * FILE: xtldr/modules/dummy/dummy.cc * DESCRIPTION: XTLDR Dummy Module * DEVELOPERS: Rafal Kupiec */ -#include +#include /* Dummy module information */ @@ -15,6 +15,7 @@ MODULE_DESCRIPTION(L"XTLDR Dummy Module"); MODULE_LICENSE(L"GPLv3"); MODULE_VERSION(L"0.1"); + /** * Stub boot routine. * @@ -27,11 +28,50 @@ MODULE_VERSION(L"0.1"); */ XTCDECL EFI_STATUS -DmBootSystem(IN PXTBL_BOOT_PARAMETERS Parameters) +Dummy::BootSystem(IN PXTBL_BOOT_PARAMETERS Parameters) { return STATUS_EFI_SUCCESS; } +/** + * Initializes DUMMY module by opening XTLDR protocol and installing DUMMY protocol. + * + * @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 + */ +XTCDECL +EFI_STATUS +Dummy::InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable) +{ + EFI_GUID DummyGuid = XT_DUMMY_BOOT_PROTOCOL_GUID; + EFI_STATUS Status; + + /* Open the XTLDR protocol */ + Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open the protocol, return error */ + return STATUS_EFI_PROTOCOL_ERROR; + } + + /* Set boot protocol routines */ + DummyProtocol.BootSystem = BootSystem; + + /* Register XTOS boot protocol */ + XtLdrProtocol->Boot.RegisterProtocol(L"DUMMYOS", &DummyGuid); + + /* Register DUMMY protocol as XTOS boot protocol */ + return XtLdrProtocol->Protocol.Install(&DummyProtocol, &DummyGuid); +} + /** * This routine is the entry point of the XT EFI boot loader module. * @@ -50,23 +90,6 @@ EFI_STATUS XtLdrModuleMain(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { - EFI_GUID DummyGuid = XT_DUMMY_BOOT_PROTOCOL_GUID; - EFI_STATUS Status; - - /* Open the XTLDR protocol */ - Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open the protocol, return error */ - return STATUS_EFI_PROTOCOL_ERROR; - } - - /* Set boot protocol routines */ - BlpDummyProtocol.BootSystem = DmBootSystem; - - /* Register XTOS boot protocol */ - XtLdrProtocol->Boot.RegisterProtocol(L"DUMMYOS", &DummyGuid); - - /* Register DUMMY protocol as XTOS boot protocol */ - return XtLdrProtocol->Protocol.Install(&BlpDummyProtocol, &DummyGuid); + /* Initialize DUMMY module */ + return Dummy::InitializeModule(ImageHandle, SystemTable); } diff --git a/xtldr/modules/dummy/includes/dummy.h b/xtldr/modules/dummy/includes/dummy.h deleted file mode 100644 index f027427..0000000 --- a/xtldr/modules/dummy/includes/dummy.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/dummy/includes/dummy.h - * DESCRIPTION: XTLDR Dummy Module header file - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_DUMMY_DUMMY_H -#define __XTLDR_DUMMY_DUMMY_H - -#include -#include - - -/* Dummy module routines forward references */ -XTCDECL -EFI_STATUS -DmBootSystem(IN PXTBL_BOOT_PARAMETERS Parameters); - -XTCDECL -EFI_STATUS -XtLdrModuleMain(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); - -#endif/* __XTLDR_DUMMY_DUMMY_H */ diff --git a/xtldr/modules/dummy/includes/dummy.hh b/xtldr/modules/dummy/includes/dummy.hh new file mode 100644 index 0000000..1dc2891 --- /dev/null +++ b/xtldr/modules/dummy/includes/dummy.hh @@ -0,0 +1,28 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/dummy/includes/dummy.hh + * DESCRIPTION: XTLDR Dummy Module header file + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_DUMMY_DUMMY_HH +#define __XTLDR_DUMMY_DUMMY_HH + +#include + + +/* DUMMY module for XTLDR */ +class Dummy +{ + private: + STATIC XTBL_BOOT_PROTOCOL DummyProtocol; + STATIC PXTBL_LOADER_PROTOCOL XtLdrProtocol; + + public: + STATIC EFI_STATUS BootSystem(IN PXTBL_BOOT_PARAMETERS Parameters); + STATIC EFI_STATUS InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); +}; + +#endif/* __XTLDR_DUMMY_DUMMY_HH */ diff --git a/xtldr/modules/dummy/includes/globals.h b/xtldr/modules/dummy/includes/globals.h deleted file mode 100644 index aab86ed..0000000 --- a/xtldr/modules/dummy/includes/globals.h +++ /dev/null @@ -1,21 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/dummy/includes/globals.h - * DESCRIPTION: XTLDR Dummy Module global variables - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_DUMMY_GLOBALS_H -#define __XTLDR_DUMMY_GLOBALS_H - -#include - - -/* XTLDR protocol handler */ -EXTERN PXTBL_LOADER_PROTOCOL XtLdrProtocol; - -/* Dummy Boot Protocol handler */ -EXTERN XTBL_BOOT_PROTOCOL BlpDummyProtocol; - -#endif/* __XTLDR_DUMMY_GLOBALS_H */ diff --git a/xtldr/modules/framebuf/CMakeLists.txt b/xtldr/modules/framebuf/CMakeLists.txt index 451bd55..f81d21f 100644 --- a/xtldr/modules/framebuf/CMakeLists.txt +++ b/xtldr/modules/framebuf/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( # Specify list of source code files list(APPEND XTLDR_FRAMEBUF_SOURCE - ${XTLDR_FRAMEBUF_SOURCE_DIR}/framebuf.c - ${XTLDR_FRAMEBUF_SOURCE_DIR}/globals.c) + ${XTLDR_FRAMEBUF_SOURCE_DIR}/framebuf.cc + ${XTLDR_FRAMEBUF_SOURCE_DIR}/data.cc) # Link bootloader executable add_executable(framebuf ${XTLDR_FRAMEBUF_SOURCE}) diff --git a/xtldr/modules/framebuf/data.cc b/xtldr/modules/framebuf/data.cc new file mode 100644 index 0000000..6bcc7fb --- /dev/null +++ b/xtldr/modules/framebuf/data.cc @@ -0,0 +1,19 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/framebuf/data.cc + * DESCRIPTION: EFI framebuffer module global and static data + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/* Framebuffer display information */ +XTBL_FRAMEBUFFER_INFORMATION FrameBuffer::DisplayInfo; + +/* Framebuffer protocol handler */ +XTBL_FRAMEBUFFER_PROTOCOL FrameBuffer::FbProtocol; + +/* XTLDR protocol handler */ +PXTBL_LOADER_PROTOCOL FrameBuffer::XtLdrProtocol; diff --git a/xtldr/modules/framebuf/framebuf.c b/xtldr/modules/framebuf/framebuf.cc similarity index 62% rename from xtldr/modules/framebuf/framebuf.c rename to xtldr/modules/framebuf/framebuf.cc index 71abaac..8763636 100644 --- a/xtldr/modules/framebuf/framebuf.c +++ b/xtldr/modules/framebuf/framebuf.cc @@ -1,12 +1,12 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/framebuf/framebuf.c + * FILE: xtldr/modules/framebuf/framebuf.cc * DESCRIPTION: EFI framebuffer support module for XTLDR * DEVELOPERS: Rafal Kupiec */ -#include +#include /* PE/COFF_O module information */ @@ -15,404 +15,6 @@ MODULE_DESCRIPTION(L"EFI FB (FrameBuffer) support"); MODULE_LICENSE(L"GPLv3"); MODULE_VERSION(L"0.2"); -/** - * Provides an EFI Frame Buffer protocol driver name used for initialization. - * - * @param Protocol - * Supplies a pointer to the memory area where framebuffer driver information will be stored. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -FbGetDisplayDriver(OUT PEFI_GRAPHICS_PROTOCOL Protocol) -{ - /* Check if framebuffer is initialized */ - if(!FbpDisplayInfo.Initialized) - { - /* Return error if framebuffer is not initialized */ - return STATUS_EFI_NOT_READY; - } - - /* Copy framebuffer driver information */ - *Protocol = FbpDisplayInfo.Protocol; - - /* Return success */ - return STATUS_EFI_SUCCESS; -} - -/** - * Returns information about EFI Frame Buffer. - * - * @param FbInfo - * Supplies a pointer to the memory area where framebuffer information will be stored. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -FbGetDisplayInformation(OUT PEFI_PHYSICAL_ADDRESS FrameBufferBase, - OUT PULONG_PTR FrameBufferSize, - OUT PXTBL_FRAMEBUFFER_MODE_INFORMATION ModeInfo) -{ - /* Check if framebuffer is initialized */ - if(!FbpDisplayInfo.Initialized) - { - /* Return error if framebuffer is not initialized */ - return STATUS_EFI_NOT_READY; - } - - /* Set basic framebuffer information */ - *FrameBufferBase = FbpDisplayInfo.FrameBufferBase; - *FrameBufferSize = FbpDisplayInfo.FrameBufferSize; - - /* Set framebuffer mode information */ - ModeInfo->Width = FbpDisplayInfo.ModeInfo.Width; - ModeInfo->Height = FbpDisplayInfo.ModeInfo.Height; - ModeInfo->Depth = FbpDisplayInfo.ModeInfo.Depth; - ModeInfo->RefreshRate = FbpDisplayInfo.ModeInfo.RefreshRate; - ModeInfo->BitsPerPixel = FbpDisplayInfo.ModeInfo.BitsPerPixel; - ModeInfo->BytesPerPixel = FbpDisplayInfo.ModeInfo.BytesPerPixel; - ModeInfo->PixelsPerScanLine = FbpDisplayInfo.ModeInfo.PixelsPerScanLine; - ModeInfo->Pitch = FbpDisplayInfo.ModeInfo.Pitch; - ModeInfo->PixelFormat = FbpDisplayInfo.ModeInfo.PixelFormat; - ModeInfo->PixelInformation = FbpDisplayInfo.ModeInfo.PixelInformation; - - /* Return success */ - return STATUS_EFI_SUCCESS; -} - -/** - * Determines the preferred (native) screen resolution from EDID. This works only with GOP. - * - * @param PreferredWidth - * Supplies a pointer to the memory area where preferred screen width will be stored. - * - * @param PreferredHeight - * Supplies a pointer to the memory area where preferred screen height will be stored. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -FbGetPreferredScreenResolution(OUT PUINT PreferredWidth, - OUT PUINT PreferredHeight) -{ - EFI_GUID GopGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; - EFI_GUID EdidGuid = EFI_EDID_ACTIVE_PROTOCOL_GUID; - PEFI_EDID_ACTIVE_PROTOCOL ActiveEdid; - EFI_STATUS Status; - - /* Check if framebuffer is initialized */ - if(!FbpDisplayInfo.Initialized) - { - /* Framebuffer not ready to use EDID protocol */ - return STATUS_EFI_NOT_READY; - } - - /* Check if GOP device driver is used */ - if(FbpDisplayInfo.Protocol != GOP) - { - /* Unsupported device driver */ - return STATUS_EFI_UNSUPPORTED; - } - - /* Open EDID protocol */ - Status = XtLdrProtocol->Protocol.OpenHandle(FbpDisplayInfo.Handle, (PVOID *)&ActiveEdid, &EdidGuid); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open EDID protocol, close GOP protocol and return */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &GopGuid); - return Status; - } - - /* Return preferred screen resolution */ - *PreferredWidth = ActiveEdid->Edid[0x38] | ((ActiveEdid->Edid[0x3A] & 0xF0) << 4); - *PreferredHeight = ActiveEdid->Edid[0x3B] | ((ActiveEdid->Edid[0x3D] & 0xF0) << 4); - - /* Close EDID & GOP protocols */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &EdidGuid); - - /* Return success */ - return STATUS_EFI_SUCCESS; -} - -/** - * Initializes FrameBuffer device on GOP and UGA compatible adapters. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -FbInitializeDisplay() -{ - EFI_GUID GopGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; - EFI_GUID UgaGuid = EFI_UNIVERSAL_GRAPHICS_ADAPTER_PROTOCOL_GUID; - PEFI_GRAPHICS_OUTPUT_MODE_INFORMATION GopModeInfo; - UINT Depth, QueryMode, Refresh; - UINT_PTR InfoSize; - EFI_STATUS Status; - - /* Check if framebuffer already initialized */ - if(!FbpDisplayInfo.Initialized) - { - /* Print debug message */ - XtLdrProtocol->Debug.Print(L"Initializing framebuffer device\n"); - - /* Attempt to open EFI GOP protocol */ - Status = XtLdrProtocol->Protocol.Open(&FbpDisplayInfo.Handle, (PVOID*)&FbpDisplayInfo.Driver.Gop, &GopGuid); - - /* Check if Graphics Output Protocol (GOP) is available */ - if(Status == STATUS_EFI_SUCCESS) - { - /* Check if there are any video modes available */ - if(FbpDisplayInfo.Driver.Gop->Mode->MaxMode == 0) - { - /* No video modes available */ - XtLdrProtocol->Debug.Print(L"ERROR: No GOP video mode available\n"); - - /* Close GOP protocol and return error */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &GopGuid); - return STATUS_EFI_UNSUPPORTED; - } - - /* Query current graphics mode */ - QueryMode = FbpDisplayInfo.Driver.Gop->Mode == NULLPTR ? 0 : FbpDisplayInfo.Driver.Gop->Mode->Mode; - Status = FbpDisplayInfo.Driver.Gop->QueryMode(FbpDisplayInfo.Driver.Gop, QueryMode, &InfoSize, &GopModeInfo); - if(Status == STATUS_EFI_NOT_STARTED) - { - /* Set the mode to circumvent buggy UEFI firmware */ - Status = FbpDisplayInfo.Driver.Gop->SetMode(FbpDisplayInfo.Driver.Gop, 0); - } - if(Status != STATUS_EFI_SUCCESS) - { - /* Unable to query GOP modes */ - XtLdrProtocol->Debug.Print(L"ERROR: Failed to get GOP native mode (Status Code: 0x%zX)\n"); - - /* Close GOP protocol and return error */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &GopGuid); - return STATUS_EFI_UNSUPPORTED; - } - - /* Store frame buffer base address and protocol used */ - FbpDisplayInfo.FrameBufferBase = FbpDisplayInfo.Driver.Gop->Mode->FrameBufferBase; - FbpDisplayInfo.DefaultMode = FbpDisplayInfo.Driver.Gop->Mode->Mode; - FbpDisplayInfo.Protocol = GOP; - - /* Get current mode information */ - Status = FbpGetModeInfo(); - if(Status != STATUS_EFI_SUCCESS) - { - /* Unable to get mode information */ - XtLdrProtocol->Debug.Print(L"ERROR: Failed to get GOP mode information (Status Code: 0x%zX)\n"); - - /* Close GOP protocol and return error */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &GopGuid); - return STATUS_EFI_UNSUPPORTED; - } - - /* Found GOP */ - XtLdrProtocol->Debug.Print(L"Found EFI-GOP compatible display adapter @ %P (%zu bytes)\n", - FbpDisplayInfo.FrameBufferBase, FbpDisplayInfo.FrameBufferSize); - - /* Close GOP protocol */ - Status = XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &GopGuid); - } - else - { - /* GOP is unavailable, attempt to open UGA protocol */ - Status = XtLdrProtocol->Protocol.Open(&FbpDisplayInfo.Handle, (PVOID*)&FbpDisplayInfo.Driver.Uga, &UgaGuid); - - /* Check if Universal Graphics Adapter (UGA) is available */ - if(Status == STATUS_EFI_SUCCESS) - { - /* Get current video mode */ - Status = FbpDisplayInfo.Driver.Uga->GetMode(FbpDisplayInfo.Driver.Uga, &FbpDisplayInfo.ModeInfo.Width, - &FbpDisplayInfo.ModeInfo.Height, &Depth, &Refresh); - if(Status != STATUS_EFI_SUCCESS) - { - /* Unable to get current UGA mode */ - XtLdrProtocol->Debug.Print(L"ERROR: Failed to get current UGA mode (Status Code: 0x%zX)\n", Status); - - /* Close UGA protocol and return error */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &UgaGuid); - return STATUS_EFI_DEVICE_ERROR; - } - - /* Find framebuffer address */ - Status = FbpFindFramebufferAddress(&FbpDisplayInfo.FrameBufferBase); - if(Status != STATUS_EFI_SUCCESS) - { - /* Unable to find framebuffer address */ - XtLdrProtocol->Debug.Print(L"ERROR: Failed to get EFI FB address (Status Code: 0x%zX)\n", Status); - - /* Close UGA protocol and return error */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &UgaGuid); - return STATUS_EFI_DEVICE_ERROR; - } - - /* Store framebuffer protocol information */ - FbpDisplayInfo.DefaultMode = 0; - FbpDisplayInfo.Protocol = UGA; - - /* Get mode information */ - Status = FbpGetModeInfo(); - if(Status != STATUS_EFI_SUCCESS) - { - /* Unable to get mode information */ - XtLdrProtocol->Debug.Print(L"ERROR: Failed to get UGA mode information (Status Code: 0x%zX)\n"); - return STATUS_EFI_UNSUPPORTED; - } - - /* Found UGA */ - XtLdrProtocol->Debug.Print(L"Found EFI-UGA compatible display adapter @ %P (%zu bytes)\n", - FbpDisplayInfo.FrameBufferBase, FbpDisplayInfo.FrameBufferSize); - - /* Close UGA protocol */ - XtLdrProtocol->Protocol.Close(FbpDisplayInfo.Handle, &UgaGuid); - } - } - - /* Make sure framebuffer initialized properly */ - if(FbpDisplayInfo.Protocol == NONE) - { - /* GOP and UGA unavailable */ - XtLdrProtocol->Debug.Print(L"WARNING: No display adapter found!\n"); - return STATUS_EFI_NOT_FOUND; - } - - XtLdrProtocol->Debug.Print(L"Current screen resolution is %ux%ux%u\n", FbpDisplayInfo.ModeInfo.Width, - FbpDisplayInfo.ModeInfo.Height, FbpDisplayInfo.ModeInfo.BitsPerPixel); - - /* Set framebuffer initialization flag */ - FbpDisplayInfo.Initialized = TRUE; - } - - /* Return success */ - return STATUS_SUCCESS; -} - -/** - * Sets custom screen resolution, based on the provided width and height. - * - * @param Width - * Supplies the width of the screen. - * - * @param Height - * Supplies the height of the screen. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -FbSetScreenResolution(IN UINT Width, - IN UINT Height) -{ - PEFI_GRAPHICS_OUTPUT_MODE_INFORMATION ModeInfo; - BOOLEAN ModeChanged; - EFI_STATUS Status; - UINT_PTR Size; - UINT Mode; - - /* Check if framebuffer is initialized */ - if(!FbpDisplayInfo.Initialized) - { - /* Framebuffer not ready to change screen mode */ - return STATUS_EFI_NOT_READY; - } - - ModeChanged = FALSE; - - /* Change screen mode depending on display adapter protocol */ - switch(FbpDisplayInfo.Protocol) - { - case GOP: - /* GOP available, check if user specified screen resolution */ - if(Width == 0 || Height == 0) - { - /* No resolution specified, temporarily set lowest supported screen resolution */ - Status = FbpDisplayInfo.Driver.Gop->SetMode(FbpDisplayInfo.Driver.Gop, 1); - if(Status == STATUS_EFI_SUCCESS) - { - /* Restore default graphics mode */ - Status = FbpDisplayInfo.Driver.Gop->SetMode(FbpDisplayInfo.Driver.Gop, FbpDisplayInfo.DefaultMode); - ModeChanged = (Status == STATUS_EFI_SUCCESS); - } - } - else - { - /* User specified screen resolution, find a corresponding mode */ - Mode = 1; - while(Mode <= FbpDisplayInfo.Driver.Gop->Mode->MaxMode) - { - /* Get mode information */ - Status = FbpDisplayInfo.Driver.Gop->QueryMode(FbpDisplayInfo.Driver.Gop, Mode, &Size, &ModeInfo); - if(Status == STATUS_EFI_SUCCESS && Size >= sizeof(*ModeInfo) && ModeInfo != NULLPTR) - { - /* Check if match found */ - if(ModeInfo->HorizontalResolution == Width && ModeInfo->VerticalResolution == Height) - { - /* Found corresponding mode, attempt to set it */ - Status = FbpDisplayInfo.Driver.Gop->SetMode(FbpDisplayInfo.Driver.Gop, Mode); - if(Status == STATUS_EFI_SUCCESS) - { - /* New mode set correctly, use it */ - ModeChanged = TRUE; - break; - } - } - } - - /* Try with next mode */ - Mode++; - } - } - break; - case UGA: - /* Set UGA screen mode, trying to keep current color depth and refresh rate */ - Status = FbpDisplayInfo.Driver.Uga->SetMode(FbpDisplayInfo.Driver.Uga, Width, Height, - FbpDisplayInfo.ModeInfo.Depth, - FbpDisplayInfo.ModeInfo.RefreshRate); - if(Status == STATUS_EFI_SUCCESS) - { - /* New mode set correctly, use it */ - ModeChanged = TRUE; - } - break; - default: - /* This should never be reached */ - break; - } - - if(!ModeChanged) - { - /* Failed to change screen mode */ - XtLdrProtocol->Debug.Print(L"ERROR: Failed to change screen mode to %ux%u (Status Code: 0x%zX)\n", - Width, Height, Status); - return STATUS_EFI_UNSUPPORTED; - } - - /* Get new screen mode information */ - Status = FbpGetModeInfo(); - if(Status == STATUS_EFI_SUCCESS) - { - XtLdrProtocol->Debug.Print(L"Changed screen resolution to %ux%ux%u\n", FbpDisplayInfo.ModeInfo.Width, - FbpDisplayInfo.ModeInfo.Height, FbpDisplayInfo.ModeInfo.BitsPerPixel); - } - - /* Return success */ - return STATUS_EFI_SUCCESS; -} /** * Finds a PCI Display Adapter and returns its framebuffer address. @@ -426,7 +28,7 @@ FbSetScreenResolution(IN UINT Width, */ XTCDECL EFI_STATUS -FbpFindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address) +FrameBuffer::FindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address) { EFI_GUID PciIoGuid = EFI_PCI_IO_PROTOCOL_GUID; PEFI_ACPI_ADDRESS_SPACE_DESCRIPTOR BarInfo; @@ -472,7 +74,7 @@ FbpFindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address) XtLdrProtocol->Debug.Print(L"ERROR: Failed to read class (Status Code: 0x%zX)\n", Status); /* Close protocol and continue with next handle */ - XtLdrProtocol->Protocol.Close(Handles[Index], &PciIoGuid); + XtLdrProtocol->Protocol.Close(&Handles[Index], &PciIoGuid); continue; } @@ -480,7 +82,7 @@ FbpFindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address) if(PciDevice.Hdr.ClassCode[2] != 0x03) { /* Not a graphics adapter, close protocol and continue with next handle */ - XtLdrProtocol->Protocol.Close(Handles[Index], &PciIoGuid); + XtLdrProtocol->Protocol.Close(&Handles[Index], &PciIoGuid); continue; } @@ -510,7 +112,7 @@ FbpFindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address) } /* Close handle and continue with next one */ - XtLdrProtocol->Protocol.Close(Handles[Index], &PciIoGuid); + XtLdrProtocol->Protocol.Close(&Handles[Index], &PciIoGuid); } /* Set framebuffer address and return success */ @@ -536,9 +138,9 @@ FbpFindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address) */ XTCDECL VOID -FbpGetColorMask(IN UINT PixelBitMask, - OUT PUSHORT ColorSize, - OUT PUSHORT ColorShift) +FrameBuffer::GetColorMask(IN UINT PixelBitMask, + OUT PUSHORT ColorSize, + OUT PUSHORT ColorShift) { UINT Shift, Size; @@ -570,6 +172,78 @@ FbpGetColorMask(IN UINT PixelBitMask, *ColorSize = Size; } +/** + * Provides an EFI Frame Buffer protocol driver name used for initialization. + * + * @param Protocol + * Supplies a pointer to the memory area where framebuffer driver information will be stored. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +FrameBuffer::GetDisplayDriver(OUT PEFI_GRAPHICS_PROTOCOL Protocol) +{ + /* Check if framebuffer is initialized */ + if(!DisplayInfo.Initialized) + { + /* Return error if framebuffer is not initialized */ + return STATUS_EFI_NOT_READY; + } + + /* Copy framebuffer driver information */ + *Protocol = DisplayInfo.Protocol; + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + +/** + * Returns information about EFI Frame Buffer. + * + * @param FbInfo + * Supplies a pointer to the memory area where framebuffer information will be stored. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +FrameBuffer::GetDisplayInformation(OUT PEFI_PHYSICAL_ADDRESS FrameBufferBase, + OUT PULONG_PTR FrameBufferSize, + OUT PXTBL_FRAMEBUFFER_MODE_INFORMATION ModeInfo) +{ + /* Check if framebuffer is initialized */ + if(!DisplayInfo.Initialized) + { + /* Return error if framebuffer is not initialized */ + return STATUS_EFI_NOT_READY; + } + + /* Set basic framebuffer information */ + *FrameBufferBase = DisplayInfo.FrameBufferBase; + *FrameBufferSize = DisplayInfo.FrameBufferSize; + + /* Set framebuffer mode information */ + ModeInfo->Width = DisplayInfo.ModeInfo.Width; + ModeInfo->Height = DisplayInfo.ModeInfo.Height; + ModeInfo->Depth = DisplayInfo.ModeInfo.Depth; + ModeInfo->RefreshRate = DisplayInfo.ModeInfo.RefreshRate; + ModeInfo->BitsPerPixel = DisplayInfo.ModeInfo.BitsPerPixel; + ModeInfo->BytesPerPixel = DisplayInfo.ModeInfo.BytesPerPixel; + ModeInfo->PixelsPerScanLine = DisplayInfo.ModeInfo.PixelsPerScanLine; + ModeInfo->Pitch = DisplayInfo.ModeInfo.Pitch; + ModeInfo->PixelFormat = DisplayInfo.ModeInfo.PixelFormat; + ModeInfo->PixelInformation = DisplayInfo.ModeInfo.PixelInformation; + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + + /** * Gets information about the current display mode and stores it in internal structure. * @@ -579,19 +253,19 @@ FbpGetColorMask(IN UINT PixelBitMask, */ XTCDECL EFI_STATUS -FbpGetModeInfo() +FrameBuffer::GetModeInformation() { PEFI_GRAPHICS_OUTPUT_MODE_INFORMATION ModeInfo; EFI_PIXEL_BITMASK PixelBitMask; XTSTATUS Status; UINT_PTR Size; - switch(FbpDisplayInfo.Protocol) + switch(DisplayInfo.Protocol) { case GOP: /* Query GOP mode information */ - Status = FbpDisplayInfo.Driver.Gop->QueryMode(FbpDisplayInfo.Driver.Gop, - FbpDisplayInfo.Driver.Gop->Mode->Mode, + Status = DisplayInfo.Driver.Gop->QueryMode(DisplayInfo.Driver.Gop, + DisplayInfo.Driver.Gop->Mode->Mode, &Size, &ModeInfo); if(Status != STATUS_EFI_SUCCESS) { @@ -600,26 +274,26 @@ FbpGetModeInfo() } /* Get pixel bit mask information */ - FbpGetPixelInformation(&FbpDisplayInfo.Driver.Gop->Mode->Info->PixelInformation); + GetPixelInformation(&DisplayInfo.Driver.Gop->Mode->Info->PixelInformation); /* Store GOP framebuffer information */ - FbpDisplayInfo.ModeInfo.Width = FbpDisplayInfo.Driver.Gop->Mode->Info->HorizontalResolution; - FbpDisplayInfo.ModeInfo.Height = FbpDisplayInfo.Driver.Gop->Mode->Info->VerticalResolution; - FbpDisplayInfo.ModeInfo.Depth = FbpDisplayInfo.ModeInfo.BitsPerPixel; - FbpDisplayInfo.ModeInfo.PixelsPerScanLine = FbpDisplayInfo.Driver.Gop->Mode->Info->PixelsPerScanLine; - FbpDisplayInfo.ModeInfo.Pitch = FbpDisplayInfo.ModeInfo.PixelsPerScanLine * - (FbpDisplayInfo.ModeInfo.BitsPerPixel / 8); - FbpDisplayInfo.ModeInfo.RefreshRate = 0; + DisplayInfo.ModeInfo.Width = DisplayInfo.Driver.Gop->Mode->Info->HorizontalResolution; + DisplayInfo.ModeInfo.Height = DisplayInfo.Driver.Gop->Mode->Info->VerticalResolution; + DisplayInfo.ModeInfo.Depth = DisplayInfo.ModeInfo.BitsPerPixel; + DisplayInfo.ModeInfo.PixelsPerScanLine = DisplayInfo.Driver.Gop->Mode->Info->PixelsPerScanLine; + DisplayInfo.ModeInfo.Pitch = DisplayInfo.ModeInfo.PixelsPerScanLine * + (DisplayInfo.ModeInfo.BitsPerPixel / 8); + DisplayInfo.ModeInfo.RefreshRate = 0; /* Store pixel format information and frame buffer size */ - FbpDisplayInfo.ModeInfo.PixelFormat = FbpDisplayInfo.Driver.Gop->Mode->Info->PixelFormat; - FbpDisplayInfo.FrameBufferSize = FbpDisplayInfo.Driver.Gop->Mode->FrameBufferSize; + DisplayInfo.ModeInfo.PixelFormat = DisplayInfo.Driver.Gop->Mode->Info->PixelFormat; + DisplayInfo.FrameBufferSize = DisplayInfo.Driver.Gop->Mode->FrameBufferSize; break; case UGA: /* Query UGA mode information */ - Status = FbpDisplayInfo.Driver.Uga->GetMode(FbpDisplayInfo.Driver.Uga, &FbpDisplayInfo.ModeInfo.Width, - &FbpDisplayInfo.ModeInfo.Height, &FbpDisplayInfo.ModeInfo.Depth, - &FbpDisplayInfo.ModeInfo.RefreshRate); + Status = DisplayInfo.Driver.Uga->GetMode(DisplayInfo.Driver.Uga, &DisplayInfo.ModeInfo.Width, + &DisplayInfo.ModeInfo.Height, &DisplayInfo.ModeInfo.Depth, + &DisplayInfo.ModeInfo.RefreshRate); if(Status != STATUS_EFI_SUCCESS) { /* Failed to get UGA mode information, return error */ @@ -628,18 +302,18 @@ FbpGetModeInfo() /* Get pixel bit mask information */ PixelBitMask = (EFI_PIXEL_BITMASK){0, 0, 0, 0}; - FbpGetPixelInformation(&PixelBitMask); + GetPixelInformation(&PixelBitMask); /* Store UGA framebuffer information */ - FbpDisplayInfo.ModeInfo.PixelsPerScanLine = FbpDisplayInfo.ModeInfo.Width; - FbpDisplayInfo.ModeInfo.Pitch = FbpDisplayInfo.ModeInfo.PixelsPerScanLine * - (FbpDisplayInfo.ModeInfo.BitsPerPixel / 8); + DisplayInfo.ModeInfo.PixelsPerScanLine = DisplayInfo.ModeInfo.Width; + DisplayInfo.ModeInfo.Pitch = DisplayInfo.ModeInfo.PixelsPerScanLine * + (DisplayInfo.ModeInfo.BitsPerPixel / 8); /* Store pixel format information and recalculate frame buffer size */ - FbpDisplayInfo.ModeInfo.PixelFormat = PixelBlueGreenRedReserved8BitPerColor; - FbpDisplayInfo.FrameBufferSize = FbpDisplayInfo.ModeInfo.Width * - FbpDisplayInfo.ModeInfo.Height * - FbpDisplayInfo.ModeInfo.BytesPerPixel + 1024; + DisplayInfo.ModeInfo.PixelFormat = PixelBlueGreenRedReserved8BitPerColor; + DisplayInfo.FrameBufferSize = DisplayInfo.ModeInfo.Width * + DisplayInfo.ModeInfo.Height * + DisplayInfo.ModeInfo.BytesPerPixel + 1024; break; default: /* This should never be reached as no other display driver is supported */ @@ -665,40 +339,40 @@ FbpGetModeInfo() */ XTCDECL VOID -FbpGetPixelInformation(IN PEFI_PIXEL_BITMASK PixelsBitMask) +FrameBuffer::GetPixelInformation(IN PEFI_PIXEL_BITMASK PixelsBitMask) { UINT CompoundMask; /* Check reported pixel format */ - switch(FbpDisplayInfo.ModeInfo.PixelFormat) + switch(DisplayInfo.ModeInfo.PixelFormat) { case PixelBlueGreenRedReserved8BitPerColor: /* BGRR, 32 bits per pixel */ - FbpDisplayInfo.ModeInfo.BitsPerPixel = 32; - FbpDisplayInfo.ModeInfo.PixelInformation.BlueShift = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.BlueSize = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.GreenShift = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.GreenSize = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.RedShift = 16; - FbpDisplayInfo.ModeInfo.PixelInformation.RedSize = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.ReservedShift = 24; - FbpDisplayInfo.ModeInfo.PixelInformation.ReservedSize = 8; + DisplayInfo.ModeInfo.BitsPerPixel = 32; + DisplayInfo.ModeInfo.PixelInformation.BlueShift = 0; + DisplayInfo.ModeInfo.PixelInformation.BlueSize = 8; + DisplayInfo.ModeInfo.PixelInformation.GreenShift = 8; + DisplayInfo.ModeInfo.PixelInformation.GreenSize = 8; + DisplayInfo.ModeInfo.PixelInformation.RedShift = 16; + DisplayInfo.ModeInfo.PixelInformation.RedSize = 8; + DisplayInfo.ModeInfo.PixelInformation.ReservedShift = 24; + DisplayInfo.ModeInfo.PixelInformation.ReservedSize = 8; break; case PixelRedGreenBlueReserved8BitPerColor: /* RGBR, 32 bits per pixel */ - FbpDisplayInfo.ModeInfo.BitsPerPixel = 32; - FbpDisplayInfo.ModeInfo.PixelInformation.BlueShift = 16; - FbpDisplayInfo.ModeInfo.PixelInformation.BlueSize = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.GreenShift = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.GreenSize = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.RedShift = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.RedSize = 8; - FbpDisplayInfo.ModeInfo.PixelInformation.ReservedShift = 24; - FbpDisplayInfo.ModeInfo.PixelInformation.ReservedSize = 8; + DisplayInfo.ModeInfo.BitsPerPixel = 32; + DisplayInfo.ModeInfo.PixelInformation.BlueShift = 16; + DisplayInfo.ModeInfo.PixelInformation.BlueSize = 8; + DisplayInfo.ModeInfo.PixelInformation.GreenShift = 8; + DisplayInfo.ModeInfo.PixelInformation.GreenSize = 8; + DisplayInfo.ModeInfo.PixelInformation.RedShift = 0; + DisplayInfo.ModeInfo.PixelInformation.RedSize = 8; + DisplayInfo.ModeInfo.PixelInformation.ReservedShift = 24; + DisplayInfo.ModeInfo.PixelInformation.ReservedSize = 8; break; case PixelBitMask: /* Assume 32 bits per pixel */ - FbpDisplayInfo.ModeInfo.BitsPerPixel = 32; + DisplayInfo.ModeInfo.BitsPerPixel = 32; /* Calculate compound mask */ CompoundMask = PixelsBitMask->RedMask | @@ -709,36 +383,408 @@ FbpGetPixelInformation(IN PEFI_PIXEL_BITMASK PixelsBitMask) /* Recalculate bits per pixel */ while((CompoundMask & (1 << 31)) == 0) { - FbpDisplayInfo.ModeInfo.BitsPerPixel--; + DisplayInfo.ModeInfo.BitsPerPixel--; CompoundMask <<= 1; } /* Set pixel information */ - FbpGetColorMask(PixelsBitMask->RedMask, &FbpDisplayInfo.ModeInfo.PixelInformation.RedSize, - &FbpDisplayInfo.ModeInfo.PixelInformation.RedShift); - FbpGetColorMask(PixelsBitMask->GreenMask, &FbpDisplayInfo.ModeInfo.PixelInformation.GreenSize, - &FbpDisplayInfo.ModeInfo.PixelInformation.GreenShift); - FbpGetColorMask(PixelsBitMask->BlueMask, &FbpDisplayInfo.ModeInfo.PixelInformation.BlueSize, - &FbpDisplayInfo.ModeInfo.PixelInformation.BlueShift); - FbpGetColorMask(PixelsBitMask->ReservedMask, &FbpDisplayInfo.ModeInfo.PixelInformation.ReservedSize, - &FbpDisplayInfo.ModeInfo.PixelInformation.ReservedShift); + GetColorMask(PixelsBitMask->RedMask, &DisplayInfo.ModeInfo.PixelInformation.RedSize, + &DisplayInfo.ModeInfo.PixelInformation.RedShift); + GetColorMask(PixelsBitMask->GreenMask, &DisplayInfo.ModeInfo.PixelInformation.GreenSize, + &DisplayInfo.ModeInfo.PixelInformation.GreenShift); + GetColorMask(PixelsBitMask->BlueMask, &DisplayInfo.ModeInfo.PixelInformation.BlueSize, + &DisplayInfo.ModeInfo.PixelInformation.BlueShift); + GetColorMask(PixelsBitMask->ReservedMask, &DisplayInfo.ModeInfo.PixelInformation.ReservedSize, + &DisplayInfo.ModeInfo.PixelInformation.ReservedShift); break; default: /* Unknown pixel format */ - FbpDisplayInfo.ModeInfo.BitsPerPixel = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.BlueShift = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.BlueSize = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.GreenShift = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.GreenSize = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.RedShift = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.RedSize = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.ReservedShift = 0; - FbpDisplayInfo.ModeInfo.PixelInformation.ReservedSize = 0; + DisplayInfo.ModeInfo.BitsPerPixel = 0; + DisplayInfo.ModeInfo.PixelInformation.BlueShift = 0; + DisplayInfo.ModeInfo.PixelInformation.BlueSize = 0; + DisplayInfo.ModeInfo.PixelInformation.GreenShift = 0; + DisplayInfo.ModeInfo.PixelInformation.GreenSize = 0; + DisplayInfo.ModeInfo.PixelInformation.RedShift = 0; + DisplayInfo.ModeInfo.PixelInformation.RedSize = 0; + DisplayInfo.ModeInfo.PixelInformation.ReservedShift = 0; + DisplayInfo.ModeInfo.PixelInformation.ReservedSize = 0; break; } /* Calculate bytes per pixel based on bits per pixel */ - FbpDisplayInfo.ModeInfo.BytesPerPixel = FbpDisplayInfo.ModeInfo.BitsPerPixel >> 3; + DisplayInfo.ModeInfo.BytesPerPixel = DisplayInfo.ModeInfo.BitsPerPixel >> 3; +} + +/** + * Determines the preferred (native) screen resolution from EDID. This works only with GOP. + * + * @param PreferredWidth + * Supplies a pointer to the memory area where preferred screen width will be stored. + * + * @param PreferredHeight + * Supplies a pointer to the memory area where preferred screen height will be stored. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +FrameBuffer::GetPreferredScreenResolution(OUT PUINT PreferredWidth, + OUT PUINT PreferredHeight) +{ + EFI_GUID GopGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; + EFI_GUID EdidGuid = EFI_EDID_ACTIVE_PROTOCOL_GUID; + PEFI_EDID_ACTIVE_PROTOCOL ActiveEdid; + EFI_STATUS Status; + + /* Check if framebuffer is initialized */ + if(!DisplayInfo.Initialized) + { + /* Framebuffer not ready to use EDID protocol */ + return STATUS_EFI_NOT_READY; + } + + /* Check if GOP device driver is used */ + if(DisplayInfo.Protocol != GOP) + { + /* Unsupported device driver */ + return STATUS_EFI_UNSUPPORTED; + } + + /* Open EDID protocol */ + Status = XtLdrProtocol->Protocol.OpenHandle(DisplayInfo.Handle, (PVOID *)&ActiveEdid, &EdidGuid); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open EDID protocol, close GOP protocol and return */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &GopGuid); + return Status; + } + + /* Return preferred screen resolution */ + *PreferredWidth = ActiveEdid->Edid[0x38] | ((ActiveEdid->Edid[0x3A] & 0xF0) << 4); + *PreferredHeight = ActiveEdid->Edid[0x3B] | ((ActiveEdid->Edid[0x3D] & 0xF0) << 4); + + /* Close EDID & GOP protocols */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &EdidGuid); + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + +/** + * Initializes FrameBuffer device on GOP and UGA compatible adapters. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +FrameBuffer::InitializeDisplay() +{ + EFI_GUID GopGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; + EFI_GUID UgaGuid = EFI_UNIVERSAL_GRAPHICS_ADAPTER_PROTOCOL_GUID; + PEFI_GRAPHICS_OUTPUT_MODE_INFORMATION GopModeInfo; + UINT Depth, QueryMode, Refresh; + UINT_PTR InfoSize; + EFI_STATUS Status; + + /* Check if framebuffer already initialized */ + if(!DisplayInfo.Initialized) + { + /* Print debug message */ + XtLdrProtocol->Debug.Print(L"Initializing framebuffer device\n"); + + /* Attempt to open EFI GOP protocol */ + Status = XtLdrProtocol->Protocol.Open(&DisplayInfo.Handle, (PVOID*)&DisplayInfo.Driver.Gop, &GopGuid); + + /* Check if Graphics Output Protocol (GOP) is available */ + if(Status == STATUS_EFI_SUCCESS) + { + /* Check if there are any video modes available */ + if(DisplayInfo.Driver.Gop->Mode->MaxMode == 0) + { + /* No video modes available */ + XtLdrProtocol->Debug.Print(L"ERROR: No GOP video mode available\n"); + + /* Close GOP protocol and return error */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &GopGuid); + return STATUS_EFI_UNSUPPORTED; + } + + /* Query current graphics mode */ + QueryMode = DisplayInfo.Driver.Gop->Mode == NULLPTR ? 0 : DisplayInfo.Driver.Gop->Mode->Mode; + Status = DisplayInfo.Driver.Gop->QueryMode(DisplayInfo.Driver.Gop, QueryMode, &InfoSize, &GopModeInfo); + if(Status == STATUS_EFI_NOT_STARTED) + { + /* Set the mode to circumvent buggy UEFI firmware */ + Status = DisplayInfo.Driver.Gop->SetMode(DisplayInfo.Driver.Gop, 0); + } + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to query GOP modes */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to get GOP native mode (Status Code: 0x%zX)\n"); + + /* Close GOP protocol and return error */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &GopGuid); + return STATUS_EFI_UNSUPPORTED; + } + + /* Store frame buffer base address and protocol used */ + DisplayInfo.FrameBufferBase = DisplayInfo.Driver.Gop->Mode->FrameBufferBase; + DisplayInfo.DefaultMode = DisplayInfo.Driver.Gop->Mode->Mode; + DisplayInfo.Protocol = GOP; + + /* Get current mode information */ + Status = GetModeInformation(); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to get mode information */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to get GOP mode information (Status Code: 0x%zX)\n"); + + /* Close GOP protocol and return error */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &GopGuid); + return STATUS_EFI_UNSUPPORTED; + } + + /* Found GOP */ + XtLdrProtocol->Debug.Print(L"Found EFI-GOP compatible display adapter @ %P (%zu bytes)\n", + DisplayInfo.FrameBufferBase, DisplayInfo.FrameBufferSize); + + /* Close GOP protocol */ + Status = XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &GopGuid); + } + else + { + /* GOP is unavailable, attempt to open UGA protocol */ + Status = XtLdrProtocol->Protocol.Open(&DisplayInfo.Handle, (PVOID*)&DisplayInfo.Driver.Uga, &UgaGuid); + + /* Check if Universal Graphics Adapter (UGA) is available */ + if(Status == STATUS_EFI_SUCCESS) + { + /* Get current video mode */ + Status = DisplayInfo.Driver.Uga->GetMode(DisplayInfo.Driver.Uga, &DisplayInfo.ModeInfo.Width, + &DisplayInfo.ModeInfo.Height, &Depth, &Refresh); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to get current UGA mode */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to get current UGA mode (Status Code: 0x%zX)\n", Status); + + /* Close UGA protocol and return error */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &UgaGuid); + return STATUS_EFI_DEVICE_ERROR; + } + + /* Find framebuffer address */ + Status = FindFramebufferAddress(&DisplayInfo.FrameBufferBase); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to find framebuffer address */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to get EFI FB address (Status Code: 0x%zX)\n", Status); + + /* Close UGA protocol and return error */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &UgaGuid); + return STATUS_EFI_DEVICE_ERROR; + } + + /* Store framebuffer protocol information */ + DisplayInfo.DefaultMode = 0; + DisplayInfo.Protocol = UGA; + + /* Get mode information */ + Status = GetModeInformation(); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to get mode information */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to get UGA mode information (Status Code: 0x%zX)\n"); + return STATUS_EFI_UNSUPPORTED; + } + + /* Found UGA */ + XtLdrProtocol->Debug.Print(L"Found EFI-UGA compatible display adapter @ %P (%zu bytes)\n", + DisplayInfo.FrameBufferBase, DisplayInfo.FrameBufferSize); + + /* Close UGA protocol */ + XtLdrProtocol->Protocol.Close(&DisplayInfo.Handle, &UgaGuid); + } + } + + /* Make sure framebuffer initialized properly */ + if(DisplayInfo.Protocol == NONE) + { + /* GOP and UGA unavailable */ + XtLdrProtocol->Debug.Print(L"WARNING: No display adapter found!\n"); + return STATUS_EFI_NOT_FOUND; + } + + XtLdrProtocol->Debug.Print(L"Current screen resolution is %ux%ux%u\n", DisplayInfo.ModeInfo.Width, + DisplayInfo.ModeInfo.Height, DisplayInfo.ModeInfo.BitsPerPixel); + + /* Set framebuffer initialization flag */ + DisplayInfo.Initialized = TRUE; + } + + /* Return success */ + return STATUS_SUCCESS; +} + +/** + * Initializes FRAMEBUF module by opening XTLDR protocol and installing FRAMEBUF protocol. + * + * @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 + */ +XTCDECL +EFI_STATUS +FrameBuffer::InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable) +{ + EFI_GUID Guid = XT_FRAMEBUFFER_PROTOCOL_GUID; + EFI_STATUS Status; + + /* Open the XTLDR protocol */ + Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open loader protocol */ + return STATUS_EFI_PROTOCOL_ERROR; + } + + /* Set initial framebuffer state */ + DisplayInfo.Protocol = NONE; + DisplayInfo.Initialized = FALSE; + + /* Set routines available via XTLDR framebuffer protocol */ + FbProtocol.GetDisplayDriver = GetDisplayDriver; + FbProtocol.GetDisplayInformation = GetDisplayInformation; + FbProtocol.GetPreferredScreenResolution = GetPreferredScreenResolution; + FbProtocol.Initialize = InitializeDisplay; + FbProtocol.SetScreenResolution = SetScreenResolution; + + /* Register XTOS boot protocol */ + return XtLdrProtocol->Protocol.Install(&FbProtocol, &Guid); +} + +/** + * Sets custom screen resolution, based on the provided width and height. + * + * @param Width + * Supplies the width of the screen. + * + * @param Height + * Supplies the height of the screen. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +FrameBuffer::SetScreenResolution(IN UINT Width, + IN UINT Height) +{ + PEFI_GRAPHICS_OUTPUT_MODE_INFORMATION ModeInfo; + BOOLEAN ModeChanged; + EFI_STATUS Status; + UINT_PTR Size; + UINT Mode; + + /* Check if framebuffer is initialized */ + if(!DisplayInfo.Initialized) + { + /* Framebuffer not ready to change screen mode */ + return STATUS_EFI_NOT_READY; + } + + ModeChanged = FALSE; + + /* Change screen mode depending on display adapter protocol */ + switch(DisplayInfo.Protocol) + { + case GOP: + /* GOP available, check if user specified screen resolution */ + if(Width == 0 || Height == 0) + { + /* No resolution specified, temporarily set lowest supported screen resolution */ + Status = DisplayInfo.Driver.Gop->SetMode(DisplayInfo.Driver.Gop, 1); + if(Status == STATUS_EFI_SUCCESS) + { + /* Restore default graphics mode */ + Status = DisplayInfo.Driver.Gop->SetMode(DisplayInfo.Driver.Gop, DisplayInfo.DefaultMode); + ModeChanged = (Status == STATUS_EFI_SUCCESS) ? TRUE : FALSE; + } + } + else + { + /* User specified screen resolution, find a corresponding mode */ + Mode = 1; + while(Mode <= DisplayInfo.Driver.Gop->Mode->MaxMode) + { + /* Get mode information */ + Status = DisplayInfo.Driver.Gop->QueryMode(DisplayInfo.Driver.Gop, Mode, &Size, &ModeInfo); + if(Status == STATUS_EFI_SUCCESS && Size >= sizeof(*ModeInfo) && ModeInfo != NULLPTR) + { + /* Check if match found */ + if(ModeInfo->HorizontalResolution == Width && ModeInfo->VerticalResolution == Height) + { + /* Found corresponding mode, attempt to set it */ + Status = DisplayInfo.Driver.Gop->SetMode(DisplayInfo.Driver.Gop, Mode); + if(Status == STATUS_EFI_SUCCESS) + { + /* New mode set correctly, use it */ + ModeChanged = TRUE; + break; + } + } + } + + /* Try with next mode */ + Mode++; + } + } + break; + case UGA: + /* Set UGA screen mode, trying to keep current color depth and refresh rate */ + Status = DisplayInfo.Driver.Uga->SetMode(DisplayInfo.Driver.Uga, Width, Height, + DisplayInfo.ModeInfo.Depth, + DisplayInfo.ModeInfo.RefreshRate); + if(Status == STATUS_EFI_SUCCESS) + { + /* New mode set correctly, use it */ + ModeChanged = TRUE; + } + break; + default: + /* This should never be reached */ + break; + } + + if(!ModeChanged) + { + /* Failed to change screen mode */ + XtLdrProtocol->Debug.Print(L"ERROR: Failed to change screen mode to %ux%u (Status Code: 0x%zX)\n", + Width, Height, Status); + return STATUS_EFI_UNSUPPORTED; + } + + /* Get new screen mode information */ + Status = GetModeInformation(); + if(Status == STATUS_EFI_SUCCESS) + { + XtLdrProtocol->Debug.Print(L"Changed screen resolution to %ux%ux%u\n", DisplayInfo.ModeInfo.Width, + DisplayInfo.ModeInfo.Height, DisplayInfo.ModeInfo.BitsPerPixel); + } + + /* Return success */ + return STATUS_EFI_SUCCESS; } /** @@ -759,28 +805,6 @@ EFI_STATUS XtLdrModuleMain(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { - EFI_GUID Guid = XT_FRAMEBUFFER_PROTOCOL_GUID; - EFI_STATUS Status; - - /* Open the XTLDR protocol */ - Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open loader protocol */ - return STATUS_EFI_PROTOCOL_ERROR; - } - - /* Set initial framebuffer state */ - FbpDisplayInfo.Protocol = NONE; - FbpDisplayInfo.Initialized = FALSE; - - /* Set routines available via XTLDR framebuffer protocol */ - FbpFrameBufferProtocol.GetDisplayDriver = FbGetDisplayDriver; - FbpFrameBufferProtocol.GetDisplayInformation = FbGetDisplayInformation; - FbpFrameBufferProtocol.GetPreferredScreenResolution = FbGetPreferredScreenResolution; - FbpFrameBufferProtocol.Initialize = FbInitializeDisplay; - FbpFrameBufferProtocol.SetScreenResolution = FbSetScreenResolution; - - /* Register XTOS boot protocol */ - return XtLdrProtocol->Protocol.Install(&FbpFrameBufferProtocol, &Guid); + /* Initialize FRAMEBUF module */ + return FrameBuffer::InitializeModule(ImageHandle, SystemTable); } diff --git a/xtldr/modules/framebuf/globals.c b/xtldr/modules/framebuf/globals.c deleted file mode 100644 index c3fd84e..0000000 --- a/xtldr/modules/framebuf/globals.c +++ /dev/null @@ -1,19 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/framebuf/globals.c - * DESCRIPTION: EFI framebuffer module global variables - * DEVELOPERS: Rafal Kupiec - */ - -#include - - -/* Framebuffer display information */ -XTBL_FRAMEBUFFER_INFORMATION FbpDisplayInfo; - -/* Framebuffer protocol handler */ -XTBL_FRAMEBUFFER_PROTOCOL FbpFrameBufferProtocol; - -/* XTLDR protocol handler */ -PXTBL_LOADER_PROTOCOL XtLdrProtocol; diff --git a/xtldr/modules/framebuf/includes/framebuf.h b/xtldr/modules/framebuf/includes/framebuf.h deleted file mode 100644 index b99b7f5..0000000 --- a/xtldr/modules/framebuf/includes/framebuf.h +++ /dev/null @@ -1,64 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/framebuf/includes/framebuf.h - * DESCRIPTION: EFI Framebuffer support module header file - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_MODULES_FRAMEBUF_H -#define __XTLDR_MODULES_FRAMEBUF_H - -#include -#include - - -/* FrameBuffer support protocol related routines forward references */ -XTCDECL -EFI_STATUS -FbGetDisplayDriver(OUT PEFI_GRAPHICS_PROTOCOL Protocol); - -XTCDECL -EFI_STATUS -FbGetDisplayInformation(OUT PEFI_PHYSICAL_ADDRESS FrameBufferBase, - OUT PULONG_PTR FrameBufferSize, - OUT PXTBL_FRAMEBUFFER_MODE_INFORMATION ModeInfo); - -XTCDECL -EFI_STATUS -FbGetPreferredScreenResolution(OUT PUINT PreferredWidth, - OUT PUINT PreferredHeight); - -XTCDECL -EFI_STATUS -FbInitializeDisplay(); - -XTCDECL -EFI_STATUS -FbSetScreenResolution(IN UINT Width, - IN UINT Height); - -XTCDECL -EFI_STATUS -FbpFindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address); - -XTCDECL -VOID -FbpGetColorMask(IN UINT EfiMask, - OUT PUSHORT ColorSize, - OUT PUSHORT ColorShift); - -XTCDECL -EFI_STATUS -FbpGetModeInfo(); - -XTCDECL -VOID -FbpGetPixelInformation(IN PEFI_PIXEL_BITMASK PixelsBitMask); - -XTCDECL -EFI_STATUS -XtLdrModuleMain(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); - -#endif /* __XTLDR_MODULES_FRAMEBUF_H */ diff --git a/xtldr/modules/framebuf/includes/framebuf.hh b/xtldr/modules/framebuf/includes/framebuf.hh new file mode 100644 index 0000000..7188d58 --- /dev/null +++ b/xtldr/modules/framebuf/includes/framebuf.hh @@ -0,0 +1,44 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/framebuf/includes/framebuf.hh + * DESCRIPTION: EFI Framebuffer support module header file + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_MODULES_FRAMEBUF_HH +#define __XTLDR_MODULES_FRAMEBUF_HH + +#include + + +class FrameBuffer +{ + private: + STATIC XTBL_FRAMEBUFFER_INFORMATION DisplayInfo; + STATIC XTBL_FRAMEBUFFER_PROTOCOL FbProtocol; + STATIC PXTBL_LOADER_PROTOCOL XtLdrProtocol; + + public: + STATIC XTCDECL EFI_STATUS GetDisplayDriver(OUT PEFI_GRAPHICS_PROTOCOL Protocol); + STATIC XTCDECL EFI_STATUS GetDisplayInformation(OUT PEFI_PHYSICAL_ADDRESS FrameBufferBase, + OUT PULONG_PTR FrameBufferSize, + OUT PXTBL_FRAMEBUFFER_MODE_INFORMATION ModeInfo); + STATIC XTCDECL EFI_STATUS GetPreferredScreenResolution(OUT PUINT PreferredWidth, + OUT PUINT PreferredHeight); + STATIC XTCDECL EFI_STATUS InitializeDisplay(); + STATIC XTCDECL EFI_STATUS InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); + STATIC XTCDECL EFI_STATUS SetScreenResolution(IN UINT Width, + IN UINT Height); + + private: + STATIC EFI_STATUS FindFramebufferAddress(OUT PEFI_PHYSICAL_ADDRESS Address); + STATIC XTCDECL VOID GetColorMask(IN UINT EfiMask, + OUT PUSHORT ColorSize, + OUT PUSHORT ColorShift); + STATIC XTCDECL EFI_STATUS GetModeInformation(); + STATIC XTCDECL VOID GetPixelInformation(IN PEFI_PIXEL_BITMASK PixelsBitMask); +}; + +#endif /* __XTLDR_MODULES_FRAMEBUF_HH */ diff --git a/xtldr/modules/framebuf/includes/globals.h b/xtldr/modules/framebuf/includes/globals.h deleted file mode 100644 index df939a6..0000000 --- a/xtldr/modules/framebuf/includes/globals.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/framebuf/includes/globals.h - * DESCRIPTION: EFI Framebuffer module global variables - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_MODULES_GLOBALS_H -#define __XTLDR_MODULES_GLOBALS_H - -#include - - -/* Framebuffer display information */ -EXTERN XTBL_FRAMEBUFFER_INFORMATION FbpDisplayInfo; - -/* Framebuffer protocol handler */ -EXTERN XTBL_FRAMEBUFFER_PROTOCOL FbpFrameBufferProtocol; - -/* XTLDR protocol handler */ -EXTERN PXTBL_LOADER_PROTOCOL XtLdrProtocol; - -#endif /* __XTLDR_MODULES_GLOBALS_H */ diff --git a/xtldr/modules/pecoff/CMakeLists.txt b/xtldr/modules/pecoff/CMakeLists.txt index e13db3c..aa8920d 100644 --- a/xtldr/modules/pecoff/CMakeLists.txt +++ b/xtldr/modules/pecoff/CMakeLists.txt @@ -8,8 +8,8 @@ include_directories( # Specify list of source code files list(APPEND XTLDR_PECOFF_SOURCE - ${XTLDR_PECOFF_SOURCE_DIR}/globals.c - ${XTLDR_PECOFF_SOURCE_DIR}/pecoff.c) + ${XTLDR_PECOFF_SOURCE_DIR}/data.cc + ${XTLDR_PECOFF_SOURCE_DIR}/pecoff.cc) # Link module executable add_executable(pecoff ${XTLDR_PECOFF_SOURCE}) diff --git a/xtldr/modules/pecoff/data.cc b/xtldr/modules/pecoff/data.cc new file mode 100644 index 0000000..f919194 --- /dev/null +++ b/xtldr/modules/pecoff/data.cc @@ -0,0 +1,16 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/pecoff/globals.cc + * DESCRIPTION: Basic PE/COFF executable file format global and static data + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/* XTOS PE/COFF Image Protocol */ +XTBL_EXECUTABLE_IMAGE_PROTOCOL PeCoff::PeProtocol; + +/* EFI XT Loader Protocol */ +PXTBL_LOADER_PROTOCOL PeCoff::XtLdrProtocol; diff --git a/xtldr/modules/pecoff/globals.c b/xtldr/modules/pecoff/globals.c deleted file mode 100644 index 6c3afc4..0000000 --- a/xtldr/modules/pecoff/globals.c +++ /dev/null @@ -1,16 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/pecoff/globals.c - * DESCRIPTION: Basic PE/COFF executable file format support global variables - * DEVELOPERS: Rafal Kupiec - */ - -#include - - -/* XTOS PE/COFF Image Protocol */ -XTBL_EXECUTABLE_IMAGE_PROTOCOL PeCoffProtocol; - -/* EFI XT Loader Protocol */ -PXTBL_LOADER_PROTOCOL XtLdrProtocol; diff --git a/xtldr/modules/pecoff/includes/globals.h b/xtldr/modules/pecoff/includes/globals.h deleted file mode 100644 index 5f2af34..0000000 --- a/xtldr/modules/pecoff/includes/globals.h +++ /dev/null @@ -1,21 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/pecoff/includes/globals.h - * DESCRIPTION: Basic PE/COFF executable file format support global variables - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_PECOFF_GLOBALS_H -#define __XTLDR_PECOFF_GLOBALS_H - -#include - - -/* XTOS PE/COFF Image Protocol */ -EXTERN XTBL_EXECUTABLE_IMAGE_PROTOCOL PeCoffProtocol; - -/* EFI XT Loader Protocol */ -EXTERN PXTBL_LOADER_PROTOCOL XtLdrProtocol; - -#endif /* __XTLDR_PECOFF_GLOBALS_H */ diff --git a/xtldr/modules/pecoff/includes/pecoff.h b/xtldr/modules/pecoff/includes/pecoff.h deleted file mode 100644 index 8dbaf95..0000000 --- a/xtldr/modules/pecoff/includes/pecoff.h +++ /dev/null @@ -1,82 +0,0 @@ -/** - * PROJECT: ExectOS - * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/pecoff/includes/pecoff.h - * DESCRIPTION: Basic PE/COFF executable file format support header - * DEVELOPERS: Rafal Kupiec - */ - -#ifndef __XTLDR_PECOFF_H -#define __XTLDR_PECOFF_H - -#include -#include - - -/* PE/COFF image protocol related routines forward references */ -XTCDECL -EFI_STATUS -PeGetEntryPoint(IN PVOID ImagePointer, - OUT PVOID *EntryPoint); - -XTCDECL -EFI_STATUS -PeGetFileSize(IN PVOID ImagePointer, - OUT PULONGLONG FileSize); - -XTCDECL -EFI_STATUS -PeGetImageSize(IN PVOID ImagePointer, - OUT PUINT ImageSize); - -XTCDECL -EFI_STATUS -PeGetMachineType(IN PVOID ImagePointer, - OUT PUSHORT MachineType); - -XTCDECL -EFI_STATUS -PeGetSection(IN PVOID ImagePointer, - IN PCHAR SectionName, - OUT PULONG *RawData); - -XTCDECL -EFI_STATUS -PeGetSubSystem(IN PVOID ImagePointer, - OUT PUSHORT SubSystem); - -XTCDECL -EFI_STATUS -PeGetVersion(IN PVOID ImagePointer, - OUT PUSHORT Version); - -XTCDECL -EFI_STATUS -PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, - IN LOADER_MEMORY_TYPE MemoryType, - IN PVOID VirtualAddress, - OUT PVOID *ImagePointer); - -XTCDECL -EFI_STATUS -PeRelocateImage(IN PVOID ImagePointer, - IN EFI_VIRTUAL_ADDRESS Address); - -XTCDECL -EFI_STATUS -PeUnloadImage(IN PVOID ImagePointer); - -XTCDECL -EFI_STATUS -PeVerifyImage(IN PVOID ImagePointer); - -XTCDECL -EFI_STATUS -PepRelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image); - -XTCDECL -EFI_STATUS -BlXtLdrModuleMain(IN EFI_HANDLE ImageHandle, - IN PEFI_SYSTEM_TABLE SystemTable); - -#endif /* __XTLDR_PECOFF_H */ diff --git a/xtldr/modules/pecoff/includes/pecoff.hh b/xtldr/modules/pecoff/includes/pecoff.hh new file mode 100644 index 0000000..78defde --- /dev/null +++ b/xtldr/modules/pecoff/includes/pecoff.hh @@ -0,0 +1,53 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/modules/pecoff/includes/pecoff.hh + * DESCRIPTION: Basic PE/COFF executable file format support header + * DEVELOPERS: Rafal Kupiec + */ + +#ifndef __XTLDR_PECOFF_HH +#define __XTLDR_PECOFF_HH + +#include + + +/* PE/COFF module for XTLDR */ +class PeCoff +{ + private: + STATIC XTBL_EXECUTABLE_IMAGE_PROTOCOL PeProtocol; + STATIC PXTBL_LOADER_PROTOCOL XtLdrProtocol; + + public: + STATIC XTCDECL EFI_STATUS GetEntryPoint(IN PVOID ImagePointer, + OUT PVOID *EntryPoint); + STATIC XTCDECL EFI_STATUS GetFileSize(IN PVOID ImagePointer, + OUT PULONGLONG FileSize); + STATIC XTCDECL EFI_STATUS GetImageSize(IN PVOID ImagePointer, + OUT PUINT ImageSize); + STATIC XTCDECL EFI_STATUS GetMachineType(IN PVOID ImagePointer, + OUT PUSHORT MachineType); + STATIC XTCDECL EFI_STATUS GetSection(IN PVOID ImagePointer, + IN PCHAR SectionName, + OUT PULONG *RawData); + STATIC XTCDECL EFI_STATUS GetSubSystem(IN PVOID ImagePointer, + OUT PUSHORT SubSystem); + STATIC XTCDECL EFI_STATUS GetVersion(IN PVOID ImagePointer, + OUT PUSHORT Version); + STATIC XTCDECL EFI_STATUS InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable); + STATIC XTCDECL EFI_STATUS LoadImage(IN PEFI_FILE_HANDLE FileHandle, + IN LOADER_MEMORY_TYPE MemoryType, + IN PVOID VirtualAddress, + OUT PVOID *ImagePointer); + STATIC XTCDECL EFI_STATUS RelocateImage(IN PVOID ImagePointer, + IN EFI_VIRTUAL_ADDRESS Address); + STATIC XTCDECL EFI_STATUS UnloadImage(IN PVOID ImagePointer); + STATIC XTCDECL EFI_STATUS VerifyImage(IN PVOID ImagePointer); + + private: + STATIC XTCDECL EFI_STATUS RelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image); +}; + +#endif /* __XTLDR_PECOFF_HH */ diff --git a/xtldr/modules/pecoff/pecoff.c b/xtldr/modules/pecoff/pecoff.cc similarity index 86% rename from xtldr/modules/pecoff/pecoff.c rename to xtldr/modules/pecoff/pecoff.cc index 9c36eda..eb1eedb 100644 --- a/xtldr/modules/pecoff/pecoff.c +++ b/xtldr/modules/pecoff/pecoff.cc @@ -1,12 +1,12 @@ /** * PROJECT: ExectOS * COPYRIGHT: See COPYING.md in the top level directory - * FILE: xtldr/modules/pecoff/pecoff.c + * FILE: xtldr/modules/pecoff/pecoff.cc * DESCRIPTION: Basic PE/COFF executable file format support module * DEVELOPERS: Rafal Kupiec */ -#include +#include /* PE/COFF_O module information */ @@ -15,6 +15,7 @@ MODULE_DESCRIPTION(L"Basic PE/COFF executable file format support"); MODULE_LICENSE(L"GPLv3"); MODULE_VERSION(L"0.1"); + /** * Returns the address of the entry point. * @@ -30,10 +31,13 @@ MODULE_VERSION(L"0.1"); */ XTCDECL EFI_STATUS -PeGetEntryPoint(IN PVOID ImagePointer, - OUT PVOID *EntryPoint) +PeCoff::GetEntryPoint(IN PVOID ImagePointer, + OUT PVOID *EntryPoint) { - PPECOFF_IMAGE_CONTEXT Image = ImagePointer; + PPECOFF_IMAGE_CONTEXT Image; + + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->PeHeader) @@ -46,12 +50,12 @@ PeGetEntryPoint(IN PVOID ImagePointer, if(Image->PeHeader->OptionalHeader32.Magic == PECOFF_IMAGE_PE_OPTIONAL_HDR64_MAGIC) { /* Get entry point from 64-bit optional header */ - *EntryPoint = (PUINT8)Image->VirtualAddress + Image->PeHeader->OptionalHeader64.AddressOfEntryPoint; + *EntryPoint = (PUCHAR)Image->VirtualAddress + Image->PeHeader->OptionalHeader64.AddressOfEntryPoint; } else { /* Get entry point from 32-bit optional header */ - *EntryPoint = (PUINT8)Image->VirtualAddress + Image->PeHeader->OptionalHeader32.AddressOfEntryPoint; + *EntryPoint = (PUCHAR)Image->VirtualAddress + Image->PeHeader->OptionalHeader32.AddressOfEntryPoint; } /* Return success */ @@ -73,13 +77,13 @@ PeGetEntryPoint(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeGetFileSize(IN PVOID ImagePointer, - OUT PULONGLONG FileSize) +PeCoff::GetFileSize(IN PVOID ImagePointer, + OUT PULONGLONG FileSize) { PPECOFF_IMAGE_CONTEXT Image; /* Get PE/COFF image pointer*/ - Image = ImagePointer; + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->ImageSize) @@ -108,13 +112,13 @@ PeGetFileSize(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeGetImageSize(IN PVOID ImagePointer, - OUT PUINT ImageSize) +PeCoff::GetImageSize(IN PVOID ImagePointer, + OUT PUINT ImageSize) { PPECOFF_IMAGE_CONTEXT Image; /* Get PE/COFF image pointer*/ - Image = ImagePointer; + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->ImageSize) @@ -143,10 +147,13 @@ PeGetImageSize(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeGetMachineType(IN PVOID ImagePointer, - OUT PUSHORT MachineType) +PeCoff::GetMachineType(IN PVOID ImagePointer, + OUT PUSHORT MachineType) { - PPECOFF_IMAGE_CONTEXT Image = ImagePointer; + PPECOFF_IMAGE_CONTEXT Image; + + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->PeHeader) @@ -178,9 +185,9 @@ PeGetMachineType(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeGetSection(IN PVOID ImagePointer, - IN PCHAR SectionName, - OUT PULONG *RawData) +PeCoff::GetSection(IN PVOID ImagePointer, + IN PCHAR SectionName, + OUT PULONG *RawData) { PPECOFF_IMAGE_SECTION_HEADER SectionHeader; PPECOFF_IMAGE_CONTEXT Image; @@ -188,7 +195,7 @@ PeGetSection(IN PVOID ImagePointer, USHORT SectionIndex; /* Get PE/COFF image pointer*/ - Image = ImagePointer; + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->PeHeader) @@ -221,7 +228,7 @@ PeGetSection(IN PVOID ImagePointer, if(RtlCompareString((PCHAR)SectionHeader[SectionIndex].Name, SectionName, SectionNameLength) == 0) { /* Store section address and return */ - *RawData = Image->Data + SectionHeader[SectionIndex].PointerToRawData; + *RawData = (PULONG)((PUCHAR)Image->Data + SectionHeader[SectionIndex].PointerToRawData); return STATUS_EFI_SUCCESS; } } @@ -245,10 +252,13 @@ PeGetSection(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeGetSubSystem(IN PVOID ImagePointer, - OUT PUSHORT SubSystem) +PeCoff::GetSubSystem(IN PVOID ImagePointer, + OUT PUSHORT SubSystem) { - PPECOFF_IMAGE_CONTEXT Image = ImagePointer; + PPECOFF_IMAGE_CONTEXT Image; + + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->PeHeader) @@ -288,10 +298,13 @@ PeGetSubSystem(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeGetVersion(IN PVOID ImagePointer, - OUT PUSHORT Version) +PeCoff::GetVersion(IN PVOID ImagePointer, + OUT PUSHORT Version) { - PPECOFF_IMAGE_CONTEXT Image = ImagePointer; + PPECOFF_IMAGE_CONTEXT Image; + + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; /* Validate input data */ if(!Image || !Image->PeHeader) @@ -316,6 +329,52 @@ PeGetVersion(IN PVOID ImagePointer, return STATUS_EFI_SUCCESS; } +/** + * Initializes PECOFF module by opening XTLDR protocol and installing PECOFF protocol. + * + * @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 + */ +XTCDECL +EFI_STATUS +PeCoff::InitializeModule(IN EFI_HANDLE ImageHandle, + IN PEFI_SYSTEM_TABLE SystemTable) +{ + EFI_GUID Guid = XT_PECOFF_IMAGE_PROTOCOL_GUID; + EFI_STATUS Status; + + /* Open the XTLDR protocol */ + Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); + if(Status != STATUS_EFI_SUCCESS) + { + /* Failed to open loader protocol */ + return STATUS_EFI_PROTOCOL_ERROR; + } + + /* Set routines available via PE/COFF image protocol */ + PeProtocol.GetEntryPoint = GetEntryPoint; + PeProtocol.GetFileSize = GetFileSize; + PeProtocol.GetImageSize = GetImageSize; + PeProtocol.GetMachineType = GetMachineType; + PeProtocol.GetSection = GetSection; + PeProtocol.GetSubSystem = GetSubSystem; + PeProtocol.GetVersion = GetVersion; + PeProtocol.LoadImage = LoadImage; + PeProtocol.RelocateImage = RelocateImage; + PeProtocol.UnloadImage = UnloadImage; + PeProtocol.VerifyImage = VerifyImage; + + /* Register PE/COFF protocol */ + return XtLdrProtocol->Protocol.Install(&PeProtocol, &Guid); +} + /** * Loads a PE/COFF image file. * @@ -337,10 +396,10 @@ PeGetVersion(IN PVOID ImagePointer, */ XTCDECL EFI_STATUS -PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, - IN LOADER_MEMORY_TYPE MemoryType, - IN PVOID VirtualAddress, - OUT PVOID *ImagePointer) +PeCoff::LoadImage(IN PEFI_FILE_HANDLE FileHandle, + IN LOADER_MEMORY_TYPE MemoryType, + IN PVOID VirtualAddress, + OUT PVOID *ImagePointer) { EFI_GUID FileInfoGuid = EFI_FILE_INFO_PROTOCOL_GUID; PPECOFF_IMAGE_SECTION_HEADER SectionHeader; @@ -433,10 +492,10 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, /* Extract DOS and PE headers */ ImageData->DosHeader = (PPECOFF_IMAGE_DOS_HEADER)Data; - ImageData->PeHeader = (PPECOFF_IMAGE_PE_HEADER)((PUINT8)Data + ImageData->DosHeader->PeHeaderOffset); + ImageData->PeHeader = (PPECOFF_IMAGE_PE_HEADER)((PUCHAR)Data + ImageData->DosHeader->PeHeaderOffset); /* Validate headers */ - Status = PeVerifyImage(ImageData); + Status = PeCoff::VerifyImage(ImageData); if(Status != STATUS_EFI_SUCCESS) { /* Header validation failed, probably broken or invalid PE/COFF image */ @@ -482,7 +541,7 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, } /* Store image data and virtual address */ - ImageData->Data = (PUINT8)(UINT_PTR)Address; + ImageData->Data = (PUCHAR)(UINT_PTR)Address; ImageData->PhysicalAddress = (PVOID)(UINT_PTR)Address; if(VirtualAddress) { @@ -534,7 +593,7 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, if(SectionSize > 0 && SectionHeader[Index].PointerToRawData != 0) { /* Copy section */ - XtLdrProtocol->Memory.CopyMemory((PUINT8)ImageData->Data + SectionHeader[Index].VirtualAddress, + XtLdrProtocol->Memory.CopyMemory((PUCHAR)ImageData->Data + SectionHeader[Index].VirtualAddress, Data + SectionHeader[Index].PointerToRawData, SectionSize); } @@ -542,7 +601,7 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, if(SectionSize < SectionHeader[Index].Misc.VirtualSize) { /* Fill remaining space with zeroes */ - XtLdrProtocol->Memory.ZeroMemory((PUINT8)ImageData->Data + SectionHeader[Index].VirtualAddress + SectionSize, + XtLdrProtocol->Memory.ZeroMemory((PUCHAR)ImageData->Data + SectionHeader[Index].VirtualAddress + SectionSize, SectionHeader[Index].Misc.VirtualSize - SectionSize); } } @@ -551,7 +610,7 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, XtLdrProtocol->Memory.FreePages((EFI_PHYSICAL_ADDRESS)(UINT_PTR)Data, Pages); /* Perform relocation fixups */ - Status = PepRelocateLoadedImage(ImageData); + Status = PeCoff::RelocateLoadedImage(ImageData); if(Status != STATUS_EFI_SUCCESS) { /* Failed to relocate image */ @@ -581,14 +640,16 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, */ XTCDECL EFI_STATUS -PeRelocateImage(IN PVOID ImagePointer, - IN EFI_VIRTUAL_ADDRESS Address) +PeCoff::RelocateImage(IN PVOID ImagePointer, + IN EFI_VIRTUAL_ADDRESS Address) { - PPECOFF_IMAGE_CONTEXT Image = ImagePointer; - - UINT64 ImageBase, OldVirtualAddress; + PPECOFF_IMAGE_CONTEXT Image; + ULONGLONG ImageBase, OldVirtualAddress; EFI_STATUS Status; + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; + /* Store original virtual address */ OldVirtualAddress = (UINT_PTR)Image->VirtualAddress; @@ -606,7 +667,7 @@ PeRelocateImage(IN PVOID ImagePointer, /* Overwrite virtual address and relocate image once again */ Image->VirtualAddress = (PVOID)(Address - OldVirtualAddress + ImageBase); - Status = PepRelocateLoadedImage(Image); + Status = PeCoff::RelocateLoadedImage(Image); if(Status != STATUS_EFI_SUCCESS) { /* Relocation failed */ @@ -620,98 +681,6 @@ PeRelocateImage(IN PVOID ImagePointer, return STATUS_EFI_SUCCESS; } -/** - * Unloads a PE/COFF image file and frees allocated memory. - * - * @param ImagePointer - * Supplies a pointer to the PE/COFF context structure representing the loaded image. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -PeUnloadImage(IN PVOID ImagePointer) -{ - PPECOFF_IMAGE_CONTEXT Image; - EFI_STATUS Status; - - /* Get PE/COFF image pointer*/ - Image = ImagePointer; - - /* Validate input data */ - if(!Image || !Image->Data) - { - /* Invalid parameter passed */ - return STATUS_EFI_INVALID_PARAMETER; - } - - /* Free memory allocated for the image */ - Status = XtLdrProtocol->Memory.FreePages(Image->ImagePages, (EFI_PHYSICAL_ADDRESS)(UINT_PTR)Image->Data); - Status |= XtLdrProtocol->Memory.FreePool(Image); - - /* Return status */ - return Status; -} - -/** - * Validates a PE/COFF image headers. - * - * @param ImagePointer - * Supplies a pointer to the PE/COFF context structure representing the loaded image. - * - * @return This routine returns a status code. - * - * @since XT 1.0 - */ -XTCDECL -EFI_STATUS -PeVerifyImage(IN PVOID ImagePointer) -{ - PPECOFF_IMAGE_CONTEXT Image = ImagePointer; - - /* Validate input data */ - if(!Image || !Image->PeHeader) - { - /* Invalid parameter passed */ - return STATUS_EFI_INVALID_PARAMETER; - } - - /* Validate file size */ - if(Image->FileSize < sizeof(PECOFF_IMAGE_DOS_HEADER)) - { - /* PE/COFF image shorter than DOS header, return error*/ - return STATUS_EFI_END_OF_FILE; - } - - /* Validate DOS header */ - if(Image->DosHeader->Magic != PECOFF_IMAGE_DOS_SIGNATURE) - { - /* Invalid DOS signature, return error */ - return STATUS_EFI_INCOMPATIBLE_VERSION; - } - - /* Validate PE header */ - if(Image->PeHeader->Signature != PECOFF_IMAGE_NT_SIGNATURE && - Image->PeHeader->Signature != PECOFF_IMAGE_XT_SIGNATURE) - { - /* Invalid PE signature, return error */ - return STATUS_EFI_INCOMPATIBLE_VERSION; - } - - /* Validate optional header */ - if(Image->PeHeader->OptionalHeader32.Magic != PECOFF_IMAGE_PE_OPTIONAL_HDR32_MAGIC && - Image->PeHeader->OptionalHeader64.Magic != PECOFF_IMAGE_PE_OPTIONAL_HDR64_MAGIC) - { - /* Invalid optional header signature, return error */ - return STATUS_EFI_INCOMPATIBLE_VERSION; - } - - /* Return success */ - return STATUS_EFI_SUCCESS; -} - /** * Relocates a loaded PE/COFF image. * @@ -724,15 +693,15 @@ PeVerifyImage(IN PVOID ImagePointer) */ XTCDECL EFI_STATUS -PepRelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image) +PeCoff::RelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image) { PPECOFF_IMAGE_BASE_RELOCATION RelocationDir, RelocationEnd; PPECOFF_IMAGE_DATA_DIRECTORY DataDirectory; USHORT Offset, Type, Count; PUSHORT TypeOffset; - UINT64 ImageBase; - PUINT32 Address; - PUINT64 LongPtr; + ULONGLONG ImageBase; + PUINT Address; + PULONGLONG LongPtr; PUINT ShortPtr; /* Make sure image is not stripped */ @@ -793,7 +762,7 @@ PepRelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image) Type = *TypeOffset >> 12; /* Check if end of the loaded address reached */ - if((PVOID)(PUSHORT)(Address + Offset) >= Image->Data + Image->ImageSize) + if((PVOID)(PUSHORT)(Address + Offset) >= (PUCHAR)Image->Data + Image->ImageSize) { /* Do not relocate after the end of loaded image */ break; @@ -816,7 +785,7 @@ PepRelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image) break; case PECOFF_IMAGE_REL_BASED_HIGHLOW: /* 32-bit relocation of hight and low half of address */ - ShortPtr = (PUINT32)((PUCHAR)Address + Offset); + ShortPtr = (PUINT)((PUCHAR)Address + Offset); *ShortPtr = *ShortPtr - ImageBase + (UINT_PTR)Image->VirtualAddress; break; default: @@ -836,6 +805,101 @@ PepRelocateLoadedImage(IN PPECOFF_IMAGE_CONTEXT Image) return STATUS_EFI_SUCCESS; } +/** + * Unloads a PE/COFF image file and frees allocated memory. + * + * @param ImagePointer + * Supplies a pointer to the PE/COFF context structure representing the loaded image. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +PeCoff::UnloadImage(IN PVOID ImagePointer) +{ + PPECOFF_IMAGE_CONTEXT Image; + EFI_STATUS Status; + + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; + + /* Validate input data */ + if(!Image || !Image->Data) + { + /* Invalid parameter passed */ + return STATUS_EFI_INVALID_PARAMETER; + } + + /* Free memory allocated for the image */ + Status = XtLdrProtocol->Memory.FreePages(Image->ImagePages, (EFI_PHYSICAL_ADDRESS)(UINT_PTR)Image->Data); + Status |= XtLdrProtocol->Memory.FreePool(Image); + + /* Return status */ + return Status; +} + +/** + * Validates a PE/COFF image headers. + * + * @param ImagePointer + * Supplies a pointer to the PE/COFF context structure representing the loaded image. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +PeCoff::VerifyImage(IN PVOID ImagePointer) +{ + PPECOFF_IMAGE_CONTEXT Image; + + /* Get PE/COFF image pointer*/ + Image = (PPECOFF_IMAGE_CONTEXT)ImagePointer; + + /* Validate input data */ + if(!Image || !Image->PeHeader) + { + /* Invalid parameter passed */ + return STATUS_EFI_INVALID_PARAMETER; + } + + /* Validate file size */ + if(Image->FileSize < sizeof(PECOFF_IMAGE_DOS_HEADER)) + { + /* PE/COFF image shorter than DOS header, return error*/ + return STATUS_EFI_END_OF_FILE; + } + + /* Validate DOS header */ + if(Image->DosHeader->Magic != PECOFF_IMAGE_DOS_SIGNATURE) + { + /* Invalid DOS signature, return error */ + return STATUS_EFI_INCOMPATIBLE_VERSION; + } + + /* Validate PE header */ + if(Image->PeHeader->Signature != PECOFF_IMAGE_NT_SIGNATURE && + Image->PeHeader->Signature != PECOFF_IMAGE_XT_SIGNATURE) + { + /* Invalid PE signature, return error */ + return STATUS_EFI_INCOMPATIBLE_VERSION; + } + + /* Validate optional header */ + if(Image->PeHeader->OptionalHeader32.Magic != PECOFF_IMAGE_PE_OPTIONAL_HDR32_MAGIC && + Image->PeHeader->OptionalHeader64.Magic != PECOFF_IMAGE_PE_OPTIONAL_HDR64_MAGIC) + { + /* Invalid optional header signature, return error */ + return STATUS_EFI_INCOMPATIBLE_VERSION; + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + /** * This routine is the entry point of the XT EFI boot loader module. * @@ -854,30 +918,6 @@ EFI_STATUS XtLdrModuleMain(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { - EFI_GUID Guid = XT_PECOFF_IMAGE_PROTOCOL_GUID; - EFI_STATUS Status; - - /* Open the XTLDR protocol */ - Status = BlGetXtLdrProtocol(SystemTable, ImageHandle, &XtLdrProtocol); - if(Status != STATUS_EFI_SUCCESS) - { - /* Failed to open loader protocol */ - return STATUS_EFI_PROTOCOL_ERROR; - } - - /* Set routines available via PE/COFF image protocol */ - PeCoffProtocol.GetEntryPoint = PeGetEntryPoint; - PeCoffProtocol.GetFileSize = PeGetFileSize; - PeCoffProtocol.GetImageSize = PeGetImageSize; - PeCoffProtocol.GetMachineType = PeGetMachineType; - PeCoffProtocol.GetSection = PeGetSection; - PeCoffProtocol.GetSubSystem = PeGetSubSystem; - PeCoffProtocol.GetVersion = PeGetVersion; - PeCoffProtocol.LoadImage = PeLoadImage; - PeCoffProtocol.RelocateImage = PeRelocateImage; - PeCoffProtocol.UnloadImage = PeUnloadImage; - PeCoffProtocol.VerifyImage = PeVerifyImage; - - /* Register PE/COFF protocol */ - return XtLdrProtocol->Protocol.Install(&PeCoffProtocol, &Guid); + /* Initialize PECOFF module */ + return PeCoff::InitializeModule(ImageHandle, SystemTable); } diff --git a/xtldr/protocol.cc b/xtldr/protocol.cc index 398a265..a7c4495 100644 --- a/xtldr/protocol.cc +++ b/xtldr/protocol.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/shell.cc b/xtldr/shell.cc index 1ff7a88..eb86649 100644 --- a/xtldr/shell.cc +++ b/xtldr/shell.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /** diff --git a/xtldr/textui.cc b/xtldr/textui.cc index f7355c5..f187244 100644 --- a/xtldr/textui.cc +++ b/xtldr/textui.cc @@ -7,7 +7,7 @@ * Aiken Harris */ -#include +#include /** diff --git a/xtldr/volume.cc b/xtldr/volume.cc index db1b0d6..8e55b2d 100644 --- a/xtldr/volume.cc +++ b/xtldr/volume.cc @@ -7,7 +7,7 @@ * Aiken Harris */ -#include +#include /** diff --git a/xtldr/xtldr.cc b/xtldr/xtldr.cc index 7f0cae6..edb428c 100644 --- a/xtldr/xtldr.cc +++ b/xtldr/xtldr.cc @@ -6,7 +6,7 @@ * DEVELOPERS: Rafal Kupiec */ -#include +#include /**