#!perl -w # (C) 2003-2007 Willem Jan Hengeveld # Web: http://www.xs4all.nl/~itsme/ # http://wiki.xda-developers.com/ # # $Id: $ # # script to convert the SPL ( bootloader ) from the format stored in # the disk-on-chip, to a format usable for reverse engineering # # - in the diskonchip, every 512 byte sector is repeated for redundancy # use strict; use IO::File; use Getopt::Long; #splits a spl image ( every 512 byte block repeated ) sub usage { return <<__EOF__ Usage: splsplit [-d | -e] [-1] splimage splfile -1 -d : decode old pdocread format -d : decode raw doc format __EOF__ } my $do_encode= 0; my $do_split_16k= 0; GetOptions( "e"=>\$do_encode, "d"=>sub { $do_encode= 0; }, "1"=>\$do_split_16k, ) or die usage(); my $ifn= shift or die usage(); my $ifh= IO::File->new($ifn, "r") or die "open $ifn: $!\n"; binmode $ifh; my $ofn= shift or die usage(); if ($ifn eq $ofn) { die "input file is the same as the output file\n"; } my $ofh= IO::File->new($ofn, "w") or die "open $ofn: $!\n"; binmode $ofh; if ($do_split_16k) { if ($do_encode) { spl_encode_16k($ifh, $ofh); } else { spl_decode_16k($ifh, $ofh); } } else { if ($do_encode) { spl_encode($ifh, $ofh); } else { spl_decode($ifh, $ofh); } } $ofh->close(); $ifh->close(); sub spl_decode { my ($ifh, $ofh)= @_; my $i= 0; my @data=("", ""); my @ofs= (0, 0); while (!$ifh->eof()) { $ofs[$i&1]= $ifh->tell(); $ifh->read($data[$i&1], 512) or die "read: $!\n"; if ($i&1) { if ($data[0] ne $data[1]) { printf("difference at offset %08lx / %08lx\n", $ofs[0], $ofs[1]); } $ofh->write($data[0]); } $i++; } } sub spl_encode { my ($ifh, $ofh)= @_; my $data=""; my $ofs=0; while (!$ifh->eof()) { $ofs= $ifh->tell(); $ifh->read($data, 512) or die "read: $!\n"; $ofh->write($data); $ofh->write($data); } } sub spl_decode_16k { my ($ifh, $ofh)= @_; my $i= 1; my @data=("", ""); my @ofs= (0, 0); while (!$ifh->eof()) { $ofs[$i&1]= $ifh->tell(); $ifh->read($data[$i&1], 0x4000) or die "read: $!\n"; if ($i&1) { if ($i>1 && $data[0] ne $data[1]) { printf("difference at offset %08lx / %08lx\n", $ofs[0], $ofs[1]); } $ofh->write($data[1]); } $i++; } $ofh->write($data[0]); } sub spl_encode_16k { my ($ifh, $ofh)= @_; my $data=""; my $ofs=0; while (!$ifh->eof()) { $ofs= $ifh->tell(); $ifh->read($data, 0x4000) or die "read: $!\n"; $ofh->write($data) if ($ofs && !$ifh->eof()); $ofh->write($data); } }