Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Fix memory allocation problem on Win 2004 using the most meme method …
Browse files Browse the repository at this point in the history
…possible
  • Loading branch information
SamuelTulach committed Jul 6, 2020
1 parent cd25c7a commit 5a3e74f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 70 deletions.
56 changes: 2 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ client/efi-mapper/
## Compiling
Compiling any of the example client programs is pretty simple. Open the solution file in Visual Studio and compile the project with it's default settings.

Compiling the driver is also pretty simple. First you need a working Linux install (or you can use Linux subsystem for Windows) and install gnu-efi (commands for Arch Linux):
Compiling the driver is also pretty simple. First you need a working Linux install (or you can use Linux subsystem for Windows) and install gnu-efi (commands for Ubuntu 20.04):
```
sudo pacman -S gnu-efi-libs
sudo apt install gnu-efi build-essential
```
That's all you need to install. Package manager (in the example apt) should take care of all the depencies for you. Once the installation is complete, clone this repo (make sure you have git installed):
```
Expand All @@ -30,58 +30,6 @@ make
```
If the compile was successful, you should now see memory.efi in the driver folder.

**Note:** Some people were reporting that they were unable to compile the driver with some errors related to GUIDs (passing them in as a pointer). If you are having the same issues please make sure that you are using latest gcc and gnu-efi libs. Ubuntu and Debian have older versions of them and therefore require you to manually compile and install latest versions.

```
[q@propc:~]$ pacman -Q --info gnu-efi-libs
Name : gnu-efi-libs
Version : 3.0.11-2
Description : Library for building UEFI Applications using GNU toolchain
Architecture : x86_64
URL : https://sourceforge.net/projects/gnu-efi/
Licenses : GPL
Groups : None
Provides : None
Depends On : None
Optional Deps : None
Required By : None
Optional For : None
Conflicts With : None
Replaces : None
Installed Size : 1943.01 KiB
Packager : Felix Yan <[email protected]>
Build Date : Sat 16 May 2020 12:57:49 PM CEST
Install Date : Tue 19 May 2020 03:12:17 PM CEST
Install Reason : Explicitly installed
Install Script : No
Validated By : Signature
[q@propc:~]$ pacman -Q --info gcc
Name : gcc
Version : 10.1.0-1
Description : The GNU Compiler Collection - C and C++ frontends
Architecture : x86_64
URL : https://gcc.gnu.org
Licenses : GPL LGPL FDL custom
Groups : base-devel
Provides : gcc-multilib
Depends On : gcc-libs=10.1.0-1 binutils>=2.28 libmpc
Optional Deps : lib32-gcc-libs: for generating code for 32-bit ABI [installed]
Required By : clang dkms
Optional For : clion xorg-xrdb
Conflicts With : None
Replaces : gcc-multilib
Installed Size : 147.19 MiB
Packager : Bartłomiej Piotrowski <[email protected]>
Build Date : Fri 08 May 2020 01:14:50 PM CEST
Install Date : Sat 16 May 2020 02:55:54 PM CEST
Install Reason : Explicitly installed
Install Script : No
Validated By : Signature
[q@propc:~]$
```

## Usage
In order to use the efi-memory driver, you need to load it. First, obtain a copy of memory.efi ([compile it](https://github.com/SamuelTulach/efi-memory#compiling) or [download it from release section](https://github.com/SamuelTulach/efi-memory/releases)) and a copy of [EDK2 efi shell](https://github.com/tianocore/edk2/releases). Now follow these steps:

Expand Down
30 changes: 14 additions & 16 deletions driver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#include <efi.h>
#include <efilib.h>

// Since Windows does not want to allocate execusable memory for our driver
// in new versions of the OS, then we have to do it ourselves (I guess)
// If you are on Windows 1909 and bellow, please use older version of the mapper
// that use ExAllocatePool
#define DRIVER_SIZE 26214400 // 25mb should be enough
__attribute__((section(".text"))) char DriverBuffer[DRIVER_SIZE]; // It has to be in the text section so it's executable (surprise)

// Our protocol GUID (should be different for every driver)
static const EFI_GUID ProtocolGuid
= { 0x2b479eea, 0x0ecf, 0x4a46, {0x96, 0x84, 0x8f, 0x14, 0xed, 0x92, 0xd9, 0xec} };
Expand Down Expand Up @@ -75,22 +82,13 @@ RunCommand(MemoryCommand* cmd)
return EFI_SUCCESS;
}

// Call ExAllocatePool
// "Allocate" memory
if (cmd->operation == 1)
{
void* function = cmd->data[0]; // Pointer to the function (supplied by client)
ExAllocatePool exalloc = (ExAllocatePool)function;
int temp = cmd->data[1]; // gcc you ok?
uintptr_t allocbase = exalloc(temp, cmd->data[2]);
*(uintptr_t*)cmd->data[3] = allocbase;
}

// Call ExFreePool
if (cmd->operation == 2)
{
void* function = cmd->data[0];
ExFreePool exfree = (ExFreePool)function;
exfree(cmd->data[1]);
if (cmd->data[2] < DRIVER_SIZE) // Only small driver allowed, big drivers bad
{
*(uintptr_t*)cmd->data[3] = &DriverBuffer; // Get rekt windows
}
}

// Call any void function (__stdcall)
Expand Down Expand Up @@ -307,7 +305,7 @@ efi_main(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
TPL_NOTIFY,
SetVirtualAddressMapEvent,
NULL,
&VirtualGuid,
VirtualGuid,
&NotifyEvent);

// Return if event create failed
Expand All @@ -322,7 +320,7 @@ efi_main(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
TPL_NOTIFY,
ExitBootServicesEvent,
NULL,
&ExitGuid,
ExitGuid,
&ExitEvent);

// Return if event create failed (yet again)
Expand Down
Binary file removed driver/main.o
Binary file not shown.
Binary file removed driver/memory.efi
Binary file not shown.
Binary file removed driver/memory.so
Binary file not shown.

0 comments on commit 5a3e74f

Please sign in to comment.