3 # D-Link DSL-G6x4T flash utility
5 # Copyright (C) 2005 Felix Fietkau <mailto@nbd.name>
6 # based on fbox recovery util by Enrik Berkhan
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 print STDERR
"Usage: $0 <ip> [firmware.bin]\n\n";
34 $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage
();
36 my $probe = IO
::Socket
::INET
->new(Proto
=> 'udp',
38 LocalPort
=> 5035) or die "socket: $!";
39 my $setip = unpack("N", inet_aton
($ip));
40 $setip > 0 or usage
();
42 print STDERR
"Looking for device: ";
43 my $packet = pack("vCCVNV", 0, 22, 2, 1, $setip, 0);
44 my $broadcast = sockaddr_in
(5035, INADDR_BROADCAST
);
49 return if --$scanning <= 0;
50 $probe->send($packet, 0, $broadcast);
55 $probe->send($packet, 0, $broadcast);
62 if (my $peer = $probe->recv($reply, 16)) {
63 next if (length($reply) < 16);
64 my ($port, $addr) = sockaddr_in
($peer);
65 my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVV", $reply);
66 $addr2 = pack("N", $addr2);
69 printf STDERR
" found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa
($addr), inet_ntoa
($addr2);
70 $box = inet_ntoa
($addr);
75 $box or die " not found!\n";
79 use base
qw(Net::FTP);
81 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
83 shift->command("USER",@_)->response()
88 my ($ok, $name, $value);
90 $ftp->command("GETENV",@_);
91 while(length($ok = $ftp->response()) < 1) {
92 my $line = $ftp->getline();
93 unless (defined($value)) {
95 ($name, $value) = split(/\s+/, $line, 2);
98 $ftp->debug_print(0, "getenv: $value\n")
106 return $ftp->_GETENV($name);
110 shift->command("REBOOT")->response() == Net
::FTP
::CMD_OK
120 my $file = shift @ARGV;
123 open FILE
, "<$file" or die "can't open firmware file\n";
124 my $ftp = ADAM2FTP
->new($box, Debug
=> 0, Timeout
=> 600) or die "can't open control connection\n";
125 $ftp->login("adam2", "adam2") or die "can't login\n";
127 my $mtd0 = $ftp->getenv("mtd0");
128 my $mtd1 = $ftp->getenv("mtd1");
129 my ($ksize, $fssize);
131 $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
132 $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
133 $ksize and $fssize or die 'cannot read partition offsets';
134 printf STDERR
"Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;
136 $ftp->command("MEDIA FLSH")->response();
138 print STDERR
"Writing to mtd1...\n";
140 my $dc = $ftp->stor("fs mtd1");
141 $dc or die "can't open data connection\n";
144 while (($ksize > 0) and ($rbytes > 0)) {
146 my $len = ($ksize > 1024 ?
1024 : $ksize);
147 $rbytes = read FILE
, $buffer, $len;
148 $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
152 $rbytes or die "no more data left to write\n";
154 print STDERR
"Writing to mtd0...\n";
156 $dc = $ftp->stor("fs mtd0");
157 $dc or die "can't open data connection\n";
159 while (($fssize > 0) and ($rbytes > 0)) {
161 my $len = ($fssize > 1024 ?
1024 : $fssize);
162 $rbytes = read FILE
, $buffer, $len;
163 $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
This page took 0.062522 seconds and 5 git commands to generate.