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