Skip to content

Commit

Permalink
another code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bgcngm committed Jan 21, 2013
1 parent d5a1a06 commit 4de5e00
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 95 deletions.
138 changes: 49 additions & 89 deletions repack-MT65xx.pl
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,33 @@

my $dir = getcwd;

my $version = "MTK-Tools by Bruno Martins\nMT65xx repack script (last update: 20-01-2013)\n";
my $version = "MTK-Tools by Bruno Martins\nMT65xx repack script (last update: 21-01-2013)\n";
my $usage = "repack-MT65xx.pl COMMAND [...]\n\nCOMMANDs are:\n\n -boot <kernel> <ramdisk-directory> <outfile>\n Repacks boot image\n\n -recovery <kernel> <ramdisk-directory> <outfile>\n Repacks recovery image\n\n -logo [--no_compression] <logo-directory> <outfile>\n Repacks logo image\n\n";

print colored ("$version", 'bold blue') . "\n";
die "Usage: $usage" unless $ARGV[0] && $ARGV[1] && $ARGV[2];

if ( $ARGV[0] eq "-boot" ) {
if ( $ARGV[0] eq "-boot" || $ARGV[0] eq "-recovery" ) {
die "Usage: $usage" unless $ARGV[3] && !$ARGV[4];
repack_boot("ROOTFS");
} elsif ( $ARGV[0] eq "-recovery" ) {
die "Usage: $usage" unless $ARGV[3] && !$ARGV[4];
repack_boot("RECOVERY");
repack_boot();
} elsif ( $ARGV[0] eq "-logo" ) {
if ( $ARGV[1] eq "--no_compression" ) {
die "Usage: $usage" unless $ARGV[2] && $ARGV[3] && !$ARGV[4];
repack_logo_uncompressed("LOGO");
}
else {
} else {
die "Usage: $usage" unless !$ARGV[3];
repack_logo("LOGO");
splice (@ARGV, 1, 0, "--compression");
}
shift (@ARGV);
repack_logo();
} else {
die "Usage: $usage";
}

sub repack_boot {
my $kernel = $ARGV[1];
my $ramdiskdir = $ARGV[2];
my $outfile = $ARGV[3];
my $signature = $_[0];
$ARGV[0] =~ s/-//;
my ($type, $kernel, $ramdiskdir, $outfile) = @ARGV;
$type =~ s/^-//;
$ramdiskdir =~ s/\/$//;
my $signature = ($type eq "boot" ? "ROOTFS" : "RECOVERY");

die colored ("Error: file '$kernel' not found", 'red') . "\n" unless ( -e $kernel );
chdir $ramdiskdir or die colored ("Error: directory '$ramdiskdir' not found", 'red') . "\n";
Expand All @@ -61,7 +57,7 @@ sub repack_boot {
die colored ("Error: $tool binary not found!", 'red') . "\n"
if system ("command -v $tool >/dev/null 2>&1");
}
print "Repacking $ARGV[0] image...\nRamdisk size: ";
print "Repacking $type image...\nRamdisk size: ";
system ("find . | cpio -o -H newc | gzip > $dir/ramdisk-repack.cpio.gz");

chdir $dir or die "\n$ramdiskdir $!";;
Expand Down Expand Up @@ -96,31 +92,35 @@ sub repack_boot {
unlink ("ramdisk-repack.cpio.gz") or die $!;
system ("rm new-ramdisk-repack.cpio.gz");

print "\nRepacked $ARGV[0] image into '$outfile'.\n";
print "\nRepacked $type image into '$outfile'.\n";
}

sub repack_logo {
my $logodir = $ARGV[1];
my $outfile = $ARGV[2];
my $signature = $_[0];
$ARGV[0] =~ s/-//;
my ($type, $logodir, $outfile) = @ARGV;
my ($logobin, $logo_length);
my (@raw_addr, @raw, @zlib_raw);
$logodir =~ s/\/$//;

chdir $logodir or die colored ("Error: directory '$logodir' not found", 'red') . "\n";
my $compression = ($type eq "--no_compression" ? 0 : 1);
my $filename = $logodir =~ s/-unpacked$//r;

my (@raw_addr, @zlib_raw) = ();
chdir $logodir or die colored ("Error: directory '$logodir' not found", 'red') . "\n";

my $i = 0;
print "Repacking $ARGV[0] image...\n";
printf ("Repacking logo image%s...\n", $compression ? "" : " (without compression)" );
for my $inputfile ( glob "./*.rgb565" ) {
open (INPUTFILE, "$inputfile") or die colored ("Error: could not open raw image '$inputfile'", 'red') . "\n";
my $input;
while (<INPUTFILE>) {
$input .= $_;
}
close INPUTFILE;
$raw[$i] = $input;

# deflate all rgb565 raw files found (compress zlib rfc1950)
$zlib_raw[$i] = compress($input,Z_BEST_COMPRESSION);
if ($compression) {
# deflate all rgb565 raw images (compress zlib rfc1950)
$zlib_raw[$i] = compress($raw[$i],Z_BEST_COMPRESSION);
}

$i++;
}
Expand All @@ -129,74 +129,34 @@ sub repack_logo {
chdir $dir or die "\n$logodir $!";;

my $num_blocks = $i;
print "Number of raw images found in the specified folder: $num_blocks\n";

my $logo_length = (4 + 4 + $num_blocks * 4);
# calculate the start address of each raw image and the new file size
for my $i (0 .. $num_blocks - 1) {
$raw_addr[$i] = $logo_length;
$logo_length += length($zlib_raw[$i]);
}

# generate logo header according to the logo size
my $logo_header = gen_header($signature, $logo_length);

my $logobin = pack('L L', $num_blocks, $logo_length);

for my $i (0 .. $num_blocks - 1) {
$logobin .= pack('L', $raw_addr[$i]);
}

for my $i (0 .. $num_blocks - 1) {
$logobin .= $zlib_raw[$i];
}

$logobin = $logo_header . $logobin;

# create the output file
open (LOGOFILE, ">$outfile");
binmode (LOGOFILE);
print LOGOFILE $logobin or die;
close (LOGOFILE);

print "\nRepacked $ARGV[0] image into '$outfile'.\n";
}

sub repack_logo_uncompressed {
my @raw;
my $logodir = $ARGV[2];
my $outfile = $ARGV[3];
my $signature = $_[0];
$ARGV[0] =~ s/-//;

chdir $logodir or die colored ("Error: directory '$logodir' not found", 'red') . "\n";

my $i = 0;
print "Repacking $ARGV[0] image (without compression)...\n";
for my $inputfile ( glob "./*.rgb565" ) {
open (INPUTFILE, "$inputfile") or die colored ("Error: could not open raw image '$inputfile'", 'red') . "\n";
while (<INPUTFILE>) {
$raw[$i] .= $_;
print "Number of images found inside the specified folder: $num_blocks\n";

if ($compression) {
$logo_length = (4 + 4 + $num_blocks * 4);
# calculate the start address of each raw image and the new file size
for my $i (0 .. $num_blocks - 1) {
$raw_addr[$i] = $logo_length;
$logo_length += length($zlib_raw[$i]);
}
close INPUTFILE;

$i++;
}
die colored ("Error: could not find any .rgb565 file under the specified directory '$logodir'", 'red') . "\n" unless $i > 0;

chdir $dir or die "\n$logodir $!";;
$logobin = pack('L L', $num_blocks, $logo_length);

my $num_blocks = $i;
print "Number of raw images found in the specified folder: $num_blocks\n";
for my $i (0 .. $num_blocks - 1) {
$logobin .= pack('L', $raw_addr[$i]);
}

my $logobin;
for my $i (0 .. $num_blocks - 1) {
$logobin .= $raw[$i];
for my $i (0 .. $num_blocks - 1) {
$logobin .= $zlib_raw[$i];
}
} else {
for my $i (0 .. $num_blocks - 1) {
$logobin .= $raw[$i];
}
$logo_length = length($logobin);
}

# generate logo header according to the logo size
my $logo_header = gen_header($signature, length($logobin));
my $logo_header = gen_header("LOGO", $logo_length);

$logobin = $logo_header . $logobin;

# create the output file
Expand All @@ -205,7 +165,7 @@ sub repack_logo_uncompressed {
print LOGOFILE $logobin or die;
close (LOGOFILE);

print "\nRepacked $ARGV[0] image into '$outfile'.\n";
print "\nRepacked logo image into '$outfile'.\n";
}

sub gen_header {
Expand Down
9 changes: 3 additions & 6 deletions unpack-MT65xx.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Term::ANSIColor;
use Scalar::Util qw(looks_like_number);

my $version = "MTK-Tools by Bruno Martins\nMT65xx unpack script (last update: 20-01-2013)\n";
my $version = "MTK-Tools by Bruno Martins\nMT65xx unpack script (last update: 21-01-2013)\n";
my $usage = "unpack-MT65xx.pl <infile> [COMMAND ...]\n Unpacks boot, recovery or logo image\n\nOptional COMMANDs are:\n\n -kernel_only\n Extract kernel only from boot or recovery image\n\n -ramdisk_only\n Extract ramdisk only from boot or recovery image\n\n -force_logo_res <width> <height>\n Forces logo image file to be unpacked by specifying image resolution,\n which must be entered in pixels\n (only useful when no zlib compressed images are found)\n\n";

print colored ("$version", 'bold blue') . "\n";
Expand Down Expand Up @@ -79,6 +79,7 @@ sub unpack_boot {
my ($bootimg, $extract) = @_;
my ($bootMagic, $kernelSize, $kernelLoadAddr, $ram1Size, $ram1LoadAddr, $ram2Size, $ram2LoadAddr, $tagsAddr, $pageSize, $unused1, $unused2, $bootName, $cmdLine, $id) = unpack('a8 L L L L L L L L L L a16 a512 a8', $bootimg);

# print input file information
print colored ("\nInput file information:\n", 'yellow') . "\n";
print " Kernel size: $kernelSize bytes / ";
printf ("load address: %#x\n", $kernelLoadAddr);
Expand All @@ -87,11 +88,7 @@ sub unpack_boot {
print " Second stage size: $ram2Size bytes / ";
printf ("load address: %#x\n", $ram2LoadAddr);
print " Page size: $pageSize bytes\n ASCIIZ product name: '$bootName'\n";
if ((substr($cmdLine, 0, 4) eq "\x00\x00\x00\x00")) {
print " Command line: (none)\n\n";
} else {
print " Command line: $cmdLine\n\n";
}
printf (" Command line: %s\n\n", substr($cmdLine, 0, 2) eq "\x00\x00" ? "(none)" : $cmdLine );

if ( $extract eq "kernel" || $extract eq "kernel and ramdisk" ) {
my($kernel) = substr($bootimg, $pageSize, $kernelSize);
Expand Down

0 comments on commit 4de5e00

Please sign in to comment.