Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 # ----------------------------------------------------------------------
0004 # extract-vmlinux - Extract uncompressed vmlinux from a kernel image
0005 #
0006 # Inspired from extract-ikconfig
0007 # (c) 2009,2010 Dick Streefland <dick@streefland.net>
0008 #
0009 # (c) 2011      Corentin Chary <corentin.chary@gmail.com>
0010 #
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 # That didn't work, so retry after decompression.
0052 try_decompress '\037\213\010' xy    gunzip
0053 try_decompress '\3757zXZ\000' abcde unxz
0054 try_decompress 'BZh'          xy    bunzip2
0055 try_decompress '\135\0\0\0'   xxx   unlzma
0056 try_decompress '\211\114\132' xy    'lzop -d'
0057 try_decompress '\002!L\030'   xxx   'lz4 -d'
0058 try_decompress '(\265/\375'   xxx   unzstd
0059 
0060 # Finally check for uncompressed images or objects:
0061 check_vmlinux $img
0062 
0063 # Bail out:
0064 echo "$me: Cannot find vmlinux." >&2