]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/VtoyUtil/VtoyUtil.c
1.0.16 release
[Ventoy.git] / EDK2 / edk2_mod / edk2-edk2-stable201911 / MdeModulePkg / Application / VtoyUtil / VtoyUtil.c
1 /******************************************************************************
2 * VtoyUtil.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include <Uefi.h>
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>
37 #include <VtoyUtil.h>
38
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;
43
44 STATIC VtoyUtilFeature gFeatureList[] =
45 {
46 { L"fix_windows_mmap", FixWindowsMemhole },
47 };
48
49 VOID EFIAPI VtoyUtilDebug(IN CONST CHAR8 *Format, ...)
50 {
51 VA_LIST Marker;
52 CHAR8 Buffer[512];
53
54 VA_START (Marker, Format);
55 AsciiVSPrint(Buffer, sizeof(Buffer), Format, Marker);
56 VA_END (Marker);
57
58 if (g_env_printf)
59 {
60 g_env_printf("%s", Buffer);
61 }
62 }
63
64 STATIC EFI_STATUS ParseCmdline(IN EFI_HANDLE ImageHandle)
65 {
66 CHAR16 *pPos = NULL;
67 CHAR16 *pCmdLine = NULL;
68 EFI_STATUS Status = EFI_SUCCESS;
69 ventoy_grub_param *pGrubParam = NULL;
70 EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
71
72 Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
73 if (EFI_ERROR(Status))
74 {
75 return Status;
76 }
77
78 pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
79 SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
80 CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);
81
82 if (StrStr(pCmdLine, L"debug"))
83 {
84 gVtoyDebugPrint = TRUE;
85 }
86
87 pPos = StrStr(pCmdLine, L"env_param=");
88 if (!pPos)
89 {
90 return EFI_INVALID_PARAMETER;
91 }
92
93 pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
94 g_env_printf = pGrubParam->grub_env_printf;
95
96 pPos = StrStr(pCmdLine, L"feature=");
97 if (!pPos)
98 {
99 return EFI_INVALID_PARAMETER;
100 }
101
102 gCurFeature = pPos + StrLen(L"feature=");
103
104 gCmdLine = pCmdLine;
105 return EFI_SUCCESS;
106 }
107
108 EFI_STATUS EFIAPI VtoyUtilEfiMain
109 (
110 IN EFI_HANDLE ImageHandle,
111 IN EFI_SYSTEM_TABLE *SystemTable
112 )
113 {
114 UINTN i;
115 UINTN Len;
116
117 ParseCmdline(ImageHandle);
118
119 for (i = 0; i < ARRAY_SIZE(gFeatureList); i++)
120 {
121 Len = StrLen(gFeatureList[i].Cmd);
122 if (StrnCmp(gFeatureList[i].Cmd, gCurFeature, Len) == 0)
123 {
124 debug("Find main proc <%s>", gFeatureList[i].Cmd);
125 gFeatureList[i].MainProc(ImageHandle, gCurFeature + Len);
126 break;
127 }
128 }
129
130 FreePool(gCmdLine);
131 gCmdLine = NULL;
132
133 return EFI_SUCCESS;
134 }
135