summaryrefslogtreecommitdiff
path: root/system/nvidia-driver/nvidia-switch
blob: c12f28e7e7d6452c2a154b987e5a5cdaee414b04 (plain)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/bin/sh

# ***************************************************************************
# *   Copyright (C) 2007-2008 by Heinz Wiesinger                            *
# *   pprkut@liwjatan.at                                                    *
# *   http://www.liwjatan.at                                                *
# *                                                                         *
# *   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, write to the                         *
# *   Free Software Foundation, Inc.,                                       *
# *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
# ***************************************************************************/
# nvidia-switch utility 0.7.5
#
# A tool to switch between nvidia-binary-driver and stock xorg-driver
# if both are installed in parallel.

set -e

ROOT="/"
CWD=$(pwd)
INC="${ROOT}usr/include/GL"
LIB="${ROOT}usr/lib"
XMOD="${ROOT}usr/lib/xorg/modules"
XLIB="$XMOD/extensions"
NV_VERSION='PKGVERSION' # This will be replaced in the build script
GL_VERSION="1.2"

remove_link(){
if [ -L "$1" ]; then
  rm -f "$1"
fi
}

remove_existing(){
if [ -e "$1" ]; then
  rm -f $1
fi
}

move_existing(){
if [ -e "$1" ]; then
  mv $1 $2
fi
}

setup_link(){
  if [ "$1" = "mv" ]; then
    mv "$2" "$3"
  else
    rm -f $2
  fi
  ln -s "$3" "$2"
}

# Move and rename files in /usr/include
# $1 = from
# $2 = to
incs(){
cd "$INC"

if [ "$2" = "cleanup" ]; then
  CMD="mv"
  THREE="$1"
else
  CMD="ln -s"
  THREE="$2"
fi

for i in glxext.h glext.h glx.h gl.h; do
  if [ -L "$i" ]; then
    rm -f "$i"
    $CMD "$i-$THREE" "$i"
  elif [ -e "$i" ]; then
    if [ "$2" = "cleanup" ]; then
      rm -f "$i-$1"
    else
      mv "$i" "$i-$1"
      ln -s "$i-$THREE" "$i"
    fi
  else
    $CMD "$i-$THREE" "$i"
  fi
done

cd "$CWD"
}

libs(){
for i in libGLcore.so libGLcore.la libglx.la; do
  if [ "$1" = "nvidia" ]; then
    #If this library exists, move it to *-xorg
    move_existing "$XLIB/$i" "$XLIB/$i-xorg"
  else
    #If .so does not exist, and *-xorg does, then remove the -xorg
    if [ -e "$XLIB/$i" ]; then
      remove_existing "$XLIB/$i-xorg"
    else
      move_existing "$XLIB/$i-xorg" "$XLIB/$i"
    fi
  fi
done
}

libs_basic(){
for i in libGL.so libGLcore.so; do
  #if this link exists, remove it, if it's a file, move it to *.nvidia
  if [ -L "$LIB/$i.$NV_VERSION" ]; then
    rm -f "$LIB/$i.$NV_VERSION"
  elif [ -e "$LIB/$i.$NV_VERSION" ]; then
    mv "$LIB/$i.$NV_VERSION" "$LIB/$i.$NV_VERSION-nvidia"
  fi
  remove_link "$LIB/$i.1"
done
}

libgl_nvidia(){
#if libGL.so.$GL_VERSION does exist, move it to libGL.so.$GL_VERSION-xorg, as it conflicts if nvidia's libGL.so
# then remove the libGL.so.1 symlink and create a new one pointing to nvidia's libGL.so
if [ -e "$LIB/libGL.so.$GL_VERSION" ]; then
  cd "$LIB"
  mv libGL.so.$GL_VERSION libGL.so.$GL_VERSION-xorg
  setup_link "" "libGL.so.1" "libGL.so.$NV_VERSION"
  cd "$CWD"
fi
}

libgl_xorg(){
#if libGL.so.$GL_VERSION does not exist and libGL.so.$GL_VERSION-xorg does, move it to libGL.so.$GL_VERSION
# then remove the libGL.so.1 symlink and create a new one pointing to nvidia's libGL.so
if [ -e "$LIB/libGL.so.$GL_VERSION" ]; then
  remove_existing "$LIB/libGL.so.$GL_VERSION-xorg"
else
  if [ -e "$LIB/libGL.so.$GL_VERSION-xorg" ]; then
    cd "$LIB"
    mv libGL.so.$GL_VERSION-xorg libGL.so.$GL_VERSION
    ln -s libGL.so.$GL_VERSION libGL.so.1
    cd "$CWD"
  fi
fi
}


libglcore_nvidia(){
#If libGLcore.so.$NV_VERSION-nvidia does exists, then remove the -nvidia and make it usable that way
if [ -e "$LIB/libGLcore.so.$NV_VERSION-nvidia" ]; then
  cd "$LIB"
  rm -f libGLcore.so.$NV_VERSION libGLcore.so.1
  ln -s libGLcore.so.$NV_VERSION-nvidia libGLcore.so.$NV_VERSION
  ln -s libGLcore.so.$NV_VERSION libGLcore.so.1
  cd "$CWD"
fi
}

lib_nvidia(){
for i in libGL.so libglx.so; do
  if [ "$i" = "libGL.so" ]; then
    cd "$LIB"
  elif [ "$i" = "libglx.so" ]; then
    cd "$XLIB"
  fi
  #If libGL.so.$NV_VERSION-nvidia does exists, then remove the -nvidia and make it usable that way
  if [ -e "$i.$NV_VERSION-nvidia" ]; then
    setup_link "" "$i.$NV_VERSION" "$i.$NV_VERSION-nvidia"
  fi
  cd "$CWD"
done
}

libglx_base(){
for i in $(ls libglx.so* | grep -v -); do
  if ! [ "$i" = "libglx.so.$NV_VERSION" ]; then
    rm -f $i
  fi
done
ln -s libglx.so.$NV_VERSION libglx.so
}

libglx_nvidia(){
# if libglx.so is not a link, back it up to *-xorg, and create a symlink to nvidia's one
# if it's a link, then remove it and create a new one.
if ! [ -L "$XLIB/libglx.so" ]; then
  cd $XLIB
  mv libglx.so libglx.so-xorg
  libglx_base
else
  cd $XLIB
  libglx_base
fi
cd $CWD
}

libglx_xorg(){
if [ "$1" = "xorg" ]; then
  CMD="ln -s"
else
  CMD="mv"
fi

#if this library exists, move it to *-nvidia
if [ -L "$XLIB/libglx.so.$NV_VERSION" ]; then
  rm $XLIB/libglx.so.$NV_VERSION
elif [ -e "$XLIB/libglx.so.$NV_VERSION" ]; then
  mv $XLIB/libglx.so.$NV_VERSION $XLIB/libglx.so.$NV_VERSION-nvidia
fi

# if libglx.so exists and is a link, remove it and create a new one
# If it does exists and is not a link, remove the old one, and create the new one.
# if it does not exist, create a symlink
if [ -L "$XLIB/libglx.so" ]; then
  cd $XLIB
  rm libglx.so
  $CMD libglx.so-xorg libglx.so
elif [ -e "$XLIB/libglx.so" ]; then
  if [ "$1" = "xorg" ]; then
    cd $XLIB
    setup_link "mv" "libglx.so" "libglx.so-xorg"
  else
    remove_existing "$XLIB/libglx.so-xorg"
  fi
else
  cd $XLIB
  $CMD libglx.so-xorg libglx.so
fi
cd $CWD
}

libwfb_nvidia(){
#Recreate the link removed by --xorg
if ! [ -e "$XMOD/libwfb.so" ]; then
  cd $XMOD
  ln -s libnvidia-wfb.so.1 libwfb.so
  cd $CWD
fi
}

nvidia_ldconfig(){
/sbin/ldconfig
#Generate correct symink for that lib
/sbin/ldconfig -l $1

if [ "$2" = "xorg" ]; then
  #Remove so-link, recreated by ldconfig
  cd $LIB
  remove_link "libGLcore.so.1"
  cd $CWD
fi
}

check(){
  echo -n "checking $2...."
  if [ -e "$1/$2" ]; then
    if [ "$3" = "exist" ]; then
      echo "ERROR: $1/$2 does exist!!!!!"
    else
      echo -n "exists"
      if [ -h "$1/$2" ]; then
        echo "(link)"
        echo -n " points to:"
        ls -o "$1/$2" | cut -d ">" -f 2
      else
        if [ "$3" = "link" ]; then
          echo " (!)"
        else
          echo ""
        fi
      fi
    fi
  else
    if [ "$3" = "exist" ]; then
      echo "does not exist"
    else
      echo "ERROR: $1/$2 does not exist!!!!!"
    fi
  fi
}

check_includes(){
  if [ "$1" = "cleanup" ]; then
    cleanup=""
    cleanup2="exist"
  else
    cleanup="link"
    cleanup2=""
  fi

  for i in glext.h glxext.h gl.h glx.h; do
    check $INC $i $cleanup
    check $INC $i-nvidia
    check $INC $i-xorg $cleanup2
    echo ""
  done
}

check_glcore(){
  if [ "$1" = "nvidia" ]; then
    CHECK="link"
    EXT=""
  else
    CHECK="exist"
    EXT="-xorg"
  fi

  for i in libGLcore.so.1 libGLcore.so.$NV_VERSION libGLcore.so.$NV_VERSION-nvidia; do
    if [ "$i" = "libGLcore.so.1" ]; then
      check $LIB $i $CHECK
    elif [ "$i" = "libGLcore.so.$NV_VERSION" ]; then
      check $LIB $i $CHECK
    else
      check $LIB $i
    fi
  done

  echo ""

  for i in libGLcore.so libGLcore.la libGLcore.so-xorg libGLcore.la-xorg; do
    if [ "$i" = "libGLcore.so$EXT" ]; then
      check $XLIB $i "exist"
    elif [ "$i" = "libGLcore.la$EXT" ]; then
      check $XLIB $i "exist"
    else
      check $XLIB $i
    fi
  done

  echo ""
}

check_glx(){
  if [ "$1" = "xorg" ]; then
    xorg="exist"
    cleanup="link"
    cleanup2=""
    EXT="-xorg"
  elif [ "$1" = "cleanup" ]; then
    xorg="exist"
    cleanup=""
    cleanup2="exist"
    EXT="-xorg"
  else
    xorg="link"
    cleanup="link"
    cleanup2=""
    EXT=""
  fi

  for i in libglx.la libglx.so libglx.so.$NV_VERSION libglx.so.$NV_VERSION-nvidia \
    libglx.so-xorg libglx.la-xorg; do
    if [ "$i" = "libglx.so" ]; then
      check $XLIB $i $cleanup
    elif [ "$i" = "libglx.so-xorg" ]; then
      check $XLIB $i $cleanup2
    elif [ "$i" = "libglx.so.$NV_VERSION" ]; then
      check $XLIB $i $xorg
    elif [ "$i" = "libglx.la$EXT" ]; then
      check $XLIB $i "exist"
    else
      check $XLIB $i
    fi
  done

  echo ""
}

check_wfb(){
  for i in libwfb.so libwfb.la libnvidia-wfb.so.1 libnvidia-wfb.so.$NV_VERSION; do
    if [ "$i" = "libnvidia-wfb.so.1" ]; then
      check $XMOD $i "link"
    else
      check $XMOD $i
    fi
  done
}

check_gl(){
  if [ "$1" = "nvidia" ]; then
    nvidia="link"
    EXT=""
  else
    nvidia="exist"
    EXT="-xorg"
  fi

  for i in libGL.la libGL.so libGL.so.1 libGL.so.$NV_VERSION libGL.so.$NV_VERSION-nvidia \
    libGL.so.$GL_VERSION libGL.so.$GL_VERSION-xorg; do
    if [ "$i" = "libGL.so" ]; then
      check $LIB $i "link"
    elif [ "$i" = "libGL.so.1" ]; then
      check $LIB $i "link"
    elif [ "$i" = "libGL.so.$NV_VERSION" ]; then
      check $LIB $i $nvidia
    elif [ "$i" = "libGL.so.${GL_VERSION}${EXT}" ]; then
      check $LIB $i "exist"
    else
      check $LIB $i
    fi
  done

  echo ""
}

nvidia_check(){
  check_includes

  check_gl "nvidia"

  check_glcore "nvidia"

  check_glx

  check_wfb
}

xorg_check(){
  check_includes

  check_gl

  check_glcore

  check_glx "xorg"

  check_wfb
}

cleanup_check(){
  check_includes "cleanup"

  check_gl

  check_glcore

  check_glx "cleanup"

  check_wfb
}

nvidia(){
  echo $'Switching to nvidia-driver files!\n'
  echo "You have to ENABLE the nvidia driver in /etc/X11/xorg.conf."
  echo "Otherwise, this may lead to improperly working drivers."

  incs "xorg" "nvidia"
  lib_nvidia
  libgl_nvidia
  libglcore_nvidia
  libs "nvidia"
  libglx_nvidia
  libwfb_nvidia

  LD_NVIDIA="/usr/lib/libGL.so.$NV_VERSION-nvidia"
  nvidia_ldconfig $LD_NVIDIA
}

xorg(){
  echo $'Switching to stock xorg files.\n'
  if [ "$1" = "cleanup" ]; then
    echo $'Cleaning up symlinks.\n'
  fi
  echo "You have to DISABLE the nvidia driver in /etc/X11/xorg.conf."
  echo "Otherwise, this may lead to improperly working drivers."

  if [ "$1" = "cleanup" ]; then
    incs "xorg" "cleanup"
  else
    incs "xorg" "xorg"
  fi

   libs_basic
   libgl_xorg
   libs ""
   if [ "$1" = "cleanup" ]; then
     libglx_xorg ""
   else
     libglx_xorg "xorg"
   fi
   remove_link "$XMOD/libwfb.so"

   LD_NVIDIA="/usr/lib/libGL.so.1.2"
   nvidia_ldconfig $LD_NVIDIA "xorg"
}

usage(){
  echo "Usage:"
  echo " --nvidia		Switch to nvidia driver files"
  echo " --xorg			Switch to stock xorg files"
  echo " --cleanup		Switch to stock xorg files and remove all created symlinks"
  echo " --install		Switch to nvidia driver files"
  echo "			  This is used on installation to handle installroot correctly"
  echo "			  Please use --nvidia for after-install switches instead"
  echo " --check-nvidia		Check if everything is setup correctly for nvidia's driver"
  echo " --check-xorg		Check if everything is setup correctly for xorg's driver"
  echo " --check-cleanup	Check if everything has been cleaned up correctly"
  echo " --help			Show this help message"
}

if [ "$1" = '--nvidia' ]; then
  nvidia
elif [ "$1" = '--install' ]; then
  ROOT=""
  CWD=$(pwd)
  INC="${ROOT}usr/include/GL"
  LIB="${ROOT}usr/lib"
  XMOD="${ROOT}usr/lib/xorg/modules"
  XLIB="$XMOD/extensions"
  nvidia
elif [ "$1" = '--xorg' ]; then
  xorg ""
elif [ "$1" = '--check-nvidia' ]; then
  nvidia_check
elif [ "$1" = '--check-xorg' ]; then
  xorg_check
elif [ "$1" = '--check-cleanup' ]; then
  cleanup_check
elif [ "$1" = '--cleanup' ]; then
  xorg "cleanup"
elif [ "$1" = '--help' ]; then
  usage
else
  usage
fi