3 # Linksys ADSL2MUE Flash utility.
5 # Copyright (C) 2008 Alexandre Lissy <alexandrelissy@free.fr>
6 # based on D-Link DSL-G6x4T flash utility by Felix Fietkau <mailto@nbd.name>
7 # based on fbox recovery util by Enrik Berkhan
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 print STDERR
"Usage: $0 <ip> [firmware.bin] [partition]\n\n";
35 $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage
();
37 my $probe = IO
::Socket
::INET
->new(Proto
=> 'udp',
39 LocalPort
=> 5035) or die "socket: $!";
40 my $setip = unpack("N", inet_aton
($ip));
41 $setip > 0 or usage
();
44 foreach my $ver ([18, 1], [22, 2]) {
45 push @packets, pack("vCCVNV", 0, @
$ver, 1, $setip, 0);
47 print STDERR
"Looking for device: ";
48 my $broadcast = sockaddr_in
(5035, INADDR_BROADCAST
);
53 return if --$scanning <= 0;
54 foreach my $packet (@packets) {
55 $probe->send($packet, 0, $broadcast);
61 foreach my $packet (@packets) {
62 $probe->send($packet, 0, $broadcast);
70 if (my $peer = $probe->recv($reply, 16)) {
71 next if (length($reply) < 16);
72 my ($port, $addr) = sockaddr_in
($peer);
73 my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVN", $reply);
74 $addr2 = pack("N", $addr2);
77 printf STDERR
" found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa
($addr2), inet_ntoa
($addr);
78 $box = inet_ntoa
($addr2);
83 $box or die " not found!\n";
87 use base
qw(Net::FTP);
89 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
91 shift->command("USER",@_)->response()
96 my ($ok, $name, $value);
98 $ftp->command("GETENV",@_);
99 while(length($ok = $ftp->response()) < 1) {
100 my $line = $ftp->getline();
101 unless (defined($value)) {
103 ($name, $value) = split(/\s+/, $line, 2);
106 $ftp->debug_print(0, "getenv: $value\n")
114 return $ftp->_GETENV($name);
118 shift->command("REBOOT")->response() == Net
::FTP
::CMD_OK
128 my $file = shift @ARGV;
129 my $part = shift @ARGV;
133 open FILE
, "<$file" or die "can't open firmware file\n";
134 my $ftp = ADAM2FTP
->new($box, Debug
=> 0, Timeout
=> 600) or die "can't open control connection\n";
135 $ftp->login("adam2", "adam2") or die "can't login\n";
137 # my $mtd0 = $ftp->getenv("mtd0");
138 # my $mtd1 = $ftp->getenv("mtd1");
139 my $mtd4 = $ftp->getenv($part);
140 # my ($ksize, $fssize);
141 my ($ossize, $mtd_start, $mtd_end);
143 # $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
144 # $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
145 $mtd4 =~ /^(0x\w+),(0x\w+)$/;
146 $ossize = hex($2) - hex($1);
147 $mtd_start = hex($1);
149 $ossize and $mtd_start and $mtd_end or die 'cannot read partition offsets';
150 printf STDERR
"Available flash space: 0x%08x ($part: 0x%08x to 0x%08x)\n", $ossize, $mtd_start, $mtd_end;
152 $ftp->command("MEDIA FLSH")->response();
155 print STDERR
"Writing to $part ...\n";
156 my $dc = $ftp->stor("data $part");
157 $dc or die "can't open data connection\n";
160 while (($ossize > 0) and ($rbytes > 0)) {
162 my $len = ($ossize > 1024 ?
1024 : $ossize);
163 $rbytes = read FILE
, $buffer, $len;
165 $rbytes and $ossize -= $dc->write($buffer, $rbytes, 600);
168 printf STDERR
"\nDone.\n";
This page took 0.052967 seconds and 5 git commands to generate.