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