Perl 对比boot.img并修正

今天翻了翻115网盘。。发现半年前写的东西居然在那,于是顺便放这里当个存档

今天看了下,发现有点看不懂了。。毕竟半年没写了。。

#!/usr/bin/perl
######################################################################
#
#   文件      : diffboot.pl
#   作者      : Michellgaby(vincent.18023962826@gmail.com)
#   说明      : 对比boot.img的参数并进行修正,输出check_boot.blk文件;
#
#   致谢      : 感谢&ROMDIYing老师,他对人类的工具化革命付出了伟大的贡献,
#               鄙人的本工具参考了他的代码,使本工具蓬荜生辉,实在令我感激
#               不尽,他乃一位功法大成者,对安卓的ROOT历史划上了崭新的一笔,
#               他所写的【batROMdiy】工具箱造福广大机油,使他们免受定制软件
#               的煎熬,让我们给&ROMDIYing掌声;(http://weibo.com/romdiying)
#
#   创建时间  : 2013年01月16日 -Michellgaby
#   使用方法  : diffboot.pl [boot.img] [flash.blk]
#
######################################################################

############################## 定义变量 ##############################
use strict;
use warnings;
my ($boot_file,$flash_file,$buffer,$ramdisk_size,$flash_correct,$count,$YN,$edit) = (undef,undef,undef,undef,undef,undef,undef,0);
my @boot  = ("boot_kernel_size", "boot_kernel_addr", "boot_ramdisk_addr", "boot_second_size", "boot_second_addr", "boot_tags_addr", "boot_page_size", "boot_unkown", "boot_name", "boot_cmdline");
my @flash = ("flash_kernel_size", "flash_kernel_addr", "flash_ramdisk_addr", "flash_second_size", "flash_second_addr", "flash_tags_addr", "flash_page_size", "flash_unkown", "flash_name", "flash_cmdline");
my @about = ("kernel  [size]", "kernel  [addr]", "ramdisk [addr]", "second  [size]", "second  [addr]", "tags    [addr]", "page    [size]","unkown  [addr]", "name    [char]", "cmdline [char]");
my @check = (1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
my @unkown= (1, 1, 1, 1, 1, 1, 1, 2, 4, 128);
########################### 检查输入的参数 ############################
if ($#ARGV == -1) { die "usage: diffboot.pl [boot.img] [flash.blk]\n"; }
if ($#ARGV == 0)  { die "usage: diffboot.pl [boot.img] [flash.blk]\n"; }
else { $boot_file = $ARGV[0]; $flash_file = $ARGV[1]; }

############################ boot.img #################################
#以只读模式打开文件,然后以二进制读取文件
open(INFILE, $boot_file) or die "Cannot open $boot_file: $!\n";
binmode INFILE;
#检测是否为boot.img文件
read(INFILE, $buffer, 8);
unless ($buffer eq "\x41\x4E\x44\x52\x4F\x49\x44\x21") { die "Unrecognised file format. Wrong identifier\n"; }
#获取boot信息
for ($count=0; $count < 10; $count++) {
  seek(INFILE, 4, 1) if ($count eq 2);
  read(INFILE, $boot[$count], $unkown[$count] * 4);
}
close INFILE;

############################ flash.blk ################################
#以只读模式打开文件,然后以二进制读取文件
open(INFILE, $flash_file) or die "Cannot open $flash_file: $!\n";
binmode INFILE;
#检测是否为boot.img文件
read(INFILE, $buffer, 8);
unless ($buffer eq "\x41\x4E\x44\x52\x4F\x49\x44\x21") { die "Unrecognised file format. Wrong identifier\n"; }
#获取flash信息
for ($count=0; $count < 10; $count++) {
  read(INFILE, $ramdisk_size, 4) if ($count eq 2);
  read(INFILE, $flash[$count], $unkown[$count] * 4);
}
$flash_correct = do { local $/; <INFILE> };
close INFILE;

############################## DIFF ###################################
#输出比较信息
unless ($boot[0] eq $flash[0]) { die "kernel size not the same\n" }
for ($count=1; $count < 10; $count++) { if ($boot[$count] eq $flash[$count] ) { $check[$count] = 1; } }
printf("%s[name]%s[boot]%s[flash]%s[Y/N]\n","-"x4,"-"x17,"-"x17,"-"x17);
for ($count=0; $count < 10; $count++) {
  if ($check[$count] eq 1) { $YN = "Yes"; } else { $YN = "No"; $edit = 1;}
  printf("%-15s %s 0x%08x %s 0x%08x %s [%s]\n",$about[$count]," "x8,unpack("V",$boot[$count])," "x12,unpack("V",$flash[$count])," "x13,$YN);
}
printf("%s\n","-"x79);
unless ($edit eq 1) { die "Check for the correct done!\n"; }

############################## EDIT ###################################
#修改boot参数
printf("%s and %s diff: press ENTER to continue...",$boot_file,$flash_file  );
$edit=<STDIN>;
open(OUTFILE, ">check_flash.blk") or die "Cannot open check_flash.blk: $!\n";
binmode OUTFILE;
printf OUTFILE "\x41\x4E\x44\x52\x4F\x49\x44\x21";
for ($count=0; $count < 10; $count++) {
  print OUTFILE $ramdisk_size if ($count eq 2);
  print OUTFILE $boot[$count];
}
print OUTFILE $flash_correct;
close OUTFILE;
printf("%s and %s diff: recreate check_flash.blk done!\n",$boot_file,$flash_file);

 

posted on 2013-08-21 14:14  GentlemanMod  阅读(381)  评论(0编辑  收藏  举报

导航