forked from reticulatedpines/magiclantern_hg_02
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fixup-crc
executable file
·46 lines (40 loc) · 964 Bytes
/
fixup-crc
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
#!/usr/bin/perl
# Assemble a hacked firmware image for the 5D Mark 2 from
# the dumped images and the replacement user code
#
# (c) Trammell Hudson
#
use warnings;
use strict;
use Getopt::Long;
use File::Slurp;
#
# This is the Worst. Checksum algorithm. Ever.
#
# It has no security or ability to detect byte order
# errors. It is literally the sum of the bytes!
#
sub checksum
{
my $image = shift;
my $sum = 0;
for( my $i=0 ; $i<length $image ; $i ++ )
{
$sum += ord( substr( $image, $i, 1 ) );
}
return ~$sum;
}
for my $file (@ARGV)
{
my $image = read_file( $file )
or warn "$file: $!\n"
and next;
# Zero the CRC and recalc it
my $old_crc = unpack( "V", substr( $image, 0x20, 4 ) );
substr( $image, 0x20, 4 ) = chr(0) x 4;
my $crc = checksum( $image );
substr( $image, 0x20, 4 ) = pack( "V", $crc );
printf "$file: New CRC: %08x OLD %08x\n", $crc, $old_crc;
# Write out the file
write_file( $file, { binmode => ':raw' }, $image );
}