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