2016-07-15 09:31:30 +00:00
#!/bin/bash
2019-03-09 21:06:11 +00:00
# halt on any error for safety and proper pipe handling
set -euo pipefail ; # <- this semicolon and comment make options apply
# even when script is corrupt by CRLF line terminators (issue #75)
# empty line must follow this comment for immediate fail with CRLF newlines
2019-01-04 22:36:48 +00:00
2018-06-20 10:29:59 +00:00
backup_path = "/opt/nvidia/libnvidia-encode-backup"
silent_flag = ''
rollback_flag = ''
print_usage( ) { printf '
SYNOPSIS
patch.sh [ OPTION] ...
DESCRIPTION
2019-01-05 12:49:13 +00:00
The patch for Nvidia drivers to increase encoder sessions
2018-06-20 10:29:59 +00:00
-s Silent mode ( No output)
-r Rollback to original ( Restore lib from backup)
2019-01-05 12:49:13 +00:00
-h Print this help message
2018-06-20 10:29:59 +00:00
'
}
2019-01-05 12:49:13 +00:00
while getopts 'rsh' flag; do
2018-06-20 10:29:59 +00:00
case " ${ flag } " in
r) rollback_flag = 'true' ; ;
s) silent_flag = 'true' ; ;
*) print_usage
exit 1 ; ;
esac
done
if [ [ $silent_flag ] ] ; then
exec 1> /dev/null
2016-07-15 09:31:30 +00:00
fi
2018-06-20 10:29:59 +00:00
declare -A patch_list = (
[ "375.39" ] = 's/\x85\xC0\x89\xC5\x75\x18/\x29\xC0\x89\xC5\x90\x90/g'
2019-01-19 17:35:33 +00:00
[ "390.77" ] = 's/\x85\xC0\x89\xC5\x75\x18/\x29\xC0\x89\xC5\x90\x90/g'
2018-12-25 21:27:27 +00:00
[ "390.87" ] = 's/\x85\xC0\x89\xC5\x75\x18/\x29\xC0\x89\xC5\x90\x90/g'
2018-06-20 10:29:59 +00:00
[ "396.24" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2018-07-04 22:26:38 +00:00
[ "396.26" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2018-09-06 22:38:55 +00:00
[ "396.37" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g' #added info from https://github.com/keylase/nvidia-patch/issues/6#issuecomment-406895356
2018-06-20 10:29:59 +00:00
# break nvenc.c:236,layout asm,step-mode,step,break *0x00007fff89f9ba45
# libnvidia-encode.so @ 0x15a45; test->sub, jne->nop-nop-nop-nop-nop-nop
2018-09-14 22:01:44 +00:00
[ "396.54" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2018-10-18 11:01:20 +00:00
[ "410.48" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2018-11-24 17:55:03 +00:00
[ "410.57" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
[ "410.73" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
[ "410.78" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2018-12-30 19:03:14 +00:00
[ "410.79" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2019-02-04 09:35:54 +00:00
[ "410.93" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2019-02-22 16:48:58 +00:00
[ "410.104" ] = 's/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
2019-01-04 19:59:11 +00:00
[ "415.18" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
[ "415.25" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-01-19 14:07:11 +00:00
[ "415.27" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-02-05 16:22:48 +00:00
[ "418.30" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-02-22 15:32:43 +00:00
[ "418.43" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-03-20 19:37:42 +00:00
[ "418.56" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-05-15 21:58:40 +00:00
[ "418.67" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-05-07 13:31:11 +00:00
[ "418.74" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x0f\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-05-07 12:56:50 +00:00
[ "430.09" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x0f\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-05-14 13:37:56 +00:00
[ "430.14" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x0f\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-06-10 15:48:45 +00:00
[ "430.26" ] = 's/\x00\x00\x00\x84\xc0\x0f\x84\x0f\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
2019-01-04 19:59:11 +00:00
)
declare -A object_list = (
[ "375.39" ] = 'libnvidia-encode.so'
2019-01-19 17:35:33 +00:00
[ "390.77" ] = 'libnvidia-encode.so'
2019-01-04 19:59:11 +00:00
[ "390.87" ] = 'libnvidia-encode.so'
[ "396.24" ] = 'libnvidia-encode.so'
[ "396.26" ] = 'libnvidia-encode.so'
[ "396.37" ] = 'libnvidia-encode.so'
[ "396.54" ] = 'libnvidia-encode.so'
[ "410.48" ] = 'libnvidia-encode.so'
[ "410.57" ] = 'libnvidia-encode.so'
[ "410.73" ] = 'libnvidia-encode.so'
[ "410.78" ] = 'libnvidia-encode.so'
[ "410.79" ] = 'libnvidia-encode.so'
2019-02-04 09:35:54 +00:00
[ "410.93" ] = 'libnvidia-encode.so'
2019-02-22 16:48:58 +00:00
[ "410.104" ] = 'libnvidia-encode.so'
2019-01-04 19:59:11 +00:00
[ "415.18" ] = 'libnvcuvid.so'
[ "415.25" ] = 'libnvcuvid.so'
2019-01-19 14:07:11 +00:00
[ "415.27" ] = 'libnvcuvid.so'
2019-02-05 16:22:48 +00:00
[ "418.30" ] = 'libnvcuvid.so'
2019-02-22 15:32:43 +00:00
[ "418.43" ] = 'libnvcuvid.so'
2019-03-20 19:37:42 +00:00
[ "418.56" ] = 'libnvcuvid.so'
2019-05-15 21:58:40 +00:00
[ "418.67" ] = 'libnvcuvid.so'
2019-05-07 13:31:11 +00:00
[ "418.74" ] = 'libnvcuvid.so'
2019-04-23 15:15:33 +00:00
[ "430.09" ] = 'libnvcuvid.so'
2019-05-14 13:37:56 +00:00
[ "430.14" ] = 'libnvcuvid.so'
2019-06-10 15:48:45 +00:00
[ "430.26" ] = 'libnvcuvid.so'
2018-06-20 10:29:59 +00:00
)
2019-01-19 13:18:54 +00:00
NVIDIA_SMI = " $( which nvidia-smi) "
2019-01-04 22:36:48 +00:00
2019-01-19 14:30:06 +00:00
if ! driver_version = $( " $NVIDIA_SMI " --query-gpu= driver_version --format= csv,noheader,nounits | head -n 1) ; then
2018-06-20 10:29:59 +00:00
echo 'Something went wrong. Check nvidia driver'
exit 1;
fi
echo " Detected nvidia driver version: $driver_version "
2019-01-12 14:48:54 +00:00
if [ [ ! " ${ patch_list [ $driver_version ]+isset } " || ! " ${ object_list [ $driver_version ]+isset } " ] ] ; then
2018-06-20 10:29:59 +00:00
echo " Patch for this ( $driver_version ) nvidia driver not found. " 1>& 2
echo "Available patches for: " 1>& 2
for drv in " ${ !patch_list[@] } " ; do
echo " $drv " 1>& 2
done
exit 1;
fi
2019-01-04 22:36:48 +00:00
patch = " ${ patch_list [ $driver_version ] } "
object = " ${ object_list [ $driver_version ] } "
2019-03-12 20:43:46 +00:00
declare -a driver_locations = (
2019-04-12 08:29:19 +00:00
'/usr/lib/x86_64-linux-gnu'
'/usr/lib/x86_64-linux-gnu/nvidia/current/'
'/usr/lib64'
" /usr/lib/nvidia- ${ driver_version %%.* } "
2019-03-12 20:43:46 +00:00
)
dir_found = ''
for driver_dir in " ${ driver_locations [@] } " ; do
if [ [ -e " $driver_dir / $object . $driver_version " ] ] ; then
dir_found = 'true'
break
fi
done
[ [ " $dir_found " ] ] || { echo "ERROR: cannot detect driver directory" ; exit 1; }
2018-06-20 10:29:59 +00:00
if [ [ $rollback_flag ] ] ; then
2019-01-04 21:59:29 +00:00
if [ [ -f " $backup_path / $object . $driver_version " ] ] ; then
cp -p " $backup_path / $object . $driver_version " \
" $driver_dir / $object . $driver_version "
2019-01-04 19:59:11 +00:00
echo " Restore from backup $object . $driver_version "
2018-06-20 10:29:59 +00:00
else
echo "Backup not found. Try to patch first."
exit 1;
fi
else
2019-01-04 21:59:29 +00:00
if [ [ ! -f " $backup_path / $object . $driver_version " ] ] ; then
echo " Attention! Backup not found. Copy current $object to backup. "
mkdir -p " $backup_path "
cp -p " $driver_dir / $object . $driver_version " \
" $backup_path / $object . $driver_version "
2018-06-20 10:29:59 +00:00
fi
2019-01-04 21:59:29 +00:00
sha1sum " $backup_path / $object . $driver_version "
sed " $patch " " $backup_path / $object . $driver_version " > \
" $driver_dir / $object . $driver_version "
sha1sum " $driver_dir / $object . $driver_version "
2018-06-20 10:29:59 +00:00
ldconfig
echo "Patched!"
fi