wok annotate tazndis/stuff/tazndis @ rev 1873

tazndis: Fix Usage().
author Eric Joseph-Alexandre <erjo@slitaz.org>
date Tue Dec 16 23:04:40 2008 +0100 (2008-12-16)
parents 74fc0f86573d
children e7355177f438
rev   line source
erjo@1850 1 #!/usr/bin/perl
erjo@1850 2
erjo@1850 3 #/*
erjo@1850 4 #* tazndis, install, remove or list of NDIS drivers.
erjo@1850 5 #*
erjo@1850 6 #* This program is a replacement for ndiswrapper utility written by
erjo@1850 7 #* Pontus Fuchs and Giridhar Pemmasani.
erjo@1850 8 #* Most part of code come from the original ndiswrapper PERL script.
erjo@1850 9 #*
erjo@1850 10 #* If you need more complexe commands consider to use the original ndiswrapper
erjo@1850 11 #* instead.
erjo@1850 12 #*
erjo@1850 13 #* Copyright (C) 2008 Eric Joseph-Alexandre
erjo@1850 14 #* Copyright (C) 2005-2006 Pontus Fuchs, Giridhar Pemmasani
erjo@1850 15 #*
erjo@1850 16 #*
erjo@1850 17 #* This program is free software; you can redistribute it and/or modify
erjo@1850 18 #* it under the terms of the GNU General Public License as published by
erjo@1850 19 #* the Free Software Foundation; either version 2 of the License, or
erjo@1850 20 #* (at your option) any later version.
erjo@1850 21 #*
erjo@1850 22 #* This program is distributed in the hope that it will be useful,
erjo@1850 23 #* but WITHOUT ANY WARRANTY; without even the implied warranty of
erjo@1850 24 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
erjo@1850 25 #* GNU General Public License for more details.
erjo@1850 26 #*
erjo@1850 27 #*/
erjo@1850 28
erjo@1850 29 $ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}";
erjo@1850 30
erjo@1850 31 my $WRAP_PCI_BUS = 5;
erjo@1850 32 my $WRAP_PCMCIA_BUS = 8;
erjo@1850 33 my $WRAP_USB_BUS = 15;
erjo@1850 34
erjo@1850 35 my %sections;
erjo@1850 36 my %parsed_sections;
erjo@1850 37 my $confdir = "/etc/ndiswrapper";
erjo@1850 38 my $src_dir;
erjo@1850 39 my $driver_name;
erjo@1850 40 my @source_disks_files;
erjo@1850 41
erjo@1850 42 my $re_dev_id = "([[:xdigit:]]{4})";
erjo@1850 43 my $re_sub_dev_conf = "$re_dev_id:$re_dev_id:$re_dev_id:$re_dev_id" .
erjo@1850 44 "\.([[:xdigit:]]+)\.conf";
erjo@1850 45 my $re_dev_conf = "$re_dev_id:$re_dev_id\.([[:xdigit:]]+)\.conf";
erjo@1850 46
erjo@1850 47 # fixup list for parameters.
erjo@1850 48 my %param_fixlist = ("EnableRadio|0" => "1",
erjo@1850 49 "IBSSGMode|0" => "2",
erjo@1850 50 "PrivacyMode|0" => "2",
erjo@1850 51 "MapRegisters|256" => "64",
erjo@1850 52 "AdhocGMode|1" => "0");
erjo@1850 53
erjo@1850 54 if (@ARGV < 1) {
erjo@1850 55 usage();
erjo@1850 56 exit(1);
erjo@1850 57 }
erjo@1850 58
erjo@1850 59 my $res;
erjo@1850 60 my $dbg_file;
erjo@1850 61
erjo@1850 62 $dbg_file = "/dev/null";
erjo@1850 63
erjo@1850 64 # "-D" is for development/debugging only
erjo@1850 65 if ($ARGV[0] eq "-D") {
erjo@1850 66 $dbg_file = "/tmp/ndiswrapper.dbg";
erjo@1850 67 $confdir = "/tmp/ndiswrapper";
erjo@1850 68 open(DBG, "> $dbg_file") or die "couldn't open $dbg_file: $!";
erjo@1850 69 shift;
erjo@1850 70 }
erjo@1850 71
erjo@1850 72 if ($ARGV[0] eq "-i" and @ARGV == 2) {
erjo@1850 73 $res = install($ARGV[1]);
erjo@1850 74 } elsif (($ARGV[0] eq "-r") and @ARGV == 2) {
erjo@1850 75 $res = remove_driver($ARGV[1]);
erjo@1850 76 } elsif ($ARGV[0] eq "-l" and @ARGV == 1) {
erjo@1850 77 $res = list_drivers();
erjo@1850 78 } elsif ($ARGV[0] eq "-v" and @ARGV == 1) {
erjo@1850 79 $res = check_version();
erjo@1850 80 } else {
erjo@1850 81 usage();
erjo@1850 82 }
erjo@1850 83 close(DBG);
erjo@1850 84 exit($res);
erjo@1850 85
erjo@1850 86 sub usage() {
erjo@1850 87 print "install/manage Windows drivers for ndiswrapper\n\n" .
erjo@1873 88 "usage: tazndis OPTION\n" .
erjo@1850 89 "-i inffile install driver described by 'inffile'\n" .
erjo@1850 90 "-r driver remove 'driver'\n" .
erjo@1850 91 "-l list installed drivers\n" .
erjo@1850 92 "-v report version information\n\n" .
erjo@1873 93 "If you need more complex operation, you may install ndiswrapper package instead.\n" ;
erjo@1850 94 }
erjo@1850 95
erjo@1850 96 #/*
erjo@1850 97 #* Begin of PERL modules substition
erjo@1850 98 #*
erjo@1850 99 #* Replaced File.pm
erjo@1850 100
erjo@1850 101 sub basename(@_){
erjo@1850 102 $var = $_[0];
erjo@1850 103 $var =~ s#.*/(.*)$#$1#;
erjo@1850 104 return $var;
erjo@1850 105 }
erjo@1850 106
erjo@1850 107 sub dirname {
erjo@1850 108 local $var = $_[0];
erjo@1850 109 $var =~ s#(.*)/.*$#$1#;
erjo@1850 110 return $var;
erjo@1850 111 }
erjo@1850 112
erjo@1850 113 # delete given single tree with no sub directories.
erjo@1850 114 sub rmtree {
erjo@1850 115 local $dir = $_[0];
erjo@1850 116 opendir(DIR, $dir) or die "couldn't delete $dir: $!\n";
erjo@1850 117 @dirs = grep(!/^.{1,2}$/, readdir(DIR));
erjo@1850 118 foreach $file (@dirs){
erjo@1850 119 unlink "$dir/$file" or die "couldn't delete file $file $!";
erjo@1850 120 }
erjo@1850 121 if(rmdir "$dir"){
erjo@1850 122 return 1;
erjo@1850 123 }else{
erjo@1850 124 return 0;
erjo@1850 125 }
erjo@1850 126 }
erjo@1850 127
erjo@1850 128 # return current path.
erjo@1850 129 sub cwd {
erjo@1850 130 local $var;
erjo@1850 131 chomp($var = `pwd`);
erjo@1850 132 return $var;
erjo@1850 133 }
erjo@1850 134
erjo@1850 135 #
erjo@1850 136 sub copy {
erjo@1850 137 local ($file1, $file2) = @_;
erjo@1850 138 open (F1, "$file1") or
erjo@1850 139 die "couldn't open file $file1 for reading $!\n";
erjo@1850 140 open (F2, ">$file2") or
erjo@1850 141 die "couldn't open file $file2 for writing $!\n";
erjo@1850 142 if ($file1 =~ /\.((bin)|(sys)|(cat))$/) {
erjo@1850 143 binmode F1;
erjo@1850 144 binmode F2;
erjo@1850 145 while (read(F1,$buffer,1)) {
erjo@1850 146 print(F2 $buffer);
erjo@1850 147 }
erjo@1850 148 } else {
erjo@1850 149 while (<F1>) {
erjo@1850 150 print F2 $_;
erjo@1850 151 }
erjo@1850 152 }
erjo@1850 153 close F1;
erjo@1850 154 close F2;
erjo@1850 155 }
erjo@1850 156
erjo@1850 157 ##
erjo@1850 158 ## End of PERL modules substition
erjo@1850 159 ##
erjo@1850 160
erjo@1850 161 sub remove_driver {
erjo@1850 162 my $driver = shift;
erjo@1850 163 if (!rmtree("$confdir/$driver", 0, 1)) {
erjo@1850 164 warn "couldn't delete $confdir/$driver: $!\n";
erjo@1850 165 }
erjo@1850 166 return 0;
erjo@1850 167 }
erjo@1850 168
erjo@1850 169 sub abort {
erjo@1850 170 remove_driver($driver_name);
erjo@1850 171 exit 1;
erjo@1850 172 }
erjo@1850 173
erjo@1850 174 sub check_version {
erjo@1850 175 my ($utils_version, $module_utils_version, $res);
erjo@1850 176 $res = 0;
erjo@1850 177 $utils_version = `loadndisdriver -v`;
erjo@1850 178 chomp($utils_version);
erjo@1850 179 $utils_version =~ s/^version: //;
erjo@1850 180 if (length($utils_version) eq 0) {
erjo@1850 181 printf "utils version is too old!\n";
erjo@1850 182 $res = -1;
erjo@1850 183 }
erjo@1850 184 $module_utils_version = 0;
erjo@1850 185 open(MODINFO, "modinfo ndiswrapper |");
erjo@1850 186 while (my $line = <MODINFO>) {
erjo@1850 187 if ($line =~ /utils_version:.*read only:\s([0-9\.]+)/) {
erjo@1850 188 $module_utils_version = $1;
erjo@1850 189 last;
erjo@1850 190 }
erjo@1850 191 }
erjo@1850 192 if ($module_utils_version eq 0) {
erjo@1850 193 printf "module version is too old!\n";
erjo@1850 194 $res = -1;
erjo@1850 195 } elsif ($utils_version ne $module_utils_version) {
erjo@1850 196 printf "utils version '%s' is incompatible with utils version needed" .
erjo@1850 197 " by driver ('%s')!\n", $utils_version, $module_utils_version;
erjo@1850 198 $res = -1;
erjo@1850 199 }
erjo@1850 200 printf "utils version: '%s', utils version needed by module: '%s'\n",
erjo@1850 201 $utils_version, $module_utils_version;
erjo@1850 202 printf "module details:\n";
erjo@1850 203 system("modinfo ndiswrapper | grep -E '^(version|vermagic|filename)'");
erjo@1850 204
erjo@1850 205 if ($res) {
erjo@1850 206 printf "\nYou may need to upgrade driver and/or utils to latest " .
erjo@1850 207 "versions available at\n" .
erjo@1850 208 "http://ndiswrapper.sourceforge.net\n";
erjo@1850 209 }
erjo@1850 210 return $res;
erjo@1850 211 }
erjo@1850 212
erjo@1850 213 sub install {
erjo@1850 214 my $inf = shift;
erjo@1850 215 chomp($inf);
erjo@1850 216 $src_dir = dirname($inf);
erjo@1850 217 $driver_name = lc(basename($inf));
erjo@1850 218
erjo@1850 219 unless ($driver_name =~ s/\.inf$//) {
erjo@1850 220 die "install argument must be .inf file\n";
erjo@1850 221 }
erjo@1850 222
erjo@1850 223 if (! -d $confdir) {
erjo@1850 224 mkdir($confdir) or die "couldn't create $confdir: $!";
erjo@1850 225 }
erjo@1850 226 (-d "$confdir/$driver_name") and
erjo@1850 227 die "driver $driver_name is already installed\n";
erjo@1850 228
erjo@1850 229 read_sections($inf);
erjo@1850 230 parse_section("Strings");
erjo@1850 231 parse_section("Version");
erjo@1850 232 parse_source_disks_files();
erjo@1850 233 mkdir("$confdir/$driver_name") or
erjo@1850 234 die "couldn't create $confdir/$driver_name: $!";
erjo@1850 235 print "installing $driver_name ...\n";
erjo@1850 236 parse_mfr();
erjo@1850 237 copy_file(basename($inf), basename($inf));
erjo@1850 238 create_fuzzy_conf($driver_name);
erjo@1850 239 return 0;
erjo@1850 240 }
erjo@1850 241
erjo@1850 242 # return lines in section
erjo@1850 243 sub get_section {
erjo@1850 244 my $name = shift;
erjo@1850 245 foreach my $key (keys %sections) {
erjo@1850 246 if (lc($key) eq lc($name)) {
erjo@1850 247 printf DBG "section: $key\n";
erjo@1850 248 return \@{$sections{$key}};
erjo@1850 249 }
erjo@1850 250 }
erjo@1850 251 printf DBG "couldn't find section \"$name\"\n";
erjo@1850 252 return 0;
erjo@1850 253 }
erjo@1850 254
erjo@1850 255 # load inf and split into different sections.
erjo@1850 256 sub read_sections {
erjo@1850 257 my $filename = shift;
erjo@1850 258 open(INF, $filename) or die "couldn't open $filename: $!";
erjo@1850 259
erjo@1850 260 my $name = "none";
erjo@1850 261 @{$sections{$name}} = ();
erjo@1850 262 while (my $line = <INF>) {
erjo@1850 263 # convert from unicode
erjo@1850 264 $line =~ s/\xff\xfe//;
erjo@1850 265 $line =~ s/\0//g;
erjo@1850 266
erjo@1850 267 chomp($line);
erjo@1850 268 $line = trim($line);
erjo@1850 269 next if ($line =~ /^$/);
erjo@1850 270 if ($line =~ /^\[(.+)\]/) {
erjo@1850 271 $name = $1;
erjo@1850 272 @{$sections{$name}} = ();
erjo@1850 273 } else {
erjo@1850 274 push(@{$sections{$name}}, $line);
erjo@1850 275 }
erjo@1850 276 }
erjo@1850 277 close(INF);
erjo@1850 278 foreach $name (keys %sections) {
erjo@1850 279 printf DBG "section: %s\n", $name;
erjo@1850 280 foreach my $line (@{$sections{$name}}) {
erjo@1850 281 printf DBG "%s: %s\n", $name, $line;
erjo@1850 282 }
erjo@1850 283 }
erjo@1850 284 }
erjo@1850 285
erjo@1850 286 sub parse_section {
erjo@1850 287 my $name = shift;
erjo@1850 288 my $lines = get_section($name);
erjo@1850 289 if (!$lines) {
erjo@1850 290 return;
erjo@1850 291 }
erjo@1850 292 $parsed_sections{$name} = ();
erjo@1850 293 foreach my $line (@{$lines}) {
erjo@1850 294 (my $key, my $val) = parse_key_value($line);
erjo@1850 295 if ($key) {
erjo@1850 296 $val = strip_quotes($val);
erjo@1850 297 $parsed_sections{$name}->{$key} = $val;
erjo@1850 298 printf DBG "$name: %s = %s\n", $key, $val;
erjo@1850 299 }
erjo@1850 300 }
erjo@1850 301 }
erjo@1850 302
erjo@1850 303 sub parse_mfr() {
erjo@1850 304 my $lines = get_section("Manufacturer");
erjo@1850 305 $lines or die "couldn't get manufacturer section - " .
erjo@1850 306 "installation may be incomplete\n";
erjo@1850 307 foreach my $line (@{$lines}) {
erjo@1850 308 my ($strkey, $val) = parse_key_value($line);
erjo@1850 309 if ($strkey) {
erjo@1850 310 my ($models, @targets) = split(",", $val);
erjo@1850 311 if ($models) {
erjo@1850 312 printf DBG "mfr: %s, %s\n", $line, $models;
erjo@1850 313 my $target = choose_target_os(@targets);
erjo@1850 314 printf DBG "target: '%s'\n", $target;
erjo@1850 315 parse_models($models, $target);
erjo@1850 316 }
erjo@1850 317 }
erjo@1850 318 }
erjo@1850 319 }
erjo@1850 320
erjo@1850 321 sub parse_models {
erjo@1850 322 my ($models, $target) = @_;
erjo@1850 323 printf DBG "models: target: '%s'.'%s'\n", $models, $target;
erjo@1850 324 my $lines = get_target_os_section($models, $target);
erjo@1850 325 if (!$lines) {
erjo@1850 326 warn "couldn't find models section \"$models\" -\n" .
erjo@1850 327 "installation may be incomplete\n";
erjo@1850 328 return -1;
erjo@1850 329 }
erjo@1850 330 foreach my $line (@{$lines}) {
erjo@1850 331 $line = del_comment($line);
erjo@1850 332 next if (length($line) eq 0);
erjo@1850 333 (my $dev_desc, my $val) = parse_key_value($line);
erjo@1850 334 my @fields = split(",", $val);
erjo@1850 335 if (@fields le 1) {
erjo@1850 336 printf "couldn't find install directive: %s\n", $line;
erjo@1850 337 next;
erjo@1850 338 }
erjo@1850 339 my $section = trim($fields[0]);
erjo@1850 340 my $hwid = trim($fields[1]);
erjo@1850 341 if ($hwid =~ /^%.+%$/) {
erjo@1850 342 $hwid = get_string_value($hwid);
erjo@1850 343 }
erjo@1850 344 # TODO: deal with compatible IDs as hwid?
erjo@1850 345 my ($bus_type, $vendor, $device, $subvendor, $subdevice) =
erjo@1850 346 parse_hwid($hwid);
erjo@1850 347 next if (!$vendor);
erjo@1850 348 printf DBG "models: %s, %s, %s\n", $section, $hwid, $vendor;
erjo@1850 349 parse_install($section, $target, $bus_type, $vendor, $device,
erjo@1850 350 $subvendor, $subdevice);
erjo@1850 351 }
erjo@1850 352 }
erjo@1850 353
erjo@1850 354 sub parse_install {
erjo@1850 355 my ($section, $target, $bus_type, $vendor, $device,
erjo@1850 356 $subvendor, $subdevice) = @_;
erjo@1850 357 my $lines = get_target_os_section($section, $target);
erjo@1850 358 if (!$lines) {
erjo@1850 359 warn "couldn't find install section \"$section\" -\n" .
erjo@1850 360 "installation may be incomplete\n";
erjo@1850 361 return -1;
erjo@1850 362 }
erjo@1850 363
erjo@1850 364 my $filename = "$vendor:$device";
erjo@1850 365 if ($subvendor) {
erjo@1850 366 $filename .= ":$subvendor:$subdevice"
erjo@1850 367 }
erjo@1850 368 $filename .= sprintf(".%X.conf", $bus_type);
erjo@1850 369
erjo@1850 370 my (@addregs, @copyfiles);
erjo@1850 371 foreach my $line (@{$lines}) {
erjo@1850 372 $line =~ s/^;\s*//;
erjo@1850 373 $line = trim(del_comment($line));
erjo@1850 374 my ($key, $val) = parse_key_value($line);
erjo@1850 375 my @array;
erjo@1850 376 if ($key) {
erjo@1850 377 if (lc($key) eq "addreg") {
erjo@1850 378 @array = split(",", $val);
erjo@1850 379 foreach my $reg (@array) {
erjo@1850 380 push @addregs, trim($reg);
erjo@1850 381 }
erjo@1850 382 } elsif (lc($key) eq "copyfiles") {
erjo@1850 383 printf DBG "copyfiles: %s\n", $val;
erjo@1850 384 @array = split(",", $val);
erjo@1850 385 foreach my $copy_file_dirs (@array) {
erjo@1850 386 my @copy_sec = split(",", $copy_file_dirs);
erjo@1850 387 foreach my $file (@copy_sec) {
erjo@1850 388 push @copyfiles, trim($file);
erjo@1850 389 }
erjo@1850 390 }
erjo@1850 391 } elsif (lc($key) eq "bustype") {
erjo@1850 392 printf DBG "bustype: %s\n", $val;
erjo@1850 393 $bus_type = $val;
erjo@1850 394 }
erjo@1850 395 }
erjo@1850 396 }
erjo@1850 397
erjo@1850 398 open(CONF, ">$confdir/$driver_name/$filename") or
erjo@1850 399 die "couldn't create file $confdir/$driver_name/$filename: $!";
erjo@1850 400
erjo@1850 401 printf CONF "sys_files|";
erjo@1850 402 foreach my $file (@copyfiles) {
erjo@1850 403 parse_copy_file($file);
erjo@1850 404 }
erjo@1850 405 printf CONF "\n";
erjo@1850 406
erjo@1850 407 my $version = get_section_value("Version", "DriverVer");
erjo@1850 408 my $provider = get_section_value("Version", "Provider");
erjo@1850 409 my $classguid = get_section_value("Version", "ClassGUID");
erjo@1850 410 my $providerstring = trim(strip_quotes(get_string_value(trim($provider))));
erjo@1850 411 $classguid =~ s/^\s*{//;
erjo@1850 412 $classguid =~ s/}\s*$//;
erjo@1850 413
erjo@1850 414 printf CONF "NdisVersion|0x50001\n";
erjo@1850 415 printf CONF "Environment|1\n";
erjo@1850 416 printf CONF "class_guid|%s\n", $classguid;
erjo@1850 417 printf CONF "driver_version|%s,%s\n", $providerstring, $version;
erjo@1850 418 printf CONF "BusType|%s\n", $bus_type;
erjo@1850 419 printf CONF "SlotNumber|01\n";
erjo@1850 420 printf CONF "NetCfgInstanceId|{28022A01-1234-5678-ABCDE-123813291A00}\n";
erjo@1850 421 printf CONF "\n";
erjo@1850 422 close(CONF);
erjo@1850 423
erjo@1850 424 open(CONF, "|sort|uniq >>$confdir/$driver_name/$filename") or
erjo@1850 425 die "couldn't create file $confdir/$driver_name/$filename: $!";
erjo@1850 426
erjo@1850 427 foreach my $reg (@addregs) {
erjo@1850 428 parse_registry($reg);
erjo@1850 429 }
erjo@1850 430 close(CONF);
erjo@1850 431 }
erjo@1850 432
erjo@1850 433 sub parse_registry {
erjo@1850 434 my ($reg, $conf) = @_;
erjo@1850 435 my $lines = get_section($reg);
erjo@1850 436 if (!$lines) {
erjo@1850 437 warn "couldn't find section \"$reg\" -\n" .
erjo@1850 438 "installation may be incomplete\n";
erjo@1850 439 return -1;
erjo@1850 440 }
erjo@1850 441
erjo@1850 442 my $driver_desc = 0;
erjo@1850 443 foreach my $line (@{$lines}) {
erjo@1850 444 $line = del_comment($line);
erjo@1850 445 my @fields = split(",", $line);
erjo@1850 446 next if (@fields lt 4);
erjo@1850 447 my $value;
erjo@1850 448 my $param = trim($fields[1]);
erjo@1850 449 if ($param =~ /^ndi\\/i) {
erjo@1850 450 if ($param =~ /^ndi\\params\\(.+)/i) {
erjo@1850 451 $param = strip_quotes(trim($1));
erjo@1850 452 $param =~ s/\\.*$//;
erjo@1850 453 next if (lc(trim($fields[2])) ne "default");
erjo@1850 454 $value = strip_quotes(trim($fields[4]));
erjo@1850 455 } else {
erjo@1850 456 printf DBG "ignoring parameter $line\n";
erjo@1850 457 next;
erjo@1850 458 }
erjo@1850 459 } else {
erjo@1850 460 $param = strip_quotes(trim($fields[2]));
erjo@1850 461 next if (length($param) eq 0);
erjo@1850 462 $value = strip_quotes(trim($fields[4]));
erjo@1850 463 }
erjo@1850 464 $value = get_string_value($value);
erjo@1850 465 if (length($param) gt 0) {
erjo@1850 466 if ($param_fixlist{"$param|$value"}) {
erjo@1850 467 my $orig_value = $value;
erjo@1850 468 $value = $param_fixlist{"$param|$value"};
erjo@1850 469 printf "forcing parameter $param from $orig_value to $value\n";
erjo@1850 470 }
erjo@1850 471 printf CONF "%s|%s\n", $param, $value;
erjo@1850 472 if ($param =~ /^DriverDesc$/) {
erjo@1850 473 $driver_desc = 1;
erjo@1850 474 }
erjo@1850 475 }
erjo@1850 476 }
erjo@1850 477 if ($driver_desc == 0) {
erjo@1850 478 printf CONF "DriverDesc|NDIS Network Adapter\n";
erjo@1850 479 }
erjo@1850 480 }
erjo@1850 481
erjo@1850 482 sub parse_copy_file {
erjo@1850 483 my $copy_name = shift;
erjo@1850 484
erjo@1850 485 if ($copy_name =~ /^\@/) {
erjo@1850 486 $copy_name =~ s/^\@//;
erjo@1850 487 $copy_name = trim($copy_name);
erjo@1850 488 if (valid_copy_file_name($copy_name)) {
erjo@1850 489 return copy_file($copy_name, $copy_name);
erjo@1850 490 }
erjo@1850 491 }
erjo@1850 492
erjo@1850 493 my $lines = get_section($copy_name);
erjo@1850 494 if (!$lines) {
erjo@1850 495 warn "couldn't find section \"$copy_name\" -\n" .
erjo@1850 496 "installation may be incomplete\n";
erjo@1850 497 return -1;
erjo@1850 498 }
erjo@1850 499 foreach my $line (@{$lines}) {
erjo@1850 500 $line = trim($line);
erjo@1850 501
erjo@1850 502 # some inf files have file names commented out; get file names from them
erjo@1850 503 $line =~ s/^\s*;//;
erjo@1850 504 my @files = split(",", $line);
erjo@1850 505 if (@files == 0) {
erjo@1850 506 printf DBG "copyfiles section $copy_name has no files\n";
erjo@1850 507 return -1;
erjo@1850 508 }
erjo@1850 509 my $src, my $dst;
erjo@1850 510 if (@files > 1 and length(trim($files[1])) > 0) {
erjo@1850 511 $src = $files[1];
erjo@1850 512 if (length(trim($files[0])) > 0) {
erjo@1850 513 $dst = $files[0];
erjo@1850 514 } else {
erjo@1850 515 $dst = $src;
erjo@1850 516 }
erjo@1850 517 } else {
erjo@1850 518 $src = $files[0];
erjo@1850 519 $dst = $src;
erjo@1850 520 }
erjo@1850 521 $src =~ s/^.*\\//;
erjo@1850 522 $dst =~ s/^.*\\//;
erjo@1850 523 printf DBG "src: '%s', dst: '%s'\n", $src, $dst;
erjo@1850 524 $src = trim(del_comment($src));
erjo@1850 525 next if (length($src) eq 0);
erjo@1850 526 if (valid_copy_file_name($src)) {
erjo@1850 527 $dst = trim(del_comment($dst));
erjo@1850 528 printf DBG "src: '%s', dst: '%s'\n", $src, $dst;
erjo@1850 529 copy_file($src, $dst);
erjo@1850 530 } else {
erjo@1850 531 printf DBG "invalid file '%s' ignored\n", $src;
erjo@1850 532 }
erjo@1850 533 }
erjo@1850 534 return 0;
erjo@1850 535 }
erjo@1850 536
erjo@1850 537 sub parse_hwid {
erjo@1850 538 my $hwid = uc(shift);
erjo@1850 539 if ($hwid =~ /(PCI\\)?VEN_(\w+)&DEV_(\w+)&SUBSYS_(\w{4})(\S{4})/) {
erjo@1850 540 return ($WRAP_PCI_BUS, $2, $3, $4, $5);
erjo@1850 541 } elsif ($hwid =~ /(PCI\\)?VEN_(\w+)&DEV_(\w+)/) {
erjo@1850 542 return ($WRAP_PCI_BUS, $2, $3, 0, 0);
erjo@1850 543 } elsif ($hwid =~ /(USB\\)?VID_(\w+)&PID_(\w+)/) {
erjo@1850 544 return ($WRAP_USB_BUS, $2, $3, 0, 0);
erjo@1850 545 } else {
erjo@1850 546 return 0;
erjo@1850 547 }
erjo@1850 548 }
erjo@1850 549
erjo@1850 550 sub parse_key_value {
erjo@1850 551 my $line = shift;
erjo@1850 552
erjo@1850 553 $line = del_comment($line);
erjo@1850 554 if ($line =~ /([^=]+)=(.+)/) {
erjo@1850 555 return (trim($1), trim($2));
erjo@1850 556 } else {
erjo@1850 557 return 0;
erjo@1850 558 }
erjo@1850 559 }
erjo@1850 560
erjo@1850 561 sub choose_target_os {
erjo@1850 562 my @targets = @_;
erjo@1850 563 my $arch = `uname -m`;
erjo@1850 564 chomp($arch);
erjo@1850 565 printf DBG "arch: %s\n", $arch;
erjo@1850 566 if ($arch =~ /64$/) {
erjo@1850 567 $arch = "amd64";
erjo@1850 568 } else {
erjo@1850 569 $arch = "x86";
erjo@1850 570 }
erjo@1850 571 printf DBG "arch: %s\n", $arch;
erjo@1850 572 my @prefs = ("NT($arch)\.5\.1", "NT($arch)\.5", "NT($arch)",
erjo@1850 573 "NT\.5\.1", "NT\.5", "NT");
erjo@1850 574 foreach my $pref (@prefs) {
erjo@1850 575 foreach my $target (@targets) {
erjo@1850 576 $target = trim($target);
erjo@1850 577 printf DBG "target: '%s', pref: '%s'\n", $target, $pref;
erjo@1850 578 if ($target =~ /NT((amd64)|(x86))/i) {
erjo@1850 579 printf DBG "target arch: '%s'\n", $1;
erjo@1850 580 next if ($1 !~ /$arch/i);
erjo@1850 581 }
erjo@1850 582 if ($target =~ /$pref/i) {
erjo@1850 583 return $target;
erjo@1850 584 }
erjo@1850 585 }
erjo@1850 586 }
erjo@1850 587 return "";
erjo@1850 588 }
erjo@1850 589
erjo@1850 590 sub get_target_os_section {
erjo@1850 591 my ($section, $target) = @_;
erjo@1850 592 my $lines;
erjo@1850 593
erjo@1850 594 chomp($section);
erjo@1850 595 $section =~ s/^\s*"\s*//;
erjo@1850 596 $section =~ s/\s*"\s*$//;
erjo@1850 597 printf DBG "section: \"%s\", target: \"%s\"\n", $section, $target;
erjo@1850 598
erjo@1850 599 if (length($target) gt 0) {
erjo@1850 600 $lines = get_section($section . "." . $target);
erjo@1850 601 return $lines if ($lines);
erjo@1850 602 }
erjo@1850 603
erjo@1850 604 my $arch = `uname -m`;
erjo@1850 605 chomp($arch);
erjo@1850 606 printf DBG "arch: %s\n", $arch;
erjo@1850 607 if ($arch =~ /64$/) {
erjo@1850 608 $arch = "AMD64";
erjo@1850 609 } else {
erjo@1850 610 $arch = "X86";
erjo@1850 611 }
erjo@1850 612 printf DBG "arch: %s\n", $arch;
erjo@1850 613
erjo@1850 614 my @prefs = ("NT$arch.5.1", "NT$arch.5", "NT$arch",
erjo@1850 615 "NT.5.1", "NT.5", "NT");
erjo@1850 616 foreach my $pref (@prefs) {
erjo@1850 617 $lines = get_section($section . "." . $pref);
erjo@1850 618 return $lines if ($lines);
erjo@1850 619 }
erjo@1850 620 $lines = get_section($section);
erjo@1850 621 return $lines if ($lines);
erjo@1850 622
erjo@1850 623 printf DBG "couldn't find section \"$section\" for \"$arch\"\n";
erjo@1850 624 return 0;
erjo@1850 625 }
erjo@1850 626
erjo@1850 627 sub get_section_value {
erjo@1850 628 (my $section, my $name) = @_;
erjo@1850 629 return $parsed_sections{$section}->{$name};
erjo@1850 630 }
erjo@1850 631
erjo@1850 632 sub get_string_value {
erjo@1850 633 my $key = shift;
erjo@1850 634 if ($key =~ /%(.+)%/) {
erjo@1850 635 $key = $1;
erjo@1850 636 return get_section_value("Strings", $key);
erjo@1850 637 } else {
erjo@1850 638 return $key;
erjo@1850 639 }
erjo@1850 640 }
erjo@1850 641
erjo@1850 642 sub copy_file {
erjo@1850 643 my ($src, $dst) = @_;
erjo@1850 644
erjo@1850 645 # ignore files not needed
erjo@1850 646 return 0 if (lc($src) =~ /\.((exe)|(dll)|(cpl)|(hlp))$/);
erjo@1850 647 my $real_file = get_file($src);
erjo@1850 648 if (length($real_file) gt 0) {
erjo@1850 649 $dst = lc($dst);
erjo@1850 650 printf DBG "copying \"$src_dir/$real_file\" to " .
erjo@1850 651 "\"$confdir/$driver_name/$dst\"\n";
erjo@1850 652 copy("$src_dir/$real_file", "$confdir/$driver_name/$dst") or
erjo@1850 653 warn "couldn't copy \"$src_dir/$real_file\" to " .
erjo@1850 654 "\"$confdir/$driver_name\": $! -\n" .
erjo@1850 655 "installation may be incomplete\n";
erjo@1850 656 printf DBG "chmod: $confdir/$driver_name/$dst\n";
erjo@1850 657 chmod(0644, "$confdir/$driver_name/$dst");
erjo@1850 658 if ($dst =~ /\.sys$/) {
erjo@1850 659 printf CONF "%s ", $dst;
erjo@1850 660 }
erjo@1850 661 } else {
erjo@1850 662 warn "couldn't find \"$src\" in \"$src_dir\"; make sure " .
erjo@1850 663 "all driver files, including .inf, .sys (and any firmware files) " .
erjo@1850 664 "are in \"$src_dir\" -\n" .
erjo@1850 665 "installation may be incomplete\n";
erjo@1850 666 }
erjo@1850 667 }
erjo@1850 668
erjo@1850 669
erjo@1850 670 # for conf files with subvendor and subdevice, create conf files with just
erjo@1850 671 # vendor and device
erjo@1850 672 sub create_fuzzy_conf {
erjo@1850 673 my $driver = shift;
erjo@1850 674 my $cwd = cwd();
erjo@1850 675 chdir("$confdir/$driver") or die "couldn't chdir to $confdir/$driver: $!";
erjo@1850 676 open(LS, "ls -1 . |") or die "couldn't open $confdir/$driver: $!";
erjo@1850 677 while (my $file = <LS>) {
erjo@1850 678 chomp($file);
erjo@1850 679 if ($file =~ /$re_sub_dev_conf/) {
erjo@1850 680 my $fuzzy_file = "$1:$2.$5.conf";
erjo@1850 681 printf DBG "file: $file, fuzzy file: $fuzzy_file\n";
erjo@1850 682 if (! -e "$confdir/$driver/$fuzzy_file") {
erjo@1850 683 symlink("$file", "$fuzzy_file") or
erjo@1850 684 warn "couldn't link $confdir/$driver/$file " .
erjo@1850 685 "to $confdir/$driver/$fuzzy_file: $!\n";
erjo@1850 686 }
erjo@1850 687 }
erjo@1850 688 }
erjo@1850 689 close(LS);
erjo@1850 690 chdir($cwd) or warn "couldn't chdir to $cwd: $!";
erjo@1850 691 return 0;
erjo@1850 692 }
erjo@1850 693
erjo@1850 694 # find a file in a case-insensitive way.
erjo@1850 695 sub get_file {
erjo@1850 696 my $file = lc(shift);
erjo@1850 697 if (opendir(DIR, "$src_dir")) {
erjo@1850 698 my @allfiles = readdir(DIR);
erjo@1850 699 foreach my $real_file (@allfiles) {
erjo@1850 700 if (lc($real_file) eq $file) {
erjo@1850 701 closedir(DIR);
erjo@1850 702 return $real_file;
erjo@1850 703 }
erjo@1850 704 }
erjo@1850 705 closedir(DIR);
erjo@1850 706 } else {
erjo@1850 707 warn "couldn't open \"$src_dir\": $! -\n" .
erjo@1850 708 "installation may be incomplete\n";
erjo@1850 709 }
erjo@1850 710 return "";
erjo@1850 711 }
erjo@1850 712
erjo@1850 713 sub strip_quotes {
erjo@1850 714 my $s = shift;
erjo@1850 715 $s =~ s/"(.*)"/$1/;
erjo@1850 716 return $s;
erjo@1850 717 }
erjo@1850 718
erjo@1850 719 sub del_comment {
erjo@1850 720 my $s = shift;
erjo@1850 721 $s =~ s/;.*//;
erjo@1850 722 return $s;
erjo@1850 723 }
erjo@1850 724
erjo@1850 725 # remove whitsepace at front and end.
erjo@1850 726 sub trim {
erjo@1850 727 my $s = shift;
erjo@1850 728 $s =~ s/^\s*//;
erjo@1850 729 $s =~ s/\s*$//;
erjo@1850 730 return $s;
erjo@1850 731 }
erjo@1850 732
erjo@1850 733 sub valid_copy_file_name {
erjo@1850 734 my $file = shift;
erjo@1850 735 $file = lc($file);
erjo@1850 736 printf DBG "file name: %s\n", $file;
erjo@1850 737 foreach my $disk_file (@source_disks_files) {
erjo@1850 738 return 1 if ($file eq $disk_file);
erjo@1850 739 }
erjo@1850 740 # some inf files may not have SourceDisksFiles section, so use
erjo@1850 741 # known file names
erjo@1850 742 return 1 if ($file =~ /\.((sys)|(bin)|(out))$/);
erjo@1850 743 return 0;
erjo@1850 744 }
erjo@1850 745
erjo@1850 746 sub parse_source_disks_files {
erjo@1850 747 my $lines = get_source_disks_files();
erjo@1850 748 if ($lines) {
erjo@1850 749 foreach my $line (@{$lines}) {
erjo@1850 750 $line = del_comment($line);
erjo@1850 751 next if (length($line) eq 0);
erjo@1850 752 my @file = split("=", $line);
erjo@1850 753 next if (@file eq 0 or length($file[0] eq 0));
erjo@1850 754 printf DBG "source disk file: \"%s\"\n", trim($file[0]);
erjo@1850 755 push (@source_disks_files, lc(trim($file[0])));
erjo@1850 756 }
erjo@1850 757 } else {
erjo@1850 758 warn "couldn't find SourceDisksFiles section - " .
erjo@1850 759 "continuing anyway...\n";
erjo@1850 760 }
erjo@1850 761 }
erjo@1850 762
erjo@1850 763 sub get_source_disks_files {
erjo@1850 764 my $arch = `uname -m`;
erjo@1850 765 chomp($arch);
erjo@1850 766 if ($arch =~ /64$/) {
erjo@1850 767 $arch = "AMD64";
erjo@1850 768 } else {
erjo@1850 769 $arch = "X86";
erjo@1850 770 }
erjo@1850 771
erjo@1850 772 my $lines = get_section("SourceDisksFiles." . $arch);
erjo@1850 773 return $lines if ($lines);
erjo@1850 774
erjo@1850 775 $lines = get_section("SourceDisksFiles");
erjo@1850 776 return $lines if ($lines);
erjo@1850 777
erjo@1850 778 return 0;
erjo@1850 779 }
erjo@1850 780
erjo@1850 781 sub device_driver_alias {
erjo@1850 782 my ($devid, $driver) = @_;
erjo@1850 783 my $done = 0;
erjo@1850 784
erjo@1850 785 $devid = uc($devid);
erjo@1850 786 if (!($devid =~ /^$re_dev_id:$re_dev_id$/)) {
erjo@1850 787 printf "'$devid' is not a valid device ID\n";
erjo@1850 788 return 1;
erjo@1850 789 }
erjo@1850 790 open(LS, "ls -1 $confdir/$driver/ |") or
erjo@1850 791 die "couldn't open $confdir/$driver: $!";
erjo@1850 792
erjo@1850 793 while (my $f = <LS>) {
erjo@1850 794 chomp($f);
erjo@1850 795 if ($f =~ /\.([[:xdigit:]]+)\.conf$/) {
erjo@1850 796 if (stat("$confdir/$driver/$devid.$1.conf")) {
erjo@1850 797 printf "Driver '$driver' is already used for '$devid'\n";
erjo@1850 798 $done = 1;
erjo@1850 799 last;
erjo@1850 800 }
erjo@1850 801 if (symlink("$f", "$confdir/$driver/$devid.$1.conf")) {
erjo@1850 802 printf "WARNING: Driver '$driver' will be used for '$devid'\n" .
erjo@1850 803 "This is safe _only_ if driver $driver is meant for " .
erjo@1850 804 "chip in device $devid\n";
erjo@1850 805 $done = 1;
erjo@1850 806 last;
erjo@1850 807 } else {
erjo@1850 808 warn "couldn't create symlink for \"$f\": $! -\n" .
erjo@1850 809 "installation may be incomplete\n";
erjo@1850 810 }
erjo@1850 811 }
erjo@1850 812 }
erjo@1850 813 close(LS);
erjo@1850 814 if ($done == 0) {
erjo@1850 815 printf "driver '$driver' is not installed (properly)!\n";
erjo@1850 816 return 1;
erjo@1850 817 }
erjo@1850 818 return 0;
erjo@1850 819 }
erjo@1850 820
erjo@1850 821 sub list_drivers {
erjo@1850 822 my $cards = get_cards();
erjo@1850 823
erjo@1850 824 open(LS, "/bin/ls -1 $confdir|") or die "couldn't open $confdir: $!";
erjo@1850 825 while (my $driver = <LS>) {
erjo@1850 826 chomp($driver);
erjo@1850 827 if (-e "$confdir/$driver") {
erjo@1850 828 printf "%s : %s\n", $driver, install_status($cards, $driver);
erjo@1850 829 }
erjo@1850 830 }
erjo@1850 831 close(LS);
erjo@1850 832 return 0;
erjo@1850 833 }
erjo@1850 834
erjo@1850 835
erjo@1850 836 sub get_cards {
erjo@1850 837 #01:00.0 Class 0300: 1002:4c66 (rev 01)
erjo@1850 838 # Subsystem: 1043:1732
erjo@1850 839 my @cards = ();
erjo@1850 840 if (open(LSPCI, "/usr/bin/lspci -vn|")) {
erjo@1850 841 my $card;
erjo@1850 842 while (my $line = <LSPCI>) {
erjo@1850 843 if ($line =~ /^[0-9a-f]+.+\s$re_dev_id:$re_dev_id/) {
erjo@1850 844 $card = {vendor => uc($1), device => uc($2)};
erjo@1850 845 printf DBG "card: %s, %s\n", $1, $2;
erjo@1850 846 } elsif ($line =~ /.+Subsystem:\s$re_dev_id:$re_dev_id/) {
erjo@1850 847 $card->{subvendor} = uc($1);
erjo@1850 848 $card->{subdevice} = uc($2);
erjo@1850 849 printf DBG "sub: %s, %s\n", $1, $2;
erjo@1850 850 push(@cards, $card);
erjo@1850 851 }
erjo@1850 852 }
erjo@1850 853 close(LSPCI);
erjo@1850 854 }
erjo@1850 855
erjo@1850 856 if (open(LSUSB, "lsusb |")) {
erjo@1850 857 my $card;
erjo@1850 858 while (my $line = <LSUSB>) {
erjo@1850 859 if ($line =~ /.+: ID\s$re_dev_id:$re_dev_id/) {
erjo@1850 860 $card = {vendor => uc($1), device => uc($2)};
erjo@1850 861 push(@cards, $card);
erjo@1850 862 }
erjo@1850 863 }
erjo@1850 864 close(LSUSB);
erjo@1850 865 }
erjo@1850 866 return \@cards;
erjo@1850 867 }
erjo@1850 868
erjo@1850 869 sub install_status {
erjo@1850 870 my ($cards, $driver) = @_;
erjo@1850 871
erjo@1850 872 if (!$cards or !$driver) {
erjo@1850 873 return;
erjo@1850 874 }
erjo@1850 875
erjo@1850 876 my ($sys, $conf, $inf);
erjo@1850 877 my ($vendor, $device, $subvendor, $subdevice, $busid, $ret);
erjo@1850 878
erjo@1850 879 $sys = $conf = $inf = 0;
erjo@1850 880 open(LS2, "/bin/ls -1 $confdir/$driver|") or
erjo@1850 881 die "couldn't open $confdir/$driver: $!";
erjo@1850 882 while (my $file = <LS2>) {
erjo@1850 883 chomp($file);
erjo@1850 884 if ($file =~ /\.sys$/) {
erjo@1850 885 $sys = 1;
erjo@1850 886 } elsif ($file =~ /\.inf$/) {
erjo@1850 887 $inf = 1;
erjo@1850 888 } elsif ($file =~ /^$re_sub_dev_conf$/) {
erjo@1850 889 $busid = hex($5);
erjo@1850 890 $conf = 1 if ($busid eq $WRAP_PCI_BUS);
erjo@1850 891 } elsif ($file =~ /^$re_dev_conf$/) {
erjo@1850 892 $busid = hex($3);
erjo@1850 893 $conf = 1 if ($busid eq $WRAP_USB_BUS or $busid eq 0 or
erjo@1850 894 $busid eq $WRAP_PCI_BUS);
erjo@1850 895 }
erjo@1850 896 }
erjo@1850 897 close(LS2);
erjo@1850 898 printf DBG "status: $sys, $inf, $conf\n";
erjo@1850 899 if ($sys eq 0 or $inf eq 0 or $conf eq 0) {
erjo@1850 900 $ret = "invalid driver!";
erjo@1850 901 return $ret;
erjo@1850 902 }
erjo@1850 903 $ret = "driver installed";
erjo@1850 904 open(LS2, "/bin/ls -1 $confdir/$driver|") or
erjo@1850 905 die "couldn't open $confdir/$driver: $!";
erjo@1850 906
erjo@1850 907 while (my $file = <LS2>) {
erjo@1850 908 chomp($file);
erjo@1850 909 next if ($file !~ /\.conf$/);
erjo@1850 910 $conf = 0;
erjo@1850 911 if ($file =~ /^$re_sub_dev_conf$/) {
erjo@1850 912 ($vendor, $device, $subvendor, $subdevice, $busid) =
erjo@1850 913 (uc($1), uc($2), uc($3), uc($4), hex($5));
erjo@1850 914 $conf = 1;
erjo@1850 915 foreach my $card (@{$cards}) {
erjo@1850 916 if ($card->{vendor} eq $vendor and
erjo@1850 917 $card->{device} eq $device and
erjo@1850 918 $card->{subvendor} eq $subvendor and
erjo@1850 919 $card->{subdevice} eq $subdevice and
erjo@1850 920 $busid eq $WRAP_PCI_BUS) {
erjo@1850 921 $ret .= "\n\tdevice ($vendor:$device" .
erjo@1850 922 ":$subvendor:$subdevice) present";
erjo@1850 923 $conf = 2;
erjo@1850 924 last;
erjo@1850 925 }
erjo@1850 926 }
erjo@1850 927 } elsif ($file =~ /^$re_dev_conf/) {
erjo@1850 928 ($vendor, $device, $subvendor, $subdevice, $busid) =
erjo@1850 929 (uc($1), uc($2), "\\*", "\\*", hex($3));
erjo@1850 930 $conf = 1;
erjo@1850 931 foreach my $card (@{$cards}) {
erjo@1850 932 if ($card->{vendor} eq $vendor and
erjo@1850 933 $card->{device} eq $device and
erjo@1850 934 ($busid eq $WRAP_USB_BUS or $busid eq 0 or
erjo@1850 935 $busid eq $WRAP_PCI_BUS)) {
erjo@1850 936 $ret .= "\n\tdevice ($vendor:$device) present";
erjo@1850 937 $conf = 2;
erjo@1850 938 last;
erjo@1850 939 }
erjo@1850 940 }
erjo@1850 941 }
erjo@1850 942 next if ($conf le 1);
erjo@1850 943 # find if kernel knows of an alternate driver for this device
erjo@1850 944 my $devstring;
erjo@1850 945 if ($busid eq $WRAP_USB_BUS or $busid eq 0) {
erjo@1850 946 $devstring = sprintf("usb:v%sp%sd", $vendor, $device);
erjo@1850 947 } elsif ($busid eq $WRAP_PCI_BUS) {
erjo@1850 948 $devstring = sprintf("pci:v0000%sd0000%ssv", $vendor, $device);
erjo@1850 949 } else {
erjo@1850 950 next;
erjo@1850 951 }
erjo@1850 952 open(MODPROBE, "modprobe -c|") or next;
erjo@1850 953 while (my $line = <MODPROBE>) {
erjo@1850 954 my $alt;
erjo@1850 955 chomp($line);
erjo@1850 956 next if $line !~ /$devstring/;
erjo@1850 957 $alt = (split(' ', $line))[-1];
erjo@1850 958 chomp($alt);
erjo@1850 959 if (length($alt) gt 0 and $alt ne "ndiswrapper") {
erjo@1850 960 $ret .= " (alternate driver: $alt)";
erjo@1850 961 last;
erjo@1850 962 }
erjo@1850 963 }
erjo@1850 964 close(MODPROBE);
erjo@1850 965 }
erjo@1850 966 close(LS2);
erjo@1850 967 printf DBG "driver: $driver, $ret\n";
erjo@1850 968 return $ret;
erjo@1850 969 }
erjo@1850 970
erjo@1850 971 ## Local Variables: ##
erjo@1850 972 ## cperl-indent-level: 4 ##
erjo@1850 973 ## End: ##