]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/VentoyDebug.c
1.1.07 release
[Ventoy.git] / EDK2 / edk2_mod / edk2-edk2-stable201911 / MdeModulePkg / Application / Ventoy / VentoyDebug.c
1 /******************************************************************************
2 * Ventoy.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 <Protocol/DriverBinding.h>
38 #include <Ventoy.h>
39
40 #define PROCOTOL_SLEEP_MSECONDS 0
41
42 #define debug_sleep() if (PROCOTOL_SLEEP_MSECONDS) gBS->Stall(1000 * PROCOTOL_SLEEP_MSECONDS)
43
44 STATIC ventoy_system_wrapper g_system_wrapper;
45
46 static struct well_known_guid g_efi_well_known_guids[] =
47 {
48 { &gEfiAbsolutePointerProtocolGuid, "AbsolutePointer" },
49 { &gEfiAcpiTableProtocolGuid, "AcpiTable" },
50 { &gEfiBlockIoProtocolGuid, "BlockIo" },
51 { &gEfiBlockIo2ProtocolGuid, "BlockIo2" },
52 { &gEfiBusSpecificDriverOverrideProtocolGuid, "BusSpecificDriverOverride" },
53 { &gEfiComponentNameProtocolGuid, "ComponentName" },
54 { &gEfiComponentName2ProtocolGuid, "ComponentName2" },
55 { &gEfiDevicePathProtocolGuid, "DevicePath" },
56 { &gEfiDriverBindingProtocolGuid, "DriverBinding" },
57 { &gEfiDiskIoProtocolGuid, "DiskIo" },
58 { &gEfiDiskIo2ProtocolGuid, "DiskIo2" },
59 { &gEfiGraphicsOutputProtocolGuid, "GraphicsOutput" },
60 { &gEfiHiiConfigAccessProtocolGuid, "HiiConfigAccess" },
61 { &gEfiHiiFontProtocolGuid, "HiiFont" },
62 { &gEfiLoadFileProtocolGuid, "LoadFile" },
63 { &gEfiLoadFile2ProtocolGuid, "LoadFile2" },
64 { &gEfiLoadedImageProtocolGuid, "LoadedImage" },
65 { &gEfiLoadedImageDevicePathProtocolGuid, "LoadedImageDevicePath"},
66 { &gEfiPciIoProtocolGuid, "PciIo" },
67 { &gEfiSerialIoProtocolGuid, "SerialIo" },
68 { &gEfiSimpleFileSystemProtocolGuid, "SimpleFileSystem" },
69 { &gEfiSimpleTextInProtocolGuid, "SimpleTextInput" },
70 { &gEfiSimpleTextInputExProtocolGuid, "SimpleTextInputEx" },
71 { &gEfiSimpleTextOutProtocolGuid, "SimpleTextOutput" },
72 };
73
74 STATIC CHAR8 gEfiGuidName[128];
75
76 static const char * ventoy_get_guid_name(EFI_GUID *guid)
77 {
78 UINTN i;
79
80 for (i = 0; i < ARRAY_SIZE(g_efi_well_known_guids); i++)
81 {
82 if (CompareGuid(g_efi_well_known_guids[i].guid, guid))
83 {
84 return g_efi_well_known_guids[i].name;
85 }
86 }
87
88 AsciiSPrint(gEfiGuidName, sizeof(gEfiGuidName), "%g", guid);
89 return gEfiGuidName;
90 }
91
92 STATIC EFI_STATUS EFIAPI ventoy_handle_protocol
93 (
94 IN EFI_HANDLE Handle,
95 IN EFI_GUID *Protocol,
96 OUT VOID **Interface
97 )
98 {
99 EFI_STATUS Status = EFI_SUCCESS;
100
101 debug("ventoy_handle_protocol:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
102 Status = g_system_wrapper.OriHandleProtocol(Handle, Protocol, Interface);
103
104 if (CompareGuid(Protocol, &gEfiSimpleFileSystemProtocolGuid))
105 {
106 EFI_FILE_PROTOCOL *FileProtocol = NULL;
107 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = *((EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **)(Interface));
108
109 pFile->OpenVolume(pFile, &FileProtocol);
110
111 trace("Handle FS Protocol: %p OpenVolume:%p, FileProtocol:%p, Open:%p",
112 pFile, pFile->OpenVolume, FileProtocol, FileProtocol->Open);
113
114 sleep(3);
115 }
116
117 return Status;
118 }
119
120 STATIC EFI_STATUS EFIAPI ventoy_open_protocol
121 (
122 IN EFI_HANDLE Handle,
123 IN EFI_GUID *Protocol,
124 OUT VOID **Interface, OPTIONAL
125 IN EFI_HANDLE AgentHandle,
126 IN EFI_HANDLE ControllerHandle,
127 IN UINT32 Attributes
128 )
129 {
130 debug("ventoy_open_protocol:<%p> %a", Handle, ventoy_get_guid_name(Protocol)); debug_sleep();
131 return g_system_wrapper.OriOpenProtocol(Handle, Protocol, Interface, AgentHandle, ControllerHandle, Attributes);
132 }
133
134 STATIC EFI_STATUS EFIAPI ventoy_locate_protocol
135 (
136 IN EFI_GUID *Protocol,
137 IN VOID *Registration, OPTIONAL
138 OUT VOID **Interface
139 )
140 {
141 debug("ventoy_locate_protocol:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
142 return g_system_wrapper.OriLocateProtocol(Protocol, Registration, Interface);
143 }
144
145 STATIC EFI_STATUS EFIAPI ventoy_locate_handle_buffer
146 (
147 IN EFI_LOCATE_SEARCH_TYPE SearchType,
148 IN EFI_GUID *Protocol, OPTIONAL
149 IN VOID *SearchKey, OPTIONAL
150 IN OUT UINTN *NoHandles,
151 OUT EFI_HANDLE **Buffer
152 )
153 {
154 debug("ventoy_locate_handle_buffer:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
155 return g_system_wrapper.OriLocateHandleBuffer(SearchType, Protocol, SearchKey, NoHandles, Buffer);
156 }
157
158 STATIC EFI_STATUS EFIAPI ventoy_protocol_per_handle
159 (
160 IN EFI_HANDLE Handle,
161 OUT EFI_GUID ***ProtocolBuffer,
162 OUT UINTN *ProtocolBufferCount
163 )
164 {
165 debug("ventoy_protocol_per_handle:%p", Handle); debug_sleep();
166 return g_system_wrapper.OriProtocolsPerHandle(Handle, ProtocolBuffer, ProtocolBufferCount);
167 }
168
169 EFI_STATUS EFIAPI ventoy_locate_handle
170 (
171 IN EFI_LOCATE_SEARCH_TYPE SearchType,
172 IN EFI_GUID *Protocol, OPTIONAL
173 IN VOID *SearchKey, OPTIONAL
174 IN OUT UINTN *BufferSize,
175 OUT EFI_HANDLE *Buffer
176 )
177 {
178 UINTN i;
179 EFI_HANDLE Handle;
180 EFI_STATUS Status = EFI_SUCCESS;
181
182 debug("ventoy_locate_handle: %d %a %p", SearchType, ventoy_get_guid_name(Protocol), SearchKey);
183 Status = g_system_wrapper.OriLocateHandle(SearchType, Protocol, SearchKey, BufferSize, Buffer);
184 debug("ventoy_locate_handle: %r Handle Count:%u", Status, *BufferSize/sizeof(EFI_HANDLE));
185
186 if (EFI_SUCCESS == Status)
187 {
188 for (i = 0; i < *BufferSize / sizeof(EFI_HANDLE); i++)
189 {
190 if (Buffer[i] == gBlockData.Handle)
191 {
192 Handle = Buffer[0];
193 Buffer[0] = Buffer[i];
194 Buffer[i] = Handle;
195 debug("####### Handle at %u", i);
196 break;
197 }
198 }
199 }
200
201 debug_sleep();
202
203 return Status;
204 }
205
206 STATIC EFI_STATUS EFIAPI ventoy_locate_device_path
207 (
208 IN EFI_GUID *Protocol,
209 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
210 OUT EFI_HANDLE *Device
211 )
212 {
213 debug("ventoy_locate_device_path:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
214 return g_system_wrapper.OriLocateDevicePath(Protocol, DevicePath, Device);
215 }
216
217 EFI_STATUS EFIAPI ventoy_wrapper_system(VOID)
218 {
219 ventoy_wrapper(gBS, g_system_wrapper, LocateProtocol, ventoy_locate_protocol);
220 ventoy_wrapper(gBS, g_system_wrapper, HandleProtocol, ventoy_handle_protocol);
221 ventoy_wrapper(gBS, g_system_wrapper, OpenProtocol, ventoy_open_protocol);
222 ventoy_wrapper(gBS, g_system_wrapper, LocateHandleBuffer, ventoy_locate_handle_buffer);
223 ventoy_wrapper(gBS, g_system_wrapper, ProtocolsPerHandle, ventoy_protocol_per_handle);
224 ventoy_wrapper(gBS, g_system_wrapper, LocateHandle, ventoy_locate_handle);
225 ventoy_wrapper(gBS, g_system_wrapper, LocateDevicePath, ventoy_locate_device_path);
226
227 return EFI_SUCCESS;
228 }
229