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
30 print STDERR "Usage: $0 <ip> [firmware.bin]\n\n";
35 $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage();
37 my $setip = unpack("N", inet_aton($ip));
38 $setip > 0 or usage();
41 foreach my $ver ([18, 1], [22, 2]) {
42 push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
44 print STDERR "Looking for device: ";
48 my $probe = IO::Socket::INET->new(Proto => 'udp',
51 LocalPort => 5035) or die "socket: $!";
52 my $sel = IO::Select->new($probe);
53 my $packet = pack("vCCVNV", 0, 18, 1, 1, 0, 0);
54 my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
56 $probe->send($packet, 0, $broadcast);
60 print "Looking for Fritz!Box ";
69 if (@ready = $sel->can_read(0.2)) {
70 $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("vCCVV", $reply);
74 $addr2 = pack("N", $addr2);
77 push @boxes, [$major, $minor1, $minor2, $addr, $addr2];
78 $scanning = 2 if ($scanning > 2);
82 if (scalar @boxes == 0) {
83 $probe->send($packet, 0, $broadcast);
91 if (scalar @boxes == 0) {
92 print " none found, giving up.\n";
100 use base qw(Net::FTP);
101 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
102 sub _USER { shift->command("USER",@_)->response() }
103 sub _PASV { shift->command("P\@SW")->response() == Net::FTP::CMD_OK }
106 my ($ok, $name, $value);
108 $ftp->command("GETENV",@_);
109 while(length($ok = $ftp->response()) < 1) {
110 my $line = $ftp->getline();
111 unless (defined($value)) {
113 ($name, $value) = split(/\s+/, $line, 2);
116 $ftp->debug_print(0, "getenv: $value\n")
123 return $ftp->_GETENV($name);
125 sub _REBOOT { shift->command("REBOOT")->response() == Net::FTP::CMD_OK }
134 delete ${*$ftp}{'net_ftp_port'};
135 delete ${*$ftp}{'net_ftp_pasv'};
137 my $data = $ftp->_data_cmd('CHECK' ,@_) or return undef;
139 if (${${*$ftp}{'net_cmd_resp'}}[0] =~ /^Flash check 0x([0-9A-F]{8})/) {
147 # passive mode geht mit Net::FTP nicht, connected zu spaet fuer ADAM2!
148 my $ftp = ADAM2FTP->new($ip, Passive => 0, Debug => 0, Timeout => 600)
149 or die "can't FTP ADAM2";
150 $ftp->login("adam2", "adam2") or die "can't login adam2";
152 my $pid = $ftp->getenv('ProductID');
153 my $hwrev = $ftp->getenv('HWRevision');
154 my $fwrev = $ftp->getenv('firmware_info');
155 my $ulrev = $ftp->getenv('urlader-version');
157 print "Product ID: $pid\n";
158 print "Hardware Revision: $hwrev\n";
159 print "Urlader Revision: $ulrev\n";
160 print "Firmware Revision: $fwrev\n";
162 $ftp->hash(\*STDOUT, 64 * 1024);
164 my $file = shift @ARGV;
167 open FILE, "<$file" or die "can't open firmware file\n";
169 my $mtd0 = $ftp->getenv("mtd0");
170 my $mtd1 = $ftp->getenv("mtd1");
171 my ($ksize, $fssize);
173 $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
174 $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
175 $ksize and $fssize or die 'cannot read partition offsets';
176 printf STDERR "Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;
178 $ftp->command("MEDIA FLSH")->response();
180 print STDERR "Writing to mtd1...\n";
182 my $dc = $ftp->stor("fs mtd1");
183 $dc or die "can't open data connection\n";
186 while (($ksize > 0) and ($rbytes > 0)) {
188 my $len = ($ksize > 1024 ? 1024 : $ksize);
189 $rbytes = read FILE, $buffer, $len;
190 $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
194 $rbytes or die "no more data left to write\n";
196 print STDERR "Writing to mtd0...\n";
198 $dc = $ftp->stor("fs mtd0");
199 $dc or die "can't open data connection\n";
201 while (($fssize > 0) and ($rbytes > 0)) {
203 my $len = ($fssize > 1024 ? 1024 : $fssize);
204 $rbytes = read FILE, $buffer, $len;
205 $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);