2
0

adsl2mue_flash.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env perl
  2. #
  3. # Linksys ADSL2MUE Flash utility.
  4. #
  5. # Copyright (C) 2008 Alexandre Lissy <alexandrelissy@free.fr>
  6. # based on D-Link DSL-G6x4T flash utility by Felix Fietkau <mailto@nbd.name>
  7. # based on fbox recovery util by Enrik Berkhan
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. #
  23. use IO::Socket::INET;
  24. use Socket;
  25. use strict;
  26. use warnings;
  27. sub usage() {
  28. print STDERR "Usage: $0 <ip> [firmware.bin] [partition]\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 $probe = IO::Socket::INET->new(Proto => 'udp',
  34. Broadcast => 1,
  35. LocalPort => 5035) or die "socket: $!";
  36. my $setip = unpack("N", inet_aton($ip));
  37. $setip > 0 or usage();
  38. my @packets;
  39. foreach my $ver ([18, 1], [22, 2]) {
  40. push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
  41. }
  42. print STDERR "Looking for device: ";
  43. my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
  44. my $scanning;
  45. my $box;
  46. $SIG{"ALRM"} = sub {
  47. return if --$scanning <= 0;
  48. foreach my $packet (@packets) {
  49. $probe->send($packet, 0, $broadcast);
  50. }
  51. print STDERR ".";
  52. };
  53. $scanning = 10;
  54. foreach my $packet (@packets) {
  55. $probe->send($packet, 0, $broadcast);
  56. }
  57. print STDERR ".";
  58. while($scanning) {
  59. my $reply;
  60. alarm(1);
  61. if (my $peer = $probe->recv($reply, 16)) {
  62. next if (length($reply) < 16);
  63. my ($port, $addr) = sockaddr_in($peer);
  64. my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVN", $reply);
  65. $addr2 = pack("N", $addr2);
  66. if ($code == 1) {
  67. $scanning = 0;
  68. printf STDERR " found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa($addr2), inet_ntoa($addr);
  69. $box = inet_ntoa($addr2);
  70. }
  71. }
  72. }
  73. $box or die " not found!\n";
  74. {
  75. package ADAM2FTP;
  76. use base qw(Net::FTP);
  77. # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
  78. sub _USER {
  79. shift->command("USER",@_)->response()
  80. }
  81. sub _GETENV {
  82. my $ftp = shift;
  83. my ($ok, $name, $value);
  84. $ftp->command("GETENV",@_);
  85. while(length($ok = $ftp->response()) < 1) {
  86. my $line = $ftp->getline();
  87. unless (defined($value)) {
  88. chomp($line);
  89. ($name, $value) = split(/\s+/, $line, 2);
  90. }
  91. }
  92. $ftp->debug_print(0, "getenv: $value\n")
  93. if $ftp->debug();
  94. return $value;
  95. }
  96. sub getenv {
  97. my $ftp = shift;
  98. my $name = shift;
  99. return $ftp->_GETENV($name);
  100. }
  101. sub _REBOOT {
  102. shift->command("REBOOT")->response() == Net::FTP::CMD_OK
  103. }
  104. sub reboot {
  105. my $ftp = shift;
  106. $ftp->_REBOOT;
  107. $ftp->close;
  108. }
  109. }
  110. my $file = shift @ARGV;
  111. my $part = shift @ARGV;
  112. $file || exit 0;
  113. $part || exit 0;
  114. open FILE, "<$file" or die "can't open firmware file\n";
  115. my $ftp = ADAM2FTP->new($box, Debug => 0, Timeout => 600) or die "can't open control connection\n";
  116. $ftp->login("adam2", "adam2") or die "can't login\n";
  117. # my $mtd0 = $ftp->getenv("mtd0");
  118. # my $mtd1 = $ftp->getenv("mtd1");
  119. my $mtd4 = $ftp->getenv($part);
  120. # my ($ksize, $fssize);
  121. my ($ossize, $mtd_start, $mtd_end);
  122. # $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
  123. # $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
  124. $mtd4 =~ /^(0x\w+),(0x\w+)$/;
  125. $ossize = hex($2) - hex($1);
  126. $mtd_start = hex($1);
  127. $mtd_end = hex($2);
  128. $ossize and $mtd_start and $mtd_end or die 'cannot read partition offsets';
  129. printf STDERR "Available flash space: 0x%08x ($part: 0x%08x to 0x%08x)\n", $ossize, $mtd_start, $mtd_end;
  130. $ftp->command("MEDIA FLSH")->response();
  131. $ftp->binary();
  132. print STDERR "Writing to $part ...\n";
  133. my $dc = $ftp->stor("data $part");
  134. $dc or die "can't open data connection\n";
  135. my $rbytes = 1;
  136. while (($ossize > 0) and ($rbytes > 0)) {
  137. my $buffer;
  138. my $len = ($ossize > 1024 ? 1024 : $ossize);
  139. $rbytes = read FILE, $buffer, $len;
  140. printf STDERR ".";
  141. $rbytes and $ossize -= $dc->write($buffer, $rbytes, 600);
  142. }
  143. printf STDERR "\nDone.\n";
  144. $dc->close();