xref: /linux/scripts/extract-ikconfig (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11da177e4SLinus Torvalds#!/bin/sh
27b76bfc8SDick Streefland# ----------------------------------------------------------------------
37b76bfc8SDick Streefland# extract-ikconfig - Extract the .config file from a kernel image
47b76bfc8SDick Streefland#
57b76bfc8SDick Streefland# This will only work when the kernel was compiled with CONFIG_IKCONFIG.
67b76bfc8SDick Streefland#
77b76bfc8SDick Streefland# The obscure use of the "tr" filter is to work around older versions of
87b76bfc8SDick Streefland# "grep" that report the byte offset of the line instead of the pattern.
97b76bfc8SDick Streefland#
10532cf290SDick Streefland# (c) 2009,2010 Dick Streefland <dick@streefland.net>
117b76bfc8SDick Streefland# Licensed under the terms of the GNU General Public License.
127b76bfc8SDick Streefland# ----------------------------------------------------------------------
131da177e4SLinus Torvalds
147b76bfc8SDick Streeflandcf1='IKCFG_ST\037\213\010'
157b76bfc8SDick Streeflandcf2='0123456789'
161da177e4SLinus Torvalds
177b76bfc8SDick Streeflanddump_config()
181da177e4SLinus Torvalds{
197b76bfc8SDick Streefland	if	pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
201da177e4SLinus Torvalds	then
217b76bfc8SDick Streefland		pos=${pos%%:*}
22532cf290SDick Streefland		tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
23532cf290SDick Streefland		if	[ $? != 1 ]
24532cf290SDick Streefland		then	# exit status must be 0 or 2 (trailing garbage warning)
25532cf290SDick Streefland			cat $tmp1
267b76bfc8SDick Streefland			exit 0
277b76bfc8SDick Streefland		fi
28532cf290SDick Streefland	fi
29532cf290SDick Streefland}
30532cf290SDick Streefland
31532cf290SDick Streeflandtry_decompress()
32532cf290SDick Streefland{
33532cf290SDick Streefland	for	pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
34532cf290SDick Streefland	do
35532cf290SDick Streefland		pos=${pos%%:*}
36532cf290SDick Streefland		tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
37532cf290SDick Streefland		dump_config $tmp2
38532cf290SDick Streefland	done
397b76bfc8SDick Streefland}
407b76bfc8SDick Streefland
417b76bfc8SDick Streefland# Check invocation:
427b76bfc8SDick Streeflandme=${0##*/}
437b76bfc8SDick Streeflandimg=$1
447b76bfc8SDick Streeflandif	[ $# -ne 1 -o ! -s "$img" ]
457b76bfc8SDick Streeflandthen
467b76bfc8SDick Streefland	echo "Usage: $me <kernel-image>" >&2
477b76bfc8SDick Streefland	exit 2
481da177e4SLinus Torvaldsfi
491da177e4SLinus Torvalds
50532cf290SDick Streefland# Prepare temp files:
51532cf290SDick Streeflandtmp1=/tmp/ikconfig$$.1
52532cf290SDick Streeflandtmp2=/tmp/ikconfig$$.2
53532cf290SDick Streeflandtrap "rm -f $tmp1 $tmp2" 0
54532cf290SDick Streefland
557b76bfc8SDick Streefland# Initial attempt for uncompressed images or objects:
567b76bfc8SDick Streeflanddump_config "$img"
571da177e4SLinus Torvalds
58532cf290SDick Streefland# That didn't work, so retry after decompression.
59532cf290SDick Streeflandtry_decompress '\037\213\010' xy    gunzip
60ab94e466SDick Streeflandtry_decompress '\3757zXZ\000' abcde unxz
61532cf290SDick Streeflandtry_decompress 'BZh'          xy    bunzip2
62532cf290SDick Streeflandtry_decompress '\135\0\0\0'   xxx   unlzma
63532cf290SDick Streeflandtry_decompress '\211\114\132' xy    'lzop -d'
6499b2cddeSAlex Pilontry_decompress '\002\041\114\030' xyy 'lz4 -d -l'
65*c11efc57SThitat Auareesuksakultry_decompress '\050\265\057\375' xxx unzstd
661da177e4SLinus Torvalds
677b76bfc8SDick Streefland# Bail out:
687b76bfc8SDick Streeflandecho "$me: Cannot find kernel config." >&2
691da177e4SLinus Torvaldsexit 1
70