xref: /OK3568_Linux_fs/kernel/scripts/extract-ikconfig (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun# ----------------------------------------------------------------------
3*4882a593Smuzhiyun# extract-ikconfig - Extract the .config file from a kernel image
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun# This will only work when the kernel was compiled with CONFIG_IKCONFIG.
6*4882a593Smuzhiyun#
7*4882a593Smuzhiyun# The obscure use of the "tr" filter is to work around older versions of
8*4882a593Smuzhiyun# "grep" that report the byte offset of the line instead of the pattern.
9*4882a593Smuzhiyun#
10*4882a593Smuzhiyun# (c) 2009,2010 Dick Streefland <dick@streefland.net>
11*4882a593Smuzhiyun# Licensed under the terms of the GNU General Public License.
12*4882a593Smuzhiyun# ----------------------------------------------------------------------
13*4882a593Smuzhiyun
14*4882a593Smuzhiyuncf1='IKCFG_ST\037\213\010'
15*4882a593Smuzhiyuncf2='0123456789'
16*4882a593Smuzhiyun
17*4882a593Smuzhiyundump_config()
18*4882a593Smuzhiyun{
19*4882a593Smuzhiyun	if	pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
20*4882a593Smuzhiyun	then
21*4882a593Smuzhiyun		pos=${pos%%:*}
22*4882a593Smuzhiyun		tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
23*4882a593Smuzhiyun		if	[ $? != 1 ]
24*4882a593Smuzhiyun		then	# exit status must be 0 or 2 (trailing garbage warning)
25*4882a593Smuzhiyun			cat $tmp1
26*4882a593Smuzhiyun			exit 0
27*4882a593Smuzhiyun		fi
28*4882a593Smuzhiyun	fi
29*4882a593Smuzhiyun}
30*4882a593Smuzhiyun
31*4882a593Smuzhiyuntry_decompress()
32*4882a593Smuzhiyun{
33*4882a593Smuzhiyun	for	pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
34*4882a593Smuzhiyun	do
35*4882a593Smuzhiyun		pos=${pos%%:*}
36*4882a593Smuzhiyun		tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
37*4882a593Smuzhiyun		dump_config $tmp2
38*4882a593Smuzhiyun	done
39*4882a593Smuzhiyun}
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun# Check invocation:
42*4882a593Smuzhiyunme=${0##*/}
43*4882a593Smuzhiyunimg=$1
44*4882a593Smuzhiyunif	[ $# -ne 1 -o ! -s "$img" ]
45*4882a593Smuzhiyunthen
46*4882a593Smuzhiyun	echo "Usage: $me <kernel-image>" >&2
47*4882a593Smuzhiyun	exit 2
48*4882a593Smuzhiyunfi
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun# Prepare temp files:
51*4882a593Smuzhiyuntmp1=/tmp/ikconfig$$.1
52*4882a593Smuzhiyuntmp2=/tmp/ikconfig$$.2
53*4882a593Smuzhiyuntrap "rm -f $tmp1 $tmp2" 0
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun# Initial attempt for uncompressed images or objects:
56*4882a593Smuzhiyundump_config "$img"
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun# That didn't work, so retry after decompression.
59*4882a593Smuzhiyuntry_decompress '\037\213\010' xy    gunzip
60*4882a593Smuzhiyuntry_decompress '\3757zXZ\000' abcde unxz
61*4882a593Smuzhiyuntry_decompress 'BZh'          xy    bunzip2
62*4882a593Smuzhiyuntry_decompress '\135\0\0\0'   xxx   unlzma
63*4882a593Smuzhiyuntry_decompress '\211\114\132' xy    'lzop -d'
64*4882a593Smuzhiyuntry_decompress '\002\041\114\030' xyy 'lz4 -d -l'
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun# Bail out:
67*4882a593Smuzhiyunecho "$me: Cannot find kernel config." >&2
68*4882a593Smuzhiyunexit 1
69