]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/DiskService_diskpart.c
1 /******************************************************************************
2 * DiskService_diskpart.c
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 STATIC BOOL
IsDiskpartExist(void)
33 ret
= IsFileExist("%s\\system32\\diskpart.exe", DISK_GetWindowsDir());
36 Log("diskpart.exe not exist");
43 STATIC BOOL
IsCmdExist(void)
47 ret
= IsFileExist("%s\\system32\\cmd.exe", DISK_GetWindowsDir());
50 Log("cmd.exe not exist");
56 STATIC BOOL
DSPT_CommProc(const char *Cmd
)
58 CHAR CmdBuf
[MAX_PATH
];
59 CHAR CmdFile
[MAX_PATH
];
61 PROCESS_INFORMATION Pi
;
63 GetCurrentDirectoryA(sizeof(CmdBuf
), CmdBuf
);
64 sprintf_s(CmdFile
, sizeof(CmdFile
), "%s\\ventoy\\diskpart_%u.txt", CmdBuf
, GetCurrentProcessId());
66 SaveBufToFile(CmdFile
, Cmd
, (int)strlen(Cmd
));
69 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
70 Si
.wShowWindow
= SW_HIDE
;
72 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "C:\\Windows\\system32\\diskpart.exe /s \"%s\"", CmdFile
);
74 Log("CreateProcess <%s>", CmdBuf
);
75 CreateProcessA(NULL
, CmdBuf
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
77 Log("Wair process ...");
78 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
79 Log("Process finished...");
81 CHECK_CLOSE_HANDLE(Pi
.hProcess
);
82 CHECK_CLOSE_HANDLE(Pi
.hThread
);
89 STATIC BOOL
CMD_CommProc(char* Cmd
)
92 PROCESS_INFORMATION Pi
;
95 Si
.dwFlags
|= STARTF_USESHOWWINDOW
;
96 Si
.wShowWindow
= SW_HIDE
;
98 Log("CreateProcess <%s>", Cmd
);
99 CreateProcessA(NULL
, Cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &Si
, &Pi
);
101 Log("Wair process ...");
102 WaitForSingleObject(Pi
.hProcess
, INFINITE
);
103 Log("Process finished...");
105 CHECK_CLOSE_HANDLE(Pi
.hProcess
);
106 CHECK_CLOSE_HANDLE(Pi
.hThread
);
111 BOOL
DSPT_CleanDisk(int DriveIndex
)
115 Log("CleanDiskByDiskpart <%d>", DriveIndex
);
117 if (!IsDiskpartExist())
122 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "select disk %d\r\nclean\r\n", DriveIndex
);
123 return DSPT_CommProc(CmdBuf
);
126 BOOL
DSPT_FormatVolume(char DriveLetter
, int fs
, DWORD ClusterSize
)
128 const char* fsname
= NULL
;
132 Log("FormatVolumeByDiskpart <%C:>", DriveLetter
);
134 if (!IsDiskpartExist())
139 fsname
= GetVentoyFsFmtNameByTypeA(fs
);
143 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "select volume %C:\r\nformat FS=%s LABEL=Ventoy UNIT=%u QUICK OVERRIDE\r\n", DriveLetter
, fsname
, ClusterSize
);
147 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "select volume %C:\r\nformat FS=%s LABEL=Ventoy QUICK OVERRIDE\r\n", DriveLetter
, fsname
);
150 Log("Diskpart cmd:<%s>", CmdBuf
);
152 DSPT_CommProc(CmdBuf
);
154 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "%C:\\", DriveLetter
);
155 GetVolumeInformationA(CmdBuf
, NULL
, 0, NULL
, NULL
, NULL
, FsName
, sizeof(FsName
));
156 VentoyStringToUpper(FsName
);
158 Log("New fs name after run diskpart:<%s>", FsName
);
160 if (strcmp(FsName
, fsname
) == 0)
162 Log("FormatVolumeByDiskpart <%C:> SUCCESS", DriveLetter
);
167 Log("FormatVolumeByDiskpart <%C:> FAILED", DriveLetter
);
173 BOOL
CMD_FormatVolume(char DriveLetter
, int fs
, DWORD ClusterSize
)
175 const char* fsname
= NULL
;
179 Log("FormatVolumeByCmd <%C:>", DriveLetter
);
186 fsname
= GetVentoyFsFmtNameByTypeA(fs
);
190 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "cmd.exe /c \"echo Y|format %C: /V:Ventoy /fs:%s /q /A:%u /X\"", DriveLetter
, fsname
, ClusterSize
);
194 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "cmd.exe /c \"echo Y|format %C: /V:Ventoy /fs:%s /q /X\"", DriveLetter
, fsname
);
197 Log("Cmd.exe <%s>", CmdBuf
);
199 CMD_CommProc(CmdBuf
);
201 sprintf_s(CmdBuf
, sizeof(CmdBuf
), "%C:\\", DriveLetter
);
202 GetVolumeInformationA(CmdBuf
, NULL
, 0, NULL
, NULL
, NULL
, FsName
, sizeof(FsName
));
203 VentoyStringToUpper(FsName
);
205 Log("New fs name after run cmd.exe:<%s>", FsName
);
207 if (strcmp(FsName
, fsname
) == 0)
209 Log("FormatVolumeByCmd <%C:> SUCCESS", DriveLetter
);
214 Log("FormatVolumeByCmd <%C:> FAILED", DriveLetter
);