]> glassweightruler.freedombox.rocks Git - Ventoy.git/commitdiff
Optimization for Windows unattended install process.
authorlongpanda <admin@ventoy.net>
Thu, 10 Nov 2022 02:10:10 +0000 (10:10 +0800)
committerlongpanda <admin@ventoy.net>
Thu, 10 Nov 2022 02:10:10 +0000 (10:10 +0800)
INSTALL/ventoy/vtoyjump32.exe
INSTALL/ventoy/vtoyjump64.exe
vtoyjump/vtoyjump/vtoyjump.c

index 9adcf1270a6a4e39f91e0eaa05c09b323c277408..5ef9ccd067c79bb029051e25334c6d4f0f74ff1b 100644 (file)
Binary files a/INSTALL/ventoy/vtoyjump32.exe and b/INSTALL/ventoy/vtoyjump32.exe differ
index 1d7f5ff1608ce9ae58c2be5c9d9580ea47c5bac1..f3b3da73a0a8816343515987c56dd9edd5d8a9db 100644 (file)
Binary files a/INSTALL/ventoy/vtoyjump64.exe and b/INSTALL/ventoy/vtoyjump64.exe differ
index 4a45db794052315f337f99c09c16a83633a03b28..bbf566e4cdd254d8d22e88caa3347a0e319de4d0 100644 (file)
@@ -128,6 +128,64 @@ static void TrimString(CHAR *String, BOOL TrimLeft)
     return;\r
 }\r
 \r
     return;\r
 }\r
 \r
+static int VentoyProcessRunCmd(const char *Fmt, ...)\r
+{\r
+    int Len = 0;\r
+    va_list Arg;\r
+    STARTUPINFOA Si;\r
+    PROCESS_INFORMATION Pi;\r
+    char szBuf[1024] = { 0 };\r
+\r
+    va_start(Arg, Fmt);\r
+    Len += vsnprintf_s(szBuf + Len, sizeof(szBuf)-Len, sizeof(szBuf)-Len, Fmt, Arg);\r
+    va_end(Arg);\r
+\r
+    GetStartupInfoA(&Si);\r
+    Si.dwFlags |= STARTF_USESHOWWINDOW;\r
+    Si.wShowWindow = SW_HIDE;\r
+\r
+    Log("Process Run: <%s>", szBuf);\r
+    CreateProcessA(NULL, szBuf, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);\r
+    WaitForSingleObject(Pi.hProcess, INFINITE);\r
+\r
+    return 0;\r
+}\r
+\r
+static CHAR VentoyGetFirstFreeDriveLetter(BOOL Reverse)\r
+{\r
+    int i;\r
+    CHAR Letter = 'T';\r
+    DWORD Drives;\r
+\r
+    Drives = GetLogicalDrives();\r
+\r
+    if (Reverse)\r
+    {\r
+        for (i = 25; i >= 2; i--)\r
+        {\r
+            if (0 == (Drives & (1 << i)))\r
+            {\r
+                Letter = 'A' + i;\r
+                break;\r
+            }\r
+        }\r
+    }\r
+    else\r
+    {\r
+        for (i = 2; i < 26; i++)\r
+        {\r
+            if (0 == (Drives & (1 << i)))\r
+            {\r
+                Letter = 'A' + i;\r
+                break;\r
+            }\r
+        }\r
+    }\r
+\r
+    Log("FirstFreeDriveLetter %u %C:", Reverse, Letter);\r
+    return Letter;\r
+}\r
+\r
 void Log(const char *Fmt, ...)\r
 {\r
     va_list Arg;\r
 void Log(const char *Fmt, ...)\r
 {\r
     va_list Arg;\r
@@ -910,51 +968,7 @@ UINT64 GetVentoyEfiPartStartSector(HANDLE hDrive)
     return StartSector;\r
 }\r
 \r
     return StartSector;\r
 }\r
 \r
-static int VentoyRunImdisk(const char *IsoPath, const char *imdiskexe)\r
-{\r
-    CHAR Letter;\r
-    CHAR Cmdline[512];\r
-    WCHAR CmdlineW[512];\r
-    PROCESS_INFORMATION Pi;\r
-\r
-    Log("VentoyRunImdisk <%s> <%s>", IsoPath, imdiskexe);\r
-\r
-    Letter = GetIMDiskMountLogicalDrive();\r
-    sprintf_s(Cmdline, sizeof(Cmdline), "%s -a -o ro -f \"%s\" -m %C:", imdiskexe, IsoPath, Letter);\r
-    Log("mount iso to %C: use imdisk cmd <%s>", Letter, Cmdline);\r
-\r
-    if (IsUTF8Encode(IsoPath))\r
-    {\r
-        STARTUPINFOW Si;\r
-        GetStartupInfoW(&Si);\r
-        Si.dwFlags |= STARTF_USESHOWWINDOW;\r
-        Si.wShowWindow = SW_HIDE;\r
-\r
-        Utf8ToUtf16(Cmdline, CmdlineW);\r
-        CreateProcessW(NULL, CmdlineW, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);\r
-\r
-        Log("This is UTF8 encoding");\r
-    }\r
-    else\r
-    {\r
-        STARTUPINFOA Si;\r
-        GetStartupInfoA(&Si);\r
-        Si.dwFlags |= STARTF_USESHOWWINDOW;\r
-        Si.wShowWindow = SW_HIDE;\r
-\r
-        CreateProcessA(NULL, Cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);\r
-\r
-        Log("This is ANSI encoding");\r
-    }\r
-\r
-    Log("Wait for imdisk process ...");\r
-    WaitForSingleObject(Pi.hProcess, INFINITE);\r
-    Log("imdisk process finished");\r
-\r
-    return 0;\r
-}\r
-\r
-int VentoyMountISOByImdisk(const char *IsoPath, DWORD PhyDrive)\r
+static int VentoyCopyImdisk(DWORD PhyDrive, CHAR *ImPath)\r
 {\r
     int rc = 1;\r
     BOOL bRet;\r
 {\r
     int rc = 1;\r
     BOOL bRet;\r
@@ -963,12 +977,17 @@ int VentoyMountISOByImdisk(const char *IsoPath, DWORD PhyDrive)
     CHAR PhyPath[MAX_PATH];\r
     GET_LENGTH_INFORMATION LengthInfo;\r
 \r
     CHAR PhyPath[MAX_PATH];\r
     GET_LENGTH_INFORMATION LengthInfo;\r
 \r
-    Log("VentoyMountISOByImdisk %s", IsoPath);\r
-\r
     if (IsFileExist("X:\\Windows\\System32\\imdisk.exe"))\r
     {\r
     if (IsFileExist("X:\\Windows\\System32\\imdisk.exe"))\r
     {\r
-        Log("imdisk.exe exist, use it directly...");\r
-        VentoyRunImdisk(IsoPath, "imdisk.exe");\r
+        Log("imdisk.exe already exist, no need to copy...");\r
+        strcpy_s(ImPath, MAX_PATH, "imdisk.exe");        \r
+        return 0;\r
+    }\r
+\r
+    if (IsFileExist("X:\\Windows\\System32\\ventoy\\imdisk.exe"))\r
+    {\r
+        Log("imdisk.exe already copied, no need to copy...");\r
+        strcpy_s(ImPath, MAX_PATH, "ventoy\\imdisk.exe");\r
         return 0;\r
     }\r
 \r
         return 0;\r
     }\r
 \r
@@ -1008,13 +1027,13 @@ int VentoyMountISOByImdisk(const char *IsoPath, DWORD PhyDrive)
             CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");\r
             CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");\r
         }\r
             CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.exe", "ventoy\\imdisk.exe");\r
             CopyFileFromFatDisk("/ventoy/imdisk/32/imdisk.cpl", "ventoy\\imdisk.cpl");\r
         }\r
-        \r
+\r
         GetCurrentDirectoryA(sizeof(PhyPath), PhyPath);\r
         strcat_s(PhyPath, sizeof(PhyPath), "\\ventoy\\imdisk.sys");\r
 \r
         if (LoadNtDriver(PhyPath) == 0)\r
         GetCurrentDirectoryA(sizeof(PhyPath), PhyPath);\r
         strcat_s(PhyPath, sizeof(PhyPath), "\\ventoy\\imdisk.sys");\r
 \r
         if (LoadNtDriver(PhyPath) == 0)\r
-        {\r
-            VentoyRunImdisk(IsoPath, "ventoy\\imdisk.exe");\r
+        {        \r
+            strcpy_s(ImPath, MAX_PATH, "ventoy\\imdisk.exe");\r
             rc = 0;\r
         }\r
     }\r
             rc = 0;\r
         }\r
     }\r
@@ -1027,6 +1046,66 @@ End:
     return rc;\r
 }\r
 \r
     return rc;\r
 }\r
 \r
+static int VentoyRunImdisk(const char *IsoPath, const char *imdiskexe)\r
+{\r
+    CHAR Letter;\r
+    CHAR Cmdline[512];\r
+    WCHAR CmdlineW[512];\r
+    PROCESS_INFORMATION Pi;\r
+\r
+    Log("VentoyRunImdisk <%s> <%s>", IsoPath, imdiskexe);\r
+\r
+    Letter = GetIMDiskMountLogicalDrive();\r
+    sprintf_s(Cmdline, sizeof(Cmdline), "%s -a -o ro -f \"%s\" -m %C:", imdiskexe, IsoPath, Letter);\r
+    Log("mount iso to %C: use imdisk cmd <%s>", Letter, Cmdline);\r
+\r
+    if (IsUTF8Encode(IsoPath))\r
+    {\r
+        STARTUPINFOW Si;\r
+        GetStartupInfoW(&Si);\r
+        Si.dwFlags |= STARTF_USESHOWWINDOW;\r
+        Si.wShowWindow = SW_HIDE;\r
+\r
+        Utf8ToUtf16(Cmdline, CmdlineW);\r
+        CreateProcessW(NULL, CmdlineW, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);\r
+\r
+        Log("This is UTF8 encoding");\r
+    }\r
+    else\r
+    {\r
+        STARTUPINFOA Si;\r
+        GetStartupInfoA(&Si);\r
+        Si.dwFlags |= STARTF_USESHOWWINDOW;\r
+        Si.wShowWindow = SW_HIDE;\r
+\r
+        CreateProcessA(NULL, Cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);\r
+\r
+        Log("This is ANSI encoding");\r
+    }\r
+\r
+    Log("Wait for imdisk process ...");\r
+    WaitForSingleObject(Pi.hProcess, INFINITE);\r
+    Log("imdisk process finished");\r
+\r
+    return 0;\r
+}\r
+\r
+int VentoyMountISOByImdisk(const char *IsoPath, DWORD PhyDrive)\r
+{\r
+    int rc = 1;\r
+    CHAR ImPath[MAX_PATH];\r
+\r
+    Log("VentoyMountISOByImdisk %s", IsoPath);\r
+\r
+    if (0 == VentoyCopyImdisk(PhyDrive, ImPath))\r
+    {\r
+        VentoyRunImdisk(IsoPath, ImPath);\r
+        rc = 0;\r
+    }\r
+\r
+    return rc;\r
+}\r
+\r
 static int GetIsoId(CONST CHAR *IsoPath, IsoId *ids)\r
 {\r
     int i;\r
 static int GetIsoId(CONST CHAR *IsoPath, IsoId *ids)\r
 {\r
     int i;\r
@@ -1863,14 +1942,30 @@ static int UnattendVarExpand(const char *script, const char *tmpfile)
 \r
 //#define VAR_DEBUG 1\r
 \r
 \r
 //#define VAR_DEBUG 1\r
 \r
-static int ProcessUnattendedInstallation(const char *script)\r
+static int CreateUnattendRegKey(const char *file)\r
 {\r
     DWORD dw;\r
     HKEY hKey;\r
     LSTATUS Ret;\r
 {\r
     DWORD dw;\r
     HKEY hKey;\r
     LSTATUS Ret;\r
+\r
+#ifndef VAR_DEBUG\r
+    Ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "System\\Setup", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw);\r
+    if (ERROR_SUCCESS == Ret)\r
+    {\r
+        Ret = RegSetValueEx(hKey, "UnattendFile", 0, REG_SZ, file, (DWORD)(strlen(file) + 1));\r
+    }\r
+#endif\r
+\r
+    return 0;\r
+}\r
+\r
+static int ProcessUnattendedInstallation(const char *script, DWORD PhyDrive)\r
+{\r
     CHAR Letter;\r
     CHAR Letter;\r
+    CHAR DrvLetter;\r
     CHAR TmpFile[MAX_PATH];\r
     CHAR CurDir[MAX_PATH];\r
     CHAR TmpFile[MAX_PATH];\r
     CHAR CurDir[MAX_PATH];\r
+    CHAR ImPath[MAX_PATH];\r
 \r
     Log("Copy unattended XML ...");\r
     \r
 \r
     Log("Copy unattended XML ...");\r
     \r
@@ -1888,7 +1983,7 @@ static int ProcessUnattendedInstallation(const char *script)
 #ifdef VAR_DEBUG\r
     sprintf_s(CurDir, sizeof(CurDir), "%C:\\AutounattendXXX.xml", Letter);\r
 #else\r
 #ifdef VAR_DEBUG\r
     sprintf_s(CurDir, sizeof(CurDir), "%C:\\AutounattendXXX.xml", Letter);\r
 #else\r
-    sprintf_s(CurDir, sizeof(CurDir), "%C:\\Autounattend.xml", Letter);\r
+    sprintf_s(CurDir, sizeof(CurDir), "%C:\\Unattend.xml", Letter);\r
 #endif\r
 \r
     if (UnattendNeedVarExpand(script))\r
 #endif\r
 \r
     if (UnattendNeedVarExpand(script))\r
@@ -1897,21 +1992,32 @@ static int ProcessUnattendedInstallation(const char *script)
         UnattendVarExpand(script, TmpFile);\r
         \r
         Log("Expand Copy file <%s> --> <%s>", script, CurDir);\r
         UnattendVarExpand(script, TmpFile);\r
         \r
         Log("Expand Copy file <%s> --> <%s>", script, CurDir);\r
-        CopyFile(TmpFile, CurDir, FALSE);\r
+        CopyFileA(TmpFile, CurDir, FALSE);\r
     }\r
     else\r
     {\r
         Log("No var expand copy file <%s> --> <%s>", script, CurDir);\r
     }\r
     else\r
     {\r
         Log("No var expand copy file <%s> --> <%s>", script, CurDir);\r
-        CopyFile(script, CurDir, FALSE);\r
+        CopyFileA(script, CurDir, FALSE);\r
     }\r
     }\r
-    \r
-#ifndef VAR_DEBUG\r
-    Ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "System\\Setup", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dw);\r
-    if (ERROR_SUCCESS == Ret)\r
+\r
+    VentoyCopyImdisk(PhyDrive, ImPath);\r
+    DrvLetter = VentoyGetFirstFreeDriveLetter(FALSE);\r
+    VentoyProcessRunCmd("%s -a -s 64M -m %C: -p \"/fs:FAT32 /q /y\"", ImPath, DrvLetter);\r
+\r
+    Sleep(300);\r
+\r
+    sprintf_s(TmpFile, sizeof(TmpFile), "%C:\\Unattend.xml", DrvLetter);\r
+    if (CopyFileA(CurDir, TmpFile, FALSE))\r
     {\r
     {\r
-        Ret = RegSetValueEx(hKey, "UnattendFile", 0, REG_SZ, CurDir, (DWORD)(strlen(CurDir) + 1));\r
+        DeleteFileA(CurDir);\r
+        Log("Move file <%s> ==> <%s>, use the later as unattend XML", CurDir, TmpFile);\r
+        CreateUnattendRegKey(TmpFile);\r
+    }\r
+    else\r
+    {\r
+        Log("Failed to copy file <%s> ==> <%s>, use OLD", CurDir, TmpFile);\r
+        CreateUnattendRegKey(CurDir);\r
     }\r
     }\r
-#endif\r
 \r
     return 0;\r
 }\r
 \r
     return 0;\r
 }\r
@@ -2261,7 +2367,7 @@ static int VentoyHook(ventoy_os_param *param)
         if (IsFileExist("%s", VTOY_AUTO_FILE))\r
         {\r
             Log("use auto install script %s...", VTOY_AUTO_FILE);\r
         if (IsFileExist("%s", VTOY_AUTO_FILE))\r
         {\r
             Log("use auto install script %s...", VTOY_AUTO_FILE);\r
-            ProcessUnattendedInstallation(VTOY_AUTO_FILE);\r
+            ProcessUnattendedInstallation(VTOY_AUTO_FILE, VtoyDiskNum);\r
         }\r
         else\r
         {\r
         }\r
         else\r
         {\r
@@ -2482,22 +2588,6 @@ End:
 }\r
 \r
 \r
 }\r
 \r
 \r
-static int vtoy_cmd_delete_file(char *File)\r
-{\r
-    CHAR szCmd[MAX_PATH];\r
-    STARTUPINFOA Si;\r
-    PROCESS_INFORMATION Pi;\r
-\r
-    GetStartupInfoA(&Si);\r
-    Si.dwFlags |= STARTF_USESHOWWINDOW;\r
-    Si.wShowWindow = SW_HIDE;\r
-    sprintf_s(szCmd, sizeof(szCmd), "cmd.exe /c del /F /Q %s", File);\r
-    CreateProcessA(NULL, szCmd, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);\r
-    WaitForSingleObject(Pi.hProcess, INFINITE);\r
-\r
-    return 0;\r
-}\r
-\r
 int real_main(int argc, char **argv)\r
 {\r
     int i = 0;\r
 int real_main(int argc, char **argv)\r
 {\r
     int i = 0;\r
@@ -2575,6 +2665,8 @@ int real_main(int argc, char **argv)
     if (g_os_param_reserved[0] == 4)\r
     {\r
         Log("Open cmd for debug ...");\r
     if (g_os_param_reserved[0] == 4)\r
     {\r
         Log("Open cmd for debug ...");\r
+        Si.dwFlags |= STARTF_USESHOWWINDOW;\r
+        Si.wShowWindow = SW_NORMAL;\r
         sprintf_s(LunchFile, sizeof(LunchFile), "%s", "cmd.exe");\r
     }\r
 \r
         sprintf_s(LunchFile, sizeof(LunchFile), "%s", "cmd.exe");\r
     }\r
 \r
@@ -2724,4 +2816,3 @@ int main(int argc, char **argv)
         return real_main(argc, argv);\r
     }\r
 }\r
         return real_main(argc, argv);\r
     }\r
 }\r
-\r