2
0

adam2flash-502T.pl 10 KB

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