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 13check_vmlinux() 14{ 15 if file "$1" | grep -q 'Linux kernel.*boot executable' || 16 readelf -h "$1" > /dev/null 2>&1 17 then 18 cat "$1" 19 exit 0 20 fi 21} 22 23try_decompress() 24{ 25 # The obscure use of the "tr" filter is to work around older versions of 26 # "grep" that report the byte offset of the line instead of the pattern. 27 28 # Try to find the header ($1) and decompress from here 29 for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"` 30 do 31 pos=${pos%%:*} 32 tail -c+$pos "$img" | $3 > $tmp 2> /dev/null 33 check_vmlinux $tmp 34 done 35} 36 37# Check invocation: 38me=${0##*/} 39img=$1 40if [ $# -ne 1 -o ! -s "$img" ] 41then 42 echo "Usage: $me <kernel-image>" >&2 43 exit 2 44fi 45 46# Prepare temp files: 47tmp=$(mktemp /tmp/vmlinux-XXX) 48trap "rm -f $tmp" 0 49 50# That didn't work, so retry after decompression. 51try_decompress '\037\213\010' xy gunzip 52try_decompress '\3757zXZ\000' abcde unxz 53try_decompress 'BZh' xy bunzip2 54try_decompress '\135\0\0\0' xxx unlzma 55try_decompress '\211\114\132' xy 'lzop -d' 56try_decompress '\002!L\030' xxx 'lz4 -d' 57try_decompress '(\265/\375' xxx unzstd 58 59# Finally check for uncompressed images or objects: 60check_vmlinux $img 61 62# Bail out: 63echo "$me: Cannot find vmlinux." >&2 64