_In_ PHYSICAL_ADDRESS Address
);
+NTSTATUS
+MmPapFreePages (
+ _In_ PVOID Address,
+ _In_ ULONG WhichList
+ );
+
NTSTATUS
MmPapAllocatePagesInRange (
_Inout_ PVOID* PhysicalAddress,
_In_ ULONGLONG Size
);
+BOOLEAN
+BlMmTranslateVirtualAddress (
+ _In_ PVOID VirtualAddress,
+ _Out_ PPHYSICAL_ADDRESS PhysicalAddress
+ );
+
/* BLOCK ALLOCATOR ROUTINES **************************************************/
NTSTATUS
if (*BufferSize)
{
- EfiPrintf(L"Aligned free not yet implemented\r\n");
- Status = STATUS_NOT_IMPLEMENTED;
- //Status = MmPapFreePages(*Buffer, 1);
+ Status = MmPapFreePages(*Buffer, BL_MM_INCLUDE_MAPPED_ALLOCATED);
*Buffer = NULL;
*BufferSize = 0;
/* Free the prefetch buffer is one was allocated */
if (BlockIopPrefetchBuffer)
{
- EfiPrintf(L"Failure path not implemented %lx\r\n", Status);
- //MmPapFreePages(BlockIopPrefetchBuffer, 1);
+ MmPapFreePages(BlockIopPrefetchBuffer, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
}
}
/* Unmap the hive */
- //MmPapFreePages(KeyHive->ImageBase, 1);
- EfiPrintf(L"Leaking hive memory\r\n");
+ MmPapFreePages(KeyHive->BaseBlock, BL_MM_INCLUDE_MAPPED_ALLOCATED);
/* Free the hive and hive path */
BlMmFreeHeap(KeyHive->FilePath);
RtlCopyMemory(NewBaseBlock, BaseBlock, HiveSize);
/* Free the old data */
- EfiPrintf(L"Leaking old hive buffer\r\n");
- //MmPapFreePages(BaseBlock, 1);
+ MmPapFreePages(BaseBlock, BL_MM_INCLUDE_MAPPED_ALLOCATED);
/* Update our pointers */
BaseBlock = NewBaseBlock;
/* If we had logging data, free it */
if (LogData)
{
- EfiPrintf(L"Leaking log buffer\r\n");
- //MmPapFreePages(LogData, 1);
+ MmPapFreePages(LogData, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
/* Check if this is the failure path */
/* If we mapped the hive, free it */
if (BaseBlock)
{
- EfiPrintf(L"Leaking base block on failure\r\n");
- //MmPapFreePages(BaseBlock, 1u);
+ MmPapFreePages(BaseBlock, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
/* If we opened the device, close it */
if (File->Flags & BL_IMG_REMOTE_FILE)
{
/* Then only free the memory in that scenario */
- EfiPrintf(L"TODO\r\n");
- //return MmPapFreePages(File->BaseAddress, TRUE);
+ return MmPapFreePages(File->BaseAddress, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
}
_In_ ULONG ImageFlags
)
{
- EfiPrintf(L"leaking the shit out of %p\r\n", ImageBase);
- return STATUS_NOT_IMPLEMENTED;
+ PHYSICAL_ADDRESS PhysicalAddress;
+ NTSTATUS Status;
+
+ /* Make sure required parameters are present */
+ if (!(ImageBase) || !(ImageSize))
+ {
+ return STATUS_INVALID_PARAMETER;
+ }
+
+ /* Check if this was a physical allocation */
+ if (!(ImageFlags & BL_LOAD_IMG_VIRTUAL_BUFFER))
+ {
+ return MmPapFreePages(ImageBase, BL_MM_INCLUDE_MAPPED_ALLOCATED);
+ }
+
+ /* It's virtual, so translate it first */
+ if (!BlMmTranslateVirtualAddress(ImageBase, &PhysicalAddress))
+ {
+ return STATUS_INVALID_PARAMETER;
+ }
+
+ /* Unmap the virtual mapping */
+ Status = BlMmUnmapVirtualAddressEx(ImageBase, ROUND_TO_PAGES(ImageSize));
+ if (NT_SUCCESS(Status))
+ {
+ /* Now free the physical pages */
+ Status = BlMmFreePhysicalPages(PhysicalAddress);
+ }
+
+ /* All done */
+ return Status;
}
NTSTATUS
else
{
/* Free the physical buffer */
- //MmPapFreePages(VirtualAddress, 1);
- EfiPrintf(L"Leaking memory\r\n");
+ MmPapFreePages(BaseAddress, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
}
if (HashBuffer)
{
/* Free it */
- EfiPrintf(L"Leadking hash: %p\r\n", HashBuffer);
- //MmPapFreePages(HashBuffer, TRUE);
+ MmPapFreePages(HashBuffer, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
/* Check if we have a certificate directory */
else
{
/* Into a physical buffer -- free it */
- EfiPrintf(L"Leaking physical pages\r\n");
- // MmPapFreePages(VirtualAddress, TRUE);
+ MmPapFreePages(VirtualAddress, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
}
}
if (BootData)
{
/* Free it */
- //MmPapFreePages(bootData, TRUE);
+ MmPapFreePages(BootData, BL_MM_INCLUDE_MAPPED_ALLOCATED);
}
/* All done */
if ((NT_SUCCESS(Status)) && (MmTranslationType != BlNone))
{
/* TODO */
+ EfiPrintf(L"unhandled virtual path\r\n");
Status = STATUS_NOT_IMPLEMENTED;
}
}
return Status;
}
+BOOLEAN
+BlMmTranslateVirtualAddress (
+ _In_ PVOID VirtualAddress,
+ _Out_ PPHYSICAL_ADDRESS PhysicalAddress
+ )
+{
+ /* Make sure arguments are present */
+ if (!(VirtualAddress) || !(PhysicalAddress))
+ {
+ return FALSE;
+ }
+
+ EfiPrintf(L"Unhandled virtual path\r\n");
+ return FALSE;
+ //return MmArchTranslateVirtualAddress(VirtualAddress, PhysicalAddress, NULL);
+}
+
NTSTATUS
BlpMmInitialize (
_In_ PBL_MEMORY_DATA MemoryData,
/* Does the memory we received not exactly fall onto the beginning of its descriptor? */
if (LocalDescriptor.BasePage != FoundDescriptor->BasePage)
{
- EfiPrintf(L"Local Page: %08I64X Found Page: %08I64X\r\n", LocalDescriptor.BasePage, FoundDescriptor->BasePage);
TempDescriptor = MmMdInitByteGranularDescriptor(FoundDescriptor->Flags,
FoundDescriptor->Type,
FoundDescriptor->BasePage,
LocalDescriptor.VirtualPage + LocalDescriptor.PageCount : 0;
if (LocalEndPage != FoundEndPage)
{
- EfiPrintf(L"Local Page: %08I64X Found Page: %08I64X\r\n", LocalEndPage, FoundEndPage);
TempDescriptor = MmMdInitByteGranularDescriptor(FoundDescriptor->Flags,
FoundDescriptor->Type,
LocalEndPage,
)
{
/* Call the physical allocator */
- EfiPrintf(L"Leaking memory!\r\n");
+ EfiPrintf(L"Leaking memory: %p!\r\n", Address.QuadPart);
return STATUS_SUCCESS;
//return MmPapFreePhysicalPages(4, 0, Address);
}
+NTSTATUS
+MmPapFreePages (
+ _In_ PVOID Address,
+ _In_ ULONG WhichList
+ )
+{
+ EfiPrintf(L"Leaking memory: %p!\r\n", Address);
+ return STATUS_SUCCESS;
+}
+
NTSTATUS
BlMmGetMemoryMap (
_In_ PLIST_ENTRY MemoryMap,