]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/VtoyUtil/VtoyUtil.c
Fix the boot issue for Windows UEFI on some Dell server. (introduced since 1.0.48)
[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"vtoyefitest"))
83 {
84 gST->ConOut->OutputString(gST->ConOut, L"\r\n##########################");
85 gST->ConOut->OutputString(gST->ConOut, L"\r\n######### VTOY #########");
86 gST->ConOut->OutputString(gST->ConOut, L"\r\n##########################");
87 return EFI_SUCCESS;
88 }
89
90 if (StrStr(pCmdLine, L"debug"))
91 {
92 gVtoyDebugPrint = TRUE;
93 }
94
95 pPos = StrStr(pCmdLine, L"env_param=");
96 if (!pPos)
97 {
98 return EFI_INVALID_PARAMETER;
99 }
100
101 pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
102 g_env_printf = pGrubParam->grub_env_printf;
103
104 pPos = StrStr(pCmdLine, L"feature=");
105 if (!pPos)
106 {
107 return EFI_INVALID_PARAMETER;
108 }
109
110 gCurFeature = pPos + StrLen(L"feature=");
111
112 gCmdLine = pCmdLine;
113
114 return EFI_SUCCESS;
115 }
116
117 EFI_STATUS EFIAPI VtoyUtilEfiMain
118 (
119 IN EFI_HANDLE ImageHandle,
120 IN EFI_SYSTEM_TABLE *SystemTable
121 )
122 {
123 UINTN i;
124 UINTN Len;
125
126 ParseCmdline(ImageHandle);
127
128 for (i = 0; gCurFeature && i < ARRAY_SIZE(gFeatureList); i++)
129 {
130 Len = StrLen(gFeatureList[i].Cmd);
131 if (StrnCmp(gFeatureList[i].Cmd, gCurFeature, Len) == 0)
132 {
133 debug("Find main proc <%s>", gFeatureList[i].Cmd);
134 gFeatureList[i].MainProc(ImageHandle, gCurFeature + Len);
135 break;
136 }
137 }
138
139 if (gCmdLine)
140 {
141 FreePool(gCmdLine);
142 gCmdLine = NULL;
143 }
144
145 return EFI_SUCCESS;
146 }
147