#!perl -w # (C) 2003-2007 Willem Jan Hengeveld # Web: http://www.xs4all.nl/~itsme/ # http://wiki.xda-developers.com/ # # $Id: fillwithrandom.pl 1502 2007-04-15 07:54:20Z itsme $ # use strict; use IO::File; use Getopt::Long; use Dumpvalue; my $d= new Dumpvalue; my $osromname; my $osromsavename; my @filestoerase; sub usage { return <<__EOF__ Usage: fillwithrandom [options] -ro osrom where to load the os image from -wo osrom where to write output image -f filename file to erase from rom __EOF__ } GetOptions( "ro=s" => \$osromname, "wo=s" => \$osromsavename, "f=s" => \@filestoerase, ) or die usage(); my $rng= IO::File->new("/dev/random", "r") or die "/dev/random: $!\n"; binmode $rng; my $dummyfilename= 0x80040000; my %rominfo; parsedumprom($osromname, \%rominfo); my $rom= LoadRom($osromname); for my $range (@{$rominfo{nuls}}) { MakeRandom(\$rom, $range); } for my $file (@filestoerase) { for my $range (@{$rominfo{data}{$file}}) { MakeRandom(\$rom, $range); } MakeDummyModent(\$rom, $rominfo{modents}{$file}); MakeDummyFilent(\$rom, $rominfo{filents}{$file}); MakeRandom(\$rom, $rominfo{names}{$file}); } SaveRom($osromsavename, $rom); exit(0); sub MakeRandom { my ($rom, $range)= @_; my $data; $rng->read($data, $range->{length}); substr($$rom, $range->{start}-0x80040000, $range->{length})= $data; } sub MakeDummyModent { my ($rom, $range)= @_; substr($$rom, $range->{start}-0x80040000, $range->{length})= pack("VVVVVVVV", 7, 0, 0, 0, $dummyfilename, 0, 0, 0); } sub MakeDummyFilent { my ($rom, $range)= @_; substr($$rom, $range->{start}-0x80040000, $range->{length})= pack("VVVVVVV", 7, 0, 0, 0, 0, $dummyfilename, 0); } sub LoadRom { my ($name)= @_; my $fh=IO::File->new($name, "r") or die "$name: $!\n"; binmode $fh; my $data; $fh->read($data, -s $fh); $fh->close(); return $data; } sub SaveRom { my ($name, $data)= @_; my $fh=IO::File->new($name, "w+") or die "$name: $!\n"; binmode $fh; $fh->write($data); $fh->close(); } sub parsedumprom { my ($romfile, $info)= @_; open FH, "dumprom $romfile|"; while () { s/\s+$//; if (/^(\w+)\s-\s\w+\sL(\w+)\sNUL$/) { push @{$info->{nuls}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\smodent\s+\w+\s\w{8}\s\w{16}\s+\w+\s+\w{8}\s(.*)/) { push @{$info->{modents}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\smodname\s(.*)/) { push @{$info->{names}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\se32\sstruct.*\sbase=\w+\sv\S+\s\w+\s(.*)/) { push @{$info->{data}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\so32\sstruct\s(.*)/) { push @{$info->{data}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\so32\sregion.*\sf=\w+\sfor\s(.*)/) { push @{$info->{data}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\sfilent\s+\w+\s\w{8}\s\w{16}\s+\w+\s+\w+\s\w{8}\s(.*)/) { push @{$info->{filents}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\sfilename\s(.*)/) { push @{$info->{names}{$3}}, {start=>hex($1), length=>hex($2)}; } elsif (/^(\w+)\s-\s\w+\sL(\w+)\sfiledata\s(.*)/) { push @{$info->{data}{$3}}, {start=>hex($1), length=>hex($2)}; } } close FH; }