]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - EDK2/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/VentoyDebug.c
1.0.07 release
[Ventoy.git] / EDK2 / 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 <Ventoy.h>
38
39 STATIC ventoy_system_wrapper g_system_wrapper;
40
41 static struct well_known_guid g_efi_well_known_guids[] =
42 {
43 { &gEfiAbsolutePointerProtocolGuid, "AbsolutePointer" },
44 { &gEfiAcpiTableProtocolGuid, "AcpiTable" },
45 { &gEfiBlockIoProtocolGuid, "BlockIo" },
46 { &gEfiBlockIo2ProtocolGuid, "BlockIo2" },
47 { &gEfiBusSpecificDriverOverrideProtocolGuid, "BusSpecificDriverOverride" },
48 { &gEfiComponentNameProtocolGuid, "ComponentName" },
49 { &gEfiComponentName2ProtocolGuid, "ComponentName2" },
50 { &gEfiDevicePathProtocolGuid, "DevicePath" },
51 { &gEfiDriverBindingProtocolGuid, "DriverBinding" },
52 { &gEfiDiskIoProtocolGuid, "DiskIo" },
53 { &gEfiDiskIo2ProtocolGuid, "DiskIo2" },
54 { &gEfiGraphicsOutputProtocolGuid, "GraphicsOutput" },
55 { &gEfiHiiConfigAccessProtocolGuid, "HiiConfigAccess" },
56 { &gEfiHiiFontProtocolGuid, "HiiFont" },
57 { &gEfiLoadFileProtocolGuid, "LoadFile" },
58 { &gEfiLoadFile2ProtocolGuid, "LoadFile2" },
59 { &gEfiLoadedImageProtocolGuid, "LoadedImage" },
60 { &gEfiLoadedImageDevicePathProtocolGuid, "LoadedImageDevicePath"},
61 { &gEfiPciIoProtocolGuid, "PciIo" },
62 { &gEfiSerialIoProtocolGuid, "SerialIo" },
63 { &gEfiSimpleFileSystemProtocolGuid, "SimpleFileSystem" },
64 { &gEfiSimpleTextInProtocolGuid, "SimpleTextInput" },
65 { &gEfiSimpleTextInputExProtocolGuid, "SimpleTextInputEx" },
66 { &gEfiSimpleTextOutProtocolGuid, "SimpleTextOutput" },
67 };
68
69 STATIC CHAR8 gEfiGuidName[128];
70
71 static const char * ventoy_get_guid_name(EFI_GUID *guid)
72 {
73 UINTN i;
74
75 for (i = 0; i < ARRAY_SIZE(g_efi_well_known_guids); i++)
76 {
77 if (CompareGuid(g_efi_well_known_guids[i].guid, guid))
78 {
79 return g_efi_well_known_guids[i].name;
80 }
81 }
82
83 AsciiSPrint(gEfiGuidName, sizeof(gEfiGuidName), "%g", guid);
84 return gEfiGuidName;
85 }
86
87 EFI_STATUS EFIAPI
88 ventoy_wrapper_fs_open(EFI_FILE_HANDLE This, EFI_FILE_HANDLE *New, CHAR16 *Name, UINT64 Mode, UINT64 Attributes)
89 {
90 (VOID)This;
91 (VOID)New;
92 (VOID)Name;
93 (VOID)Mode;
94 (VOID)Attributes;
95 return EFI_SUCCESS;
96 }
97
98 EFI_STATUS EFIAPI
99 ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This, EFI_FILE_HANDLE *New, CHAR16 *Name, UINT64 Mode, UINT64 Attributes, EFI_FILE_IO_TOKEN *Token)
100 {
101 return ventoy_wrapper_fs_open(This, New, Name, Mode, Attributes);
102 }
103
104 EFI_STATUS EFIAPI
105 ventoy_wrapper_file_delete(EFI_FILE_HANDLE This)
106 {
107 (VOID)This;
108 return EFI_SUCCESS;
109 }
110
111 EFI_STATUS EFIAPI
112 ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This, EFI_GUID *Type, UINTN Len, VOID *Data)
113 {
114 return EFI_SUCCESS;
115 }
116
117 EFI_STATUS EFIAPI
118 ventoy_wrapper_file_flush(EFI_FILE_HANDLE This)
119 {
120 (VOID)This;
121 return EFI_SUCCESS;
122 }
123
124 /* Ex version */
125 EFI_STATUS EFIAPI
126 ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This, EFI_FILE_IO_TOKEN *Token)
127 {
128 (VOID)This;
129 (VOID)Token;
130 return EFI_SUCCESS;
131 }
132
133
134 EFI_STATUS EFIAPI
135 ventoy_wrapper_file_write(EFI_FILE_HANDLE This, UINTN *Len, VOID *Data)
136 {
137 (VOID)This;
138 (VOID)Len;
139 (VOID)Data;
140
141 return EFI_WRITE_PROTECTED;
142 }
143
144 EFI_STATUS EFIAPI
145 ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL *This, IN OUT EFI_FILE_IO_TOKEN *Token)
146 {
147 return ventoy_wrapper_file_write(This, &(Token->BufferSize), Token->Buffer);
148 }
149
150
151 static EFI_STATUS EFIAPI
152 ventoy_wrapper_file_close(EFI_FILE_HANDLE This)
153 {
154 (VOID)This;
155 return EFI_SUCCESS;
156 }
157
158
159 static EFI_STATUS EFIAPI
160 ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This, UINT64 Position)
161 {
162 (VOID)This;
163
164 g_efi_file_replace.CurPos = Position;
165 return EFI_SUCCESS;
166 }
167
168 static EFI_STATUS EFIAPI
169 ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This, UINT64 *Position)
170 {
171 (VOID)This;
172
173 *Position = g_efi_file_replace.CurPos;
174
175 return EFI_SUCCESS;
176 }
177
178
179 static EFI_STATUS EFIAPI
180 ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This, EFI_GUID *Type, UINTN *Len, VOID *Data)
181 {
182 EFI_FILE_INFO *Info = (EFI_FILE_INFO *) Data;
183
184 debug("ventoy_wrapper_file_get_info ... %u", *Len);
185
186 if (!CompareGuid(Type, &gEfiFileInfoGuid))
187 {
188 return EFI_INVALID_PARAMETER;
189 }
190
191 if (*Len == 0)
192 {
193 *Len = 384;
194 return EFI_BUFFER_TOO_SMALL;
195 }
196
197 ZeroMem(Data, sizeof(EFI_FILE_INFO));
198
199 Info->Size = sizeof(EFI_FILE_INFO);
200 Info->FileSize = g_efi_file_replace.FileSizeBytes;
201 Info->PhysicalSize = g_efi_file_replace.FileSizeBytes;
202 Info->Attribute = EFI_FILE_READ_ONLY;
203 //Info->FileName = EFI_FILE_READ_ONLY;
204
205 *Len = Info->Size;
206
207 return EFI_SUCCESS;
208 }
209
210 static EFI_STATUS EFIAPI
211 ventoy_wrapper_file_read(EFI_FILE_HANDLE This, UINTN *Len, VOID *Data)
212 {
213 EFI_LBA Lba;
214 UINTN ReadLen = *Len;
215
216 (VOID)This;
217
218 debug("ventoy_wrapper_file_read ... %u", *Len);
219
220 if (g_efi_file_replace.CurPos + ReadLen > g_efi_file_replace.FileSizeBytes)
221 {
222 ReadLen = g_efi_file_replace.FileSizeBytes - g_efi_file_replace.CurPos;
223 }
224
225 Lba = g_efi_file_replace.CurPos / 2048 + g_efi_file_replace.BlockIoSectorStart;
226
227 ventoy_block_io_read(NULL, 0, Lba, ReadLen, Data);
228
229 *Len = ReadLen;
230
231 g_efi_file_replace.CurPos += ReadLen;
232
233 return EFI_SUCCESS;
234 }
235
236
237 EFI_STATUS EFIAPI
238 ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL *This, IN OUT EFI_FILE_IO_TOKEN *Token)
239 {
240 return ventoy_wrapper_file_read(This, &(Token->BufferSize), Token->Buffer);
241 }
242
243 EFI_STATUS EFIAPI ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL *File)
244 {
245 File->Revision = EFI_FILE_PROTOCOL_REVISION2;
246 File->Open = ventoy_wrapper_fs_open;
247 File->Close = ventoy_wrapper_file_close;
248 File->Delete = ventoy_wrapper_file_delete;
249 File->Read = ventoy_wrapper_file_read;
250 File->Write = ventoy_wrapper_file_write;
251 File->GetPosition = ventoy_wrapper_file_get_pos;
252 File->SetPosition = ventoy_wrapper_file_set_pos;
253 File->GetInfo = ventoy_wrapper_file_get_info;
254 File->SetInfo = ventoy_wrapper_file_set_info;
255 File->Flush = ventoy_wrapper_file_flush;
256 File->OpenEx = ventoy_wrapper_file_open_ex;
257 File->ReadEx = ventoy_wrapper_file_read_ex;
258 File->WriteEx = ventoy_wrapper_file_write_ex;
259 File->FlushEx = ventoy_wrapper_file_flush_ex;
260
261 return EFI_SUCCESS;
262 }
263
264 STATIC EFI_STATUS EFIAPI ventoy_handle_protocol
265 (
266 IN EFI_HANDLE Handle,
267 IN EFI_GUID *Protocol,
268 OUT VOID **Interface
269 )
270 {
271 EFI_STATUS Status = EFI_SUCCESS;
272
273 debug("ventoy_handle_protocol:%a", ventoy_get_guid_name(Protocol));
274 Status = g_system_wrapper.OriHandleProtocol(Handle, Protocol, Interface);
275
276 if (CompareGuid(Protocol, &gEfiSimpleFileSystemProtocolGuid))
277 {
278 EFI_FILE_PROTOCOL *FileProtocol = NULL;
279 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = *((EFI_SIMPLE_FILE_SYSTEM_PROTOCOL **)(Interface));
280
281 pFile->OpenVolume(pFile, &FileProtocol);
282
283 debug("Handle FS Protocol: %p OpenVolume:%p, FileProtocol:%p, Open:%p",
284 pFile, pFile->OpenVolume, FileProtocol, FileProtocol->Open);
285
286 sleep(3);
287 }
288
289 return Status;
290 }
291
292 STATIC EFI_STATUS EFIAPI ventoy_open_protocol
293 (
294 IN EFI_HANDLE Handle,
295 IN EFI_GUID *Protocol,
296 OUT VOID **Interface, OPTIONAL
297 IN EFI_HANDLE AgentHandle,
298 IN EFI_HANDLE ControllerHandle,
299 IN UINT32 Attributes
300 )
301 {
302 debug("ventoy_open_protocol:%a", ventoy_get_guid_name(Protocol));
303 return g_system_wrapper.OriOpenProtocol(Handle, Protocol, Interface, AgentHandle, ControllerHandle, Attributes);
304 }
305
306 STATIC EFI_STATUS EFIAPI ventoy_locate_protocol
307 (
308 IN EFI_GUID *Protocol,
309 IN VOID *Registration, OPTIONAL
310 OUT VOID **Interface
311 )
312 {
313 debug("ventoy_locate_protocol:%a", ventoy_get_guid_name(Protocol));
314 return g_system_wrapper.OriLocateProtocol(Protocol, Registration, Interface);
315 }
316
317 EFI_STATUS EFIAPI ventoy_wrapper_system(VOID)
318 {
319 ventoy_wrapper(gBS, g_system_wrapper, LocateProtocol, ventoy_locate_protocol);
320 ventoy_wrapper(gBS, g_system_wrapper, HandleProtocol, ventoy_handle_protocol);
321 ventoy_wrapper(gBS, g_system_wrapper, OpenProtocol, ventoy_open_protocol);
322
323 return EFI_SUCCESS;
324 }
325