]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/DiskService_diskpart.c
6d02e90517e45dd1725c1fe94955797b038f061e
[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("C:\\Windows\\system32\\diskpart.exe");
34 if (!ret)
35 {
36 Log("diskpart.exe not exist");
37 }
38
39 return ret;
40 }
41
42 STATIC BOOL DSPT_CommProc(const char *Cmd)
43 {
44 CHAR CmdBuf[MAX_PATH];
45 CHAR CmdFile[MAX_PATH];
46 STARTUPINFOA Si;
47 PROCESS_INFORMATION Pi;
48
49 GetCurrentDirectoryA(sizeof(CmdBuf), CmdBuf);
50 sprintf_s(CmdFile, sizeof(CmdFile), "%s\\ventoy\\diskpart_%u.txt", CmdBuf, GetCurrentProcessId());
51
52 SaveBufToFile(CmdFile, Cmd, (int)strlen(Cmd));
53
54 GetStartupInfoA(&Si);
55 Si.dwFlags |= STARTF_USESHOWWINDOW;
56 Si.wShowWindow = SW_HIDE;
57
58 sprintf_s(CmdBuf, sizeof(CmdBuf), "C:\\Windows\\system32\\diskpart.exe /s \"%s\"", CmdFile);
59
60 Log("CreateProcess <%s>", CmdBuf);
61 CreateProcessA(NULL, CmdBuf, NULL, NULL, FALSE, 0, NULL, NULL, &Si, &Pi);
62
63 Log("Wair process ...");
64 WaitForSingleObject(Pi.hProcess, INFINITE);
65 Log("Process finished...");
66
67 CHECK_CLOSE_HANDLE(Pi.hProcess);
68 CHECK_CLOSE_HANDLE(Pi.hThread);
69
70 DeleteFileA(CmdFile);
71 return TRUE;
72 }
73
74 BOOL DSPT_CleanDisk(int DriveIndex)
75 {
76 CHAR CmdBuf[128];
77
78 Log("CleanDiskByDiskpart <%d>", DriveIndex);
79
80 if (!IsDiskpartExist())
81 {
82 return FALSE;
83 }
84
85 sprintf_s(CmdBuf, sizeof(CmdBuf), "select disk %d\r\nclean\r\n", DriveIndex);
86 return DSPT_CommProc(CmdBuf);
87 }
88
89 BOOL DSPT_FormatVolume(char DriveLetter, int fs)
90 {
91 const char* fsname = NULL;
92 CHAR CmdBuf[256];
93
94 Log("FormatVolumeByDiskpart <%C:>", DriveLetter);
95
96 if (!IsDiskpartExist())
97 {
98 return FALSE;
99 }
100
101 if (fs == 1)
102 {
103 fsname = "NTFS";
104 }
105 else
106 {
107 fsname = "FAT32";
108 }
109
110 sprintf_s(CmdBuf, sizeof(CmdBuf), "select volume %C:\r\nformat FS=%s LABEL=Ventoy QUICK OVERRIDE\r\n", DriveLetter, fsname);
111 return DSPT_CommProc(CmdBuf);
112 }