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