1 /******************************************************************************
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
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.
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.
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/>.
26 #include <VersionHelpers.h>
27 #include "Ventoy2Disk.h"
28 #include "DiskService.h"
30 STATIC BOOL
IsPowershellExist(void)
34 if (!IsWindows8OrGreater())
36 Log("This is before Windows8 powershell disk not supported.");
40 ret
= IsFileExist("C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe");
43 Log("powershell.exe not exist");
49 int PSHELL_GetPartitionNumber(int PhyDrive
, UINT64 Offset
)
58 DRIVE_LAYOUT_INFORMATION_EX
*pDriveLayout
= NULL
;
60 Log("PSHELL_GetPartitionNumber PhyDrive:%d Offset:%llu", PhyDrive
, Offset
);
62 hDrive
= GetPhysicalHandle(PhyDrive
, FALSE
, FALSE
, FALSE
);
63 if (hDrive
== INVALID_HANDLE_VALUE
)
68 BufLen
= (DWORD
)(sizeof(PARTITION_INFORMATION_EX
)* 256);
70 pDriveLayout
= malloc(BufLen
);
75 memset(pDriveLayout
, 0, BufLen
);
77 bRet
= DeviceIoControl(hDrive
,
78 IOCTL_DISK_GET_DRIVE_LAYOUT_EX
, NULL
,
86 Log("Failed to ioctrl get drive layout ex %u", LASTERR
);
90 Log("PhyDrive:%d PartitionStyle=%s PartitionCount=%u", PhyDrive
,
91 (pDriveLayout
->PartitionStyle
== PARTITION_STYLE_MBR
) ? "MBR" : "GPT", pDriveLayout
->PartitionCount
);
93 for (i
= 0; i
< pDriveLayout
->PartitionCount
; i
++)
95 PartStart
= pDriveLayout
->PartitionEntry
[i
].StartingOffset
.QuadPart
;
96 if (PartStart
== (LONGLONG
)Offset
)
98 Log("[*] [%d] PartitionNumber=%u Offset=%lld Length=%lld ",
100 pDriveLayout
->PartitionEntry
[i
].PartitionNumber
,
101 pDriveLayout
->PartitionEntry
[i
].StartingOffset
.QuadPart
,
102 pDriveLayout
->PartitionEntry
[i
].PartitionLength
.QuadPart
104 partnum
= (int)pDriveLayout
->PartitionEntry
[i
].PartitionNumber
;
108 Log("[ ] [%d] PartitionNumber=%u Offset=%lld Length=%lld ",
110 pDriveLayout
->PartitionEntry
[i
].PartitionNumber
,
111 pDriveLayout
->PartitionEntry
[i
].StartingOffset
.QuadPart
,
112 pDriveLayout
->PartitionEntry
[i
].PartitionLength
.QuadPart
119 CHECK_CLOSE_HANDLE(hDrive
);
120 CHECK_FREE(pDriveLayout
);
126 STATIC BOOL
PSHELL_CommProc(const char *Cmd
)
130 PROCESS_INFORMATION Pi
;
132 if (!IsPowershellExist())
137 GetStartupInfoA(&Si
);
138 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
139 Si
.wShowWindow
= SW_HIDE
;
141 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -Command \"&{ %s }\"", Cmd
);
143 Log("CreateProcess <%s>", CmdBuf
);
144 CreateProcessA(NULL
, CmdBuf
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
146 Log("Wair process ...");
147 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
148 Log("Process finished...");
150 CHECK_CLOSE_HANDLE(Pi
.hProcess
);
151 CHECK_CLOSE_HANDLE(Pi
.hThread
);
157 BOOL
PSHELL_CleanDisk(int DriveIndex
)
162 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "Clear-Disk -Number %d -RemoveData -RemoveOEM -Confirm:$false", DriveIndex
);
163 ret
= PSHELL_CommProc(CmdBuf
);
164 Log("CleanDiskByPowershell<%d> ret:%d (%s)", DriveIndex
, ret
, ret
? "SUCCESS" : "FAIL");
170 BOOL
PSHELL_DeleteVtoyEFIPartition(int DriveIndex
, UINT64 EfiPartOffset
)
176 Part
= PSHELL_GetPartitionNumber(DriveIndex
, EfiPartOffset
);
183 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "Remove-Partition -DiskNumber %d -PartitionNumber %d -Confirm:$false", DriveIndex
, Part
);
184 ret
= PSHELL_CommProc(CmdBuf
);
187 Log("PSHELL_DeleteVtoyEFIPartition<%d> ret:%d (%s)", DriveIndex
, ret
, ret
? "SUCCESS" : "FAIL");
192 BOOL
PSHELL_ChangeVtoyEFI2ESP(int DriveIndex
, UINT64 Offset
)
198 Part
= PSHELL_GetPartitionNumber(DriveIndex
, Offset
);
205 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "Set-Partition -DiskNumber %d -PartitionNumber %d -gpttype '{C12A7328-F81F-11D2-BA4B-00A0C93EC93B}' -Confirm:$false", DriveIndex
, Part
);
206 ret
= PSHELL_CommProc(CmdBuf
);
209 Log("PSHELL_ChangeVtoyEFI2ESP<%d> ret:%d (%s)", DriveIndex
, ret
, ret
? "SUCCESS" : "FAIL");
214 BOOL
PSHELL_ChangeVtoyEFI2Basic(int DriveIndex
, UINT64 Offset
)
220 Part
= PSHELL_GetPartitionNumber(DriveIndex
, Offset
);
227 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "Set-Partition -DiskNumber %d -PartitionNumber %d -gpttype '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' -Confirm:$false", DriveIndex
, Part
);
228 ret
= PSHELL_CommProc(CmdBuf
);
231 Log("PSHELL_ChangeVtoyEFI2Basic<%d> ret:%d (%s)", DriveIndex
, ret
, ret
? "SUCCESS" : "FAIL");
235 BOOL
PSHELL_ShrinkVolume(int DriveIndex
, const char* VolumeGuid
, CHAR DriveLetter
, UINT64 OldBytes
, UINT64 ReduceBytes
)
243 Part
= PSHELL_GetPartitionNumber(DriveIndex
, SIZE_1MB
);
250 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "Resize-Partition -DiskNumber %d -PartitionNumber %d -Size %llu -Confirm:$false",
251 DriveIndex
, Part
, OldBytes
- ReduceBytes
);
252 ret
= PSHELL_CommProc(CmdBuf
);
255 Log("PSHELL_ShrinkVolume<%d> %C: ret:%d (%s)", DriveIndex
, DriveLetter
, ret
, ret
? "SUCCESS" : "FAIL");