{"type":"rich","version":"1.0","provider_name":"phorkie","provider_url":"https:\/\/p.cweiske.de\/","title":"split_bootimg.pl","author_name":"Christian Weiske","cache_age":86400,"width":900,"height":900,"html":"<!-- embedding all files of https:\/\/p.cweiske.de\/197 -->\n<link rel=\"stylesheet\" href=\"https:\/\/p.cweiske.de\/css\/embed.css\"\/>\n<div class=\"phork\" id=\"197\">\n    <div class=\"phork-file\">\n <div class=\"phork-content\">\n  <div class=\"code\"><pre class=\"txt\">copied from http:\/\/www.enck.org\/tools\/split_bootimg_pl.txt<\/pre><\/div>\n\n <\/div>\n <div class=\"phork-meta\">\n  <a href=\"https:\/\/p.cweiske.de\/197\/rev-raw\/2a3db1f76d8d99ed8eeadbad4fbd7e8987387213\/README.txt\" style=\"float: right\">view raw source<\/a>\n  <a href=\"https:\/\/p.cweiske.de\/197#README.txt\">README.txt<\/a>\n <\/div>\n<\/div>\n    <div class=\"phork-file\">\n <div class=\"phork-content\">\n  <div class=\"code\"><pre class=\"txt\">#!\/usr\/bin\/perl\r\n######################################################################\r\n#\r\n#   File          : split_bootimg.pl\r\n#   Author(s)     : William Enck &lt;enck@cse.psu.edu&gt;\r\n#   Description   : Split appart an Android boot image created \r\n#                   with mkbootimg. The format can be found in\r\n#                   android-src\/system\/core\/mkbootimg\/bootimg.h\r\n#\r\n#                   Thanks to alansj on xda-developers.com for \r\n#                   identifying the format in bootimg.h and \r\n#                   describing initial instructions for splitting\r\n#                   the boot.img file.\r\n#\r\n#   Last Modified : Tue Dec  2 23:36:25 EST 2008\r\n#   By            : William Enck &lt;enck@cse.psu.edu&gt;\r\n#\r\n#   Copyright (c) 2008 The Pennsylvania State University\r\n#   Systems and Internet Infrastructure Security Laboratory\r\n#\r\n#   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); \r\n#   you may not use this file except in compliance with the License. \r\n#   You may obtain a copy of the License at \r\n#\r\n#       http:\/\/www.apache.org\/licenses\/LICENSE-2.0 \r\n#\r\n#   Unless required by applicable law or agreed to in writing, software \r\n#   distributed under the License is distributed on an &quot;AS IS&quot; BASIS,\r\n#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n#   See the License for the specific language governing permissions and\r\n#   limitations under the License. \r\n#\r\n######################################################################\r\n\r\nuse strict;\r\nuse warnings;\r\n\r\n# Turn on print flushing\r\n$|++;\r\n\r\n######################################################################\r\n## Global Variables and Constants\r\n\r\nmy $SCRIPT = __FILE__;\r\nmy $IMAGE_FN = undef;\r\n\r\n# Constants (from bootimg.h)\r\nuse constant BOOT_MAGIC =&gt; 'ANDROID!';\r\nuse constant BOOT_MAGIC_SIZE =&gt; 8;\r\nuse constant BOOT_NAME_SIZE =&gt; 16;\r\nuse constant BOOT_ARGS_SIZE =&gt; 512;\r\n\r\n# Unsigned integers are 4 bytes\r\nuse constant UNSIGNED_SIZE =&gt; 4;\r\n\r\n# Parsed Values\r\nmy $PAGE_SIZE = undef;\r\nmy $KERNEL_SIZE = undef;\r\nmy $RAMDISK_SIZE = undef;\r\nmy $SECOND_SIZE = undef;\r\n\r\n######################################################################\r\n## Main Code\r\n\r\n&amp;parse_cmdline();\r\n&amp;parse_header($IMAGE_FN);\r\n\r\n=format (from bootimg.h)\r\n** +-----------------+\r\n** | boot header     | 1 page\r\n** +-----------------+\r\n** | kernel          | n pages\r\n** +-----------------+\r\n** | ramdisk         | m pages\r\n** +-----------------+\r\n** | second stage    | o pages\r\n** +-----------------+\r\n**\r\n** n = (kernel_size + page_size - 1) \/ page_size\r\n** m = (ramdisk_size + page_size - 1) \/ page_size\r\n** o = (second_size + page_size - 1) \/ page_size\r\n=cut\r\n\r\nmy $n = int(($KERNEL_SIZE + $PAGE_SIZE - 1) \/ $PAGE_SIZE);\r\nmy $m = int(($RAMDISK_SIZE + $PAGE_SIZE - 1) \/ $PAGE_SIZE);\r\nmy $o = int(($SECOND_SIZE + $PAGE_SIZE - 1) \/ $PAGE_SIZE);\r\n\r\nmy $k_offset = $PAGE_SIZE;\r\nmy $r_offset = $k_offset + ($n * $PAGE_SIZE);\r\nmy $s_offset = $r_offset + ($m * $PAGE_SIZE);\r\n\r\n(my $base = $IMAGE_FN) =~ s\/.*\\\/(.*)$\/$1\/;\r\nmy $k_file = $base . &quot;-kernel&quot;;\r\nmy $r_file = $base . &quot;-ramdisk.gz&quot;;\r\nmy $s_file = $base . &quot;-second.gz&quot;;\r\n\r\n# The kernel is always there\r\nprint &quot;Writing $k_file ...&quot;;\r\n&amp;dump_file($IMAGE_FN, $k_file, $k_offset, $KERNEL_SIZE);\r\nprint &quot; complete.\\n&quot;;\r\n\r\n# The ramdisk is always there\r\nprint &quot;Writing $r_file ...&quot;;\r\n&amp;dump_file($IMAGE_FN, $r_file, $r_offset, $RAMDISK_SIZE);\r\nprint &quot; complete.\\n&quot;;\r\n\r\n# The Second stage bootloader is optional\r\nunless ($SECOND_SIZE == 0) {\r\n    print &quot;Writing $s_file ...&quot;;\r\n    &amp;dump_file($IMAGE_FN, $s_file, $s_offset, $SECOND_SIZE);\r\n    print &quot; complete.\\n&quot;;\r\n}\r\n    \r\n######################################################################\r\n## Supporting Subroutines\r\n\r\n=header_format (from bootimg.h)\r\nstruct boot_img_hdr\r\n{\r\n    unsigned char magic[BOOT_MAGIC_SIZE];\r\n\r\n    unsigned kernel_size;  \/* size in bytes *\/\r\n    unsigned kernel_addr;  \/* physical load addr *\/\r\n\r\n    unsigned ramdisk_size; \/* size in bytes *\/\r\n    unsigned ramdisk_addr; \/* physical load addr *\/\r\n\r\n    unsigned second_size;  \/* size in bytes *\/\r\n    unsigned second_addr;  \/* physical load addr *\/\r\n\r\n    unsigned tags_addr;    \/* physical addr for kernel tags *\/\r\n    unsigned page_size;    \/* flash page size we assume *\/\r\n    unsigned unused[2];    \/* future expansion: should be 0 *\/\r\n\r\n    unsigned char name[BOOT_NAME_SIZE]; \/* asciiz product name *\/\r\n\r\n    unsigned char cmdline[BOOT_ARGS_SIZE];\r\n\r\n    unsigned id[8]; \/* timestamp \/ checksum \/ sha1 \/ etc *\/\r\n};\r\n=cut\r\nsub parse_header {\r\n    my ($fn) = @_;\r\n    my $buf = undef;\r\n\r\n    open INF, $fn or die &quot;Could not open $fn: $!\\n&quot;;\r\n    binmode INF;\r\n\r\n    # Read the Magic\r\n    read(INF, $buf, BOOT_MAGIC_SIZE);\r\n    unless ($buf eq BOOT_MAGIC) {\r\n\tdie &quot;Android Magic not found in $fn. Giving up.\\n&quot;;\r\n    }\r\n\r\n    # Read kernel size and address (assume little-endian)\r\n    read(INF, $buf, UNSIGNED_SIZE * 2);\r\n    my ($k_size, $k_addr) = unpack(&quot;VV&quot;, $buf);\r\n\r\n    # Read ramdisk size and address (assume little-endian)\r\n    read(INF, $buf, UNSIGNED_SIZE * 2);\r\n    my ($r_size, $r_addr) = unpack(&quot;VV&quot;, $buf);\r\n\r\n    # Read second size and address (assume little-endian)\r\n    read(INF, $buf, UNSIGNED_SIZE * 2);\r\n    my ($s_size, $s_addr) = unpack(&quot;VV&quot;, $buf);\r\n\r\n    # Ignore tags_addr\r\n    read(INF, $buf, UNSIGNED_SIZE);\r\n\r\n    # get the page size (assume little-endian)\r\n    read(INF, $buf, UNSIGNED_SIZE);\r\n    my ($p_size) = unpack(&quot;V&quot;, $buf);\r\n\r\n    # Ignore unused\r\n    read(INF, $buf, UNSIGNED_SIZE * 2);\r\n\r\n    # Read the name (board name)\r\n    read(INF, $buf, BOOT_NAME_SIZE);\r\n    my $name = $buf;\r\n\r\n    # Read the command line\r\n    read(INF, $buf, BOOT_ARGS_SIZE);\r\n    my $cmdline = $buf;\r\n\r\n    # Ignore the id\r\n    read(INF, $buf, UNSIGNED_SIZE * 8);\r\n\r\n    # Close the file\r\n    close INF;\r\n\r\n    # Print important values\r\n    printf &quot;Page size: %d (0x%08x)\\n&quot;, $p_size, $p_size;\r\n    printf &quot;Kernel size: %d (0x%08x)\\n&quot;, $k_size, $k_size;\r\n    printf &quot;Ramdisk size: %d (0x%08x)\\n&quot;, $r_size, $r_size;\r\n    printf &quot;Second size: %d (0x%08x)\\n&quot;, $s_size, $s_size;\r\n    printf &quot;Board name: $name\\n&quot;;\r\n    printf &quot;Command line: $cmdline\\n&quot;;\r\n\r\n    # Save the values\r\n    $PAGE_SIZE = $p_size;\r\n    $KERNEL_SIZE = $k_size;\r\n    $RAMDISK_SIZE = $r_size;\r\n    $SECOND_SIZE = $s_size;\r\n}\r\n\r\nsub dump_file {\r\n    my ($infn, $outfn, $offset, $size) = @_;\r\n    my $buf = undef;\r\n\r\n    open INF, $infn or die &quot;Could not open $infn: $!\\n&quot;;\r\n    open OUTF, &quot;&gt;$outfn&quot; or die &quot;Could not open $outfn: $!\\n&quot;;\r\n\r\n    binmode INF;\r\n    binmode OUTF;\r\n\r\n    seek(INF, $offset, 0) or die &quot;Could not seek in $infn: $!\\n&quot;;\r\n    read(INF, $buf, $size) or die &quot;Could not read $infn: $!\\n&quot;;\r\n    print OUTF $buf or die &quot;Could not write $outfn: $!\\n&quot;;\r\n\r\n    close INF;\r\n    close OUTF;\r\n}\r\n\r\n######################################################################\r\n## Configuration Subroutines\r\n\r\nsub parse_cmdline {\r\n    unless ($#ARGV == 0) {\r\n\tdie &quot;Usage: $SCRIPT boot.img\\n&quot;;\r\n    }\r\n    $IMAGE_FN = $ARGV[0];\r\n}\r\n\r\n\r\n<\/pre><\/div>\n\n <\/div>\n <div class=\"phork-meta\">\n  <a href=\"https:\/\/p.cweiske.de\/197\/rev-raw\/2a3db1f76d8d99ed8eeadbad4fbd7e8987387213\/split_bootimg.pl.txt\" style=\"float: right\">view raw source<\/a>\n  <a href=\"https:\/\/p.cweiske.de\/197#split_bootimg.pl.txt\">split_bootimg.pl.txt<\/a>\n <\/div>\n<\/div>\n<\/div>\n"}
