fix initramfs
[openwrt.git] / scripts / adam2flash-502T.pl
1 #!/usr/bin/perl
2 #
3 # D-Link DSL-502T flash utility
4 #
5 # Copyright (c) 2007 Oliver Jowett <oliver@opencloud.com>
6 #
7 # Based on adam2flash.pl for the D-Link DSL-G6x4T, which is:
8 # Copyright (C) 2005 Felix Fietkau <mailto@nbd.name>
9 # based on fbox recovery util by Enrik Berkhan
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #
25
26 # The default DSL-502T mtd map looks like this:
27 #
28 # mtd0 0x90091000,0x903f0000 # filesystem
29 # mtd1 0x90010090,0x90091000 # kernel
30 # mtd2 0x90000000,0x90010000 # bootloader - DO NOT MODIFY
31 # mtd3 0x903f0000,0x90400000 # config space - DO NOT MODIFY
32 # mtd4 0x90010000,0x903f0000 # firmware signature + kernel + filesystem, used to flash new firmware
33 #
34 # i.e. the flash layout is:
35 #
36 # 90000000-9000FFFF mtd2 bootloader
37 # 90010000-9001008F ---- firmware signature )
38 # 90010090-90090FFF mtd1 kernel ) mtd4 spans these three regions
39 # 90091000-903EFFFF mtd0 filesystem )
40 # 903F0000-903FFFFF mtd3 config space
41 #
42 # The ADAM2 bootloader uses the mtd1 settings to find the start of the image to boot.
43 # The image to load contains information about the loadable size of the image. If ADAM2 sees
44 # that the image appears to extend beyond the end of mtd1, it will refuse to load it. On
45 # the DSL-502T, this manifests as the USB light blinking rapidly on boot.
46 #
47 # The OpenWRT kernel does not follow quite the same layout:
48 # (a) it does not have a 0x90-byte firmware signature prefix
49 # (b) it is larger than the default mtd1 size
50 #
51 # (a) would be avoidable (build a custom image with a 0x90-byte prefix) but (b) is unavoidable.
52 # So we *have* to change mtd1. The simplest thing to do seems to make it span all of
53 # the flashable area, producing this layout:
54 #
55 # mtd0 0x90091000,0x903f0000 # filesystem
56 # mtd1 0x90010000,0x903f0000 # kernel (CHANGED)
57 # mtd2 0x90000000,0x90010000 # bootloader - DO NOT MODIFY
58 # mtd3 0x903f0000,0x90400000 # config space - DO NOT MODIFY
59 # mtd4 0x90010000,0x903f0000 # kernel + filesystem, used to flash new firmware
60 #
61 # *** NOTE NOTE NOTE NOTE ***
62 #
63 # /dev/mtd0 .. /dev/mtd4 when using OpenWRT do **NOT** correspond to the ADAM2 mtd0-4 settings!
64 # Instead, OpenWRT scans the MTD itself and determines its own boundaries which are arranged
65 # quite differently to ADAM2. It will look something like this, see dmsg on boot:
66 #
67 # (/dev/mtd0) 0x00000000-0x00010000 : "loader" # Bootloader, read-only
68 # (/dev/mtd1) 0x003f0000-0x00400000 : "config" # Config space
69 # (/dev/mtd2) 0x00010000-0x003f0000 : "linux" # Firmware area (kernel + root fs + JFFS area)
70 # (/dev/mtd3) 0x000d0d58-0x003f0000 : "rootfs" # Root FS, starts immediately after kernel
71 # (/dev/mtd4) 0x00280000-0x003f0000 : "rootfs_data" # If rootfs is squashfs, start of JFFS area.
72 #
73 # All of those boundaries are autodetected by examining the data in flash.
74 #
75 # *** NOTE NOTE NOTE NOTE ***
76
77 use IO::Socket::INET;
78 use Socket;
79 use strict;
80 use warnings;
81
82 sub usage() {
83 print STDERR "Usage: $0 <ip> [-setmtd1] [-noflash] [firmware.bin]\n\n";
84 print STDERR "Acquires the ADAM2 bootloader of a D-Link DSL-504T at <ip>\n";
85 print STDERR "Power off the device, start this script, then power it on.\n";
86 print STDERR "<ip> may be any spare address on the local subnet.\n\n";
87 print STDERR "If a firmware file is specified, MTD settings are verified and\n";
88 print STDERR "then the firmware is written to the router's flash.\n";
89 print STDERR "The firmware type (D-Link or OpenWRT) is automatically detected.\n\n";
90 print STDERR " -setmtd1 update mtd1 if it is not the appropriate value for this firmware\n";
91 print STDERR " -noflash does normal checks, updates mtd1 if requested, but does not actually write firmware\n\n";
92 exit 0;
93 }
94
95 my $ip = shift @ARGV;
96 $ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage();
97
98 my $probe = IO::Socket::INET->new(Proto => 'udp',
99 Broadcast => 1,
100 LocalPort => 5035) or die "socket: $!";
101 my $setip = unpack("N", inet_aton($ip));
102 $setip > 0 or usage();
103
104 my @packets;
105 foreach my $ver ([18, 1], [22, 2]) {
106 push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
107 }
108 print STDERR "Looking for device: ";
109 my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
110 my $scanning;
111 my $box;
112
113 $SIG{"ALRM"} = sub {
114 return if --$scanning <= 0;
115 foreach my $packet (@packets) {
116 $probe->send($packet, 0, $broadcast);
117 }
118 print STDERR ".";
119 };
120
121 $scanning = 15;
122 foreach my $packet (@packets) {
123 $probe->send($packet, 0, $broadcast);
124 }
125 print STDERR ".";
126
127 while($scanning) {
128 my $reply;
129
130 alarm(1);
131 if (my $peer = $probe->recv($reply, 16)) {
132 next if (length($reply) < 16);
133 my ($port, $addr) = sockaddr_in($peer);
134 my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVV", $reply);
135 $addr2 = pack("N", $addr2);
136 if ($code == 2) {
137 $scanning = 0;
138 printf STDERR " found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa($addr), inet_ntoa($addr2);
139 $box = inet_ntoa($addr);
140 }
141 }
142 }
143
144 $box or die " not found!\n";
145
146 {
147 package ADAM2FTP;
148 use base qw(Net::FTP);
149
150 # ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
151 sub _USER {
152 shift->command("USER",@_)->response()
153 }
154
155 sub _GETENV {
156 my $ftp = shift;
157 my ($ok, $name, $value);
158
159 $ftp->command("GETENV",@_);
160 while(length($ok = $ftp->response()) < 1) {
161 my $line = $ftp->getline();
162 unless (defined($value)) {
163 chomp($line);
164 ($name, $value) = split(/\s+/, $line, 2);
165 }
166 }
167 $ftp->debug_print(0, "getenv: $value\n")
168 if $ftp->debug();
169 return $value;
170 }
171
172 sub getenv {
173 my $ftp = shift;
174 my $name = shift;
175 return $ftp->_GETENV($name);
176 }
177
178 sub _REBOOT {
179 shift->command("REBOOT")->response() == Net::FTP::CMD_OK
180 }
181
182 sub reboot {
183 my $ftp = shift;
184 $ftp->_REBOOT;
185 $ftp->close;
186 }
187 }
188
189 my $file;
190 my $arg;
191 my $noflash = 0;
192 my $setmtd1 = 0;
193 while ($arg = shift @ARGV) {
194 if ($arg eq "-noflash") { $noflash = 1; }
195 elsif ($arg eq "-setmtd1") { $setmtd1 = 1; }
196 else { $file = $arg; }
197 }
198
199 if (!$file) {
200 print STDERR "No firmware file specified, exiting.\n";
201 exit 0;
202 }
203
204 #
205 # Firmware checks
206 #
207
208 open FILE, "<$file" or die "can't open firmware file\n";
209
210 # D-Link firmware starts with "MTD4" little-endian, then has an image header at 0x90
211 # OpenWRT firmware just starts with an image header at 0x00
212
213 my $signature;
214 my $sbytes = read FILE, $signature, 4;
215 ($sbytes == 4) or die "can't read firmware signature: $!";
216
217 my $expectedmtd4 = "0x90010000,0x903f0000";
218 my $fwtype;
219 my $expectedmtd1;
220
221 if ($signature eq "4DTM") {
222 seek FILE, 0x90, 0 or die "can't read firmware signature: $!";
223 $sbytes = read FILE, $signature, 4;
224 ($sbytes == 4) or die "can't read firmware signature: $!";
225 if ($signature eq "\x42\xfa\xed\xfe") {
226 $fwtype = "D-Link (little-endian)";
227 $expectedmtd1 = "0x90010090,0x90091000";
228 } elsif ($signature eq "\xde\xad\xbe\x42") {
229 $fwtype = "D-Link (big-endian)";
230 $expectedmtd1 = "0x90010090,0x90091000";
231 }
232 } elsif ($signature eq "\x42\xfa\xed\xfe") {
233 $fwtype = "OpenWRT (little-endian)";
234 $expectedmtd1 = "0x90010000,0x903f0000";
235 } elsif ($signature eq "\xde\xad\xbe\x42") {
236 $fwtype = "OpenWRT (big-endian)";
237 $expectedmtd1 = "0x90010000,0x903f0000";
238 }
239
240 $fwtype or die "Unknown firmware signature (are you sure that's the right firmware?)";
241 print STDERR "Firmware type: $fwtype\n";
242
243 #
244 # Bootloader login
245 #
246
247 print STDERR "logging into ADAM2 bootloader.. ";
248 my $ftp = ADAM2FTP->new($box, Debug => 0, Timeout => 600) or die "can't open control connection\n";
249 $ftp->login("adam2", "adam2") or die "can't login\n";
250 print STDERR "ok.\n";
251
252 #
253 # Hardware checks
254 #
255
256 print STDERR "checking hardware.. ";
257 my $prd = $ftp->getenv("ProductID");
258 my $usb = $ftp->getenv("usb_prod");
259 print STDERR "$prd / $usb.\n";
260 ($prd eq "AR7RD" || $prd eq "AR7DB") or die "doesn't look like a DSL-502T?";
261 ($usb eq "DSL-502T") or die "doesn't look like a DSL-502T?";
262
263 #
264 # MTD checks and update
265 #
266
267 print STDERR "checking MTD settings.. ";
268
269 my $mtd4 = $ftp->getenv("mtd4");
270 ($mtd4 eq $expectedmtd4) or die "MTD4 was not as expected (should be '$expectedmtd4', was '$mtd4'). Cowardly refusing to do anything about it!";
271
272 # check MTD1 setting and update if needed
273 my $mtd1 = $ftp->getenv("mtd1");
274 if ($mtd1 ne $expectedmtd1) {
275 die "MTD1 was not as expected (should be '$expectedmtd1', was '$mtd1'). Run with -setmtd1 to reset mtd1" unless ($setmtd1);
276 print STDERR "Setting mtd1.. ";
277 ($ftp->command("SETENV","mtd1,$expectedmtd1")->response() == Net::FTP::CMD_OK) or die "can't set mtd1";
278 $file = shift @ARGV;
279 }
280
281 print STDERR "ok.\n";
282
283 #
284 # Firmware size check
285 #
286
287 my $fwsize = (stat(FILE))[7];
288 printf STDERR "Firmware size: 0x%08x\n", $fwsize;
289 my $flashsize;
290 $mtd4 =~ /^(0x\w+),(0x\w+)$/ and $flashsize = hex($2) - hex($1);
291 printf STDERR "Available flash space: 0x%08x\n", $flashsize;
292 die "firmware is too large" if ($flashsize < $fwsize);
293
294 #
295 # Flash it!
296 #
297
298 if ($noflash) {
299 print STDERR "Not flashing firmware as -noflash was specified.\n";
300 exit 0;
301 }
302
303 seek FILE, 0, 0, or die "can't seek in firmware: $!";
304
305 print STDERR "Preparing to flash.. ";
306 ($ftp->command("MEDIA FLSH")->response() == Net::FTP::CMD_OK) or die "can't set MEDIA FLSH";
307 $ftp->binary() or die "can't set binary mode";
308 print STDERR "ok.\n";
309 print STDERR "Erasing flash and establishing data connection (this may take a while): ";
310
311 my $dc = $ftp->stor("fs mtd4");
312 $dc or die "can't open data connection: $!\n";
313 print STDERR "ok.\n";
314
315 print STDERR "Writing firmware: ";
316 while ($fwsize > 0) {
317 my $buffer;
318 my $len = ($fwsize > 1024 ? 1024 : $fwsize);
319
320 my $rbytes = read FILE, $buffer, $len;
321 ($rbytes < 0) and die "read error on firmware file: $!";
322 ($rbytes == $len) or die "short read on firmware file ($rbytes < $len)";
323
324 my $wbytes = $dc->write($buffer, $len, 600);
325 ($wbytes < 0) and die "write error on FTP data connection: $!";
326 ($rbytes == $wbytes) or die "short write on FTP data connection ($wbytes < $rbytes)";
327
328 $fwsize -= $len;
329 print STDERR ".";
330 }
331
332 $dc->close();
333 print STDERR " done.\n";
334
335 #
336 # Reboot
337 #
338
339 print STDERR "Rebooting device.\n";
340 $ftp->reboot();
This page took 0.078446 seconds and 5 git commands to generate.