]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - EDK2/edk2_mod/edk2-edk2-stable201911/MdeModulePkg/Application/Ventoy/Ventoy.c
change ntldr --> NTLDR
[Ventoy.git] / EDK2 / edk2_mod / edk2-edk2-stable201911 / MdeModulePkg / Application / Ventoy / Ventoy.c
index c05e772f16f1f87d7235e951f5539cc1879336a4..2eeaa7a29589f0987fb17d04f2ac2e0180f40ee2 100644 (file)
@@ -37,6 +37,7 @@
 #include <Ventoy.h>
 
 BOOLEAN gDebugPrint = FALSE;
 #include <Ventoy.h>
 
 BOOLEAN gDebugPrint = FALSE;
+BOOLEAN gLoadIsoEfi = FALSE;
 ventoy_ram_disk g_ramdisk_param;
 ventoy_chain_head *g_chain;
 ventoy_img_chunk *g_chunk;
 ventoy_ram_disk g_ramdisk_param;
 ventoy_chain_head *g_chain;
 ventoy_img_chunk *g_chunk;
@@ -52,6 +53,12 @@ static grub_env_get_pf grub_env_get = NULL;
 ventoy_grub_param_file_replace *g_file_replace_list = NULL;
 ventoy_efi_file_replace g_efi_file_replace;
 
 ventoy_grub_param_file_replace *g_file_replace_list = NULL;
 ventoy_efi_file_replace g_efi_file_replace;
 
+CONST CHAR16 gIso9660EfiDriverPath[] = ISO9660_EFI_DRIVER_PATH;
+
+BOOLEAN g_fix_windows_1st_cdrom_issue = FALSE;
+
+STATIC BOOLEAN g_hook_keyboard = FALSE;
+
 CHAR16 gFirstTryBootFile[256] = {0};
 
 /* Boot filename */
 CHAR16 gFirstTryBootFile[256] = {0};
 
 /* Boot filename */
@@ -63,7 +70,7 @@ CONST CHAR16 *gEfiBootFileName[] =
     L"\\EFI\\BOOT\\GRUBX64.EFI",
     L"\\EFI\\BOOT\\BOOTx64.EFI",
     L"\\EFI\\BOOT\\bootx64.efi",
     L"\\EFI\\BOOT\\GRUBX64.EFI",
     L"\\EFI\\BOOT\\BOOTx64.EFI",
     L"\\EFI\\BOOT\\bootx64.efi",
-    L"\\efi\\boot\\bootx64.efi",
+    L"\\efi\\boot\\bootx64.efi"
 };
 
 VOID EFIAPI VtoyDebug(IN CONST CHAR8  *Format, ...)
 };
 
 VOID EFIAPI VtoyDebug(IN CONST CHAR8  *Format, ...)
@@ -191,11 +198,12 @@ static void EFIAPI ventoy_dump_chain(ventoy_chain_head *chain)
     debug("os_param->vtoy_img_size=<%llu>",    chain->os_param.vtoy_img_size);
     debug("os_param->vtoy_img_location_addr=<0x%llx>", chain->os_param.vtoy_img_location_addr);
     debug("os_param->vtoy_img_location_len=<%u>",    chain->os_param.vtoy_img_location_len);
     debug("os_param->vtoy_img_size=<%llu>",    chain->os_param.vtoy_img_size);
     debug("os_param->vtoy_img_location_addr=<0x%llx>", chain->os_param.vtoy_img_location_addr);
     debug("os_param->vtoy_img_location_len=<%u>",    chain->os_param.vtoy_img_location_len);
-    debug("os_param->vtoy_reserved=<%u %u %u %u>",    
+    debug("os_param->vtoy_reserved=<%u %u %u %u %u>",    
           g_os_param_reserved[0], 
           g_os_param_reserved[1], 
           g_os_param_reserved[2], 
           g_os_param_reserved[0], 
           g_os_param_reserved[1], 
           g_os_param_reserved[2], 
-          g_os_param_reserved[3]
+          g_os_param_reserved[3],
+          g_os_param_reserved[4]
           );
 
     ventoy_debug_pause();
           );
 
     ventoy_debug_pause();
@@ -479,6 +487,93 @@ STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle)
     }
 }
 
     }
 }
 
+
+STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle)
+{
+    UINTN i = 0;
+    UINTN Count = 0;
+    EFI_HANDLE Parent = NULL;
+    EFI_HANDLE *Handles = NULL;
+    EFI_STATUS Status = EFI_SUCCESS;
+    EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
+    EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;
+
+    Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid, 
+                                     NULL, &Count, &Handles);
+    if (EFI_ERROR(Status))
+    {
+        return Status;
+    }
+
+    debug("ventoy_find_iso_disk_fs fs count:%u", Count);
+
+    for (i = 0; i < Count; i++)
+    {
+        Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
+        if (EFI_ERROR(Status))
+        {
+            continue;
+        }
+
+        Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid, 
+                                   (VOID **)&pDevPath,
+                                   ImageHandle,
+                                   Handles[i],
+                                   EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+        if (EFI_ERROR(Status))
+        {
+            debug("Failed to open device path protocol %r", Status);
+            continue;
+        }
+
+        debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
+        Parent = ventoy_get_parent_handle(pDevPath);
+
+        if (Parent == gBlockData.RawBlockIoHandle)
+        {
+            debug("Find ventoy disk fs");
+            gBlockData.DiskFsHandle = Handles[i];
+            gBlockData.pDiskFs = pFile;
+            gBlockData.pDiskFsDevPath = pDevPath;
+            break;
+        }
+    }
+
+    FreePool(Handles);
+
+    return EFI_SUCCESS;
+}
+
+STATIC EFI_STATUS EFIAPI ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle)
+{
+    EFI_HANDLE Image = NULL;
+    EFI_STATUS Status = EFI_SUCCESS;
+    CHAR16 LogVar[4] = L"5";
+            
+    Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath, 
+                               gIso9660EfiDriverPath, 
+                               sizeof(gIso9660EfiDriverPath), 
+                               &Image);
+    debug("load iso efi driver status:%r", Status);
+
+    if (gDebugPrint)
+    {
+        gRT->SetVariable(L"FS_LOGGING", &gShellVariableGuid, 
+                         EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
+                         sizeof(LogVar), LogVar);
+    }
+
+    gRT->SetVariable(L"FS_NAME_NOCASE", &gShellVariableGuid, 
+                     EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
+                     sizeof(LogVar), LogVar);
+
+    gBlockData.IsoDriverImage = Image;
+    Status = gBS->StartImage(Image, NULL, NULL);
+    debug("Start iso efi driver status:%r", Status);
+
+    return EFI_SUCCESS;
+}
+
 STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
 {   
     UINT32 i = 0;
 STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
 {   
     UINT32 i = 0;
@@ -490,6 +585,7 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
     EFI_STATUS Status = EFI_SUCCESS;
     ventoy_grub_param *pGrubParam = NULL;
     EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
     EFI_STATUS Status = EFI_SUCCESS;
     ventoy_grub_param *pGrubParam = NULL;
     EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
+    ventoy_chain_head *chain = NULL;
 
     Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
     if (EFI_ERROR(Status))
 
     Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
     if (EFI_ERROR(Status))
@@ -507,6 +603,11 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
         gDebugPrint = TRUE;
     }
 
         gDebugPrint = TRUE;
     }
 
+    if (StrStr(pCmdLine, L"isoefi=on"))
+    {
+        gLoadIsoEfi = TRUE;
+    }
+
     pPos = StrStr(pCmdLine, L"FirstTry=@");
     if (pPos)
     {
     pPos = StrStr(pCmdLine, L"FirstTry=@");
     if (pPos)
     {
@@ -556,12 +657,15 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
         );
 
     pPos = StrStr(pCmdLine, L"mem:");
         );
 
     pPos = StrStr(pCmdLine, L"mem:");
-    g_chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
+    chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
 
     pPos = StrStr(pPos, L"size:");
     size = StrDecimalToUintn(pPos + 5);
 
 
     pPos = StrStr(pPos, L"size:");
     size = StrDecimalToUintn(pPos + 5);
 
-    debug("memory addr:%p size:%lu", g_chain, size);
+    debug("memory addr:%p size:%lu", chain, size);
+    
+    g_chain = AllocatePool(size);
+    CopyMem(g_chain, chain, size);
 
     if (StrStr(pCmdLine, L"memdisk"))
     {
 
     if (StrStr(pCmdLine, L"memdisk"))
     {
@@ -580,11 +684,18 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
         g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
 
         /* Workaround for Windows & ISO9660 */
         g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);
 
         /* Workaround for Windows & ISO9660 */
-        if (g_os_param_reserved[2] == 1 && g_os_param_reserved[3] == 0)
+        if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[3] == 0)
         {
             g_fixup_iso9660_secover_enable = TRUE;
         }
 
         {
             g_fixup_iso9660_secover_enable = TRUE;
         }
 
+        if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[4] != 1)
+        {
+            g_hook_keyboard = TRUE;
+        }
+
+        debug("internal param: secover:%u keyboard:%u", g_fixup_iso9660_secover_enable, g_hook_keyboard);
+
         for (i = 0; i < sizeof(ventoy_os_param); i++)
         {
             chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
         for (i = 0; i < sizeof(ventoy_os_param); i++)
         {
             chksum += *((UINT8 *)(&(g_chain->os_param)) + i);
@@ -603,6 +714,17 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
         }
     }
 
         }
     }
 
+    g_fix_windows_1st_cdrom_issue = FALSE;
+    if (ventoy_chain_windows == g_os_param_reserved[2] || 
+        ventoy_chain_wim == g_os_param_reserved[2])
+    {
+        if (ventoy_is_cdrom_dp_exist())
+        {
+            debug("fixup the 1st cdrom influences when boot windows ...");
+            g_fix_windows_1st_cdrom_issue = TRUE;
+        }
+    }
+
     FreePool(pCmdLine);
     return EFI_SUCCESS;
 }
     FreePool(pCmdLine);
     return EFI_SUCCESS;
 }
@@ -612,6 +734,11 @@ EFI_STATUS EFIAPI ventoy_clean_env(VOID)
     FreePool(g_sector_flag);
     g_sector_flag_num = 0;
 
     FreePool(g_sector_flag);
     g_sector_flag_num = 0;
 
+    if (gLoadIsoEfi && gBlockData.IsoDriverImage)
+    {
+        gBS->UnloadImage(gBlockData.IsoDriverImage);
+    }
+
     gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
 
     gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
     gBS->DisconnectController(gBlockData.Handle, NULL, NULL);
 
     gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
@@ -626,6 +753,44 @@ EFI_STATUS EFIAPI ventoy_clean_env(VOID)
         FreePool((VOID *)(UINTN)g_chain->os_param.vtoy_img_location_addr);
     }
 
         FreePool((VOID *)(UINTN)g_chain->os_param.vtoy_img_location_addr);
     }
 
+    FreePool(g_chain);
+
+    return EFI_SUCCESS;
+}
+
+STATIC EFI_STATUS ventoy_hook_start(VOID)
+{
+    /* don't add debug print in this function */
+
+    if (g_fix_windows_1st_cdrom_issue)
+    {
+        ventoy_hook_1st_cdrom_start();
+    }
+
+    /* let this the last */
+    if (g_hook_keyboard)
+    {
+        ventoy_hook_keyboard_start();
+    }
+
+    return EFI_SUCCESS;
+}
+
+STATIC EFI_STATUS ventoy_hook_stop(VOID)
+{
+    /* don't add debug print in this function */
+
+    if (g_fix_windows_1st_cdrom_issue)
+    {
+        ventoy_hook_1st_cdrom_stop();
+    }
+
+    /* let this the last */
+    if (g_hook_keyboard)
+    {
+        ventoy_hook_keyboard_stop();
+    }
+
     return EFI_SUCCESS;
 }
 
     return EFI_SUCCESS;
 }
 
@@ -707,16 +872,20 @@ EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
             if (gDebugPrint)
             {
                 gST->ConIn->Reset(gST->ConIn, FALSE);
             if (gDebugPrint)
             {
                 gST->ConIn->Reset(gST->ConIn, FALSE);
-                //ventoy_wrapper_system();
             }
             }
-
+            
             if (g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC)
             {
                 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
                 pFile->OpenVolume = ventoy_wrapper_open_volume;
             }
             if (g_file_replace_list && g_file_replace_list->magic == GRUB_FILE_REPLACE_MAGIC)
             {
                 ventoy_wrapper_push_openvolume(pFile->OpenVolume);
                 pFile->OpenVolume = ventoy_wrapper_open_volume;
             }
-            
+
+            ventoy_hook_start();
+            /* can't add debug print here */
+            //ventoy_wrapper_system();
             Status = gBS->StartImage(Image, NULL, NULL);
             Status = gBS->StartImage(Image, NULL, NULL);
+            ventoy_hook_stop();
+            
             if (EFI_ERROR(Status))
             {
                 debug("Failed to start image %r", Status);
             if (EFI_ERROR(Status))
             {
                 debug("Failed to start image %r", Status);
@@ -743,7 +912,6 @@ EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
     return EFI_SUCCESS;
 }
 
     return EFI_SUCCESS;
 }
 
-
 EFI_STATUS EFIAPI VentoyEfiMain
 (
     IN EFI_HANDLE         ImageHandle,
 EFI_STATUS EFIAPI VentoyEfiMain
 (
     IN EFI_HANDLE         ImageHandle,
@@ -751,6 +919,7 @@ EFI_STATUS EFIAPI VentoyEfiMain
 )
 {
     EFI_STATUS Status = EFI_SUCCESS;
 )
 {
     EFI_STATUS Status = EFI_SUCCESS;
+    EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *Protocol;
     
     g_sector_flag_num = 512; /* initial value */
 
     
     g_sector_flag_num = 512; /* initial value */
 
@@ -760,6 +929,12 @@ EFI_STATUS EFIAPI VentoyEfiMain
         return EFI_OUT_OF_RESOURCES;
     }
 
         return EFI_OUT_OF_RESOURCES;
     }
 
+    Status = gBS->HandleProtocol(gST->ConsoleInHandle, &gEfiSimpleTextInputExProtocolGuid, (VOID **)&Protocol);
+    if (EFI_SUCCESS == Status)
+    {
+        g_con_simple_input_ex = Protocol;
+    }
+
     gST->ConOut->ClearScreen(gST->ConOut);
     ventoy_clear_input();
 
     gST->ConOut->ClearScreen(gST->ConOut);
     ventoy_clear_input();
 
@@ -782,6 +957,12 @@ EFI_STATUS EFIAPI VentoyEfiMain
         ventoy_save_variable();
         ventoy_find_iso_disk(ImageHandle);
 
         ventoy_save_variable();
         ventoy_find_iso_disk(ImageHandle);
 
+        if (gLoadIsoEfi)
+        {
+            ventoy_find_iso_disk_fs(ImageHandle);
+            ventoy_load_isoefi_driver(ImageHandle);
+        }
+
         ventoy_debug_pause();
         
         ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
         ventoy_debug_pause();
         
         ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);