https://bitbucket.org/hudson/magic-lantern
Raw File
Tip revision: 5e164a63a53ac66522e2ca02d01678b0d67d07bd authored by Trammell Hudson on 24 March 2011, 13:29:27 UTC
Allow enable-liveview to be toggled on and off
Tip revision: 5e164a6
fixup-crc
#!/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 );
}
back to top