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 "Ventoy2Disk.h"
27 #include "DiskService.h"
29 BOOL
DISK_CleanDisk(int DriveIndex
)
33 ret
= VDS_CleanDisk(DriveIndex
);
36 ret
= PSHELL_CleanDisk(DriveIndex
);
43 BOOL
DISK_DeleteVtoyEFIPartition(int DriveIndex
, UINT64 EfiPartOffset
)
47 ret
= VDS_DeleteVtoyEFIPartition(DriveIndex
, EfiPartOffset
);
50 ret
= PSHELL_DeleteVtoyEFIPartition(DriveIndex
, EfiPartOffset
);
56 BOOL
DISK_ChangeVtoyEFI2ESP(int DriveIndex
, UINT64 Offset
)
60 ret
= VDS_ChangeVtoyEFI2ESP(DriveIndex
, Offset
);
63 ret
= PSHELL_ChangeVtoyEFI2ESP(DriveIndex
, Offset
);
70 BOOL
DISK_ChangeVtoyEFI2Basic(int DriveIndex
, UINT64 Offset
)
74 ret
= VDS_ChangeVtoyEFI2Basic(DriveIndex
, Offset
);
77 ret
= PSHELL_ChangeVtoyEFI2Basic(DriveIndex
, Offset
);
83 BOOL
DISK_ChangeVtoyEFIAttr(int DriveIndex
, UINT64 Offset
, UINT64 Attr
)
87 ret
= VDS_ChangeVtoyEFIAttr(DriveIndex
, Offset
, Attr
);
92 BOOL
DISK_ShrinkVolume(int DriveIndex
, const char* VolumeGuid
, CHAR DriveLetter
, UINT64 OldBytes
, UINT64 ReduceBytes
)
96 ret
= VDS_ShrinkVolume(DriveIndex
, VolumeGuid
, DriveLetter
, OldBytes
, ReduceBytes
);
99 if (LASTERR
== VDS_E_SHRINK_DIRTY_VOLUME
)
101 Log("VDS shrink return dirty, no need to run powershell.");
105 ret
= PSHELL_ShrinkVolume(DriveIndex
, VolumeGuid
, DriveLetter
, OldBytes
, ReduceBytes
);
119 } TEXTOUTPUT
, * PTEXTOUTPUT
;
121 // Callback command types
142 // FMIFS callback definition
143 typedef BOOLEAN(__stdcall
* PFMIFSCALLBACK
)(CALLBACKCOMMAND Command
, DWORD SubAction
, PVOID ActionInfo
);
146 // Chkdsk command in FMIFS
147 typedef VOID(__stdcall
* PCHKDSK
)(PWCHAR DriveRoot
,
151 BOOL CheckOnlyIfDirty
,
155 PFMIFSCALLBACK Callback
);
159 #define FMIFS_HARDDISK 0xC
160 #define FMIFS_FLOPPY 0x8
161 // Format command in FMIFS
162 typedef VOID(__stdcall
* PFORMATEX
)(PWCHAR DriveRoot
,
168 PFMIFSCALLBACK Callback
);
172 static int g_dll_format_error
= 0;
173 BOOLEAN __stdcall
FormatExCallback(CALLBACKCOMMAND Command
, DWORD Modifier
, PVOID Argument
)
181 percent
= (PDWORD
)Argument
;
182 Log("Format percent: %d \n", *percent
);
189 status
= (PBOOLEAN
)Argument
;
190 if (*status
== FALSE
)
192 g_dll_format_error
= 1;
204 BOOL
DLL_FormatVolume(char DriveLetter
, int fs
, DWORD ClusterSize
)
206 PWCHAR Label
= L
"Ventoy";
207 PWCHAR Format
= NULL
;
208 WCHAR RootDirectory
[MAX_PATH
] = { 0 };
212 ifsModule
= LoadLibraryA("fmifs.dll");
213 if (NULL
== ifsModule
)
215 Log("LoadLibrary fmifs.dll failed %u", LASTERR
);
219 Log("Find ifsModule");
221 FormatEx
= (PFORMATEX
)GetProcAddress(ifsModule
, "FormatEx");
222 if (FormatEx
== NULL
)
224 Log("Failed to get FormatEx handler\n");
227 Log("Find FormatEx=%p", FormatEx
);
229 RootDirectory
[0] = DriveLetter
;
230 RootDirectory
[1] = L
':';
231 RootDirectory
[2] = L
'\\';
232 RootDirectory
[3] = (WCHAR
)0;
236 driveType
= GetDriveTypeW(RootDirectory
);
237 if (driveType
!= DRIVE_FIXED
)
238 media
= FMIFS_FLOPPY
;
239 if (driveType
== DRIVE_FIXED
)
240 media
= FMIFS_HARDDISK
;
242 Format
= GetVentoyFsFmtNameByTypeW(fs
);
244 g_dll_format_error
= 0;
246 Log("Call FormatEx Function for %C: %s ClusterSize=%u(%uKB)", DriveLetter
, GetVentoyFsFmtNameByTypeA(fs
), ClusterSize
, ClusterSize
/ 1024);
247 FormatEx(RootDirectory
, media
, Format
, Label
, TRUE
, ClusterSize
, FormatExCallback
);
248 FreeLibrary(ifsModule
);
250 if (g_dll_format_error
)
252 Log("Format failed by DLL");
256 Log("Format success by DLL");
261 BOOL
DISK_FormatVolume(char DriveLetter
, int fs
, UINT64 VolumeSize
)
263 DWORD ClusterSize
= 0;
266 ClusterSize
= (DWORD
)GetClusterSize();
267 Log("DISK_FormatVolume %C:\\ %s VolumeSize=%llu ClusterSize=%u(%uKB)",
268 DriveLetter
, GetVentoyFsNameByType(fs
), (ULONGLONG
)VolumeSize
, ClusterSize
, ClusterSize
/1024);
270 ret
= DLL_FormatVolume(DriveLetter
, fs
, ClusterSize
);
273 ret
= VDS_FormatVolume(DriveLetter
, fs
, ClusterSize
);
276 ret
= DSPT_FormatVolume(DriveLetter
, fs
, ClusterSize
);
279 ret
= PSHELL_FormatVolume(DriveLetter
, fs
, ClusterSize
);