// File: 16F88_g.lkr // Generic linker script for the PIC16F88 processor #IFDEF _DEBUG LIBPATH . CODEPAGE NAME=page0 START=0x0 END=0x7FF CODEPAGE NAME=page1 START=0x800 END=0xEFF CODEPAGE NAME=debug START=0xF00 END=0xFFF PROTECTED CODEPAGE NAME=.idlocs START=0x2000 END=0x2003 PROTECTED CODEPAGE NAME=.device_id START=0x2006 END=0x2006 PROTECTED CODEPAGE NAME=.config START=0x2007 END=0x2009 PROTECTED CODEPAGE NAME=eedata START=0x2100 END=0x21FF PROTECTED DATABANK NAME=sfr0 START=0x0 END=0x1F PROTECTED DATABANK NAME=sfr1 START=0x80 END=0x9F PROTECTED DATABANK NAME=sfr2 START=0x100 END=0x10F PROTECTED DATABANK NAME=sfr3 START=0x180 END=0x18F PROTECTED SHAREBANK NAME=dbgnobnk START=0x70 END=0x70 PROTECTED SHAREBANK NAME=dbgnobnk START=0xF0 END=0xF0 PROTECTED SHAREBANK NAME=dbgnobnk START=0x170 END=0x170 PROTECTED SHAREBANK NAME=dbgnobnk START=0x1F0 END=0x1F0 PROTECTED SHAREBANK NAME=sfrnobnk START=0x71 END=0x7F SHAREBANK NAME=sfrnobnk START=0xF1 END=0xFF PROTECTED SHAREBANK NAME=sfrnobnk START=0x171 END=0x17F PROTECTED SHAREBANK NAME=sfrnobnk START=0x1F1 END=0x1FF PROTECTED DATABANK NAME=gpr0 START=0x20 END=0x6F DATABANK NAME=gpr1 START=0xA0 END=0xEF DATABANK NAME=gpr2 START=0x110 END=0x16F DATABANK NAME=gpr3 START=0x190 END=0x1E4 DATABANK NAME=dbg3 START=0x1E5 END=0x1EF PROTECTED SECTION NAME=PROG0 ROM=page0 // ROM code space SECTION NAME=PROG1 ROM=page1 // ROM code space SECTION NAME=DEBUG ROM=debug // ICD debug executive SECTION NAME=IDLOCS ROM=.idlocs // ID locations SECTION NAME=DEVICEID ROM=.device_id // Device ID SECTION NAME=DEEPROM ROM=eedata // Data EEPROM #ELSE LIBPATH . CODEPAGE NAME=page0 START=0x0 END=0x7FF CODEPAGE NAME=page1 START=0x800 END=0xFFF CODEPAGE NAME=.idlocs START=0x2000 END=0x2003 PROTECTED CODEPAGE NAME=.device_id START=0x2006 END=0x2006 PROTECTED CODEPAGE NAME=.config START=0x2007 END=0x2009 PROTECTED CODEPAGE NAME=eedata START=0x2100 END=0x21FF PROTECTED DATABANK NAME=sfr0 START=0x0 END=0x1F PROTECTED DATABANK NAME=sfr1 START=0x80 END=0x9F PROTECTED DATABANK NAME=sfr2 START=0x100 END=0x10F PROTECTED DATABANK NAME=sfr3 START=0x180 END=0x18F PROTECTED SHAREBANK NAME=sfrnobnk START=0x70 END=0x7F SHAREBANK NAME=sfrnobnk START=0xF0 END=0xFF PROTECTED SHAREBANK NAME=sfrnobnk START=0x170 END=0x17F PROTECTED SHAREBANK NAME=sfrnobnk START=0x1F0 END=0x1FF PROTECTED DATABANK NAME=gpr0 START=0x20 END=0x6F DATABANK NAME=gpr1 START=0xA0 END=0xEF DATABANK NAME=gpr2 START=0x110 END=0x16F DATABANK NAME=gpr3 START=0x190 END=0x1EF SECTION NAME=PROG0 ROM=page0 // ROM code space SECTION NAME=PROG1 ROM=page1 // ROM code space SECTION NAME=IDLOCS ROM=.idlocs // ID locations SECTION NAME=DEVICEID ROM=.device_id // Device ID SECTION NAME=DEEPROM ROM=eedata // Data EEPROM #FI .pl?h=v18.06.6&id=8e58c644fd2d73adc12f50b10c8aad9fcb5c5765'>adam2flash.pl
blob: bd0fa69a61b5626466f6207c782fca8855fbee44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl
#
#   D-Link DSL-G6x4T flash utility
#
#   Copyright (C) 2005 Felix Fietkau <mailto@nbd.name>
#   based on fbox recovery util by Enrik Berkhan
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use IO::Socket::INET;
use Socket;
use strict;
use warnings;

sub usage() {
	print STDERR "Usage: $0 <ip> [firmware.bin]\n\n";
	exit 0;
}

my $ip = shift @ARGV;
$ip and $ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ or usage();

my $probe = IO::Socket::INET->new(Proto => 'udp',
                                  Broadcast => 1,
                                  LocalPort => 5035) or die "socket: $!";
my $setip = unpack("N", inet_aton($ip));
$setip > 0 or usage();

my @packets;
foreach my $ver ([18, 1], [22, 2]) {
	push @packets, pack("vCCVNV", 0, @$ver, 1, $setip, 0);
}
print STDERR "Looking for device: ";
my $broadcast = sockaddr_in(5035, INADDR_BROADCAST);
my $scanning;
my $box;

$SIG{"ALRM"} = sub {
	return if --$scanning <= 0;
	foreach my $packet (@packets) {
		$probe->send($packet, 0, $broadcast);
	}
	print STDERR ".";
};

$scanning = 10;
foreach my $packet (@packets) {
	$probe->send($packet, 0, $broadcast);
}
print STDERR ".";

while($scanning) {
	my $reply;

	alarm(1);
	if (my $peer = $probe->recv($reply, 16)) {
		next if (length($reply) < 16);
		my ($port, $addr) = sockaddr_in($peer);
		my ($major, $minor1, $minor2, $code, $addr2) = unpack("vCCVV", $reply);
		$addr2 = pack("N", $addr2);
		if ($code == 2) {
			$scanning = 0;
			printf STDERR " found!\nADAM2 version $major.$minor1.$minor2 at %s (%s)\n", inet_ntoa($addr), inet_ntoa($addr2);
			$box = inet_ntoa($addr);
		}
	}
}

$box or die " not found!\n";

{
	package ADAM2FTP;
	use base qw(Net::FTP);
	
	# ADAM2 requires upper case commands, some brain dead firewall doesn't ;-)
	sub _USER {
		shift->command("USER",@_)->response()
	}
	
	sub _GETENV {
		my $ftp = shift;
		my ($ok, $name, $value);
		
		$ftp->command("GETENV",@_);
			while(length($ok = $ftp->response()) < 1) {
			my $line = $ftp->getline();
			unless (defined($value)) {
				chomp($line);
				($name, $value) = split(/\s+/, $line, 2);
			}
		}
		$ftp->debug_print(0, "getenv: $value\n")
		if $ftp->debug();
		return $value;
	}
	
	sub getenv {
		my $ftp = shift;
		my $name = shift;
		return $ftp->_GETENV($name);
	}
	
	sub _REBOOT {
		shift->command("REBOOT")->response() == Net::FTP::CMD_OK
	}
	
	sub reboot {
		my $ftp = shift;
		$ftp->_REBOOT;
		$ftp->close;
	}
}

my $file = shift @ARGV;
$file || exit 0;

open FILE, "<$file" or die "can't open firmware file\n";
my $ftp = ADAM2FTP->new($box, Debug => 0, Timeout => 600) or die "can't open control connection\n";
$ftp->login("adam2", "adam2") or die "can't login\n";

my $mtd0 = $ftp->getenv("mtd0");
my $mtd1 = $ftp->getenv("mtd1");
my ($ksize, $fssize);

$mtd1 =~ /^(0x\w+),(0x\w+)$/ and $ksize = hex($2) - hex($1);
$mtd0 =~ /^(0x\w+),(0x\w+)$/ and $fssize = hex($2) - hex($1);
$ksize and $fssize or die 'cannot read partition offsets';
printf STDERR "Available flash space: 0x%08x (0x%08x + 0x%08x)\n", $ksize + $fssize, $ksize, $fssize;

$ftp->command("MEDIA FLSH")->response();
$ftp->binary();
print STDERR "Writing to mtd1...\n";

my $dc = $ftp->stor("fs mtd1");
$dc or die "can't open data connection\n";
my $rbytes = 1;

while (($ksize > 0) and ($rbytes > 0)) {
	my $buffer;
	my $len = ($ksize > 1024 ? 1024 : $ksize);
	$rbytes = read FILE, $buffer, $len;
	$rbytes and $ksize -= $dc->write($buffer, $rbytes, 600);
}

$dc->close();
$rbytes or die "no more data left to write\n";

print STDERR "Writing to mtd0...\n";

$dc = $ftp->stor("fs mtd0");
$dc or die "can't open data connection\n";

while (($fssize > 0) and ($rbytes > 0)) {
	my $buffer;
	my $len = ($fssize > 1024 ? 1024 : $fssize);
	$rbytes = read FILE, $buffer, $len;
	$rbytes and $fssize -= $dc->write($buffer, $rbytes, 600);
}

$dc->close();
$ftp->reboot();