| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Copyright (C) 2007-2012 Jean Delvare <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# This script feeds the i2c-stub driver with dump data from a real |
| 16 |
# I2C or SMBus chip. This can be useful when writing a driver for |
| 17 |
# a device you do not have access to, but of which you have a dump. |
| 18 |
|
| 19 |
use strict; |
| 20 |
use vars qw($bus_nr @addr $err); |
| 21 |
|
| 22 |
# Kernel version detection code by Mark M. Hoffman, |
| 23 |
# copied from sensors-detect. |
| 24 |
use vars qw(@kernel_version); |
| 25 |
|
| 26 |
sub initialize_kernel_version |
| 27 |
{ |
| 28 |
`uname -r` =~ /(\d+)\.(\d+)\.(\d+)(.*)/; |
| 29 |
@kernel_version = ($1, $2, $3, $4); |
| 30 |
} |
| 31 |
|
| 32 |
sub kernel_version_at_least |
| 33 |
{ |
| 34 |
my ($vers, $plvl, $slvl) = @_; |
| 35 |
return 1 if ($kernel_version[0] > $vers || |
| 36 |
($kernel_version[0] == $vers && |
| 37 |
($kernel_version[1] > $plvl || |
| 38 |
($kernel_version[1] == $plvl && |
| 39 |
($kernel_version[2] >= $slvl))))); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
# Find out the i2c bus number of i2c-stub |
| 44 |
sub get_i2c_stub_bus_number |
| 45 |
{ |
| 46 |
my $nr; |
| 47 |
|
| 48 |
open(FH, "i2cdetect -l |") || die "Can't run i2cdetect"; |
| 49 |
while (<FH>) { |
| 50 |
next unless m/^i2c-(\d+).*\tSMBus stub/; |
| 51 |
$nr = $1; |
| 52 |
last; |
| 53 |
} |
| 54 |
close(FH); |
| 55 |
|
| 56 |
return $nr; |
| 57 |
} |
| 58 |
|
| 59 |
# Unload i2c-stub if we need an address it doesn't offer |
| 60 |
sub check_chip_addr { |
| 61 |
my $chip_addr_file = shift; |
| 62 |
my @addr = @{shift()}; |
| 63 |
local $_; |
| 64 |
|
| 65 |
open(CHIP_ADDR, $chip_addr_file) || return; |
| 66 |
$_ = <CHIP_ADDR>; |
| 67 |
chomp; |
| 68 |
my %stub_addr = map { $_ => 1 } split ','; |
| 69 |
close(CHIP_ADDR); |
| 70 |
|
| 71 |
foreach my $addr (@addr) { |
| 72 |
unless (exists $stub_addr{$addr}) { |
| 73 |
print STDERR "Cycling i2c-stub to get the address we need\n"; |
| 74 |
system("/sbin/rmmod", "i2c-stub"); |
| 75 |
return; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
# Load the required kernel drivers if needed |
| 81 |
sub load_kernel_drivers |
| 82 |
{ |
| 83 |
local $_; |
| 84 |
my @addr = @{shift()}; |
| 85 |
my $nr; |
| 86 |
|
| 87 |
# i2c-stub may be loaded without the address we want |
| 88 |
check_chip_addr("/sys/module/i2c_stub/parameters/chip_addr", \@addr); |
| 89 |
|
| 90 |
# Maybe everything is already loaded |
| 91 |
$nr = get_i2c_stub_bus_number(); |
| 92 |
return $nr if defined $nr; |
| 93 |
|
| 94 |
system("/sbin/modprobe", "i2c-dev") == 0 || exit 1; |
| 95 |
if (kernel_version_at_least(2, 6, 19)) { |
| 96 |
system("/sbin/modprobe", "i2c-stub", |
| 97 |
"chip_addr=".join(',', @addr)) == 0 || exit 1; |
| 98 |
} else { |
| 99 |
system("/sbin/modprobe", "i2c-stub") == 0 || exit 1; |
| 100 |
} |
| 101 |
# udev may take some time to create the device node |
| 102 |
if (!(-x "/bin/udevadm" && system("/bin/udevadm settle") == 0) |
| 103 |
&& !(-x "/sbin/udevsettle" && system("/sbin/udevsettle") == 0)) { |
| 104 |
sleep(1); |
| 105 |
} |
| 106 |
|
| 107 |
$nr = get_i2c_stub_bus_number(); |
| 108 |
if (!defined($nr)) { |
| 109 |
print STDERR "Please load i2c-stub first\n"; |
| 110 |
exit 2; |
| 111 |
} |
| 112 |
|
| 113 |
return $nr; |
| 114 |
} |
| 115 |
|
| 116 |
sub process_dump |
| 117 |
{ |
| 118 |
my ($addr, $dump) = @_; |
| 119 |
my $err = 0; |
| 120 |
my ($bytes, $words); |
| 121 |
|
| 122 |
open(DUMP, $dump) || die "Can't open $dump: $!\n"; |
| 123 |
OUTER_LOOP: |
| 124 |
while (<DUMP>) { |
| 125 |
if (m/^([0-9a-f]+0)[ :|]?(( ?[0-9a-fX]{2}){16})/i) { |
| 126 |
# Byte dump |
| 127 |
my $offset = hex($1); |
| 128 |
my @values = split(/ ?/, $2); |
| 129 |
shift(@values); |
| 130 |
for (my $i = 0; $i < 16 && (my $val = shift(@values)); $i++) { |
| 131 |
next if $val =~ m/X/; |
| 132 |
if (system("i2cset", "-y", $bus_nr, $addr, |
| 133 |
sprintf("0x\%02x", $offset+$i), |
| 134 |
"0x$val", "b")) { |
| 135 |
$err = 3; |
| 136 |
last OUTER_LOOP; |
| 137 |
} |
| 138 |
$bytes++; |
| 139 |
} |
| 140 |
} elsif (m/^([0-9a-f][08]) ?[:|](( [0-9a-fX]{4}){8})/i) { |
| 141 |
# Word dump |
| 142 |
my $offset = hex($1); |
| 143 |
my @values = split(/ /, $2); |
| 144 |
shift(@values); |
| 145 |
for (my $i = 0; $i < 8 && (my $val = shift(@values)); $i++) { |
| 146 |
next if $val =~ m/X/; |
| 147 |
if (system("i2cset", "-y", $bus_nr, $addr, |
| 148 |
sprintf("0x\%02x", $offset+$i), |
| 149 |
"0x$val", "w")) { |
| 150 |
$err = 3; |
| 151 |
last OUTER_LOOP; |
| 152 |
} |
| 153 |
$words++; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
close(DUMP); |
| 158 |
|
| 159 |
if ($bytes) { |
| 160 |
printf SAVEOUT "$bytes byte values written to \%d-\%04x\n", |
| 161 |
$bus_nr, $addr; |
| 162 |
} |
| 163 |
|
| 164 |
if ($words) { |
| 165 |
printf SAVEOUT "$words word values written to \%d-\%04x\n", |
| 166 |
$bus_nr, $addr; |
| 167 |
} |
| 168 |
|
| 169 |
if (!$err && !$bytes && !$words) { |
| 170 |
printf SAVEOUT "Only garbage found in dump file $dump\n"; |
| 171 |
$err = 1; |
| 172 |
} |
| 173 |
|
| 174 |
return $err; |
| 175 |
} |
| 176 |
|
| 177 |
if ($>) { |
| 178 |
print "You must be root to use this script\n"; |
| 179 |
exit 1; |
| 180 |
} |
| 181 |
|
| 182 |
if (@ARGV < 2) { |
| 183 |
print STDERR "Usage: i2c-stub-from-dump <addr>[,<addr>,...] <dump file> [<dump file> ...]\n"; |
| 184 |
exit 1; |
| 185 |
} |
| 186 |
|
| 187 |
# Check the parameters |
| 188 |
@addr = split(/,/, shift @ARGV); |
| 189 |
foreach (@addr) { |
| 190 |
unless (m/^0x[0-7][0-9a-f]$/i) { |
| 191 |
print STDERR "Invalid address $_\n"; |
| 192 |
exit 1; |
| 193 |
} |
| 194 |
$_ = oct $_; |
| 195 |
} |
| 196 |
|
| 197 |
if (@addr < @ARGV) { |
| 198 |
print STDERR "Fewer addresses than dumps provided\n"; |
| 199 |
exit 4; |
| 200 |
} |
| 201 |
|
| 202 |
initialize_kernel_version(); |
| 203 |
if (@addr > 1 && !kernel_version_at_least(2, 6, 24)) { |
| 204 |
print STDERR "Multiple addresses not supported by this kernel version\n"; |
| 205 |
exit 5; |
| 206 |
} |
| 207 |
|
| 208 |
$bus_nr = load_kernel_drivers(\@addr); |
| 209 |
|
| 210 |
# We don't want to see the output of 256 i2cset |
| 211 |
open(SAVEOUT, ">&STDOUT"); |
| 212 |
open(STDOUT, ">/dev/null"); |
| 213 |
foreach (@addr) { |
| 214 |
if (!@ARGV) { |
| 215 |
printf STDERR "Skipping \%d-\%04x, no dump file privided\n", |
| 216 |
$bus_nr, $_; |
| 217 |
next; |
| 218 |
} |
| 219 |
$err = process_dump($_, shift @ARGV); |
| 220 |
last if $err; |
| 221 |
} |
| 222 |
close(STDOUT); |
| 223 |
|
| 224 |
exit($err); |