Skip to content

Commit

Permalink
Ring3: Added SysCallHandleProtocol and
Browse files Browse the repository at this point in the history
SIMPLE_FILE_SYSTEM_PROTOCOL wrapper.
  • Loading branch information
Mikhail Krichanov committed Feb 23, 2024
1 parent 9d797a6 commit 0068fb9
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 6 deletions.
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Dxe/DxeRing3/DxeRing3.inf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES
gEfiDiskIoProtocolGuid ## SOMETIMES_CONSUMES
gEfiBlockIoProtocolGuid ## SOMETIMES_CONSUMES
gEfiDevicePathProtocolGuid ## SOMETIMES_CONSUMES

[Depex]
TRUE
24 changes: 21 additions & 3 deletions MdeModulePkg/Core/Dxe/DxeRing3/Ring3UefiBootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,31 @@ Ring3UninstallProtocolInterface (
EFI_STATUS
EFIAPI
Ring3HandleProtocol (
IN EFI_HANDLE UserHandle,
IN EFI_HANDLE CoreUserHandle,
IN EFI_GUID *Protocol,
OUT VOID **Interface
)
{
return EFI_UNSUPPORTED;
EFI_STATUS Status;

Status = SysCall (
SysCallHandleProtocol,
CoreUserHandle,
Protocol,
Interface
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Ring3: Failed to get handle of protocol %g - %r\n", Protocol, Status));
return Status;
}

if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid)) {
Status = EFI_SUCCESS;
} else {
Status = EFI_UNSUPPORTED;
}

return Status;
}

EFI_STATUS
Expand Down Expand Up @@ -397,7 +416,6 @@ Ring3OpenProtocol (
Attributes
);
if (EFI_ERROR (Status)) {
// DEBUG ((DEBUG_ERROR, "Ring3: Failed to open protocol %g - %r\n", Protocol, Status));
return Status;
}

Expand Down
57 changes: 56 additions & 1 deletion MdeModulePkg/Core/Dxe/SysCall/BootServices.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ FindGuid (
*Core = &gEfiComponentNameProtocolGuid;
*CoreSize = sizeof (EFI_COMPONENT_NAME_PROTOCOL);

} else if (CompareGuid (Ring3, &gEfiDevicePathProtocolGuid)) {

*Core = &gEfiDevicePathProtocolGuid;
*CoreSize = sizeof (EFI_DEVICE_PATH_PROTOCOL);

} else if (CompareGuid (Ring3, &gEfiSimpleFileSystemProtocolGuid)) {

*Core = &gEfiSimpleFileSystemProtocolGuid;
*CoreSize = sizeof (EFI_SIMPLE_FILE_SYSTEM_PROTOCOL);

} else {
DEBUG ((DEBUG_ERROR, "Ring0: Unknown protocol.\n"));
return EFI_NOT_FOUND;
Expand Down Expand Up @@ -130,7 +140,8 @@ CallBootService (
VOID *CoreArgList[MAX_LIST];
EFI_HANDLE CoreHandle;

EFI_DRIVER_BINDING_PROTOCOL *CoreDriverBinding;
EFI_DRIVER_BINDING_PROTOCOL *CoreDriverBinding;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *CoreSimpleFileSystem;
//
// TODO: Check User variables.
//
Expand Down Expand Up @@ -258,6 +269,16 @@ CallBootService (
CoreDriverBinding->Supported = CoreDriverBindingSupported;
CoreDriverBinding->Start = CoreDriverBindingStart;
CoreDriverBinding->Stop = CoreDriverBindingStop;
} else if (CompareGuid ((EFI_GUID *)CoreArgList[Index], &gEfiSimpleFileSystemProtocolGuid)) {
CoreSimpleFileSystem = (EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *)CoreArgList[Index + 1];

mRing3SimpleFileSystemProtocol.OpenVolume = CoreSimpleFileSystem->OpenVolume;

CoreSimpleFileSystem->OpenVolume = CoreOpenVolume;

DisableSMAP ();
mRing3SimpleFileSystemPointer = (EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *)UserArgList[Index + 1];
EnableSMAP ();
}
}

Expand Down Expand Up @@ -324,6 +345,40 @@ CallBootService (

return Status;

case SysCallHandleProtocol:
//
// Argument 1: EFI_HANDLE CoreUserHandle
// Argument 2: EFI_GUID *Protocol
// Argument 3: VOID **Interface
//
DisableSMAP ();
Status = FindGuid ((EFI_GUID *)CoreRbp->Argument2, &CoreProtocol, &MemoryCoreSize);
EnableSMAP ();
if (EFI_ERROR (Status)) {
return Status;
}

Status = gBS->HandleProtocol (
(EFI_HANDLE)CoreRbp->Argument1,
CoreProtocol,
&Interface
);

DisableSMAP ();
if (Interface != NULL) {

Interface = AllocateRing3Copy (Interface, MemoryCoreSize, MemoryCoreSize);
if (Interface == NULL) {
EnableSMAP ();
return EFI_OUT_OF_RESOURCES;
}
}

*(VOID **)CoreRbp->Argument3 = Interface;
EnableSMAP ();

return Status;

case SysCallBlockIoReset:
//
// Argument 1: EFI_BLOCK_IO_PROTOCOL *This
Expand Down
231 changes: 230 additions & 1 deletion MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

#include "DxeMain.h"

EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;
EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mRing3SimpleFileSystemProtocol;
EFI_FILE_PROTOCOL mRing3FileProtocol;

EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *mRing3SimpleFileSystemPointer;
EFI_FILE_PROTOCOL *mRing3FilePointer;

EFI_STATUS
EFIAPI
Expand Down Expand Up @@ -180,3 +185,227 @@ CoreDriverBindingStop (

return Status;
}

EFI_STATUS
EFIAPI
CoreFileOpen (
IN EFI_FILE_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **NewHandle,
IN CHAR16 *FileName,
IN UINT64 OpenMode,
IN UINT64 Attributes
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileClose (
IN EFI_FILE_PROTOCOL *This
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileDelete (
IN EFI_FILE_PROTOCOL *This
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileRead (
IN EFI_FILE_PROTOCOL *This,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileWrite (
IN EFI_FILE_PROTOCOL *This,
IN OUT UINTN *BufferSize,
IN VOID *Buffer
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileSetPosition (
IN EFI_FILE_PROTOCOL *This,
IN UINT64 Position
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileGetPosition (
IN EFI_FILE_PROTOCOL *This,
OUT UINT64 *Position
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileGetInfo (
IN EFI_FILE_PROTOCOL *This,
IN EFI_GUID *InformationType,
IN OUT UINTN *BufferSize,
OUT VOID *Buffer
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileSetInfo (
IN EFI_FILE_PROTOCOL *This,
IN EFI_GUID *InformationType,
IN UINTN BufferSize,
IN VOID *Buffer
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileFlush (
IN EFI_FILE_PROTOCOL *This
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileOpenEx (
IN EFI_FILE_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **NewHandle,
IN CHAR16 *FileName,
IN UINT64 OpenMode,
IN UINT64 Attributes,
IN OUT EFI_FILE_IO_TOKEN *Token
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileReadEx (
IN EFI_FILE_PROTOCOL *This,
IN OUT EFI_FILE_IO_TOKEN *Token
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileWriteEx (
IN EFI_FILE_PROTOCOL *This,
IN OUT EFI_FILE_IO_TOKEN *Token
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreFileFlushEx (
IN EFI_FILE_PROTOCOL *This,
IN OUT EFI_FILE_IO_TOKEN *Token
)
{
return EFI_UNSUPPORTED;
}

EFI_STATUS
EFIAPI
CoreOpenVolume (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **Root
)
{
EFI_STATUS Status;
EFI_FILE_PROTOCOL **Ring3Root;

DisableSMAP ();
Status = CoreAllocatePool (EfiRing3MemoryType, sizeof (EFI_FILE_PROTOCOL *), (VOID **)&Ring3Root);
EnableSMAP ();
if (EFI_ERROR (Status)) {
return EFI_OUT_OF_RESOURCES;
}

Status = GoToRing3 (
2,
(VOID *)mRing3SimpleFileSystemProtocol.OpenVolume,
mRing3SimpleFileSystemPointer,
Ring3Root
);
if (EFI_ERROR (Status)) {
*Root = NULL;
DisableSMAP ();
FreePool (Ring3Root);
EnableSMAP ();
return Status;
}

*Root = AllocatePool (sizeof (EFI_FILE_PROTOCOL));

DisableSMAP ();
mRing3FileProtocol.Revision = (*Ring3Root)->Revision;
mRing3FileProtocol.Open = (*Ring3Root)->Open;
mRing3FileProtocol.Close = (*Ring3Root)->Close;
mRing3FileProtocol.Delete = (*Ring3Root)->Delete;
mRing3FileProtocol.Read = (*Ring3Root)->Read;
mRing3FileProtocol.Write = (*Ring3Root)->Write;
mRing3FileProtocol.GetPosition = (*Ring3Root)->GetPosition;
mRing3FileProtocol.SetPosition = (*Ring3Root)->SetPosition;
mRing3FileProtocol.GetInfo = (*Ring3Root)->GetInfo;
mRing3FileProtocol.SetInfo = (*Ring3Root)->SetInfo;
mRing3FileProtocol.Flush = (*Ring3Root)->Flush;
mRing3FileProtocol.OpenEx = (*Ring3Root)->OpenEx;
mRing3FileProtocol.ReadEx = (*Ring3Root)->ReadEx;
mRing3FileProtocol.WriteEx = (*Ring3Root)->WriteEx;
mRing3FileProtocol.FlushEx = (*Ring3Root)->FlushEx;

mRing3FilePointer = *Ring3Root;

FreePool (Ring3Root);
EnableSMAP ();

(*Root)->Revision = mRing3FileProtocol.Revision;
(*Root)->Open = CoreFileOpen;
(*Root)->Close = CoreFileClose;
(*Root)->Delete = CoreFileDelete;
(*Root)->Read = CoreFileRead;
(*Root)->Write = CoreFileWrite;
(*Root)->GetPosition = CoreFileGetPosition;
(*Root)->SetPosition = CoreFileSetPosition;
(*Root)->GetInfo = CoreFileGetInfo;
(*Root)->SetInfo = CoreFileSetInfo;
(*Root)->Flush = CoreFileFlush;
(*Root)->OpenEx = CoreFileOpenEx;
(*Root)->ReadEx = CoreFileReadEx;
(*Root)->WriteEx = CoreFileWriteEx;
(*Root)->FlushEx = CoreFileFlushEx;

return Status;
}
11 changes: 10 additions & 1 deletion MdeModulePkg/Core/Dxe/SysCall/SupportedProtocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <Protocol/DevicePathUtilities.h>
#include <Protocol/DiskIo.h>

extern EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;
extern EFI_DRIVER_BINDING_PROTOCOL mRing3DriverBindingProtocol;
extern EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mRing3SimpleFileSystemProtocol;
extern EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *mRing3SimpleFileSystemPointer;

EFI_STATUS
EFIAPI
Expand All @@ -36,3 +38,10 @@ CoreDriverBindingStop (
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
);

EFI_STATUS
EFIAPI
CoreOpenVolume (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
OUT EFI_FILE_PROTOCOL **Root
);
Loading

0 comments on commit 0068fb9

Please sign in to comment.