#!/usr/bin/perl

use strict;
use warnings;

use File::Slurp;
use String::CRC32;

my $filename = $ARGV[0];
die "no firmware filename given" if not defined $filename;

my $firmware = read_file($filename);

my $header2 = pack("C16", 0x00, 0x80, 0x00 x 14);

my $sum = crc32($header2 . $firmware) ^ 0xFFFFFFFF;
my $len = length($firmware) + 28;

my $header1 = pack("a4L2", 'LOAD', $len, $sum);

print unpack('H*', $header1 . $header2), "\n";
print join ' ', unpack('a4L2C16', $header1 . $header2), "\n";
print length($header1 . $header2), "\n";

write_file('belkin.' . $filename, $header1 . $header2 . $firmware);

__END__

=head1 NAME

belkinize.pl - Patch DD-WRT images for web upload to Belkin APs

=head1 SYNOPSIS

 belkinize.pl dd-wrt.v24_micro_generic.bin

=head1 DESCRIPTION

I<belkinize.pl> patches DD-WRT firmware images so that they can be used to 
reflash Belkin devices via the web interface.

=head1 ARGUMENTS

I<belkinize.pl> has the following options:

=over 4

=item B<filename>

Path to DD-WRT firmware image to patch.

=back

=head1 AUTHOR

 Jon Isbell <jon_no_spam_@isbell.net>

=cut

