adam2flash.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Socket;
  24. use strict;
  25. use warnings;
  26. sub usage() {
  27. print STDERR "Usage: $0 <ip> [firmware.bin]\n\n";
  28. exit 0;
  29. }
  30. my $ip = shift @ARGV;
  31. $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage();
  32. my $probe = IO::Socket::INET->new(Proto => 'udp',
  33. Broadcast => 1,
  34. LocalPort => 5035) or die "socket: $!";
  35. my $setip = unpack("N", inet_aton($ip));
  36. $setip > 0 or usage();
  37. my @packets;
  38. foreach my $ver ([18, 1], [22, 2]) {
  39. push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
  40. }
  41. print STDERR "Looking for device: ";
  42. my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
  43. my $scanning;
  44. my $box;
  45. $SIG{"ALRM"} = sub {
  46. return if --$scanning <= 0;
  47. foreach my $packet (@packets) {
  48. $probe->send($packet, 0, $broadcast);
  49. }
  50. print STDERR ".";
  51. };
  52. $scanning = 10;
  53. foreach my $packet (@packets) {
  54. $probe->send($packet, 0, $broadcast);
  55. }
  56. print STDERR ".";
  57. while($scanning) {
  58. my $reply;
  59. alarm(1);
  60. if (my $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. $scanning = 0;
  67. printf STDERR " found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa($addr), inet_ntoa($addr2);
  68. $box = inet_ntoa($addr);
  69. }
  70. }
  71. }
  72. $box or die " not found!\n";
  73. {
  74. package ADAM2FTP;
  75. use base qw(Net::FTP);
  76. # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
  77. sub _USER {
  78. shift->command("USER",@_)->response()
  79. }
  80. sub _GETENV {
  81. my $ftp = shift;
  82. my ($ok, $name, $value);
  83. $ftp->command("GETENV",@_);
  84. while(length($ok = $ftp->response()) < 1) {
  85. my $line = $ftp->getline();
  86. unless (defined($value)) {
  87. chomp($line);
  88. ($name, $value) = split(/\s+/, $line, 2);
  89. }
  90. }
  91. $ftp->debug_print(0, "getenv: $value\n")
  92. if $ftp->debug();
  93. return $value;
  94. }
  95. sub getenv {
  96. my $ftp = shift;
  97. my $name = shift;
  98. return $ftp->_GETENV($name);
  99. }
  100. sub _REBOOT {
  101. shift->command("REBOOT")->response() == Net::FTP::CMD_OK
  102. }
  103. sub reboot {
  104. my $ftp = shift;
  105. $ftp->_REBOOT;
  106. $ftp->close;
  107. }
  108. }
  109. my $file = shift @ARGV;
  110. $file || exit 0;
  111. open FILE, "<$file" or die "can't open firmware file\n";
  112. my $ftp = ADAM2FTP->new($box, Debug => 0, Timeout => 600) or die "can't open control connection\n";
  113. $ftp->login("adam2", "adam2") or die "can't login\n";
  114. my $mtd0 = $ftp->getenv("mtd0");
  115. my $mtd1 = $ftp->getenv("mtd1");
  116. my ($ksize, $fssize);
  117. $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
  118. $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
  119. $ksize and $fssize or die 'cannot read partition offsets';
  120. printf STDERR "Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;
  121. $ftp->command("MEDIA FLSH")->response();
  122. $ftp->binary();
  123. print STDERR "Writing to mtd1...\n";
  124. my $dc = $ftp->stor("fs mtd1");
  125. $dc or die "can't open data connection\n";
  126. my $rbytes = 1;
  127. while (($ksize > 0) and ($rbytes > 0)) {
  128. my $buffer;
  129. my $len = ($ksize > 1024 ? 1024 : $ksize);
  130. $rbytes = read FILE, $buffer, $len;
  131. $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
  132. }
  133. $dc->close();
  134. $rbytes or die "no more data left to write\n";
  135. print STDERR "Writing to mtd0...\n";
  136. $dc = $ftp->stor("fs mtd0");
  137. $dc or die "can't open data connection\n";
  138. while (($fssize > 0) and ($rbytes > 0)) {
  139. my $buffer;
  140. my $len = ($fssize > 1024 ? 1024 : $fssize);
  141. $rbytes = read FILE, $buffer, $len;
  142. $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
  143. }
  144. $dc->close();
  145. $ftp->reboot();