small bugfix in package rules file
[openwrt.git] / openwrt / scripts / dlink.pl
1 #!/usr/bin/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 print STDERR "Looking for device: ";
43 my $packet = pack("vCCVNV", 0, 22, 2, 1, $setip, 0);
44 my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
45 my $scanning;
46 my $box;
47
48 $SIG{"ALRM"} = sub {
49 return if --$scanning <= 0;
50 $probe->send($packet, 0, $broadcast);
51 print STDERR ".";
52 };
53
54 $scanning = 10;
55 $probe->send($packet, 0, $broadcast);
56 print STDERR ".";
57
58 while($scanning) {
59 my $reply;
60
61 alarm(1);
62 if (my $peer = $probe->recv($reply, 16)) {
63 next if (length($reply) < 16);
64 my ($port, $addr) = sockaddr_in($peer);
65 my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVV", $reply);
66 $addr2 = pack("N", $addr2);
67 if ($code == 2) {
68 $scanning = 0;
69 printf STDERR " found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa($addr), inet_ntoa($addr2);
70 $box = inet_ntoa($addr);
71 }
72 }
73 }
74
75 $box or die " not found!\n";
76
77 {
78 package ADAM2FTP;
79 use base qw(Net::FTP);
80
81 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
82 sub _USER {
83 shift->command("USER",@_)->response()
84 }
85
86 sub _GETENV {
87 my $ftp = shift;
88 my ($ok, $name, $value);
89
90 $ftp->command("GETENV",@_);
91 while(length($ok = $ftp->response()) < 1) {
92 my $line = $ftp->getline();
93 unless (defined($value)) {
94 chomp($line);
95 ($name, $value) = split(/\s+/, $line, 2);
96 }
97 }
98 $ftp->debug_print(0, "getenv: $value\n")
99 if $ftp->debug();
100 return $value;
101 }
102
103 sub getenv {
104 my $ftp = shift;
105 my $name = shift;
106 return $ftp->_GETENV($name);
107 }
108
109 sub _REBOOT {
110 shift->command("REBOOT")->response() == Net::FTP::CMD_OK
111 }
112
113 sub reboot {
114 my $ftp = shift;
115 $ftp->_REBOOT;
116 $ftp->close;
117 }
118 }
119
120 my $file = shift @ARGV;
121 $file || exit 0;
122
123 open FILE, "<$file" or die "can't open firmware file\n";
124 my $ftp = ADAM2FTP->new($box, Debug => 0, Timeout => 600) or die "can't open control connection\n";
125 $ftp->login("adam2", "adam2") or die "can't login\n";
126
127 my $mtd0 = $ftp->getenv("mtd0");
128 my $mtd1 = $ftp->getenv("mtd1");
129 my ($ksize, $fssize);
130
131 $mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
132 $mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
133 $ksize and $fssize or die 'cannot read partition offsets';
134 printf STDERR "Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;
135
136 $ftp->command("MEDIA FLSH")->response();
137 $ftp->binary();
138 print STDERR "Writing to mtd1...\n";
139
140 my $dc = $ftp->stor("fs mtd1");
141 $dc or die "can't open data connection\n";
142 my $rbytes = 1;
143
144 while (($ksize > 0) and ($rbytes > 0)) {
145 my $buffer;
146 my $len = ($ksize > 1024 ? 1024 : $ksize);
147 $rbytes = read FILE, $buffer, $len;
148 $rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
149 }
150
151 $dc->close();
152 $rbytes or die "no more data left to write\n";
153
154 print STDERR "Writing to mtd0...\n";
155
156 $dc = $ftp->stor("fs mtd0");
157 $dc or die "can't open data connection\n";
158
159 while (($fssize > 0) and ($rbytes > 0)) {
160 my $buffer;
161 my $len = ($fssize > 1024 ? 1024 : $fssize);
162 $rbytes = read FILE, $buffer, $len;
163 $rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
164 }
165
166 $dc->close();
167 $ftp->reboot();
This page took 0.056139 seconds and 5 git commands to generate.