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
|
package detect_devices; # $Id$
use diagnostics;
use strict;
use vars qw($pcitable_addons $usbtable_addons);
#-######################################################################################
#- misc imports
#-######################################################################################
use log;
use common;
use devices;
use run_program;
use c;
#-#####################################################################################
#- Globals
#-#####################################################################################
my @netdevices = map { my $l = $_; map { "$l$_" } (0..3) } qw(eth tr fddi plip);
my %serialprobe = ();
#-######################################################################################
#- Functions
#-######################################################################################
sub dev_is_devfs { -e "/dev/.devfsd" }
sub get {
#- Detect the default BIOS boot harddrive is kind of tricky. We may have IDE,
#- SCSI and RAID devices on the same machine. From what I see so far, the default
#- BIOS boot harddrive will be
#- 1. The first IDE device if IDE exists. Or
#- 2. The first SCSI device if SCSI exists. Or
#- 3. The first RAID device if RAID exists.
getIDE(), getSCSI(), getDAC960(), getCompaqSmartArray(), getATARAID();
}
sub hds { grep { $_->{media_type} eq 'hd' && ($::isStandalone || !isRemovableDrive($_)) } get() }
sub tapes { grep { $_->{media_type} eq 'tape' && ($::isStandalone || !isRemovableDrive($_)) } get() }
sub cdroms { grep { $_->{media_type} eq 'cdrom' } get() }
sub burners { grep { isBurner($_) } cdroms() }
sub dvdroms { grep { isDvdDrive($_) } cdroms() }
sub raw_zips { grep { member($_->{media_type}, 'fd', 'hd') && isZipDrive($_) } get() }
#-sub jazzs { grep { member($_->{media_type}, 'fd', 'hd') && isJazzDrive($_) } get() }
sub ls120s { grep { member($_->{media_type}, 'fd', 'hd') && isLS120Drive($_) } get() }
sub zips { map { $_->{device} .= 4; $_ } raw_zips() }
sub cdroms__faking_ide_scsi {
my @l = cdroms();
return @l if $::isStandalone;
if (my @l_ide = grep { $_->{bus} eq 'ide' && isBurner($_) } @l) {
require modules;
modules::add_alias('scsi_hostadapter', 'ide-scsi');
my $nb = 1 + max(-1, map { $_->{device} =~ /scd(\d+)/ } @l);
foreach my $e (@l_ide) {
log::l("IDEBurner: $e->{device}");
$e->{device} = "scd" . $nb++;
}
}
@l;
}
sub zips__faking_ide_scsi {
my @l = raw_zips();
if (my @l_ide = grep { $_->{bus} eq 'ide' && $::isInstall } @l) {
require modules;
modules::add_alias('scsi_hostadapter', 'ide-scsi');
my $nb = 1 + max(-1, map { if_($_->{device} =~ /sd(\w+)/, ord($1) - ord('a')) } getSCSI());
foreach my $e (@l_ide) {
my $faked = "sd" . chr(ord('a') + $nb++);
log::l("IDE Zip: $e->{device} => $faked");
$e->{device} = $faked;
}
}
map { $_->{device} .= 4; $_ } @l;
}
sub floppies() {
require modules;
eval { modules::load("floppy") };
my @fds = map {
my $info = (!dev_is_devfs() || -e "/dev/$_") && c::floppy_info(devices::make($_));
if_($info && $info ne '(null)', { device => $_, media_type => 'fd', info => $info })
} qw(fd0 fd1);
my @ide = ls120s() and eval { modules::load("ide-floppy") };
eval { modules::load("usb-storage") } if usbStorage();
my @scsi = grep { $_->{media_type} eq 'fd' && !isZipDrive($_) && !isJazzDrive($_) } getSCSI();
@ide, @scsi, @fds;
}
sub floppies_dev() { map { $_->{device} } floppies() }
sub floppy { first(floppies_dev()) }
#- example ls120, model = "LS-120 SLIM 02 UHD Floppy"
sub get_sys_cdrom_info {
my (@drives) = @_;
my @drives_order;
foreach (cat_("/proc/sys/dev/cdrom/info")) {
my ($t, $l) = split ':';
my @l = split ' ', $l;
if ($t eq 'drive name') {
@drives_order = map {
s/^sr/scd/;
my $dev = $_;
first(grep { $_->{device} eq $dev } @drives);
} @l;
} else {
my $capacity;
if ($t eq 'Can write CD-R') {
$capacity = 'burner';
} elsif ($t eq 'Can read DVD') {
$capacity = 'DVD';
}
if ($capacity) {
each_index {
($drives_order[$::i] || {})->{capacity} .= "$capacity " if $_;
} @l;
}
}
}
}
sub isBurner {
my ($e) = @_;
$e->{capacity} =~ /burner/ and return 1;
#- do not work for SCSI
my $f = tryOpen($e->{device}); #- SCSI burner are not detected this way.
$f && c::isBurner(fileno($f));
}
sub isDvdDrive {
my ($e) = @_;
$e->{capacity} =~ /DVD/ || $e->{info} =~ /DVD/ and return 1;
#- do not work for SCSI
my $f = tryOpen($e->{device});
$f && c::isDvdDrive(fileno($f));
}
sub isZipDrive { $_[0]->{info} =~ /ZIP\s+\d+/ } #- accept ZIP 100, untested for bigger ZIP drive.
sub isJazzDrive { $_[0]->{info} =~ /\bJAZZ?\b/i } #- accept "iomega jaz 1GB"
sub isLS120Drive { $_[0]->{info} =~ /LS-?120|144MB/ }
sub isRemovableDrive { &isZipDrive || &isLS120Drive || $_[0]->{media_type} eq 'fd' } #-or &isJazzDrive }
sub isFloppyOrHD {
my ($dev) = @_;
require partition_table_raw;
my $geom = partition_table_raw::get_geometry(devices::make($dev));
$geom->{totalsectors} < 10 << 11 ? 'fd' : 'hd';
}
sub getSCSI() {
my $err = sub { log::l("ERROR: unexpected line in /proc/scsi/scsi: $_[0]"); };
my ($first, @l) = common::join_lines(cat_("/proc/scsi/scsi"));
$first =~ /^Attached devices:/ or $err->($first);
@l = map_index {
my ($id) = /^Host:.*?Id: (\d+)/ or $err->($_);
my ($vendor, $model) = /^\s*Vendor:\s*(.*?)\s+Model:\s*(.*?)\s+Rev:/m or $err->($_);
my ($type) = /^\s*Type:\s*(.*)/m or $err->($_);
{ info => "$vendor $model", id => $id, channel => 0, device => "sg$::i", raw_type => $type, bus => 'SCSI' };
} @l;
each_index {
my $dev = "sd" . chr($::i + ord('a'));
put_in_hash $_, { device => $dev, media_type => isZipDrive($_) ? 'hd' : isFloppyOrHD($dev) };
} grep { $_->{raw_type} =~ /Direct-Access/ } @l;
each_index {
put_in_hash $_, { device => "st$::i", media_type => 'tape' };
} grep { $_->{raw_type} =~ /Sequential-Access/ } @l;
each_index {
put_in_hash $_, { device => "scd$::i", media_type => 'cdrom' };
} grep { $_->{raw_type} =~ /CD-ROM|WORM/ } @l;
each_index {
put_in_hash $_, { media_type => 'scanner' };
} grep { $_->{raw_type} =~ /Scanner/ } @l;
get_sys_cdrom_info(@l);
@l;
}
my %eide_hds = (
"ASUS" => "Asus",
"CD-ROM CDU" => "Sony",
"CD-ROM Drive/F5D" => "ASUSTeK",
"Compaq" => "Compaq",
"CONNER" => "Conner Peripherals",
"IBM" => "IBM",
"FUJITSU" => "Fujitsu",
"HITACHI" => "Hitachi",
"Lite-On" => "Lite-On Technology Corp.",
"LTN" => "Lite-On Technology Corp.",
"IOMEGA" => "Iomega",
"MAXTOR" => "Maxtor",
"Maxtor" => "Maxtor",
"Micropolis" => "Micropolis",
"PLEXTOR" => "Plextor",
"QUANTUM" => "Quantum",
"SAMSUNG" => "Samsung",
"Seagate " => "Seagate Technology",
"ST3" => "Seagate Technology",
"TEAC" => "Teac",
"TOSHIBA" => "Toshiba",
"TEAC" => "Teac",
"TOSHIBA" => "Toshiba",
"WDC" => "Western Digital Corp.",
);
sub getIDE() {
my @idi;
#- what about a system with absolutely no IDE on it, like some sparc machine.
-e "/proc/ide" or return ();
#- Great. 2.2 kernel, things are much easier and less error prone.
foreach my $d (sort @{[glob_('/proc/ide/hd*')]}) {
cat_("$d/driver") =~ /ide-scsi/ and next; #- already appears in /proc/scsi/scsi
my $t = chomp_(cat_("$d/media"));
my $type = $ {{disk => 'hd', cdrom => 'cdrom', tape => 'tape', floppy => 'fd'}}{$t} or next;
my $info = chomp_(cat_("$d/model")) || "(none)";
my $num = ord (($d =~ /(.)$/)[0]) - ord 'a';
my ($vendor, $model) = map {
if_($info =~ /^$_\b\s*(.*)/, $eide_hds{$_}, $1);
} keys %eide_hds;
push @idi, { media_type => $type, device => basename($d), info => $info, channel => $num/2, id => $num%2, bus => 'ide', Vendor => $vendor, Model => $model };
}
get_sys_cdrom_info(@idi);
@idi;
}
sub getCompaqSmartArray() {
my (@idi, $f);
foreach ('array/ida', 'cpqarray/ida', 'cciss/cciss') {
my $prefix = "/proc/driver/$_"; #- kernel 2.4 places it here
$prefix = "/proc/$_" if !-e "${prefix}0"; #- kernel 2.2
my ($name) = m|/(.*)|;
for (my $i = 0; -r ($f = "${prefix}$i"); $i++) {
foreach (cat_($f)) {
if (m|^\s*($name/.*?):|) {
push @idi, { device => $1, info => "Compaq RAID logical disk", media_type => 'hd', bus => 'ida' };
}
}
}
}
@idi;
}
sub getDAC960() {
my %idi;
#- We are looking for lines of this format:DAC960#0:
#- /dev/rd/c0d0: RAID-7, Online, 17928192 blocks, Write Thru0123456790123456789012
foreach (syslog()) {
my ($device, $info) = m|/dev/(rd/.*?): (.*?),| or next;
$idi{$device} = { info => $info, media_type => 'hd', device => $device, bus => 'dac960' };
}
values %idi;
}
sub getATARAID {
my %l;
foreach (syslog()) {
my ($device) = m|^\s*(ataraid/d\d+):| or next;
$l{$device} = { info => 'ATARAID block device', media_type => 'hd', device => $device, bus => 'ataraid' };
log::l("ATARAID: $device");
}
values %l;
}
sub getNet() {
grep { !(($::isStandalone || $::live) && /plip/) && c::hasNetDevice($_) } @netdevices;
}
#sub getISDN() {
# mapgrep(sub {member (($_[0] =~ /\s*(\w*):/), @netdevices), $1 }, split(/\n/, cat_("/proc/net/dev")));
#}
$pcitable_addons = <<'EOF';
# add here lines conforming the pcitable format (0xXXXX\t0xXXXX\t"\w+"\t".*")
EOF
$usbtable_addons = <<'EOF';
# add here lines conforming the usbtable format (0xXXXX\t0xXXXX\t"\w+"\t".*")
EOF
sub add_addons {
my ($addons, @l) = @_;
foreach (split "\n", $addons) {
/^\s/ and die "bad detect_devices::probeall_addons line \"$_\"";
s/^#.*//;
s/"(.*?)"/$1/g;
next if /^$/;
my ($vendor, $id, $driver, $description) = split("\t", $_, 4) or die "bad detect_devices::probeall_addons line \"$_\"";
foreach (@l) {
$_->{vendor} == hex $vendor && $_->{id} == hex $id or next;
put_in_hash($_, { driver => $driver, description => $description });
}
}
@l;
}
sub pci_probe {
my ($probe_type) = @_;
log::l("full pci_probe") if $probe_type;
add_addons($pcitable_addons, map {
my %l;
@l{qw(vendor id subvendor subid pci_bus pci_device pci_function media_type driver description)} = split "\t";
$l{$_} = hex $l{$_} foreach qw(vendor id subvendor subid);
$l{bus} = 'PCI';
\%l
} c::pci_probe($probe_type || 0));
}
sub usb_probe {
-e "/proc/bus/usb/devices" or return ();
my @l = add_addons($usbtable_addons, map {
my %l;
@l{qw(vendor id media_type driver description)} = split "\t";
$l{$_} = hex $l{$_} foreach qw(vendor id);
$l{bus} = 'USB';
\%l
} c::usb_probe());
use Data::Dumper;
log::l(Dumper \@l, backtrace());
@l;
}
sub pcmcia_probe {
-e '/var/run/stab' || -e '/var/lib/pcmcia/stab' or return ();
my (@devs, $desc);
foreach (cat_('/var/run/stab'), cat_('/var/lib/pcmcia/stab')) {
if (/^Socket\s+\d+:\s+(.*)/) {
$desc = $1;
} else {
my (undef, $type, $module, undef, $device) = split;
push @devs, { description => $desc, driver => $module, type => $type, device => $device };
}
}
@devs;
}
# pci_probe with $probe_type is unsafe for pci! (bug in kernel&hardware)
# pcmcia_probe provides field "device", used in network.pm
# => probeall with $probe_type is unsafe
sub probeall {
my ($probe_type) = @_;
require sbus_probing::main;
pci_probe($probe_type), usb_probe(), pcmcia_probe(), sbus_probing::main::probe();
}
sub matching_desc {
my ($regexp) = @_;
grep { $_->{description} =~ /$regexp/i } probeall();
}
sub stringlist {
map {
sprintf("%-16s: %s%s%s",
$_->{driver} ? $_->{driver} : 'unknown',
$_->{description} eq '(null)' ? sprintf("Vendor=0x%04x Device=0x%04x", $_->{vendor}, $_->{id}) : $_->{description},
$_->{media_type} ? sprintf(" [%s]", $_->{media_type}) : '',
$_->{subid} && $_->{subid} != 0xffff ? sprintf(" SubVendor=0x%04x SubDevice=0x%04x", $_->{subvendor}, $_->{subid}) : '',
);
} probeall(@_);
}
sub tryOpen($) {
local *F;
sysopen F, devices::make($_[0]), c::O_NONBLOCK() and *F;
}
sub tryWrite($) {
local *F;
sysopen F, devices::make($_[0]), 1 | c::O_NONBLOCK() and *F;
}
sub syslog {
-r "/tmp/syslog" and return map { /<\d+>(.*)/ } cat_("/tmp/syslog");
`$ENV{LD_LOADER} /bin/dmesg`;
}
sub get_mac_model() {
my $mac_model = cat_("/proc/device-tree/model") || die "Can't open /proc/device-tree/model";
log::l("Mac model: $mac_model");
$mac_model;
}
sub get_mac_generation() {
my $generation = cat_("/proc/cpuinfo") || die "Can't open /proc/cpuinfo";
my @genarray = split(/\n/, $generation);
my $count = 0;
while ($count <= @genarray) {
if ($genarray[$count] =~ /pmac-generation/) {
@genarray = split(/:/, $genarray[$count]);
return $genarray[1];
}
$count++;
}
return "Unknown Generation";
}
sub hasSMP { c::detectSMP() }
sub hasPCMCIA { $::o->{pcmcia} } #- because /proc/pcmcia seems not to be present on 2.4 at least (or use /var/run/stab)
#- try to detect a laptop, we assume pcmcia service is an indication of a laptop or
#- the following regexp to match graphics card apparently only used for such systems.
sub isLaptop {
hasPCMCIA() || (matching_desc('C&T.*655[45]\d') || matching_desc('C&T.*68554') ||
matching_desc('Neomagic.*Magic(Media|Graph)') ||
matching_desc('ViRGE.MX') || matching_desc('S3.*Savage.*[IM]X') ||
matching_desc('ATI.*(Mobility|LT)'));
}
sub hasUltra66 {
die "hasUltra66 deprecated";
#- keep it BUT DO NOT USE IT as now included in kernel.
cat_("/proc/cmdline") =~ /(ide2=(\S+)(\s+ide3=(\S+))?)/ and return $1;
my @l = map { $_->{verbatim} } matching_desc('HPT|Ultra66') or return;
my $ide = sprintf "ide2=0x%x,0x%x ide3=0x%x,0x%x",
@l == 2 ?
(map_index { hex($_) + (odd($::i) ? 1 : -1) } map { (split ' ')[3..4] } @l) :
(map_index { hex($_) + (odd($::i) ? 1 : -1) } map { (split ' ')[3..6] } @l);
log::l("HPT|Ultra66: found $ide");
$ide;
}
sub whatParport() {
my @res = ();
foreach (0..3) {
my $elem = {};
local *F;
open F, "/proc/parport/$_/autoprobe" or open F, "/proc/sys/dev/parport/parport$_/autoprobe" or next;
{
local $_;
while (<F>) {
if (/(.*):(.*);/) {
$elem->{$1} = $2;
$elem->{$1} =~ s/Hewlett[-\s_]Packard/HP/;
$elem->{$1} =~ s/HEWLETT[-\s_]PACKARD/HP/;
}
}
}
push @res, { port => "/dev/lp$_", val => $elem};
}
@res;
}
sub usbMice { grep { $_->{media_type} =~ /\|Mouse/ && $_->{driver} !~ /Tablet:wacom/ ||
$_->{driver} =~ /Mouse:USB/ } usb_probe() }
sub usbWacom { grep { $_->{driver} =~ /Tablet:wacom/ } usb_probe() }
sub usbKeyboards { grep { $_->{media_type} =~ /\|Keyboard/ } usb_probe() }
|