This commit is contained in:
longpanda 2020-07-03 23:20:35 +08:00
parent 0ddc76a3aa
commit 1d034f0a24
33 changed files with 2364 additions and 140 deletions

View file

@ -0,0 +1,179 @@
/******************************************************************************
* Memhole.c
*
* Copyright (c) 2020, longpanda <admin@ventoy.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Protocol/LoadedImage.h>
#include <Guid/FileInfo.h>
#include <Guid/FileSystemInfo.h>
#include <Protocol/BlockIo.h>
#include <Protocol/RamDisk.h>
#include <Protocol/SimpleFileSystem.h>
#include <VtoyUtil.h>
STATIC BOOLEAN IsMemContiguous
(
IN CONST EFI_MEMORY_DESCRIPTOR *Prev,
IN CONST EFI_MEMORY_DESCRIPTOR *Curr,
IN CONST EFI_MEMORY_DESCRIPTOR *Next
)
{
UINTN Addr1 = 0;
UINTN Addr2 = 0;
if (Prev == NULL || Curr == NULL || Next == NULL)
{
return FALSE;
}
if (Prev->Type == EfiBootServicesData &&
Curr->Type == EfiConventionalMemory &&
Next->Type == EfiBootServicesData)
{
Addr1 = Prev->PhysicalStart + MultU64x64(SIZE_4KB, Prev->NumberOfPages);
Addr2 = Curr->PhysicalStart + MultU64x64(SIZE_4KB, Curr->NumberOfPages);
if (Addr1 == Curr->PhysicalStart && Addr2 == Next->PhysicalStart)
{
return TRUE;
}
}
return FALSE;
}
STATIC EFI_MEMORY_DESCRIPTOR* GetMemDesc
(
OUT UINTN *pSize,
OUT UINTN *pItemSize,
OUT UINTN *pDescCount
)
{
UINTN Size = 0;
UINTN MapKey = 0;
UINTN ItemSize = 0;
UINTN DescCount = 0;
UINT32 Version = 0;
EFI_STATUS Status = EFI_SUCCESS;
EFI_MEMORY_DESCRIPTOR *pDesc = NULL;
EFI_MEMORY_DESCRIPTOR *Curr = NULL;
Status = gBS->GetMemoryMap(&Size, pDesc, &MapKey, &ItemSize, &Version);
if (EFI_BUFFER_TOO_SMALL != Status)
{
debug("GetMemoryMap: %r", Status);
return NULL;
}
Size += SIZE_1KB;
pDesc = AllocatePool(Size);
if (!pDesc)
{
debug("AllocatePool: %lu failed", Size);
return NULL;
}
ZeroMem(pDesc, Size);
Status = gBS->GetMemoryMap(&Size, pDesc, &MapKey, &ItemSize, &Version);
if (EFI_ERROR(Status))
{
debug("GetMemoryMap: %r", Status);
FreePool(pDesc);
return NULL;
}
Curr = pDesc;
while (Curr && Curr < (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)pDesc + Size))
{
DescCount++;
Curr = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Curr + ItemSize);
}
*pSize = Size;
*pItemSize = ItemSize;
*pDescCount = DescCount;
debug("GetMemoryMap: ItemSize:%lu Count:%lu", ItemSize, DescCount);
return pDesc;
}
EFI_STATUS FixWindowsMemhole(IN EFI_HANDLE ImageHandle, IN CONST CHAR16 *CmdLine)
{
UINTN Size = 0;
UINTN ItemSize = 0;
UINTN DescCount = 0;
UINTN TotalMem = 0;
EFI_STATUS Status = EFI_SUCCESS;
EFI_PHYSICAL_ADDRESS AllocAddr = 0;
EFI_MEMORY_DESCRIPTOR *pDescs = NULL;
EFI_MEMORY_DESCRIPTOR *Prev = NULL;
EFI_MEMORY_DESCRIPTOR *Next = NULL;
EFI_MEMORY_DESCRIPTOR *Curr = NULL;
(VOID)ImageHandle;
(VOID)CmdLine;
pDescs = GetMemDesc(&Size, &ItemSize, &DescCount);
if (!pDescs)
{
return EFI_NOT_FOUND;
}
if (DescCount < 500)
{
FreePool(pDescs);
Printf("There is no need to fixup (%lu)\n", DescCount);
return EFI_SUCCESS;
}
Curr = pDescs;
while ((UINT8 *)Curr < (UINT8 *)pDescs + Size)
{
Next = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Curr + ItemSize);
if (IsMemContiguous(Prev, Curr, Next))
{
AllocAddr = Curr->PhysicalStart;
Status = gBS->AllocatePages(AllocateAddress, EfiBootServicesData, Curr->NumberOfPages, &AllocAddr);
if (EFI_SUCCESS == Status)
{
TotalMem += MultU64x64(SIZE_4KB, Curr->NumberOfPages);
}
}
Prev = Curr;
Curr = Next;
}
Printf("Fixup Windows mmap issue OK (%lu)\n", TotalMem);
return EFI_SUCCESS;
}

View file

@ -0,0 +1,135 @@
/******************************************************************************
* VtoyUtil.c
*
* Copyright (c) 2020, longpanda <admin@ventoy.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Protocol/LoadedImage.h>
#include <Guid/FileInfo.h>
#include <Guid/FileSystemInfo.h>
#include <Protocol/BlockIo.h>
#include <Protocol/RamDisk.h>
#include <Protocol/SimpleFileSystem.h>
#include <VtoyUtil.h>
BOOLEAN gVtoyDebugPrint = FALSE;
STATIC CONST CHAR16 *gCurFeature= NULL;
STATIC CHAR16 *gCmdLine = NULL;
STATIC grub_env_printf_pf g_env_printf = NULL;
STATIC VtoyUtilFeature gFeatureList[] =
{
{ L"fix_windows_mmap", FixWindowsMemhole },
};
VOID EFIAPI VtoyUtilDebug(IN CONST CHAR8 *Format, ...)
{
VA_LIST Marker;
CHAR8 Buffer[512];
VA_START (Marker, Format);
AsciiVSPrint(Buffer, sizeof(Buffer), Format, Marker);
VA_END (Marker);
if (g_env_printf)
{
g_env_printf("%s", Buffer);
}
}
STATIC EFI_STATUS ParseCmdline(IN EFI_HANDLE ImageHandle)
{
CHAR16 *pPos = NULL;
CHAR16 *pCmdLine = NULL;
EFI_STATUS Status = EFI_SUCCESS;
ventoy_grub_param *pGrubParam = NULL;
EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
if (EFI_ERROR(Status))
{
return Status;
}
pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
if (StrStr(pCmdLine, L"debug"))
{
gVtoyDebugPrint = TRUE;
}
pPos = StrStr(pCmdLine, L"env_param=");
if (!pPos)
{
return EFI_INVALID_PARAMETER;
}
pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
g_env_printf = pGrubParam->grub_env_printf;
pPos = StrStr(pCmdLine, L"feature=");
if (!pPos)
{
return EFI_INVALID_PARAMETER;
}
gCurFeature = pPos + StrLen(L"feature=");
gCmdLine = pCmdLine;
return EFI_SUCCESS;
}
EFI_STATUS EFIAPI VtoyUtilEfiMain
(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
UINTN i;
UINTN Len;
ParseCmdline(ImageHandle);
for (i = 0; i < ARRAY_SIZE(gFeatureList); i++)
{
Len = StrLen(gFeatureList[i].Cmd);
if (StrnCmp(gFeatureList[i].Cmd, gCurFeature, Len) == 0)
{
debug("Find main proc <%s>", gFeatureList[i].Cmd);
gFeatureList[i].MainProc(ImageHandle, gCurFeature + Len);
break;
}
}
FreePool(gCmdLine);
gCmdLine = NULL;
return EFI_SUCCESS;
}

View file

@ -0,0 +1,61 @@
/******************************************************************************
* VtoyUtil.h
*
* Copyright (c) 2020, longpanda <admin@ventoy.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __VTOYUTIL_H__
#define __VTOYUTIL_H__
#pragma pack(1)
typedef EFI_STATUS (*VTOY_UTIL_PROC_PF)(IN EFI_HANDLE ImageHandle, IN CONST CHAR16 *CmdLine);
typedef const char * (*grub_env_get_pf)(const char *name);
typedef int (*grub_env_printf_pf)(const char *fmt, ...);
typedef struct ventoy_grub_param_file_replace
{
UINT32 magic;
char old_file_name[4][256];
UINT32 old_file_cnt;
UINT32 new_file_virtual_id;
}ventoy_grub_param_file_replace;
typedef struct ventoy_grub_param
{
grub_env_get_pf grub_env_get;
ventoy_grub_param_file_replace file_replace;
grub_env_printf_pf grub_env_printf;
}ventoy_grub_param;
#pragma pack()
typedef struct VtoyUtilFeature
{
CONST CHAR16 *Cmd;
VTOY_UTIL_PROC_PF MainProc;
}VtoyUtilFeature;
extern BOOLEAN gVtoyDebugPrint;
VOID EFIAPI VtoyUtilDebug(IN CONST CHAR8 *Format, ...);
#define debug(expr, ...) if (gVtoyDebugPrint) VtoyUtilDebug("[VTOY] "expr"\n", ##__VA_ARGS__)
#define Printf VtoyUtilDebug
EFI_STATUS FixWindowsMemhole(IN EFI_HANDLE ImageHandle, IN CONST CHAR16 *CmdLine);
#endif

View file

@ -0,0 +1,80 @@
#************************************************************************************
# Copyright (c) 2020, longpanda <admin@ventoy.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
#************************************************************************************
[Defines]
INF_VERSION = 0x00010005
BASE_NAME = VtoyUtil
FILE_GUID = a43466a0-68c6-469d-ba4b-678bbe90bc47
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 1.0
ENTRY_POINT = VtoyUtilEfiMain
[Sources]
VtoyUtil.h
VtoyUtil.c
Memhole.c
[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
ShellPkg/ShellPkg.dec
[LibraryClasses]
UefiApplicationEntryPoint
UefiLib
DebugLib
[Guids]
gShellVariableGuid
gEfiVirtualCdGuid
gEfiFileInfoGuid
[Protocols]
gEfiLoadedImageProtocolGuid
gEfiBlockIoProtocolGuid
gEfiDevicePathProtocolGuid
gEfiSimpleFileSystemProtocolGuid
gEfiRamDiskProtocolGuid
gEfiAbsolutePointerProtocolGuid
gEfiAcpiTableProtocolGuid
gEfiBlockIo2ProtocolGuid
gEfiBusSpecificDriverOverrideProtocolGuid
gEfiComponentNameProtocolGuid
gEfiComponentName2ProtocolGuid
gEfiDriverBindingProtocolGuid
gEfiDiskIoProtocolGuid
gEfiDiskIo2ProtocolGuid
gEfiGraphicsOutputProtocolGuid
gEfiHiiConfigAccessProtocolGuid
gEfiHiiFontProtocolGuid
gEfiLoadFileProtocolGuid
gEfiLoadFile2ProtocolGuid
gEfiLoadedImageProtocolGuid
gEfiLoadedImageDevicePathProtocolGuid
gEfiPciIoProtocolGuid
gEfiSerialIoProtocolGuid
gEfiSimpleTextInProtocolGuid
gEfiSimpleTextInputExProtocolGuid
gEfiSimpleTextOutProtocolGuid