Created
July 13, 2011 05:37
-
-
Save fkfk/1079781 to your computer and use it in GitHub Desktop.
Extact initramfs from zImage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Compress::Zlib; | |
sub get_unpackdata{ | |
my $infile = $_[0]; | |
my $code; | |
open IN, " < $infile "; | |
binmode(IN); | |
while(<IN>){ | |
$code .= unpack("H*",$_); | |
} | |
close IN; | |
return $code; | |
} | |
sub writefile{ | |
my ( $outfile, $data ) = @_; | |
open OUT, " > $outfile "; | |
print OUT $data; | |
close OUT; | |
} | |
sub get_kernel{ | |
my $code = $_[0]; | |
my $gz = get_gz($code); | |
return ( defined $gz ? Compress::Zlib::memGunzip(pack("H*",$gz)) : 1); | |
} | |
sub get_gz{ | |
my $code = $_[0]; | |
$code =~ /^.+?(1f8b08.+?)$/; | |
return $1; | |
} | |
sub get_cpio{ | |
my $code = $_[0]; | |
my $count = $_[1] ||= 0; | |
my $start = unpack("H*","070701"); | |
my $trailer = unpack("H*","TRAILER!!!"); | |
$code =~ /^.+?($start.+?$trailer)/; | |
if( defined $1 ){ | |
return pack("H*",$1); | |
} else { | |
$count++; | |
if( $count == 2){ | |
return 1; | |
} | |
get_cpio(get_gz($code),$count); | |
} | |
} | |
my $zImage = $ARGV[0] ||= ""; | |
if( -f $zImage ){ | |
my $kernel = get_kernel(get_unpackdata($zImage)); | |
die("Error!: can't find kernel image.\n") if( $kernel eq 1); | |
my $cpio = get_cpio(unpack("H*",$kernel)); | |
die("Error!: can't find initramfs image.\n") if( $cpio eq 1); | |
writefile("initramfs.cpio", $cpio); | |
} else { | |
print "Usage: ./mkinitramfs.pl zImage\n"; | |
} | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment