]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/VentoyDebug.c
add IMG to no iso tip (#480)
[Ventoy.git] / EDK2 / edk2_mod / edk2-edk2-stable201911 / MdeModulePkg / Application / Ventoy / VentoyDebug.c
index 4c595b464fc8fb862cac87a4d7edde326bf1d1b7..9bdfaaa9efae8328768694114a347f705453bb8a 100644 (file)
 #include <Protocol/BlockIo.h>
 #include <Protocol/RamDisk.h>
 #include <Protocol/SimpleFileSystem.h>
+#include <Protocol/DriverBinding.h>
 #include <Ventoy.h>
 
+#define PROCOTOL_SLEEP_MSECONDS  0
+
+#define debug_sleep() if (PROCOTOL_SLEEP_MSECONDS) gBS->Stall(1000 * PROCOTOL_SLEEP_MSECONDS)
+
 STATIC ventoy_system_wrapper g_system_wrapper;
 
 static struct well_known_guid g_efi_well_known_guids[] = 
@@ -84,183 +89,6 @@ static const char * ventoy_get_guid_name(EFI_GUID *guid)
     return gEfiGuidName;
 }
 
-EFI_STATUS EFIAPI
-ventoy_wrapper_fs_open(EFI_FILE_HANDLE This, EFI_FILE_HANDLE *New, CHAR16 *Name, UINT64 Mode, UINT64 Attributes)
-{
-    (VOID)This;
-    (VOID)New;
-    (VOID)Name;
-    (VOID)Mode;
-    (VOID)Attributes;
-    return EFI_SUCCESS;
-}
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_open_ex(EFI_FILE_HANDLE This, EFI_FILE_HANDLE *New, CHAR16 *Name, UINT64 Mode, UINT64 Attributes, EFI_FILE_IO_TOKEN *Token)
-{
-       return ventoy_wrapper_fs_open(This, New, Name, Mode, Attributes);
-}
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_delete(EFI_FILE_HANDLE This)
-{
-    (VOID)This;
-       return EFI_SUCCESS;
-}
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_set_info(EFI_FILE_HANDLE This, EFI_GUID *Type, UINTN Len, VOID *Data)
-{
-       return EFI_SUCCESS;
-}
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_flush(EFI_FILE_HANDLE This)
-{
-    (VOID)This;
-       return EFI_SUCCESS;
-}
-
-/* Ex version */
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_flush_ex(EFI_FILE_HANDLE This, EFI_FILE_IO_TOKEN *Token)
-{
-    (VOID)This;
-    (VOID)Token;
-       return EFI_SUCCESS;
-}
-
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_write(EFI_FILE_HANDLE This, UINTN *Len, VOID *Data)
-{
-    (VOID)This;
-    (VOID)Len;
-    (VOID)Data;
-
-       return EFI_WRITE_PROTECTED;
-}
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_write_ex(IN EFI_FILE_PROTOCOL *This, IN OUT EFI_FILE_IO_TOKEN *Token)
-{
-       return ventoy_wrapper_file_write(This, &(Token->BufferSize), Token->Buffer);
-}
-
-
-static EFI_STATUS EFIAPI
-ventoy_wrapper_file_close(EFI_FILE_HANDLE This)
-{
-    (VOID)This;
-    return EFI_SUCCESS;
-}
-
-
-static EFI_STATUS EFIAPI
-ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This, UINT64 Position)
-{
-    (VOID)This;
-    
-    g_efi_file_replace.CurPos = Position;
-    return EFI_SUCCESS;
-}
-
-static EFI_STATUS EFIAPI
-ventoy_wrapper_file_get_pos(EFI_FILE_HANDLE This, UINT64 *Position)
-{
-    (VOID)This;
-
-    *Position = g_efi_file_replace.CurPos;
-
-    return EFI_SUCCESS;
-}
-
-
-static EFI_STATUS EFIAPI
-ventoy_wrapper_file_get_info(EFI_FILE_HANDLE This, EFI_GUID *Type, UINTN *Len, VOID *Data)
-{
-    EFI_FILE_INFO *Info = (EFI_FILE_INFO *) Data;
-
-    debug("ventoy_wrapper_file_get_info ... %u", *Len);
-
-    if (!CompareGuid(Type, &gEfiFileInfoGuid))
-    {
-        return EFI_INVALID_PARAMETER;
-    }
-
-    if (*Len == 0)
-    {
-        *Len = 384;
-        return EFI_BUFFER_TOO_SMALL;
-    }
-
-    ZeroMem(Data, sizeof(EFI_FILE_INFO));
-
-    Info->Size = sizeof(EFI_FILE_INFO);
-    Info->FileSize = g_efi_file_replace.FileSizeBytes;
-    Info->PhysicalSize = g_efi_file_replace.FileSizeBytes;
-    Info->Attribute = EFI_FILE_READ_ONLY;
-    //Info->FileName = EFI_FILE_READ_ONLY;
-
-    *Len = Info->Size;
-    
-    return EFI_SUCCESS;
-}
-
-static EFI_STATUS EFIAPI
-ventoy_wrapper_file_read(EFI_FILE_HANDLE This, UINTN *Len, VOID *Data)
-{
-    EFI_LBA Lba;
-    UINTN ReadLen = *Len;
-    
-    (VOID)This;
-
-    debug("ventoy_wrapper_file_read ... %u", *Len);
-
-    if (g_efi_file_replace.CurPos + ReadLen > g_efi_file_replace.FileSizeBytes)
-    {
-        ReadLen = g_efi_file_replace.FileSizeBytes - g_efi_file_replace.CurPos;
-    }
-
-    Lba = g_efi_file_replace.CurPos / 2048 + g_efi_file_replace.BlockIoSectorStart;
-
-    ventoy_block_io_read(NULL, 0, Lba, ReadLen, Data);
-
-    *Len = ReadLen;
-
-    g_efi_file_replace.CurPos += ReadLen;
-
-    return EFI_SUCCESS;
-}
-
-
-EFI_STATUS EFIAPI
-ventoy_wrapper_file_read_ex(IN EFI_FILE_PROTOCOL *This, IN OUT EFI_FILE_IO_TOKEN *Token)
-{
-       return ventoy_wrapper_file_read(This, &(Token->BufferSize), Token->Buffer);
-}
-
-EFI_STATUS EFIAPI ventoy_wrapper_file_procotol(EFI_FILE_PROTOCOL *File)
-{
-    File->Revision    = EFI_FILE_PROTOCOL_REVISION2;
-    File->Open        = ventoy_wrapper_fs_open;
-    File->Close       = ventoy_wrapper_file_close;
-    File->Delete      = ventoy_wrapper_file_delete;
-    File->Read        = ventoy_wrapper_file_read;
-    File->Write       = ventoy_wrapper_file_write;
-    File->GetPosition = ventoy_wrapper_file_get_pos;
-    File->SetPosition = ventoy_wrapper_file_set_pos;
-    File->GetInfo     = ventoy_wrapper_file_get_info;
-    File->SetInfo     = ventoy_wrapper_file_set_info;
-    File->Flush       = ventoy_wrapper_file_flush;
-    File->OpenEx      = ventoy_wrapper_file_open_ex;
-    File->ReadEx      = ventoy_wrapper_file_read_ex;
-    File->WriteEx     = ventoy_wrapper_file_write_ex;
-    File->FlushEx     = ventoy_wrapper_file_flush_ex;
-
-    return EFI_SUCCESS;
-}
-
 STATIC EFI_STATUS EFIAPI ventoy_handle_protocol
 (
     IN  EFI_HANDLE                Handle,
@@ -270,7 +98,7 @@ STATIC EFI_STATUS EFIAPI ventoy_handle_protocol
 {
     EFI_STATUS Status = EFI_SUCCESS;
     
-    debug("ventoy_handle_protocol:%a", ventoy_get_guid_name(Protocol)); 
+    debug("ventoy_handle_protocol:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
     Status = g_system_wrapper.OriHandleProtocol(Handle, Protocol, Interface);
 
     if (CompareGuid(Protocol, &gEfiSimpleFileSystemProtocolGuid))
@@ -280,7 +108,7 @@ STATIC EFI_STATUS EFIAPI ventoy_handle_protocol
         
         pFile->OpenVolume(pFile, &FileProtocol);
         
-        debug("Handle FS Protocol: %p OpenVolume:%p, FileProtocol:%p, Open:%p", 
+        trace("Handle FS Protocol: %p OpenVolume:%p, FileProtocol:%p, Open:%p", 
             pFile, pFile->OpenVolume, FileProtocol, FileProtocol->Open); 
 
         sleep(3);
@@ -299,7 +127,7 @@ STATIC EFI_STATUS EFIAPI ventoy_open_protocol
     IN  UINT32                     Attributes
 )
 {
-    debug("ventoy_open_protocol:%a", ventoy_get_guid_name(Protocol));
+    debug("ventoy_open_protocol:<%p> %a", Handle, ventoy_get_guid_name(Protocol));  debug_sleep();
     return g_system_wrapper.OriOpenProtocol(Handle, Protocol, Interface, AgentHandle, ControllerHandle, Attributes);
 }
 
@@ -310,15 +138,91 @@ STATIC EFI_STATUS EFIAPI ventoy_locate_protocol
     OUT VOID      **Interface
 )
 {
-    debug("ventoy_locate_protocol:%a", ventoy_get_guid_name(Protocol));
+    debug("ventoy_locate_protocol:%a", ventoy_get_guid_name(Protocol));  debug_sleep();
     return g_system_wrapper.OriLocateProtocol(Protocol, Registration, Interface);
 }
 
+STATIC EFI_STATUS EFIAPI ventoy_locate_handle_buffer
+(
+    IN     EFI_LOCATE_SEARCH_TYPE        SearchType,
+    IN     EFI_GUID                     *Protocol,      OPTIONAL
+    IN     VOID                         *SearchKey,     OPTIONAL
+    IN OUT UINTN                        *NoHandles,
+    OUT    EFI_HANDLE                  **Buffer
+)
+{
+    debug("ventoy_locate_handle_buffer:%a", ventoy_get_guid_name(Protocol));  debug_sleep();
+    return g_system_wrapper.OriLocateHandleBuffer(SearchType, Protocol, SearchKey, NoHandles, Buffer);
+}
+
+STATIC EFI_STATUS EFIAPI ventoy_protocol_per_handle
+(
+    IN  EFI_HANDLE      Handle,
+    OUT EFI_GUID        ***ProtocolBuffer,
+    OUT UINTN           *ProtocolBufferCount
+)
+{
+    debug("ventoy_protocol_per_handle:%p", Handle);  debug_sleep();
+    return g_system_wrapper.OriProtocolsPerHandle(Handle, ProtocolBuffer, ProtocolBufferCount);
+}
+
+EFI_STATUS EFIAPI ventoy_locate_handle
+(
+    IN     EFI_LOCATE_SEARCH_TYPE    SearchType,
+    IN     EFI_GUID                 *Protocol,    OPTIONAL
+    IN     VOID                     *SearchKey,   OPTIONAL
+    IN OUT UINTN                    *BufferSize,
+    OUT    EFI_HANDLE               *Buffer
+)
+{
+    UINTN i;
+    EFI_HANDLE Handle;
+    EFI_STATUS Status = EFI_SUCCESS;
+    
+    debug("ventoy_locate_handle: %d %a %p", SearchType, ventoy_get_guid_name(Protocol), SearchKey); 
+    Status = g_system_wrapper.OriLocateHandle(SearchType, Protocol, SearchKey, BufferSize, Buffer);
+    debug("ventoy_locate_handle: %r Handle Count:%u", Status, *BufferSize/sizeof(EFI_HANDLE));
+
+    if (EFI_SUCCESS == Status)
+    {
+        for (i = 0; i < *BufferSize / sizeof(EFI_HANDLE); i++)
+        {
+            if (Buffer[i] == gBlockData.Handle)
+            {
+                Handle = Buffer[0];
+                Buffer[0] = Buffer[i];
+                Buffer[i] = Handle;
+                debug("####### Handle at %u", i);
+                break;
+            }
+        }
+    }
+
+    debug_sleep();
+
+    return Status;
+}
+
+STATIC EFI_STATUS EFIAPI ventoy_locate_device_path
+(
+    IN     EFI_GUID                          *Protocol,
+    IN OUT EFI_DEVICE_PATH_PROTOCOL         **DevicePath,
+    OUT    EFI_HANDLE                        *Device
+)
+{
+    debug("ventoy_locate_device_path:%a", ventoy_get_guid_name(Protocol));  debug_sleep();
+    return g_system_wrapper.OriLocateDevicePath(Protocol, DevicePath, Device);
+}
+
 EFI_STATUS EFIAPI ventoy_wrapper_system(VOID)
 {
-    ventoy_wrapper(gBS, g_system_wrapper, LocateProtocol, ventoy_locate_protocol);
-    ventoy_wrapper(gBS, g_system_wrapper, HandleProtocol, ventoy_handle_protocol);
-    ventoy_wrapper(gBS, g_system_wrapper, OpenProtocol,    ventoy_open_protocol);
+    ventoy_wrapper(gBS, g_system_wrapper, LocateProtocol,       ventoy_locate_protocol);
+    ventoy_wrapper(gBS, g_system_wrapper, HandleProtocol,       ventoy_handle_protocol);
+    ventoy_wrapper(gBS, g_system_wrapper, OpenProtocol,         ventoy_open_protocol);
+    ventoy_wrapper(gBS, g_system_wrapper, LocateHandleBuffer,   ventoy_locate_handle_buffer);
+    ventoy_wrapper(gBS, g_system_wrapper, ProtocolsPerHandle,   ventoy_protocol_per_handle);
+    ventoy_wrapper(gBS, g_system_wrapper, LocateHandle,         ventoy_locate_handle);
+    ventoy_wrapper(gBS, g_system_wrapper, LocateDevicePath,     ventoy_locate_device_path);
 
     return EFI_SUCCESS;
 }