]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/DiskService_diskpart.c
Fix the order issue in TreeView mode. (#3218)
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / DiskService_diskpart.c
1 /******************************************************************************
2 * DiskService_diskpart.c
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include <Windows.h>
22 #include <winternl.h>
23 #include <commctrl.h>
24 #include <initguid.h>
25 #include <vds.h>
26 #include "Ventoy2Disk.h"
27 #include "DiskService.h"
28
29 STATIC BOOL IsDiskpartExist(void)
30 {
31 BOOL ret;
32
33 ret = IsFileExist("%s\\system32\\diskpart.exe", DISK_GetWindowsDir());
34 if (!ret)
35 {
36 Log("diskpart.exe not exist");
37 }
38
39 return ret;
40 }
41
42
43 STATIC BOOL IsCmdExist(void)
44 {
45 BOOL ret;
46
47 ret = IsFileExist("%s\\system32\\cmd.exe", DISK_GetWindowsDir());
48 if (!ret)
49 {
50 Log("cmd.exe not exist");
51 }
52
53 return ret;
54 }
55
56 STATIC BOOL DSPT_CommProc(const char *Cmd)
57 {
58 CHAR CmdBuf[MAX_PATH];
59 CHAR CmdFile[MAX_PATH];
60 STARTUPINFOA Si;
61 PROCESS_INFORMATION Pi;
62
63 GetCurrentDirectoryA(sizeof(CmdBuf), CmdBuf);
64 sprintf_s(CmdFile, sizeof(CmdFile), "%s\\ventoy\\diskpart_%u.txt", CmdBuf, GetCurrentProcessId());
65
66 SaveBufToFile(CmdFile, Cmd, (int)strlen(Cmd));
67
68 GetStartupInfoA(&Si);
69 Si.dwFlags |= STARTF_USESHOWWINDOW;
70 Si.wShowWindow = SW_HIDE;
71
72 sprintf_s(CmdBuf, sizeof(CmdBuf), "C:\\Windows\\system32\\diskpart.exe /s \"%s\"", CmdFile);
73
74 Log("CreateProcess <%s>", CmdBuf);
75 CreateProcessA(NULL, CmdBuf, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);
76
77 Log("Wair process ...");
78 WaitForSingleObject(Pi.hProcess, INFINITE);
79 Log("Process finished...");
80
81 CHECK_CLOSE_HANDLE(Pi.hProcess);
82 CHECK_CLOSE_HANDLE(Pi.hThread);
83
84 DeleteFileA(CmdFile);
85 return TRUE;
86 }
87
88
89 STATIC BOOL CMD_CommProc(char* Cmd)
90 {
91 STARTUPINFOA Si;
92 PROCESS_INFORMATION Pi;
93
94 GetStartupInfoA(&Si);
95 Si.dwFlags |= STARTF_USESHOWWINDOW;
96 Si.wShowWindow = SW_HIDE;
97
98 Log("CreateProcess <%s>", Cmd);
99 CreateProcessA(NULL, Cmd, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);
100
101 Log("Wair process ...");
102 WaitForSingleObject(Pi.hProcess, INFINITE);
103 Log("Process finished...");
104
105 CHECK_CLOSE_HANDLE(Pi.hProcess);
106 CHECK_CLOSE_HANDLE(Pi.hThread);
107
108 return TRUE;
109 }
110
111 BOOL DSPT_CleanDisk(int DriveIndex)
112 {
113 CHAR CmdBuf[128];
114
115 Log("CleanDiskByDiskpart <%d>", DriveIndex);
116
117 if (!IsDiskpartExist())
118 {
119 return FALSE;
120 }
121
122 sprintf_s(CmdBuf, sizeof(CmdBuf), "select disk %d\r\nclean\r\n", DriveIndex);
123 return DSPT_CommProc(CmdBuf);
124 }
125
126 BOOL DSPT_FormatVolume(char DriveLetter, int fs, DWORD ClusterSize)
127 {
128 const char* fsname = NULL;
129 CHAR CmdBuf[256];
130 CHAR FsName[128];
131
132 Log("FormatVolumeByDiskpart <%C:>", DriveLetter);
133
134 if (!IsDiskpartExist())
135 {
136 return FALSE;
137 }
138
139 fsname = GetVentoyFsFmtNameByTypeA(fs);
140
141 if (ClusterSize > 0)
142 {
143 sprintf_s(CmdBuf, sizeof(CmdBuf), "select volume %C:\r\nformat FS=%s LABEL=Ventoy UNIT=%u QUICK OVERRIDE\r\n", DriveLetter, fsname, ClusterSize);
144 }
145 else
146 {
147 sprintf_s(CmdBuf, sizeof(CmdBuf), "select volume %C:\r\nformat FS=%s LABEL=Ventoy QUICK OVERRIDE\r\n", DriveLetter, fsname);
148 }
149
150 Log("Diskpart cmd:<%s>", CmdBuf);
151
152 DSPT_CommProc(CmdBuf);
153
154 sprintf_s(CmdBuf, sizeof(CmdBuf), "%C:\\", DriveLetter);
155 GetVolumeInformationA(CmdBuf, NULL, 0, NULL, NULL, NULL, FsName, sizeof(FsName));
156 VentoyStringToUpper(FsName);
157
158 Log("New fs name after run diskpart:<%s>", FsName);
159
160 if (strcmp(FsName, fsname) == 0)
161 {
162 Log("FormatVolumeByDiskpart <%C:> SUCCESS", DriveLetter);
163 return TRUE;
164 }
165 else
166 {
167 Log("FormatVolumeByDiskpart <%C:> FAILED", DriveLetter);
168 return FALSE;
169 }
170 }
171
172
173 BOOL CMD_FormatVolume(char DriveLetter, int fs, DWORD ClusterSize)
174 {
175 const char* fsname = NULL;
176 CHAR CmdBuf[256];
177 CHAR FsName[128];
178
179 Log("FormatVolumeByCmd <%C:>", DriveLetter);
180
181 if (!IsCmdExist())
182 {
183 return FALSE;
184 }
185
186 fsname = GetVentoyFsFmtNameByTypeA(fs);
187
188 if (ClusterSize > 0)
189 {
190 sprintf_s(CmdBuf, sizeof(CmdBuf), "cmd.exe /c \"echo Y|format %C: /V:Ventoy /fs:%s /q /A:%u /X\"", DriveLetter, fsname, ClusterSize);
191 }
192 else
193 {
194 sprintf_s(CmdBuf, sizeof(CmdBuf), "cmd.exe /c \"echo Y|format %C: /V:Ventoy /fs:%s /q /X\"", DriveLetter, fsname);
195 }
196
197 Log("Cmd.exe <%s>", CmdBuf);
198
199 CMD_CommProc(CmdBuf);
200
201 sprintf_s(CmdBuf, sizeof(CmdBuf), "%C:\\", DriveLetter);
202 GetVolumeInformationA(CmdBuf, NULL, 0, NULL, NULL, NULL, FsName, sizeof(FsName));
203 VentoyStringToUpper(FsName);
204
205 Log("New fs name after run cmd.exe:<%s>", FsName);
206
207 if (strcmp(FsName, fsname) == 0)
208 {
209 Log("FormatVolumeByCmd <%C:> SUCCESS", DriveLetter);
210 return TRUE;
211 }
212 else
213 {
214 Log("FormatVolumeByCmd <%C:> FAILED", DriveLetter);
215 return FALSE;
216 }
217 }