2
0

adam2flash-fritzbox.pl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env perl
  2. #
  3. # D-Link DSL-G6x4T flash utility
  4. #
  5. # Copyright (C) 2005 Felix Fietkau <mailto@nbd.name>
  6. # based on fbox recovery util by Enrik Berkhan
  7. #
  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.
  12. #
  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.
  17. #
  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
  21. #
  22. use IO::Socket::INET;
  23. use IO::Select;
  24. use Socket;
  25. use strict;
  26. use warnings;
  27. sub usage() {
  28. print STDERR "Usage: $0 <ip> [firmware.bin]\n\n";
  29. exit 0;
  30. }
  31. my $ip = shift @ARGV;
  32. $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage();
  33. my $setip = unpack("N", inet_aton($ip));
  34. $setip > 0 or usage();
  35. my @packets;
  36. foreach my $ver ([18, 1], [22, 2]) {
  37. push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
  38. }
  39. print STDERR "Looking for device: ";
  40. my $scanning;
  41. my $box;
  42. my $probe = IO::Socket::INET->new(Proto => 'udp',
  43. Broadcast => 1,
  44. LocalAddr => $ip,
  45. LocalPort => 5035) or die "socket: $!";
  46. my $sel = IO::Select->new($probe);
  47. my $packet = pack("vCCVNV", 0, 18, 1, 1, 0, 0);
  48. my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
  49. $probe->send($packet, 0, $broadcast);
  50. scan_again:
  51. print "Looking for Fritz!Box ";
  52. my @boxes = ();
  53. my $peer;
  54. $scanning = 100;
  55. print "o";
  56. while($scanning) {
  57. my $reply;
  58. my @ready;
  59. if (@ready = $sel->can_read(0.2)) {
  60. $peer = $probe->recv($reply, 16);
  61. next if (length($reply) < 16);
  62. my ($port, $addr) = sockaddr_in($peer);
  63. my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVV", $reply);
  64. $addr2 = pack("N", $addr2);
  65. if ($code == 2) {
  66. print "O";
  67. push @boxes, [$major, $minor1, $minor2, $addr, $addr2];
  68. $scanning = 2 if ($scanning > 2);
  69. }
  70. } else {
  71. $scanning--;
  72. if (scalar @boxes == 0) {
  73. $probe->send($packet, 0, $broadcast);
  74. print "o";
  75. } else {
  76. print ".";
  77. }
  78. }
  79. }
  80. if (scalar @boxes == 0) {
  81. print " none found, giving up.\n";
  82. exit 1;
  83. } else {
  84. print " found!\n";
  85. }
  86. {
  87. package ADAM2FTP;
  88. use base qw(Net::FTP);
  89. # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
  90. sub _USER { shift->command("USER",@_)->response() }
  91. sub _PASV { shift->command("P\@SW")->response() == Net::FTP::CMD_OK }
  92. sub _GETENV {
  93. my $ftp = shift;
  94. my ($ok, $name, $value);
  95. $ftp->command("GETENV",@_);
  96. while(length($ok = $ftp->response()) < 1) {
  97. my $line = $ftp->getline();
  98. unless (defined($value)) {
  99. chomp($line);
  100. ($name, $value) = split(/\s+/, $line, 2);
  101. }
  102. }
  103. $ftp->debug_print(0, "getenv: $value\n")
  104. if $ftp->debug();
  105. return $value;
  106. }
  107. sub getenv {
  108. my $ftp = shift;
  109. my $name = shift;
  110. return $ftp->_GETENV($name);
  111. }
  112. sub _REBOOT { shift->command("REBOOT")->response() == Net::FTP::CMD_OK }
  113. sub reboot {
  114. my $ftp = shift;
  115. $ftp->_REBOOT;
  116. $ftp->close;
  117. }
  118. sub check {
  119. my $ftp = shift;
  120. delete ${*$ftp}{'net_ftp_port'};
  121. delete ${*$ftp}{'net_ftp_pasv'};
  122. my $data = $ftp->_data_cmd('CHECK' ,@_) or return undef;
  123. my $sum;
  124. if (${${*$ftp}{'net_cmd_resp'}}[0] =~ /^Flash check 0x([0-9A-F]{8})/) {
  125. $sum = hex($1);
  126. }
  127. $data->_close();
  128. return $sum;
  129. }
  130. }
  131. # passive mode geht mit Net::FTP nicht, connected zu spaet fuer ADAM2!
  132. my $ftp = ADAM2FTP->new($ip, Passive => 0, Debug => 0, Timeout => 600)
  133. or die "can't FTP ADAM2";
  134. $ftp->login("adam2", "adam2") or die "can't login adam2";
  135. $ftp->binary();
  136. my $pid = $ftp->getenv('ProductID');
  137. my $hwrev = $ftp->getenv('HWRevision');
  138. my $fwrev = $ftp->getenv('firmware_info');
  139. my $ulrev = $ftp->getenv('urlader-version');
  140. print "Product ID: $pid\n";
  141. print "Hardware Revision: $hwrev\n";
  142. print "Urlader Revision: $ulrev\n";
  143. print "Firmware Revision: $fwrev\n";
  144. $ftp->hash(\*STDOUT, 64 * 1024);
  145. my $file = shift @ARGV;
  146. $file || exit 0;
  147. open FILE, "<$file" or die "can't open firmware file\n";
  148. my $mtd0 = $ftp->getenv("mtd0");
  149. my $mtd1 = $ftp->getenv("mtd1");
  150. my ($ksize, $fssize);
  151. $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
  152. $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
  153. $ksize and $fssize or die 'cannot read partition offsets';
  154. printf STDERR "Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;
  155. $ftp->command("MEDIA FLSH")->response();
  156. $ftp->binary();
  157. print STDERR "Writing to mtd1...\n";
  158. my $dc = $ftp->stor("fs mtd1");
  159. $dc or die "can't open data connection\n";
  160. my $rbytes = 1;
  161. while (($ksize > 0) and ($rbytes > 0)) {
  162. my $buffer;
  163. my $len = ($ksize > 1024 ? 1024 : $ksize);
  164. $rbytes = read FILE, $buffer, $len;
  165. $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
  166. }
  167. $dc->close();
  168. $rbytes or die "no more data left to write\n";
  169. print STDERR "Writing to mtd0...\n";
  170. $dc = $ftp->stor("fs mtd0");
  171. $dc or die "can't open data connection\n";
  172. while (($fssize > 0) and ($rbytes > 0)) {
  173. my $buffer;
  174. my $len = ($fssize > 1024 ? 1024 : $fssize);
  175. $rbytes = read FILE, $buffer, $len;
  176. $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
  177. }
  178. $dc->close();
  179. $ftp->reboot();