1 #!/bin/sh 2 # SPDX-License-Identifier: GPL-2.0-only 3 # ---------------------------------------------------------------------- 4 # extract-vmlinux - Extract uncompressed vmlinux from a kernel image 5 # 6 # Inspired from extract-ikconfig 7 # (c) 2009,2010 Dick Streefland <dick@streefland.net> 8 # 9 # (c) 2011 Corentin Chary <corentin.chary@gmail.com> 10 # 11 # ---------------------------------------------------------------------- 12 13 check_vmlinux() 14 { 15 # Use readelf to check if it's a valid ELF 16 # TODO: find a better to way to check that it's really vmlinux 17 # and not just an elf 18 readelf -h $1 > /dev/null 2>&1 19 if [ $? -ne 0 ]; then 20 # On ARM64, the kernel might be a PE file instead 21 # and output of file command might be something like following: 22 # $ file cuttlefish_assembly/vmlinux 23 # cuttlefish_assembly/vmlinux: MS-DOS executable PE32+ executable (EFI application) Aarch64 (stripped to external PDB), for MS Windows 24 # $ 25 file $1 | grep -q "MS-DOS executable" || return 1 26 fi 27 28 cat $1 29 exit 0 30 } 31 32 try_decompress() 33 { 34 # The obscure use of the "tr" filter is to work around older versions of 35 # "grep" that report the byte offset of the line instead of the pattern. 36 37 # Try to find the header ($1) and decompress from here 38 for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"` 39 do 40 pos=${pos%%:*} 41 tail -c+$pos "$img" | $3 > $tmp 2> /dev/null 42 check_vmlinux $tmp 43 done 44 } 45 46 # Check invocation: 47 me=${0##*/} 48 img=$1 49 if [ $# -ne 1 -o ! -s "$img" ] 50 then 51 echo "Usage: $me <kernel-image>" >&2 52 exit 2 53 fi 54 55 # Prepare temp files: 56 tmp=$(mktemp /tmp/vmlinux-XXX) 57 trap "rm -f $tmp" 0 58 59 # That didn't work, so retry after decompression. 60 try_decompress '\037\213\010' xy gunzip 61 try_decompress '\3757zXZ\000' abcde unxz 62 try_decompress 'BZh' xy bunzip2 63 try_decompress '\135\0\0\0' xxx unlzma 64 try_decompress '\211\114\132' xy 'lzop -d' 65 try_decompress '\004\"M\030' xxx 'lz4 -d' 66 try_decompress '\002!L\030' xxx 'lz4 -d' 67 try_decompress '(\265/\375' xxx unzstd 68 69 # Finally check for uncompressed images or objects: 70 check_vmlinux $img 71 72 # Bail out: 73 echo "$me: Cannot find vmlinux." >&2 74