summaryrefslogtreecommitdiffstats
path: root/tools/tools/kdrv/KernelDriver
blob: b840ff48150a8e1cf874a9e64fb250e61835115c (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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
#!/bin/sh
# Tcl magic -*- tcl -*- \
exec tclsh $0 $*
################################################################################
#
# KernelDriver - FreeBSD driver source installer
#
################################################################################
#
# Copyright (C) 1997
#      Michael Smith.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the author nor the names of any co-contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Michael Smith AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL Michael Smith OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
################################################################################
#
# KernelDriver provides a means for installing source-form drivers into FreeBSD 
# kernel source trees in an automated fashion.  It can also remove drivers it 
# has installed.
#
# Driver information is read from a control file, with the following syntax :
#
# description {<text>}		Driver description; used in comments inserted into
#				files.
# driver <name>			The name of the driver. (Note that this can't end in .drvinfo :)
# filei386 <path> <name>	The file <name> in the driver package is installed into
#				<path> in the kernel source tree.  Files whose names
#				end in '.c' have an entry added to i386/conf/files.i386.
# fileconf <path> <name>	The file <name> in the driver package is installed into
#				<path> in the kernel source tree.  Files whose names
#				end in '.c' have an entry added to conf/files.
# optioni386 <name> <hdr>	Adds an entry to i386/conf/options.i386, such that
#				the option <name> will be placed in the header <hdr>.
# optionconf <name> <hdr>	Adds an entry to conf/options, such that
#				the option <name> will be placed in the header <hdr>.
# linttext			Lines between this and a subsequent 'end' line are added
#				to the LINT file to provide configuration examples,
#				comments, etc.
# end				Ends a text region.
# 
# Possible additions :
#
# patch <name>		Applies the patch contained in <name>; patch is invoked
#			at the top level of the kernel source tree, and the 
#			patch must apply cleanly (this is checked).
#
# option <name> <file>	Adds an entry to i386/conf/options.i386
#
# Lines beginning with '#' or blanks are considered comments, except in
# 'linttext' regions.
#
################################################################################
#
# $FreeBSD$
#
################################################################################

################################################################################
# findDrvFile
#
# Given (hint), use it to locate a driver information file.
# (Possible extension; support drivers in gzipped tarballs...)
#
proc findDrvFile_try {hint} {

    # points to something already
    if {[file exists $hint]} {
	# unwind symbolic links
	while {[file type $hint] == "link"} {
	    set hint [file readlink $hint];
	}
	switch [file type $hint] {
	    file {
		# run with it as it is
		return $hint;
	    }
	    directory {
		# look for a drvinfo file in the directory
		set candidate [glob -nocomplain "$hint/*.drvinfo"];
		switch [llength $candidate] {
		    0 {
			# nothing there
		    }
		    1 {
			return $candidate;
		    }
		    default {
			error "multiple driver info files in directory : $hint";
		    }
		}
	    }
	    default {
		error "driver info file may be a typewriter : $hint";
	    }
	}
    }
    # maybe we need an extension
    if {[file exists $hint.drvinfo]} {
	return $hint.drvinfo;
    }
    error "can't find a driver info file using '$hint'";
}

proc findDrvFile {hint} {

    set result [findDrvFile_try $hint];
    if {$result != ""} {
	return $result;
    }
    set result [findDrvFile_try ${hint}.drvinfo];
    if {$result != ""} {
	return $result;
    }
    error "can't find driver information file using : $hint";
}

################################################################################
# readDrvFile
#
# Reads the contents of (fname), which are expected to be in the format 
# described above, and fill in the global Drv array.
#
proc readDrvFile {fname} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ read options from '$fname'";}
    set fh [open $fname r];
    
    # set defaults
    set Drv(description) "";
    set Drv(driver) "";
    set Drv(filesi386) "";
    set Drv(filesconf) "";
    set Drv(optionsi386) "";
    set Drv(optionsconf) "";
    set Drv(patches) "";
    set Drv(linttext) "";

    while {[gets $fh line] >= 0} {
	
	# blank lines/comments
	if {([llength $line] == 0) ||
	    ([string index $line 0] == "\#")} {
	    continue ;
	}

	# get keyword, process
	switch -- [lindex $line 0] {
	    description {
		set Drv(description) [lindex $line 1];
	    }
	    driver {
		set Drv(driver) [lindex $line 1];
	    }
	    filei386 {
		set path [lindex $line 1];
		set plast [expr [string length $path] -1];
		if {[string index $path $plast] != "/"} {
		    append path "/";
		}
		set name [lindex $line 2];
		set Drv(filei386:$name) $path;
		lappend Drv(filesi386) $name;
	    }
	    fileconf {
		set path [lindex $line 1];
		set plast [expr [string length $path] -1];
		if {[string index $path $plast] != "/"} {
		    append path "/";
		}
		set name [lindex $line 2];
		set Drv(fileconf:$name) $path;
		lappend Drv(filesconf) $name;
	    }
	    optioni386 {
		set opt [lindex $line 1];
		set hdr [lindex $line 2];
		lappend Drv(optionsi386) $opt;
		set Drv(optioni386:$opt) $hdr;
	    }
	    optionconf {
		set opt [lindex $line 1];
		set hdr [lindex $line 2];
		lappend Drv(optionsconf) $opt;
		set Drv(optionconf:$opt) $hdr;
	    }
	    patch {
		lappend Drv(patches) [lindex $line 1];
	    }
	    linttext {
		while {[gets $fh line] >= 0} {
		    if {$line == "end"} {
			break ;
		    }
		    lappend Drv(linttext) $line;
		}
	    }
	}
    }
    close $fh;
    if {$Options(verbose)} {
	printDrv;
    }
}
		
################################################################################
# validateDrvPackage
#
# With the global Drv filled in, check that the files required are all in
# (dir), and that the kernel config at (kpath) can be written.
#
proc validateDrvPackage {dir kpath} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ checking driver package...";}
    set missing "";
    set unwritable "";

    # check files, patches
    foreach f $Drv(filesi386) {
	if {![file readable $dir$f]} {
	    lappend missing $f;
	}
    }
    foreach f $Drv(filesconf) {
	if {![file readable $dir$f]} {
	    lappend missing $f;
	}
    }
    foreach f $Drv(patches) {
	if {![file readable $dir$f]} {
	    lappend missing $f;
	}
    }
    if {$missing != ""} {
	error "missing files : $missing";
    }

    # check writability
    if {$Options(verbose)} {puts "+ checking kernel source writability...";}
    foreach f $Drv(filesi386) {
	set p $Drv(filei386:$f);
	if {![file isdirectory $kpath$p]} {
	    lappend missing $p;
	} else {
	    if {![file writable $kpath$p]} {
		if {[lsearch -exact $unwritable $p] == -1} {
		    lappend unwritable $p;
		}
	    }
	}
    }
    foreach f $Drv(filesconf) {
	set p $Drv(fileconf:$f);
	if {![file isdirectory $kpath$p]} {
	    lappend missing $p;
	} else {
	    if {![file writable $kpath$p]} {
		if {[lsearch -exact $unwritable $p] == -1} {
		    lappend unwritable $p;
		}
	    }
	}
    }
    foreach f [list \
		   "conf/files" \
		   "i386/conf/files.i386" \
		   "i386/conf/options.i386" \
		   "i386/conf/LINT"] {
	if {![file writable $kpath$f]} {
	    lappend unwritable $f;
	}
    }
    if {$missing != ""} {
	error "missing directories : $missing";
    }
    if {$unwritable != ""} {
	error "can't write to : $unwritable";
    }
}

################################################################################
# installDrvFiles
#
# Install the files listed in the global Drv into (kpath) from (dir)
#
proc installDrvFiles {dir kpath} {

    global Drv Options;

    # clear 'installed' record
    set Drv(installedi386) "";
    set Drv(installedconf) "";
    set failed "";

    if {$Options(verbose)} {puts "+ installing driver files...";}
    foreach f $Drv(filesi386) {
	if {$Options(verbose)} {puts "$f -> $kpath$Drv(filei386:$f)";}
	if {$Options(real)} {
	    if {[catch {exec cp $dir$f $kpath$Drv(filei386:$f)} msg]} {
		lappend failed $f;
	    } else {
		lappend Drv(installedi386) $f;
	    }
	}
    }
    foreach f $Drv(filesconf) {
	if {$Options(verbose)} {puts "$f -> $kpath$Drv(fileconf:$f)";}
	if {$Options(real)} {
	    if {[catch {exec cp $dir$f $kpath$Drv(fileconf:$f)} msg]} {
		lappend failed $f;
	    } else {
		lappend Drv(installedconf) $f;
	    }
	}
    }
    if {$failed != ""} {
	error "failed to install files : $failed";
    }
}

################################################################################
# backoutDrvChanges
#
# Remove files from a failed installation in (kpath)
#
proc backoutDrvChanges {kpath} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ backing out installed files...";}
    # delete installed files
    foreach f $Drv(installedi386) {
	exec rm -f $kpath$Drv(filei386:$f)$f;
    }
    foreach f $Drv(installedconf) {
	exec rm -f $kpath$Drv(fileconf:$f)$f;
    }
}

################################################################################
# registerDrvFiles
#
# Adds an entry to i386/conf/files.i386 and conf/files for the .c files in the driver.  
# (kpath) points to the kernel.
#
# A comment is added to the file preceeding the new entries :
#
#  ## driver: <drivername>
#  # <description>
#  # filei386: <path><file>
#  <file spec (.c files only)>
#  ## enddriver
#
# We only append to the end of the file.
#
# Add linttext to the LINT file.
# Add options to i386/conf/options.i386 if any are specified
#
proc registerDrvFiles {kpath} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ registering installed files...";}

# Add stuff to LINT
    if {$Drv(linttext) != ""} {

	if {$Options(verbose)} {puts "+ updating LINT...";}
	if {$Options(real)} {
	    set fname [format "%si386/conf/LINT" $kpath];
	    set fh [open $fname a];

	    # header
	    puts $fh "\#\# driver: $Drv(driver)";
	    puts $fh "\# $Drv(description)";
	    foreach l $Drv(linttext) {
		puts $fh $l;
	    }
	    puts $fh "\#\# enddriver";
	    close $fh;
	}
    }

# Do filesi386 stuff
    if {$Options(real)} {
	set fname [format "%si386/conf/files.i386" $kpath];
	set fh [open $fname a];

	# header
	puts $fh "\#\# driver: $Drv(driver)";
	puts $fh "\# $Drv(description)";
	# file information
	foreach f $Drv(filesi386) {
	    puts $fh "\# file: $Drv(filei386:$f)$f";
	    # is it a compilable object?
	    if {[string match "*.c" $f]} {
		puts $fh "$Drv(filei386:$f)$f\t\toptional\t$Drv(driver)\tdevice-driver";
	    }
	}
	puts $fh "\#\# enddriver";
	close $fh;
    }
    if {$Drv(optionsi386) != ""} {
	if {$Options(verbose)} {puts "+ adding options...";}
	if {$Options(real)} {
	    set fname [format "%si386/conf/options.i386" $kpath];
	    set fh [open $fname a];

	    # header
	    puts $fh "\#\# driver: $Drv(driver)";
	    puts $fh "\# $Drv(description)";
	    # options
	    foreach opt $Drv(optionsi386) {
		puts $fh "$opt\t$Drv(optioni386:$opt)";
	    }
	    puts $fh "\#\# enddriver";
	    close $fh;
	}
    }

# Do filesconf stuff
    if {$Options(real)} {
	set fname [format "%sconf/files" $kpath];
	set fh [open $fname a];

	# header
	puts $fh "\#\# driver: $Drv(driver)";
	puts $fh "\# $Drv(description)";
	# file information
	foreach f $Drv(filesconf) {
	    puts $fh "\# file: $Drv(fileconf:$f)$f";
	    # is it a compilable object?
	    if {[string match "*.c" $f]} {
		puts $fh "$Drv(fileconf:$f)$f\t\toptional\t$Drv(driver)\tdevice-driver";
	    }
	}
	puts $fh "\#\# enddriver";
	close $fh;
    }
     if {$Drv(optionsconf) != ""} {
 	if {$Options(verbose)} {puts "+ adding options...";}
 	if {$Options(real)} {
 	    set fname [format "%sconf/options" $kpath];
 	    set fh [open $fname a];

 	    # header
 	    puts $fh "\#\# driver: $Drv(driver)";
 	    puts $fh "\# $Drv(description)";
 	    # options
 	    foreach opt $Drv(optionsconf) {
 		puts $fh "$opt\t$Drv(optionconf:$opt)";
 	    }
 	    puts $fh "\#\# enddriver";
 	    close $fh;
 	}
     }

}

################################################################################
# listInstalledDrv
#
# List all drivers recorded as installed, in the kernel at (kpath)
#
# XXX : fix me so I understand conf/{options,files} stuff!
proc listInstalledDrv {kpath} {

    global Drv;

    # pick up all the i386 options information first
    set fname [format "%si386/conf/options.i386" $kpath];
    if {![file readable $fname]} {
	error "not a kernel directory";
    }
    set fh [open $fname r];

    while {[gets $fh line] >= 0} {
    
	# got a driver?
	if {[scan $line "\#\# driver: %s" driver] == 1} {
	    # read driver details, ignore
	    gets $fh line;
	    # loop reading option details
	    while {[gets $fh line] >= 0} {
		# end of driver info
		if {$line == "\#\# enddriver"} {
		    break ;
		}
		# parse option/header tuple
		if {[scan $line "%s %s" opt hdr] == 2} {
		    # remember that this driver uses this option
		    lappend drivers($driver:optionsi386) $opt;
		    # remember that this option goes in this header
		    set optionsi386($opt) $hdr;
		}
	    }
	}
    }
    close $fh;

    # pick up all the conf options information first
    set fname [format "%sconf/options" $kpath];
    if {![file readable $fname]} {
	error "not a kernel directory";
    }
    set fh [open $fname r];

    while {[gets $fh line] >= 0} {
    
	# got a driver?
	if {[scan $line "\#\# driver: %s" driver] == 1} {
	    # read driver details, ignore
	    gets $fh line;
	    # loop reading option details
	    while {[gets $fh line] >= 0} {
		# end of driver info
		if {$line == "\#\# enddriver"} {
		    break ;
		}
		# parse option/header tuple
		if {[scan $line "%s %s" opt hdr] == 2} {
		    # remember that this driver uses this option
		    lappend drivers($driver:optionsconf) $opt;
		    # remember that this option goes in this header
		    set optionsconf($opt) $hdr;
		}
	    }
	}
    }
    close $fh;

    set fname [format "%si386/conf/files.i386" $kpath];
    set fh [open $fname r];
    
    while {[gets $fh line] >= 0} {

	# got a driver? 
	if {[scan $line "\#\# driver: %s" driver] == 1} {
	    # clear global and reset
	    catch {unset Drv};
	    set Drv(driver) $driver;
	    # read driver details
	    gets $fh line;
	    set Drv(description) [string range $line 2 end];
	    set Drv(filesi386) "";
	    # options?
	    if {[info exists drivers($Drv(driver):optionsi386)]} {
		set Drv(optionsi386) $drivers($Drv(driver):optionsi386);
		# get pathnames
		foreach opt $Drv(optionsi386) {
		    set Drv(optioni386:$opt) $optionsi386($opt);
		}
	    }
	    # loop reading file details
	    while {[gets $fh line] >= 0} {
		if {$line == "\#\# enddriver"} {
		    # print this driver and loop
		    printDrv;
		    break ;
		}
		if {[scan $line "\# filei386: %s" fpath] == 1} {
		    set f [file tail $fpath];	
		    set Drv(filei386:$f) "[file dirname $fpath]/";
		    lappend Drv(filesi386) $f;
		}
	    }
	}
    }
    close $fh;

    set fname [format "%sconf/files" $kpath];
    set fh [open $fname r];
    
    while {[gets $fh line] >= 0} {

	# got a driver? 
	if {[scan $line "\#\# driver: %s" driver] == 1} {
	    # clear global and reset
	    catch {unset Drv};
	    set Drv(driver) $driver;
	    # read driver details
	    gets $fh line;
	    set Drv(description) [string range $line 2 end];
	    set Drv(filesconf) "";
	    # options?
	    if {[info exists drivers($Drv(driver):optionsconf)]} {
		set Drv(optionsconf) $drivers($Drv(driver):optionsconf);
		# get pathnames
		foreach opt $Drv(optionsconf) {
		    set Drv(optionconf:$opt) $optionsconf($opt);
		}
	    }
	    # loop reading file details
	    while {[gets $fh line] >= 0} {
		if {$line == "\#\# enddriver"} {
		    # print this driver and loop
		    printDrv;
		    break ;
		}
		if {[scan $line "\# fileconf: %s" fpath] == 1} {
		    set f [file tail $fpath];	
		    set Drv(fileconf:$f) "[file dirname $fpath]/";
		    lappend Drv(filesconf) $f;
		}
	    }
	}
    }
    close $fh;
}

################################################################################
# printDrv
#
# Print the contents of the global Drv.
#
proc printDrv {} {

    global Drv Options;

    puts "$Drv(driver) : $Drv(description)";
    if {$Options(verbose)} {
	foreach f $Drv(filesi386) {
	    puts " $Drv(filei386:$f)$f"
	}
	foreach f $Drv(filesconf) {
	    puts " $Drv(fileconf:$f)$f"
	}
	if {[info exists Drv(optionsi386)]} {
	    foreach opt $Drv(optionsi386) {
		puts " $opt in $Drv(optioni386:$opt)";
	    }
	}
	if {[info exists Drv(optionsconf)]} {
	    foreach opt $Drv(optionsconf) {
		puts " $opt in $Drv(optionconf:$opt)";
	    }
	}
    }
}

################################################################################
# findInstalledDrv
#
# Given a kernel tree at (kpath), get driver details about an installed
# driver (drvname)
#

proc findInstalledDrvi386 {drvname kpath} {

    global Drv;

    set fname [format "%si386/conf/files.i386" $kpath];
    set fh [open $fname r];
    
    puts "checking i386/conf/files.i386";

    while {[gets $fh line] >= 0} {
	if {[scan $line "\#\# driver: %s" name] == 1} {
	    if {$name != $drvname} {
		continue ;		# not us
	    }
	    # read information
	    set Drv(driver) $drvname;
	    set line [gets $fh];
	    set Drv(description) [string range $line 2 end];
	    set Drv(filesi386) "";
	    # loop reading file details
	    while {[gets $fh line] >= 0} {
		if {$line == "\#\# enddriver"} {
		    close $fh;
		    return 1;		# all done
		}
		if {[scan $line "\# file: %s" fpath] == 1} {
		    set f [file tail $fpath];	
		    set Drv(filei386:$f) "[file dirname $fpath]/";
		    lappend Drv(filesi386) $f;
		}
	    }
	    close $fh;
	    error "unexpected EOF reading '$fname'";
	}
    }
    close $fh

    return 0;
}

proc findInstalledDrvconf {drvname kpath} {

    global Drv;

    set fname [format "%sconf/files" $kpath];
    set fh [open $fname r];

    puts "checking conf/files";
    
    while {[gets $fh line] >= 0} {
	if {[scan $line "\#\# driver: %s" name] == 1} {
	    if {$name != $drvname} {
		continue ;		# not us
	    }
	    # read information
	    set Drv(driver) $drvname;
	    set line [gets $fh];
	    set Drv(description) [string range $line 2 end];
	    set Drv(filesconf) "";
	    # loop reading file details
	    while {[gets $fh line] >= 0} {
		if {$line == "\#\# enddriver"} {
		    close $fh;
		    return 1;		# all done
		}
		if {[scan $line "\# file: %s" fpath] == 1} {
		    set f [file tail $fpath];	
		    set Drv(fileconf:$f) "[file dirname $fpath]/";
		    lappend Drv(filesconf) $f;
		}
	    }
	    close $fh;
	    error "unexpected EOF reading '$fname'";
	}
    }
    close $fh

    return 0;
}

proc findInstalledDrv {drvname kpath} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ look for driver '$drvname' in '$kpath'";}

# Whoops... won't work in a single if statement due to expression shortcircuiting
    set a [findInstalledDrvi386 $drvname $kpath];
    set b [findInstalledDrvconf $drvname $kpath];
    if {$a || $b} {
	return;
    }

    error "driver '$drvname' not recorded as installed";
}

################################################################################
# validateDrvRemoval
#
# Verify that we can remove the driver described in the global Drv installed
# at (kpath).
#
proc validateDrvRemoval {kpath} {

    global Drv Options;

    set missing "";
    set unwritable "";

    if {$Options(verbose)} {puts "+ checking for removabilty...";}

    # admin files?
    foreach f [list \
		   "i386/conf/files.i386" \
		   "i386/conf/options.i386" \
		   "i386/conf/LINT" \
		   "conf/files" \
		   "conf/options" ] { 
	if {![file exists $kpath$f]} {
	    lappend missing $kpath$f;
	} else {
	    if {![file writable $kpath$f]} {
		lappend unwritable $f;
	    }
	}
    }
    # driver components?
    foreach f $Drv(filesi386) {
	set p $Drv(filei386:$f);
	if {![file isdirectory $kpath$p]} {
	    lappend missing $p;
	} else {
	    if {![file writable $kpath$p]} {
		if {[lsearch -exact $unwritable $p] == -1} {
		    lappend unwritable $p;
		}
	    }
	}
    }
    foreach f $Drv(filesconf) {
	set p $Drv(fileconf:$f);
	if {![file isdirectory $kpath$p]} {
	    lappend missing $p;
	} else {
	    if {![file writable $kpath$p]} {
		if {[lsearch -exact $unwritable $p] == -1} {
		    lappend unwritable $p;
		}
	    }
	}
    }
    if {$missing != ""} {
	error "files/directories missing : $missing";
    }
    if {$unwritable != ""} {
	error "can't write to : $unwritable";
    }
}

################################################################################
# deleteDrvFiles
#
# Delete the files belonging to the driver devfined in the global Drv in
# the kernel tree at (kpath)
#
proc deleteDrvFiles {kpath} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ delete driver files...";}

    # loop deleting files
    foreach f $Drv(filesi386) {
	if {$Options(verbose)} {puts "- $Drv(filei386:$f)$f";}
	if {$Options(real)} {
	    exec rm $kpath$Drv(filei386:$f)$f;
	}
    }
    foreach f $Drv(filesconf) {
	if {$Options(verbose)} {puts "- $Drv(fileconf:$f)$f";}
	if {$Options(real)} {
	    exec rm $kpath$Drv(fileconf:$f)$f;
	}
    }
}    

################################################################################
# unregisterDrvFiles
#
# Remove any mention of the current driver from the files.i386 and LINT
# files in (ksrc)
#
proc unregisterDrvFiles {ksrc} {

    global Drv Options;

    if {$Options(verbose)} {puts "+ deregister driver files...";}

    # don't really do it?
    if {!$Options(real)} { return ; }

    foreach f [list \
		   "i386/conf/files.i386" \
		   "i386/conf/options.i386" \
		   "i386/conf/LINT" \
		   "conf/files" \
		   "conf/options" ] {
	set ifh [open $ksrc$f r];
	set ofh [open $ksrc$f.new w];
	set copying 1;

	while {[gets $ifh line] >= 0} {

	    if {[scan $line "\#\# driver: %s" name] == 1} {
		if {$name == $Drv(driver)} {
		    set copying 0;			# don't copy this one
		}
	    }
	    if {$copying} {
		puts $ofh $line;		# copy through
	    }
	    if {$line == "\#\# enddriver"} {	# end of driver detail
		set copying 1;
	    }
	}
	close $ifh;
	close $ofh;
	exec mv $ksrc$f.new $ksrc$f;		# move new over old
    }
}

################################################################################
# usage
#
# Remind the user what goes where
#
proc usage {} {

    global argv0;

    set progname [file tail $argv0];

    puts stderr "Usage is :";
    puts stderr "  $progname \[-v -n\] add <drvinfo> \[<kpath>\]";
    puts stderr "  $progname \[-v -n\] delete <drvname> \[<kpath>\]";
    puts stderr "  $progname \[-v\] list \[<kpath>\]";
    puts stderr "  <drvinfo> is a driver info file";
    puts stderr "  <drvname> is a driver name";
    puts stderr "  <kpath> is the path to the kernel source (default /sys/)";
    puts stderr "  -v  be verbose";
    puts stderr "  -n  don't actually do anything";
    exit ;
}

################################################################################
# getOptions
#
# Parse commandline options, return anything that doesn't look like an option
#
proc getOptions {} {

    global argv Options;

    set Options(real) 1;
    set Options(verbose) 0;
    set ret "";
    
    for {set index 0} {$index < [llength $argv]} {incr index} {
	
	switch -- [lindex $argv $index] {

	    -n {
		set Options(real) 0;		# 'do-nothing' mode
	    }
	    -v {
		set Options(verbose) 1;		# brag
	    }
	    default {
		lappend ret [lindex $argv $index];
	    }
	}
    }
    return $ret;
}

################################################################################
# getKpath
#
# Given (hint), return the kernel path.  If (hint) is empty, return /sys.
# If the kernel path is not a directory, complain and dump the usage.
#
proc getKpath {hint} {

    set kpath "";

    # check the kernel path
    if {$hint == ""} {
	set kpath "/sys/";
    } else {
	set kpath $hint;
    }
    if {![file isdirectory $kpath]} {
	puts "not a directory : $kpath";
	usage ;
    }
    set plast [expr [string length $kpath] -1];
    if {[string index $kpath $plast] != "/"} {
	append kpath "/";
    }
    return $kpath;
}

################################################################################
# main
#
# Start somewhere here.
#
proc main {} {

    global Options;

    # Work out what we're trying to do
    set cmdline [getOptions];
    set mode [lindex $cmdline 0];

    # do stuff
    switch -- $mode {
	add {
	    set hint [lindex $cmdline 1];
	    set kpath [getKpath [lindex $cmdline 2]];

	    # check driver file argument
	    if {[catch {set drv [findDrvFile $hint]} msg]} {
		puts stderr $msg;
		usage ;
	    }
	    if {([file type $drv] != "file") ||
		![file readable $drv]} {
		puts "can't read driver file : $drv";
		usage ; 
	    }
	    set drvdir "[file dirname $drv]/";

	    # read driver file
	    if {[catch {readDrvFile $drv} msg]} {
		puts stderr $msg;
		exit ;
	    }
	    # validate driver
	    if {[catch {validateDrvPackage $drvdir $kpath} msg]} {
		puts stderr $msg;
		exit ;
	    }
	    # install new files
	    if {[catch {installDrvFiles $drvdir $kpath} msg]} {
		backoutDrvChanges $kpath;		# oops, unwind
		puts stderr $msg;
		exit ;
	    }
	    # register files in config
	    if {[catch {registerDrvFiles $kpath} msg]} {
		backoutDrvChanges $kpath;		# oops, unwind
		puts stderr $msg;
		exit ;
	    }
	}
	delete {
	    set drv [lindex $cmdline 1];
	    set kpath [getKpath [lindex $cmdline 2]];

	    if {[string last ".drvinfo" $drv] != -1} {
		set drv [string range $drv 0 [expr [string length $drv] - 9]];
		puts "Driver name ends in .drvinfo, removing, is now $drv";
	    }

	    if {[catch {findInstalledDrv $drv $kpath} msg]} {
		puts stderr $msg;
		exit ;
	    }
	    if {[catch {validateDrvRemoval $kpath} msg]} {
		puts stderr $msg;
		exit ;
	    }
	    if {[catch {unregisterDrvFiles $kpath} msg]} {
		puts stderr $msg;
		exit ;
	    }
	    if {[catch {deleteDrvFiles $kpath} msg]} {
		puts stderr $msg;
		exit ;
	    }
	}
	list { 
	    set kpath [getKpath [lindex $cmdline 1]];
	    if {[catch {listInstalledDrv $kpath} msg]} {
		puts stderr "can't list drivers in '$kpath' : $msg";
	    }
	}
	default {
	    puts stderr "unknown command '$mode'";
	    usage ;
	}
    }
}



################################################################################
main;
OpenPOWER on IntegriCloud