Back to home page

LXR

 
 

    


0001 #!/bin/sh
0002 # ----------------------------------------------------------------------
0003 # extract-vmlinux - Extract uncompressed vmlinux from a kernel image
0004 #
0005 # Inspired from extract-ikconfig
0006 # (c) 2009,2010 Dick Streefland <dick@streefland.net>
0007 #
0008 # (c) 2011      Corentin Chary <corentin.chary@gmail.com>
0009 #
0010 # Licensed under the GNU General Public License, version 2 (GPLv2).
0011 # ----------------------------------------------------------------------
0012 
0013 check_vmlinux()
0014 {
0015         # Use readelf to check if it's a valid ELF
0016         # TODO: find a better to way to check that it's really vmlinux
0017         #       and not just an elf
0018         readelf -h $1 > /dev/null 2>&1 || return 1
0019 
0020         cat $1
0021         exit 0
0022 }
0023 
0024 try_decompress()
0025 {
0026         # The obscure use of the "tr" filter is to work around older versions of
0027         # "grep" that report the byte offset of the line instead of the pattern.
0028 
0029         # Try to find the header ($1) and decompress from here
0030         for     pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
0031         do
0032                 pos=${pos%%:*}
0033                 tail -c+$pos "$img" | $3 > $tmp 2> /dev/null
0034                 check_vmlinux $tmp
0035         done
0036 }
0037 
0038 # Check invocation:
0039 me=${0##*/}
0040 img=$1
0041 if      [ $# -ne 1 -o ! -s "$img" ]
0042 then
0043         echo "Usage: $me <kernel-image>" >&2
0044         exit 2
0045 fi
0046 
0047 # Prepare temp files:
0048 tmp=$(mktemp /tmp/vmlinux-XXX)
0049 trap "rm -f $tmp" 0
0050 
0051 # Initial attempt for uncompressed images or objects:
0052 check_vmlinux $img
0053 
0054 # That didn't work, so retry after decompression.
0055 try_decompress '\037\213\010' xy    gunzip
0056 try_decompress '\3757zXZ\000' abcde unxz
0057 try_decompress 'BZh'          xy    bunzip2
0058 try_decompress '\135\0\0\0'   xxx   unlzma
0059 try_decompress '\211\114\132' xy    'lzop -d'
0060 
0061 # Bail out:
0062 echo "$me: Cannot find vmlinux." >&2