diff --git a/xtldr2/protocol.c b/xtldr2/protocol.c new file mode 100644 index 0000000..6e8d1bb --- /dev/null +++ b/xtldr2/protocol.c @@ -0,0 +1,111 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtldr/protocol.c + * DESCRIPTION: XT Boot Loader protocol support + * DEVELOPERS: Rafal Kupiec + */ + +#include + + +/** + * This routine locates and opens the requested XT boot loader protocol. + * + * @param ProtocolHandler + * Supplies the address where a pointer to the opened protocol is returned. + * + * @param ProtocolGuid + * Supplies a pointer to the unique protocol GUID. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlOpenXtProtocol(OUT PVOID *ProtocolHandler, + IN PEFI_GUID ProtocolGuid) +{ + PEFI_HANDLE Handles = NULL; + EFI_STATUS Status; + UINT_PTR Count; + UINT Index; + + /* Try to locate the handles */ + Status = EfiSystemTable->BootServices->LocateHandleBuffer(ByProtocol, ProtocolGuid, NULL, &Count, &Handles); + if(Status != STATUS_EFI_SUCCESS) + { + /* Unable to get handles */ + return Status; + } + + /* Check if any handles returned */ + if(Count > 0) + { + /* Iterate through all given handles */ + for(Index = 0; Index < Count; Index++) + { + /* Try to open protocol */ + Status = EfiSystemTable->BootServices->OpenProtocol(Handles[Index], ProtocolGuid, + ProtocolHandler, EfiImageHandle, NULL, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); + + /* Check if successfully opened the loader protocol */ + if(Status == STATUS_EFI_SUCCESS) + { + /* Protocol found and successfully opened */ + break; + } + } + } + + /* Free handles */ + EfiSystemTable->BootServices->FreePool(Handles); + + /* Make sure the loaded protocol has been found */ + if(*ProtocolHandler == NULL) + { + /* Protocol not found */ + return STATUS_EFI_NOT_FOUND; + } + + /* Return success */ + return STATUS_EFI_SUCCESS; +} + +/** + * This routine registers XTLDR protocol for further usage by modules. + * + * @return This routine returns status code. + * + * @since XT 1.0 + */ +XTCDECL +EFI_STATUS +BlpRegisterXtLoaderProtocol() +{ + EFI_GUID Guid = XT_BOOT_LOADER_PROTOCOL_GUID; + XTBL_LOADER_PROTOCOL LdrProtocol; + EFI_HANDLE Handle = NULL; + + /* Set all routines available via loader protocol */ + LdrProtocol.Console.ClearScreen = BlConsoleClearScreen; + LdrProtocol.Console.DisableCursor = BlConsoleDisableCursor; + LdrProtocol.Console.EnableCursor = BlConsoleEnableCursor; + LdrProtocol.Console.Print = BlConsolePrint; + LdrProtocol.Debug.Print = BlDebugPrint; + LdrProtocol.Memory.AllocatePages = BlMemoryAllocatePages; + LdrProtocol.Memory.AllocatePool = BlMemoryAllocatePool; + LdrProtocol.Memory.FreePages = BlMemoryFreePages; + LdrProtocol.Memory.FreePool = BlMemoryFreePool; + LdrProtocol.Protocol.Open = BlOpenXtProtocol; + LdrProtocol.Util.ExitBootServices = BlExitBootServices; + LdrProtocol.Util.GetImageHandle = BlGetEfiImageHandle; + LdrProtocol.Util.GetSystemTable = BlGetEfiSystemTable; + LdrProtocol.Util.SleepExecution = BlSleepExecution; + + /* Register XTLDR loader protocol */ + BlDebugPrint(L"Registering XT loader protocol\n"); + return EfiSystemTable->BootServices->InstallProtocolInterface(&Handle, &Guid, EFI_NATIVE_INTERFACE, &LdrProtocol); +}