Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2441981
  • 博文数量: 293
  • 博客积分: 2660
  • 博客等级: 少校
  • 技术积分: 3632
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 17:50
文章分类

全部博文(293)

文章存档

2015年(13)

2014年(58)

2013年(73)

2012年(25)

2011年(30)

2010年(86)

2009年(8)

分类: Android平台

2014-03-30 10:57:49


1. 建立boot_tools文件夹。
2. 把两个工具split.pl和mkbootimg放到这个文件夹下。
3. 准备好需要解压的boot.img放到任意目录
4. 打开命令行,在boot_tools下执行./split.pl /存放boot.img的目录/boot.img,会生成boot.img-kernel和boot.img-ramdisk.gz
例:
lishengjie@lishengjie:~/my-work-space/linux_tools/boot_zip$ ./split.pl boot.img Page size: 2048 (0x00000800)
Kernel size: 5744372 (0x0057a6f4)
Ramdisk size: 436341 (0x0006a875)
Second size: 0 (0x00000000)
Board name: 
Command line: mem=239M console=ttyS1,115200n8 init=/init mtdparts=sprd-nand:384k@256k(2ndbl),256k(params),256k(pt),10m(boot),10m(recovery),120m(system),60m(sps),10m(factory),2m(cache),256k(misc),20m(fota),20m(cp),-(userdata)
Writing boot.img-kernel ... complete.
Writing boot.img-ramdisk.gz ... complete.
5. 执行gzip -dc boot.img-ramdisk.gz | cpio -i,解压boot.img-ramdisk.gz,会在boot_tools文件夹内解压出很多文件
例:
lishengjie@lishengjie:~/my-work-space/linux_tools/boot_zip$ gzip -dc boot.img-ramdisk.gz | cpio -i
1231 块
6. 把这些文件剪切到一个新文件夹里。如:/boot_tools/new/
7. 进入新文件夹new,修改init.rc文件
8. 执行find . | cpio -o -H newc | gzip > ../newramdisk.cpio.gz 将这些文件重新打包,在文件夹外层生成newramdisk.cpio.gz
例:
lishengjie@lishengjie:~/my-work-space/linux_tools/boot_zip/new$ find . | cpio -o -H newc | gzip > ../newramdisk.cpio.gz
1231 块
9. 在boot_tools文件夹下执行./mkbootimg --cmdline 'no_console_suspend=1 console=null' --kernel boot.img-kernel --ramdisk newramdisk.cpio.gz -o mynewimage.img,重新打包出img文件:mynewimage.img。
例:
lishengjie@lishengjie:~/my-work-space/linux_tools/boot_zip$ ./mkbootimg --cmdline 'no_console_suspend=1 console=null' --kernel boot.img-kernel --ramdisk newramdisk.cpio.gz -o mynewimage.img
10.将mynewimage.img改名为boot.img即可。
------通过以上步骤完成boot.img的解包,修改,圧包操作!!!
附split.pl源码:

点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. ######################################################################
  3. #
  4. # File : split_bootimg.pl
  5. # Author(s) : William Enck <enck@cse.psu.edu>
  6. # Description : Split appart an Android boot image created
  7. # with mkbootimg. The format can be found in
  8. # android-src/system/core/mkbootimg/bootimg.h
  9. #
  10. # Thanks to alansj on xda-developers.com for
  11. # identifying the format in bootimg.h and
  12. # describing initial instructions for splitting
  13. # the boot.img file.
  14. #
  15. # Last Modified : Tue Dec 2 23:36:25 EST 2008
  16. # By : William Enck <enck@cse.psu.edu>
  17. #
  18. # Copyright (c) 2008 William Enck
  19. #
  20. ######################################################################

  21. use strict;
  22. use warnings;

  23. # Turn on print flushing
  24. $|++;

  25. ######################################################################
  26. ## Global Variables and Constants

  27. my $SCRIPT = __FILE__;
  28. my $IMAGE_FN = undef;

  29. # Constants (from bootimg.h)
  30. use constant BOOT_MAGIC => 'ANDROID!';
  31. use constant BOOT_MAGIC_SIZE => 8;
  32. use constant BOOT_NAME_SIZE => 16;
  33. use constant BOOT_ARGS_SIZE => 512;

  34. # Unsigned integers are 4 bytes
  35. use constant UNSIGNED_SIZE => 4;

  36. # Parsed Values
  37. my $PAGE_SIZE = undef;
  38. my $KERNEL_SIZE = undef;
  39. my $RAMDISK_SIZE = undef;
  40. my $SECOND_SIZE = undef;

  41. ######################################################################
  42. ## Main Code

  43. &parse_cmdline();
  44. &parse_header($IMAGE_FN);

  45. =format (from bootimg.h)
  46. ** +-----------------+
  47. ** | boot header | 1 page
  48. ** +-----------------+
  49. ** | kernel | n pages
  50. ** +-----------------+
  51. ** | ramdisk | m pages
  52. ** +-----------------+
  53. ** | second stage | o pages
  54. ** +-----------------+
  55. **
  56. ** n = (kernel_size + page_size - 1) / page_size
  57. ** m = (ramdisk_size + page_size - 1) / page_size
  58. ** o = (second_size + page_size - 1) / page_size
  59. =cut

  60. my $n = int(($KERNEL_SIZE + $PAGE_SIZE - 1) / $PAGE_SIZE);
  61. my $m = int(($RAMDISK_SIZE + $PAGE_SIZE - 1) / $PAGE_SIZE);
  62. my $o = int(($SECOND_SIZE + $PAGE_SIZE - 1) / $PAGE_SIZE);

  63. my $k_offset = $PAGE_SIZE;
  64. my $r_offset = $k_offset + ($n * $PAGE_SIZE);
  65. my $s_offset = $r_offset + ($m * $PAGE_SIZE);

  66. (my $base = $IMAGE_FN) =~ s/.*\/(.*)$/$1/;
  67. my $k_file = $base . "-kernel";
  68. my $r_file = $base . "-ramdisk.gz";
  69. my $s_file = $base . "-second.gz";

  70. # The kernel is always there
  71. print "Writing $k_file ...";
  72. &dump_file($IMAGE_FN, $k_file, $k_offset, $KERNEL_SIZE);
  73. print " complete.\n";

  74. # The ramdisk is always there
  75. print "Writing $r_file ...";
  76. &dump_file($IMAGE_FN, $r_file, $r_offset, $RAMDISK_SIZE);
  77. print " complete.\n";

  78. # The Second stage bootloader is optional
  79. unless ($SECOND_SIZE == 0) {
  80.     print "Writing $s_file ...";
  81.     &dump_file($IMAGE_FN, $s_file, $s_offset, $SECOND_SIZE);
  82.     print " complete.\n";
  83. }
  84.     
  85. ######################################################################
  86. ## Supporting Subroutines

  87. =header_format (from bootimg.h)
  88. struct boot_img_hdr
  89. {
  90.     unsigned char magic[BOOT_MAGIC_SIZE];

  91.     unsigned kernel_size; /* size in bytes */
  92.     unsigned kernel_addr; /* physical load addr */

  93.     unsigned ramdisk_size; /* size in bytes */
  94.     unsigned ramdisk_addr; /* physical load addr */

  95.     unsigned second_size; /* size in bytes */
  96.     unsigned second_addr; /* physical load addr */

  97.     unsigned tags_addr; /* physical addr for kernel tags */
  98.     unsigned page_size; /* flash page size we assume */
  99.     unsigned unused[2]; /* future expansion: should be 0 */

  100.     unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */

  101.     unsigned char cmdline[BOOT_ARGS_SIZE];

  102.     unsigned id[8]; /* timestamp / checksum / sha1 / etc */
  103. };
  104. =cut
  105. sub parse_header {
  106.     my ($fn) = @_;
  107.     my $buf = undef;

  108.     open INF, $fn or die "Could not open $fn: $!\n";
  109.     binmode INF;

  110.     # Read the Magic
  111.     read(INF, $buf, BOOT_MAGIC_SIZE);
  112.     unless ($buf eq BOOT_MAGIC) {
  113.     die "Android Magic not found in $fn. Giving up.\n";
  114.     }

  115.     # Read kernel size and address (assume little-endian)
  116.     read(INF, $buf, UNSIGNED_SIZE * 2);
  117.     my ($k_size, $k_addr) = unpack("VV", $buf);

  118.     # Read ramdisk size and address (assume little-endian)
  119.     read(INF, $buf, UNSIGNED_SIZE * 2);
  120.     my ($r_size, $r_addr) = unpack("VV", $buf);

  121.     # Read second size and address (assume little-endian)
  122.     read(INF, $buf, UNSIGNED_SIZE * 2);
  123.     my ($s_size, $s_addr) = unpack("VV", $buf);

  124.     # Ignore tags_addr
  125.     read(INF, $buf, UNSIGNED_SIZE);

  126.     # get the page size (assume little-endian)
  127.     read(INF, $buf, UNSIGNED_SIZE);
  128.     my ($p_size) = unpack("V", $buf);

  129.     # Ignore unused
  130.     read(INF, $buf, UNSIGNED_SIZE * 2);

  131.     # Read the name (board name)
  132.     read(INF, $buf, BOOT_NAME_SIZE);
  133.     my $name = $buf;

  134.     # Read the command line
  135.     read(INF, $buf, BOOT_ARGS_SIZE);
  136.     my $cmdline = $buf;

  137.     # Ignore the id
  138.     read(INF, $buf, UNSIGNED_SIZE * 8);

  139.     # Close the file
  140.     close INF;

  141.     # Print important values
  142.     printf "Page size: %d (0x%08x)\n", $p_size, $p_size;
  143.     printf "Kernel size: %d (0x%08x)\n", $k_size, $k_size;
  144.     printf "Ramdisk size: %d (0x%08x)\n", $r_size, $r_size;
  145.     printf "Second size: %d (0x%08x)\n", $s_size, $s_size;
  146.     printf "Board name: $name\n";
  147.     printf "Command line: $cmdline\n";

  148.     # Save the values
  149.     $PAGE_SIZE = $p_size;
  150.     $KERNEL_SIZE = $k_size;
  151.     $RAMDISK_SIZE = $r_size;
  152.     $SECOND_SIZE = $s_size;
  153. }

  154. sub dump_file {
  155.     my ($infn, $outfn, $offset, $size) = @_;
  156.     my $buf = undef;

  157.     open INF, $infn or die "Could not open $infn: $!\n";
  158.     open OUTF, ">$outfn" or die "Could not open $outfn: $!\n";

  159.     binmode INF;
  160.     binmode OUTF;

  161.     seek(INF, $offset, 0) or die "Could not seek in $infn: $!\n";
  162.     read(INF, $buf, $size) or die "Could not read $infn: $!\n";
  163.     print OUTF $buf or die "Could not write $outfn: $!\n";

  164.     close INF;
  165.     close OUTF;
  166. }

  167. ######################################################################
  168. ## Configuration Subroutines

  169. sub parse_cmdline {
  170.     unless ($#ARGV == 0) {
  171.     die "Usage: $SCRIPT boot.img\n";
  172.     }
  173.     $IMAGE_FN = $ARGV[0];
  174. }


阅读(8741) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~