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
();
43 foreach my $ver ([18, 1], [22, 2]) {
44 push @packets, pack("vCCVNV", 0, @
$ver, 1, $setip, 0);
46 print STDERR
"Looking for device: ";
47 my $broadcast = sockaddr_in
(5035, INADDR_BROADCAST
);
52 return if --$scanning <= 0;
53 foreach my $packet (@packets) {
54 $probe->send($packet, 0, $broadcast);
60 foreach my $packet (@packets) {
61 $probe->send($packet, 0, $broadcast);
69 if (my $peer = $probe->recv($reply, 16)) {
70 next if (length($reply) < 16);
71 my ($port, $addr) = sockaddr_in
($peer);
72 my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVV", $reply);
73 $addr2 = pack("N", $addr2);
76 printf STDERR
" found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa
($addr), inet_ntoa
($addr2);
77 $box = inet_ntoa
($addr);
82 $box or die " not found!\n";
86 use base
qw(Net::FTP);
88 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
90 shift->command("USER",@_)->response()
95 my ($ok, $name, $value);
97 $ftp->command("GETENV",@_);
98 while(length($ok = $ftp->response()) < 1) {
99 my $line = $ftp->getline();
100 unless (defined($value)) {
102 ($name, $value) = split(/\s+/, $line, 2);
105 $ftp->debug_print(0, "getenv: $value\n")
113 return $ftp->_GETENV($name);
117 shift->command("REBOOT")->response() == Net
::FTP
::CMD_OK
127 my $file = shift @ARGV;
130 open FILE
, "<$file" or die "can't open firmware file\n";
131 my $ftp = ADAM2FTP
->new($box, Debug
=> 0, Timeout
=> 600) or die "can't open control connection\n";
132 $ftp->login("adam2", "adam2") or die "can't login\n";
134 my $mtd0 = $ftp->getenv("mtd0");
135 my $mtd1 = $ftp->getenv("mtd1");
136 my ($ksize, $fssize);
138 $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
139 $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
140 $ksize and $fssize or die 'cannot read partition offsets';
141 printf STDERR
"Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;
143 $ftp->command("MEDIA FLSH")->response();
145 print STDERR
"Writing to mtd1...\n";
147 my $dc = $ftp->stor("fs mtd1");
148 $dc or die "can't open data connection\n";
151 while (($ksize > 0) and ($rbytes > 0)) {
153 my $len = ($ksize > 1024 ?
1024 : $ksize);
154 $rbytes = read FILE
, $buffer, $len;
155 $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
159 $rbytes or die "no more data left to write\n";
161 print STDERR
"Writing to mtd0...\n";
163 $dc = $ftp->stor("fs mtd0");
164 $dc or die "can't open data connection\n";
166 while (($fssize > 0) and ($rbytes > 0)) {
168 my $len = ($fssize > 1024 ?
1024 : $fssize);
169 $rbytes = read FILE
, $buffer, $len;
170 $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
This page took 0.059299 seconds and 5 git commands to generate.