-
Notifications
You must be signed in to change notification settings - Fork 0
/
itest
executable file
·85 lines (63 loc) · 1.8 KB
/
itest
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
#! /usr/bin/perl
use Getopt::Long;
sub usage;
sub num;
GetOptions(
'help' => sub { usage 0 },
'byte=s%' => \$opt_bytes,
'word=s%' => \$opt_words,
'dword=s%' => \$opt_dwords,
) || usage 1;
usage 1 if @ARGV != 2;
open(F, $ARGV[0]) || die "$ARGV[0]: $!\n"; sysread F, $prog, -s F; close F;
$prog_len = length $prog;
for (sort keys %{$opt_bytes}) {
$addr = num $_;
$val = num $opt_bytes->{$_};
die sprintf("0x%x", $addr) . ": larger than $ARGV[0]\n" if $addr >= $prog_len;
substr($prog, $addr, 1) = pack("C", $val);
printf "[0x%08x] = 0x%02x\n", $addr, $val;
}
for (sort keys %{$opt_words}) {
$addr = num $_;
$val = num $opt_words->{$_};
die sprintf("0x%x", $addr) . ": larger than $ARGV[0]\n" if $addr >= $prog_len - 1;
substr($prog, $addr, 2) = pack("v", $val);
printf "[0x%08x] = 0x%04x\n", $addr, $val;
}
for (sort keys %{$opt_dwords}) {
$addr = num $_;
$val = num $opt_dwords->{$_};
die sprintf("0x%x", $addr) . ": larger than $ARGV[0]\n" if $addr >= $prog_len - 3;
substr($prog, $addr, 4) = pack("V", $val);
printf "[0x%08x] = 0x%08x\n", $addr, $val;
}
open(F, "+<$ARGV[1]") || die "$ARGV[1]: $!\n";
$len = $prog_len < 0x1be ? $prog_len : 0x1be;
syswrite F, $prog, $len;
if($prog_len > 0x1fe) {
sysseek F, 0x1fe, 0;
syswrite F, substr($prog, 0x1fe), $prog_len - 0x1fe;
}
close F;
sub usage
{
my $err = shift;
if($err) {
print STDERR "Try 'itest --help' for more information.\n";
exit $err;
}
print <<" usage";
Usage: itest [OPTIONS] PROGRAM IMAGE
Copy PROGRAM to disk image IMAGE.
--byte ADDR=N Write N to ADDR in PROGRAM.
--word ADDR=N Write N to ADDR in PROGRAM.
--dword ADDR=N Write N to ADDR in PROGRAM.
--help Write this help text.
usage
exit $err;
}
sub num
{
$_[0] =~ /^0/ ? oct $_[0] : $_[0]
}