Move flashing scripts to scripts/flashing/
[openwrt/svn-archive/archive.git] / scripts / flashing / adam2flash.pl
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
23 use IO::Socket::INET;
24 use Socket;
25 use strict;
26 use warnings;
27
28 sub usage() {
29 print STDERR "Usage: $0 <ip> [firmware.bin]\n\n";
30 exit 0;
31 }
32
33 my $ip = shift @ARGV;
34 $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage();
35
36 my $probe = IO::Socket::INET->new(Proto => 'udp',
37 Broadcast => 1,
38 LocalPort => 5035) or die "socket: $!";
39 my $setip = unpack("N", inet_aton($ip));
40 $setip > 0 or usage();
41
42 my @packets;
43 foreach my $ver ([18, 1], [22, 2]) {
44 push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
45 }
46 print STDERR "Looking for device: ";
47 my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
48 my $scanning;
49 my $box;
50
51 $SIG{"ALRM"} = sub {
52 return if --$scanning <= 0;
53 foreach my $packet (@packets) {
54 $probe->send($packet, 0, $broadcast);
55 }
56 print STDERR ".";
57 };
58
59 $scanning = 10;
60 foreach my $packet (@packets) {
61 $probe->send($packet, 0, $broadcast);
62 }
63 print STDERR ".";
64
65 while($scanning) {
66 my $reply;
67
68 alarm(1);
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);
74 if ($code == 2) {
75 $scanning = 0;
76 printf STDERR " found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa($addr), inet_ntoa($addr2);
77 $box = inet_ntoa($addr);
78 }
79 }
80 }
81
82 $box or die " not found!\n";
83
84 {
85 package ADAM2FTP;
86 use base qw(Net::FTP);
87
88 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
89 sub _USER {
90 shift->command("USER",@_)->response()
91 }
92
93 sub _GETENV {
94 my $ftp = shift;
95 my ($ok, $name, $value);
96
97 $ftp->command("GETENV",@_);
98 while(length($ok = $ftp->response()) < 1) {
99 my $line = $ftp->getline();
100 unless (defined($value)) {
101 chomp($line);
102 ($name, $value) = split(/\s+/, $line, 2);
103 }
104 }
105 $ftp->debug_print(0, "getenv: $value\n")
106 if $ftp->debug();
107 return $value;
108 }
109
110 sub getenv {
111 my $ftp = shift;
112 my $name = shift;
113 return $ftp->_GETENV($name);
114 }
115
116 sub _REBOOT {
117 shift->command("REBOOT")->response() == Net::FTP::CMD_OK
118 }
119
120 sub reboot {
121 my $ftp = shift;
122 $ftp->_REBOOT;
123 $ftp->close;
124 }
125 }
126
127 my $file = shift @ARGV;
128 $file || exit 0;
129
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";
133
134 my $mtd0 = $ftp->getenv("mtd0");
135 my $mtd1 = $ftp->getenv("mtd1");
136 my ($ksize, $fssize);
137
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;
142
143 $ftp->command("MEDIA FLSH")->response();
144 $ftp->binary();
145 print STDERR "Writing to mtd1...\n";
146
147 my $dc = $ftp->stor("fs mtd1");
148 $dc or die "can't open data connection\n";
149 my $rbytes = 1;
150
151 while (($ksize > 0) and ($rbytes > 0)) {
152 my $buffer;
153 my $len = ($ksize > 1024 ? 1024 : $ksize);
154 $rbytes = read FILE, $buffer, $len;
155 $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
156 }
157
158 $dc->close();
159 $rbytes or die "no more data left to write\n";
160
161 print STDERR "Writing to mtd0...\n";
162
163 $dc = $ftp->stor("fs mtd0");
164 $dc or die "can't open data connection\n";
165
166 while (($fssize > 0) and ($rbytes > 0)) {
167 my $buffer;
168 my $len = ($fssize > 1024 ? 1024 : $fssize);
169 $rbytes = read FILE, $buffer, $len;
170 $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
171 }
172
173 $dc->close();
174 $ftp->reboot();