-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriver.sh
More file actions
executable file
·435 lines (373 loc) · 11.5 KB
/
webdriver.sh
File metadata and controls
executable file
·435 lines (373 loc) · 11.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/bin/bash
#
# webdriver.sh - bash script for managing Nvidia's web drivers
# Copyright © 2017 vulgo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
/usr/bin/sw_vers -productVersion | grep "10.13" > /dev/null 2>&1
if [ $? -ne 0 ]; then
printf "Unsupported macOS version"
exit 1
fi
MAC_OS_BUILD=$(/usr/bin/sw_vers -buildVersion)
DOWNLOADS_DIR=~/Downloads
REMOTE_UPDATE_PLIST="https://gfestage.nvidia.com/mac-update"
CHANGES_MADE=false
PROMPT_REBOOT=true
NO_CACHE_UPDATE=false
REINSTALL=false
DOWNLOADED_UPDATE_PLIST="$DOWNLOADS_DIR/.nvwebupdates.plist"
DOWNLOADED_PKG="$DOWNLOADS_DIR/.nvweb.pkg"
EXTRACTED_PKG_DIR="$DOWNLOADS_DIR/.nvwebinstall"
SQL_QUERY_FILE="$DOWNLOADS_DIR/.nvweb.sql"
SQL_DEVELOPER_NAME="NVIDIA Corporation"
SQL_TEAM_ID="6KR3T733EC"
INSTALLED_VERSION="/Library/Extensions/GeForceWeb.kext/Contents/Info.plist"
DRIVERS_DIR_HINT="NVWebDrivers.pkg"
BREW_PREFIX=$(brew --prefix 2> /dev/null)
function usage {
echo "Usage: "$(basename $0)" [-f] [-c] [-p|-r|-u url|-m [build]]"
echo " -f Re-install"
echo " -c Don't update caches"
echo " -p Download the updates property list and exit"
echo " -r Uninstall Nvidia web drivers"
echo " -u url Install driver package at url, no version checks"
echo " -m [build] Modify the current driver's NVDARequiredOS"
}
let COMMAND_COUNT=0; while getopts ":hpu:rm:cf" OPTION; do
if [ "$OPTION" = "h" ]; then
usage
exit 0
elif [ "$OPTION" = "p" ]; then
COMMAND="GET_PLIST_AND_EXIT"
let COMMAND_COUNT+=1
elif [ "$OPTION" = "u" ]; then
COMMAND="USER_PROVIDED_URL"
REMOTE_URL="$OPTARG"
let COMMAND_COUNT+=1
elif [ "$OPTION" = "r" ]; then
COMMAND="UNINSTALL_DRIVERS_AND_EXIT"
let COMMAND_COUNT+=1
elif [ "$OPTION" = "m" ]; then
MOD_REQUIRED_OS="$OPTARG"
COMMAND="SET_REQUIRED_OS_AND_EXIT"
let COMMAND_COUNT+=1
elif [ "$OPTION" = "c" ]; then
NO_CACHE_UPDATE=true
PROMPT_REBOOT=false
elif [ "$OPTION" = "f" ]; then
REINSTALL=true
elif [ "$OPTION" = "?" ]; then
printf "Invalid option: -$OPTARG\n"
usage
exit 1
elif [ "$OPTION" = ":" ]; then
if [ $OPTARG = "m" ]; then
MOD_REQUIRED_OS="$MAC_OS_BUILD"
COMMAND="SET_REQUIRED_OS_AND_EXIT"
let COMMAND_COUNT+=1
else
printf "Missing parameter\n"
usage
exit 1
fi
fi
if [ $COMMAND_COUNT -gt 1 ]; then
printf "Too many options\n"
usage
exit 1
fi
done
function silent {
"$@" > /dev/null 2>&1
}
function error {
# error message exit_code
delete_temporary_files
printf "Error: $1 ($2)\n"
if [ $CHANGES_MADE = false ]; then
printf "No changes were made\n"
else
unset_nvram
fi
exit 1
}
function on_error {
# on_error message exit_code
if [ $2 -ne 0 ]; then
error "$1" $2
fi
}
function delete_temporary_files {
silent rm -rf "$EXTRACTED_PKG_DIR"
silent rm -f "$DOWNLOADED_PKG"
silent rm -f "$SQL_QUERY_FILE"
silent rm -f "$DOWNLOADED_UPDATE_PLIST"
}
# COMMAND GET_PLIST_AND_EXIT
if [ "$COMMAND" = "GET_PLIST_AND_EXIT" ]; then
DESTINATION="$DOWNLOADS_DIR/NvidiaUpdates.plist"
printf "Downloading '$DESTINATION'\n"
curl --connect-timeout 15 -s -m 45 -o "$DESTINATION" "$REMOTE_UPDATE_PLIST"
on_error "Couldn't get updates data from Nvidia" $?
open -R "$DESTINATION"
exit 0
fi
# Check root
if [ "$(id -u)" != "0" ]; then
printf "Run it as root: sudo $(basename $0) $@"
exit 0
fi
# Check SIP/file system permissions
silent touch /System
on_error "Is SIP enabled?" $?
#
function bye {
printf "Complete."
if [ $PROMPT_REBOOT = true ]; then
printf " You should reboot now.\n"
else
printf "\n"
fi
exit 0
}
function warning {
# warning message
printf "Warning: $1\n"
}
function uninstall_drivers {
# Remove drivers
silent mv /Library/Extensions/NVDAEGPUSupport.kext /Library/Extensions/EGPUSupport.kext
silent rm -rf /Library/Extensions/GeForce*
silent rm -rf /Library/Extensions/NVDA*
silent rm -rf /Library/GPUBundles/GeForce*Web.bundle
silent rm -rf /System/Library/Extensions/GeForce*Web*
silent rm -rf /System/Library/Extensions/NVDA*Web*
silent mv /Library/Extensions/EGPUSupport.kext /Library/Extensions/NVDAEGPUSupport.kext
if [ -f $BREW_PREFIX/etc/webdriver.sh/uninstall.conf ]; then
$BREW_PREFIX/etc/webdriver.sh/uninstall.conf
elif [ -f /usr/local/etc/webdriver.sh/uninstall.conf ]; then
/usr/local/etc/webdriver.sh/uninstall.conf
fi
}
function update_caches {
if [ $NO_CACHE_UPDATE = true ]; then
warning "Caches are not being updated"
return 0
fi
printf "Updating caches...\n"
/usr/bin/touch /Library/Extensions /System/Library/Extensions
/usr/sbin/kextcache -u /
on_error "Couldn't update caches" $?
}
function ask {
printf "$1"
read -n 1 -s -r -p " [y/N]" INPUT
if [ "$INPUT" = "y" ] || [ "$INPUT" = "Y" ]; then
printf "\n"
return 1
else
delete_temporary_files
exit 0
fi
}
function plistb {
# plistb command file fatal
/usr/libexec/PlistBuddy -c "$1" "$2" 2> /dev/null
if [ $? -ne 0 ] && [ $3 = true ]; then
error "PlistBuddy error treated as fatal" $?
fi
}
function set_nvram {
/usr/sbin/nvram nvda_drv=1%00
}
function unset_nvram {
/usr/sbin/nvram -d nvda_drv
}
# COMMAND SET_REQUIRED_OS_AND_EXIT
if [ "$COMMAND" = "SET_REQUIRED_OS_AND_EXIT" ]; then
MOD_INFO_PLIST_PATH="/Library/Extensions/NVDAStartupWeb.kext/Contents/Info.plist"
EGPU_INFO_PLIST_PATH="/Library/Extensions/NVDAEGPUSupport.kext/Contents/Info.plist"
MOD_KEY=":IOKitPersonalities:NVDAStartup:NVDARequiredOS"
if [ -f "$MOD_INFO_PLIST_PATH" ]; then
CHANGES_MADE=true
printf "Setting NVDARequiredOS to $MOD_REQUIRED_OS...\n"
plistb "Set $MOD_KEY $MOD_REQUIRED_OS" "$MOD_INFO_PLIST_PATH" true
if [ -f "$EGPU_INFO_PLIST_PATH" ]; then
printf "Found NVDAEGPUSupport.kext, setting NVDARequiredOS to $MOD_REQUIRED_OS...\n"
plistb "Set $MOD_KEY $MOD_REQUIRED_OS" "$EGPU_INFO_PLIST_PATH" true
fi
update_caches
set_nvram
bye
else
error "$MOD_INFO_PLIST_PATH not found" 2
fi
fi
# COMMAND UNINSTALL_DRIVERS_AND_EXIT
if [ "$COMMAND" = "UNINSTALL_DRIVERS_AND_EXIT" ]; then
ask "Uninstall Nvidia web drivers?"
printf "Removing files...\n"
CHANGES_MADE=true
uninstall_drivers
update_caches
unset_nvram
bye
fi
function installed_version {
if [ -f $INSTALLED_VERSION ]; then
GET_INFO_STRING=$(plistb "Print :CFBundleGetInfoString" "$INSTALLED_VERSION" false)
GET_INFO_STRING="${GET_INFO_STRING##* }"
# check version string is the format we expect
TEST="${GET_INFO_STRING//[^.]}" # get . characters
TEST="${#TEST}" # how many?
if [ "$TEST" != "5" ]; then
# <>5 dots is unexpected
warning "Current driver has unexpected version string:"
printf "$GET_INFO_STRING\n"
warning "webdriver.sh might need updating"
fi
echo "$GET_INFO_STRING";
exit 0
fi
echo "none"
}
function sql_add_kext {
printf "insert or replace into kext_policy " >> "$SQL_QUERY_FILE"
printf "(team_id, bundle_id, allowed, developer_name, flags) " >> "$SQL_QUERY_FILE"
printf "values (\"$SQL_TEAM_ID\",\"$1\",1,\"$SQL_DEVELOPER_NAME\",1);\n" >> "$SQL_QUERY_FILE"
}
# UPDATER/INSTALLER
delete_temporary_files
if [ "$COMMAND" != "USER_PROVIDED_URL" ]; then
# No URL specified, get installed web driver verison
VERSION=$(installed_version)
# Get updates file
printf 'Checking for updates...\n'
curl --connect-timeout 15 -s -m 45 -o "$DOWNLOADED_UPDATE_PLIST" "$REMOTE_UPDATE_PLIST"
on_error "Couldn't get updates data from Nvidia" $?
# Check for an update
let i=0
while true; do
REMOTE_MAC_OS_BUILD=$(plistb "Print :updates:$i:OS" "$DOWNLOADED_UPDATE_PLIST" false)
if [ $? -ne 0 ]; then
REMOTE_MAC_OS_BUILD="none"
REMOTE_URL="none"
REMOTE_VERSION="none"
break
fi
if [ "$REMOTE_MAC_OS_BUILD" = "$MAC_OS_BUILD" ]; then
REMOTE_URL=$(plistb "Print :updates:$i:downloadURL" "$DOWNLOADED_UPDATE_PLIST" false)
if [ $? -ne 0 ]; then
REMOTE_URL="none"; fi
REMOTE_VERSION=$(plistb "Print :updates:$i:version" "$DOWNLOADED_UPDATE_PLIST" false)
if [ $? -ne 0 ]; then
REMOTE_VERSION="none"; fi
break
fi
if [ $i -gt 200 ]; then
REMOTE_MAC_OS_BUILD="none"
REMOTE_URL="none"
REMOTE_VERSION="none"
break;
fi
let i+=1
done;
# Determine next action
if [ "$REMOTE_URL" = "none" ] || [ "$REMOTE_VERSION" = "none" ]; then
# no driver available, or error during check, exit
printf "No driver available for $MAC_OS_BUILD\n"
delete_temporary_files
exit 0
elif [ "$REMOTE_VERSION" = "$VERSION" ]; then
# latest already installed, exit
printf "$REMOTE_VERSION for $MAC_OS_BUILD already installed\n"
if [ $REINSTALL = true ]; then
:
else
delete_temporary_files
exit 0
fi
else
# found an update, proceed to installation
printf "Web driver $REMOTE_VERSION available...\n"
fi
else
# invoked with -u option, proceed to installation
printf "User provided URL: $REMOTE_URL\n"
PROMPT_REBOOT=false
fi
# Start
if [ $REINSTALL = true ]; then
ask "Re-install?"
else
ask "Install?"
fi
# Check URL
REMOTE_HOST=$(printf $REMOTE_URL | awk -F/ '{print $3}')
silent /usr/bin/host $REMOTE_HOST
if [ $? -ne 0 ]; then
if [ "$COMMAND" = "USER_PROVIDED_URL" ]; then
error "Unable to resolve host, check your URL" 400; fi
REMOTE_URL="https://images.nvidia.com/mac/pkg/${REMOTE_VERSION%%.*}/WebDriver-$REMOTE_VERSION.pkg"
fi
silent /usr/bin/curl -I $REMOTE_URL
if [ $? -ne 0 ]; then
error "Error downloading package headers" $?; fi
if [ "$COMMAND" != "USER_PROVIDED_URL" ]; then
printf "Using URL: $REMOTE_URL\n"; fi
# Download
printf "Downloading package...\n"
/usr/bin/curl --connect-timeout 15 -# -o "$DOWNLOADED_PKG" "$REMOTE_URL"
on_error "Couldn't download package" $?
# Extract
printf "Extracting...\n"
/usr/sbin/pkgutil --expand "$DOWNLOADED_PKG" "$EXTRACTED_PKG_DIR"
on_error "Couldn't extract package" $?
if [ ! -d "$EXTRACTED_PKG_DIR"/*"$DRIVERS_DIR_HINT" ]; then
error "Couldn't find pkgutil output directory"; fi
cd "$EXTRACTED_PKG_DIR"/*"$DRIVERS_DIR_HINT"
/bin/cat ./Payload | /usr/bin/gunzip -dc > ./tmp.cpio
on_error "Couldn't extract package" $?
/usr/bin/cpio -i < ./tmp.cpio
on_error "Couldn't extract package" $?
if [ ! -d ./Library/Extensions ] || [ ! -d ./System/Library/Extensions ]; then
error "Unexpected directory structure after extraction" 1; fi
# Make SQL
printf "Approving kexts...\n"
cd "$EXTRACTED_PKG_DIR"/*"$DRIVERS_DIR_HINT"
KEXT_INFO_PLISTS=(./Library/Extensions/*.kext/Contents/Info.plist)
for PLIST in "${KEXT_INFO_PLISTS[@]}"; do
BUNDLE_ID=$(plistb "Print :CFBundleIdentifier" "$PLIST" true)
sql_add_kext "$BUNDLE_ID"
done
sql_add_kext "com.nvidia.CUDA"
CHANGES_MADE=true
# Allow kexts
/usr/bin/sqlite3 /var/db/SystemPolicyConfiguration/KextPolicy < "$SQL_QUERY_FILE"
if [ $? -ne 0 ]; then
warning "sqlite3 exit code $?, extensions may not be loadable"; fi
# Install
printf "Installing...\n"
uninstall_drivers
cd "$EXTRACTED_PKG_DIR"/*"$DRIVERS_DIR_HINT"
cp -r ./Library/Extensions/* /Library/Extensions
cp -r ./System/Library/Extensions/GeForce*Web.bundle /Library/GPUBundles
cp -r ./System/Library/Extensions/* /System/Library/Extensions
# Update caches and exit
update_caches
set_nvram
delete_temporary_files
bye