1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <Library/DebugLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DevicePathLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiApplicationEntryPoint.h>
31 #include <Protocol/LoadedImage.h>
32 #include <Guid/FileInfo.h>
33 #include <Guid/FileSystemInfo.h>
34 #include <Protocol/BlockIo.h>
35 #include <Protocol/RamDisk.h>
36 #include <Protocol/SimpleFileSystem.h>
39 BOOLEAN gVtoyDebugPrint
= FALSE
;
40 STATIC CONST CHAR16
*gCurFeature
= NULL
;
41 STATIC CHAR16
*gCmdLine
= NULL
;
42 STATIC grub_env_printf_pf g_env_printf
= NULL
;
44 STATIC VtoyUtilFeature gFeatureList
[] =
46 { L
"fix_windows_mmap", FixWindowsMemhole
},
49 VOID EFIAPI
VtoyUtilDebug(IN CONST CHAR8
*Format
, ...)
54 VA_START (Marker
, Format
);
55 AsciiVSPrint(Buffer
, sizeof(Buffer
), Format
, Marker
);
60 g_env_printf("%s", Buffer
);
64 STATIC EFI_STATUS
ParseCmdline(IN EFI_HANDLE ImageHandle
)
67 CHAR16
*pCmdLine
= NULL
;
68 EFI_STATUS Status
= EFI_SUCCESS
;
69 ventoy_grub_param
*pGrubParam
= NULL
;
70 EFI_LOADED_IMAGE_PROTOCOL
*pImageInfo
= NULL
;
72 Status
= gBS
->HandleProtocol(ImageHandle
, &gEfiLoadedImageProtocolGuid
, (VOID
**)&pImageInfo
);
73 if (EFI_ERROR(Status
))
78 pCmdLine
= (CHAR16
*)AllocatePool(pImageInfo
->LoadOptionsSize
+ 4);
79 SetMem(pCmdLine
, pImageInfo
->LoadOptionsSize
+ 4, 0);
80 CopyMem(pCmdLine
, pImageInfo
->LoadOptions
, pImageInfo
->LoadOptionsSize
);
82 if (StrStr(pCmdLine
, L
"debug"))
84 gVtoyDebugPrint
= TRUE
;
87 pPos
= StrStr(pCmdLine
, L
"env_param=");
90 return EFI_INVALID_PARAMETER
;
93 pGrubParam
= (ventoy_grub_param
*)StrHexToUintn(pPos
+ StrLen(L
"env_param="));
94 g_env_printf
= pGrubParam
->grub_env_printf
;
96 pPos
= StrStr(pCmdLine
, L
"feature=");
99 return EFI_INVALID_PARAMETER
;
102 gCurFeature
= pPos
+ StrLen(L
"feature=");
108 EFI_STATUS EFIAPI VtoyUtilEfiMain
110 IN EFI_HANDLE ImageHandle
,
111 IN EFI_SYSTEM_TABLE
*SystemTable
117 ParseCmdline(ImageHandle
);
119 for (i
= 0; i
< ARRAY_SIZE(gFeatureList
); i
++)
121 Len
= StrLen(gFeatureList
[i
].Cmd
);
122 if (StrnCmp(gFeatureList
[i
].Cmd
, gCurFeature
, Len
) == 0)
124 debug("Find main proc <%s>", gFeatureList
[i
].Cmd
);
125 gFeatureList
[i
].MainProc(ImageHandle
, gCurFeature
+ Len
);