-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostBuildTasks.twin
More file actions
246 lines (231 loc) · 9.46 KB
/
PostBuildTasks.twin
File metadata and controls
246 lines (231 loc) · 9.46 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'**Not yet tested**
'This module is not compiled into the driver, but rather contains a procedure
'with the [RunAfterBuild] attribute. It will automatically be executed when a
'build completes, and perform the following actions:
'1) Copy the unstamped .inf template file from App.Path to the build folder,
' replacing any existing one from previous builds.
'2) Stamp the inf with current date/time/build architecture using stampinf.exe
'3) Create a catalog file from the inf allowing signing for compatible OSs (7-11 here),
' using inf2cat.exe.
'4) Run signtool.exe to sign the catalog and inf file. This will use a Windows Driver Kit
' test signature by default, but you can easily configure the arguments for signtool to
' use a real certificate you own. Note that for it to matter, this would have to be an
' EV cert and also be cross-signed by a Microsoft cert. Anything short of that your
' driver will still only load if you've enabled test signing mode or disabled signature
' enforcement entirely. That has to be done every boot from the advanced boot menu.
'
' **IMPORTANT** You'll likely need to adjust the path of these SDK/WDK utility programs
' in the first few lines of the module below.
#Const ENABLE_AUTO_POST_BUILD = 0
Module PostBuildTasks
Private Const ENABLE_STAMPINF = 1
Private Const ENABLE_INF2CAT = 0
Private Const ENABLE_SIGNTOOL = 0
Private Const szInf2Cat = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\inf2cat.exe"
Private Const szStampInf = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\stampinf.exe"
Private Const szSignTool64 = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64\signtool.exe"
Private Const szSignTool32 = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\signtool.exe"
#If Win64 Then
Private Const szSignTool = szSignTool64
#Else
Private Const szSignTool = szSignTool32
#End If
Private DeclareWide PtrSafe Function ShellExecuteEx Lib "shell32" Alias "ShellExecuteExW" (ByRef pExecInfo As SHELLEXECUTEINFO) As BOOL
Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As SHELLEXECUTEMASK
hwnd As LongPtr
lpVerb As LongPtr
lpFile As LongPtr
lpParameters As LongPtr
lpDirectory As LongPtr
nShow As SHOWWINDOW
hInstApp As LongPtr
lpIDList As LongPtr
lpClass As LongPtr
hkeyClass As LongPtr
dwHotKey As Long
hIcon_hMonitor As LongPtr
hProcess As LongPtr
End Type
Private Enum SHELLEXECUTEMASK
SEE_MASK_CLASSNAME = &H00000001
SEE_MASK_CLASSKEY = &H00000003
SEE_MASK_IDLIST = &H00000004
SEE_MASK_INVOKEIDLIST = &H0000000c
SEE_MASK_ICON = &H00000010
SEE_MASK_HOTKEY = &H00000020
SEE_MASK_NOCLOSEPROCESS = &H00000040
SEE_MASK_CONNECTNETDRV = &H00000080
SEE_MASK_FLAG_DDEWAIT = &H00000100
SEE_MASK_DOENVSUBST = &H00000200
SEE_MASK_FLAG_NO_UI = &H00000400
SEE_MASK_FLAG_SHELLEXEC = &H00000800 'internal_win40
SEE_MASK_FORCENOIDLIST = &H00001000 'internal_win40
SEE_MASK_NO_HOOKS = &H00002000 'internal_win40
SEE_MASK_UNICODE = &H00004000
SEE_MASK_NO_CONSOLE = &H00008000&
SEE_MASK_HASLINKNAME = &H00010000 ';internal_win40
SEE_MASK_FLAG_SEPVDM = &H00020000 'internal_win40
SEE_MASK_RESERVED = &H00040000 'internal_win40
SEE_MASK_HASTITLE = &H00080000 'internal_win40
SEE_MASK_ASYNCOK = &H00100000
SEE_MASK_HMONITOR = &H00200000
SEE_MASK_FILEANDURL = &H00400000 'internal
SEE_MASK_NOZONECHECKS = &H00800000
SEE_MASK_NOQUERYCLASSSTORE = &H01000000
SEE_MASK_WAITFORINPUTIDLE = &H02000000
SEE_MASK_FLAG_LOG_USAGE = &H04000000
SEE_MASK_FLAG_HINST_IS_SITE = &H08000000
End Enum
Private Enum SE_ERR
SE_ERR_FNF = 2 ' file not found
SE_ERR_PNF = 3 ' path not found
SE_ERR_ACCESSDENIED = 5 ' access denied
SE_ERR_OOM = 8 ' out of memory
SE_ERR_DLLNOTFOUND = 32
SE_ERR_SHARE = 26
SE_ERR_ASSOCINCOMPLETE = 27
SE_ERR_DDETIMEOUT = 28
SE_ERR_DDEFAIL = 29
SE_ERR_DDEBUSY = 30
SE_ERR_NOASSOC = 31
End Enum
Private Enum SHOWWINDOW
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private DeclareWide PtrSafe Function PathFileExists Lib "shlwapi" Alias "PathFileExistsW" (ByVal pszPath As String) As BOOL
Private Declare PtrSafe Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Private DeclareWide PtrSafe Function CopyFile Lib "kernel32" Alias "CopyFileW" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As BOOL) As BOOL
Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As LongPtr) As BOOL
Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As LongPtr, ByVal dwMilliseconds As Long) As WaitForObjOutcomes
Private Enum WaitForObjOutcomes
WAIT_FAILED = (&HFFFFFFFF)
WAIT_OBJECT_0 = ((STATUS_WAIT_0) + 0)
WAIT_ABANDONED = ((STATUS_ABANDONED_WAIT_0) + 0)
WAIT_ABANDONED_0 = ((STATUS_ABANDONED_WAIT_0) + 0)
WAIT_IO_COMPLETION = STATUS_USER_APC
End Enum
#If ENABLE_AUTO_POST_BUILD Then
[RunAfterBuild]
#End If
Private Sub FinishDriver()
'This copies a fresh inf template from the project folder to the build folder,
'stamps the inf with date/time and architecture info,
'creates a catalog file from the info, and finally,
'signs the driver using signtool. With a system test certificate by default, but
'you can modify this to use your own certificates.
If PathFileExists(App.LastBuildPath) = 0 Then
Debug.Print "FinishDriver::Couldn't get last build path."
Exit Sub
End If
'stampinf
If ENABLE_STAMPINF Then
If PathFileExists(szStampInf) Then
Dim sArch As String
#If Win64 Then
sArch = "x64"
#Else
sArch = "x86"
#End If
Dim sDate As String = Format$(Now, "Mm/Dd/yyyy ")
Dim st As SYSTEMTIME
GetSystemTime st
Dim sTime As String = Right$("0" & st.wHour, 2) & "." & Right$("0" & st.wMinute, 2) & "." & Right$("0" & st.wSecond, 2) & "." & Right$("00" & st.wMilliseconds, 3)
Dim sBuildPath As String
sBuildPath = Left$(App.LastBuildPath, CLng(InStrRev(App.LastBuildPath, "\")))
Dim szParam As String = "-f " & App.Title & ".inf" & " -a " & sArch & " -d " & sDate & " -v " & sTime
Debug.Print "FinishDriver::Stampinf args=" & szParam
CopyFile App.Path & "\" & App.Title & ".inf", sBuildPath & App.Title & ".inf", CFALSE
Dim see As SHELLEXECUTEINFO
see.cbSize = LenB(Of SHELLEXECUTEINFO)
see.fMask = SEE_MASK_NOCLOSEPROCESS
see.lpFile = StrPtr(szStampInf)
see.lpParameters = StrPtr(szParam)
see.lpDirectory = StrPtr(sBuildPath)
see.nShow = SW_SHOWNORMAL
ShellExecuteEx see
If see.hProcess Then
WaitForSingleObject see.hProcess, 10000
CloseHandle see.hProcess
see.hProcess = 0 'reset for next command
End If
Else
Debug.Print "FinishDriver::Error - stampinf.exe not found, check path in PostBuildTasks.twin"
End If
End If
'inf2cat
If ENABLE_INF2CAT Then
If PathFileExists(szInf2Cat) Then
Dim sOS As String
#If Win64 Then
sOS = "7_X64,8_X64,6_3_X64,10_X64" 'Sign for Windows 7, 8, 8.1, 10, 11 64bit
#Else
sOS = "7_X86,8_X86,6_3_X86,10_X86" '...32bit
#End If
Dim szCatParam As String = "/verbose /os:" & sOS
Debug.Print "FinishDriver::inf2cat args=" & szCatParam
see.cbSize = LenB(Of SHELLEXECUTEINFO)
see.fMask = SEE_MASK_NOCLOSEPROCESS
see.lpFile = StrPtr(szInf2Cat)
see.lpParameters = StrPtr(szCatParam)
see.lpDirectory = StrPtr(sBuildPath)
see.nShow = SW_SHOWNORMAL
ShellExecuteEx see
If see.hProcess Then
WaitForSingleObject see.hProcess, 10000
CloseHandle see.hProcess
see.hProcess = 0 'reset for next command
End If
Else
Debug.Print "FinishDriver::Error - inf2cat.exe not found, check path in PostBuildTasks.twin"
End If
End If
'signtool
If ENABLE_SIGNTOOL Then
If PathFileExists(szSignTool) Then
Dim szSignParam As String = "/fd sha256"
Debug.Print "FinishDriver::signtool args=" & szSignParam
see.cbSize = LenB(Of SHELLEXECUTEINFO)
see.fMask = SEE_MASK_NOCLOSEPROCESS
see.lpFile = StrPtr(szSignTool)
see.lpParameters = StrPtr(szSignParam)
see.lpDirectory = StrPtr(sBuildPath)
see.nShow = SW_SHOWNORMAL
ShellExecuteEx see
If see.hProcess Then
WaitForSingleObject see.hProcess, 10000
CloseHandle see.hProcess
see.hProcess = 0 'reset For next command
End If
Else
Debug.Print "FinishDriver::Error - signtool.exe not found, check path in PostBuildTasks.twin"
End If
End If
End Sub
End Module
[]