-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
167 lines (144 loc) · 5.65 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using WinDMAManager.Helper;
using WinDMAManager.Helper.PCIDevices;
using WinDMAManager.List;
namespace WinDMAManager
{
internal static class Program
{
private static bool? _isInitialized;
// ReSharper disable once FunctionRecursiveOnAllPaths
private static void Main()
{
// Run initialization if flag is null
_isInitialized ??= Initialize();
if ((bool)!_isInitialized)
{
Console.WriteLine("Failure to initialize! Exiting app...");
Environment.Exit(100);
}
Console.WriteLine("Match List:\r\n\t" +
"Y = In allowed list\r\n\t" +
"N = In unallowed list\r\n\t" +
"? = Not found in any list");
foreach(var l in PCIDevice.PciDevice ) Console.WriteLine($"Found Device #{l.N.ToString("D3")}, Match = {l.Match!}, {l.DeviceID}, {l.Description}");
Console.WriteLine();
var input = SelectOption();
switch (input.ToUpper())
{
case "A":
RegistryHelper.AddAllToAllowList();
Deinitialize();
break;
case "D":
RegistryHelper.AddAllToUnallowList();
Deinitialize();
break;
case "Q":
Console.WriteLine("Are you sure? [Y/N]");
var confA = Console.ReadLine()!.ToUpper();
if (confA == "Y")
{
RegistryHelper.ResetAllowList();
Deinitialize();
}
break;
case "E":
Console.WriteLine("Are you sure? [Y/N]");
var confD = Console.ReadLine()!.ToUpper();
if (confD == "Y")
{
RegistryHelper.ResetUnallowList();
Deinitialize();
}
break;
case "X":
Exit();
break;
default:
if (int.TryParse(input, out int val))
{
ModifyDevice(val);
break;
}
Console.WriteLine("Invalid input!");
break;
}
Main();
}
private static void Exit()
{
Environment.Exit(0);
}
private static bool Initialize()
{
try
{
// Initialize PCI list
PCIHelper.SearchAllPCIDevice();
// Takeover DMA registry
var takeOwnAllowDMA = RegistryHelper.TakeOwnership(RegistryHelper.AllowedDMA);
if (takeOwnAllowDMA != Task.CompletedTask)
throw new ApplicationException("Failed to take ownership on AllowedDMA registry!");
var takeOwnUnallowDMA = RegistryHelper.TakeOwnership(RegistryHelper.UnallowedDMA);
if (takeOwnUnallowDMA != Task.CompletedTask)
throw new ApplicationException("Failed to take ownership on UnallowedDMA registry!");
// Initialize DMA list
RegistryHelper.ListAllowedDMA();
RegistryHelper.ListUnallowedDMA();
// Match list
PCIHelper.MatchPCIID();
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}
private static void Deinitialize()
{
AllowedDMA.DMADevice.Clear();
UnallowedDMA.DMADevice.Clear();
PCIDevice.PciDevice.Clear();
_isInitialized = null;
}
private static string SelectOption()
{
Console.WriteLine("Options:\r\n\t" +
"A = Add all to allow list\r\n\t" +
"D = Add all to disallow list\r\n\t" +
"Q = Reset allowed DMA list\r\n\t" +
"E = Reset unallowed DMA list\r\n\t" +
"X = Exit program\r\n\t" +
"Type device # to modify specific device in the list");
Console.WriteLine("Input:");
return Console.ReadLine()!;
}
private static void ModifyDevice(int devNum)
{
List<PCIDeviceList> pciDevList = PCIDevice.PciDevice;
var device = pciDevList[devNum];
Console.WriteLine("Selected device:\r\n" +
$"{device.N}, Match = {device.Match}, {device.DeviceID}, {device.Description}");
Console.WriteLine("Action:" +
"A = Add to allow list\r\n\t" +
"D = Add to disallow list\r\n\t" +
"X = Go back to main menu\r\n\t");
var read = Console.ReadLine();
switch (read!.ToUpper())
{
case "A":
RegistryHelper.AddToAllowList(devNum);
Deinitialize();
break;
case "D":
RegistryHelper.AddToUnallowList(devNum);
Deinitialize();
break;
}
// Clear console and go back to main
Console.Clear();
Main();
}
}
}