xref: /linux/scripts/extract-ikconfig (revision 532cf2907ac3b9c2345d76251764f4f4e602c921)
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#
10*532cf290SDick 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%%:*}
22*532cf290SDick Streefland		tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
23*532cf290SDick Streefland		if	[ $? != 1 ]
24*532cf290SDick Streefland		then	# exit status must be 0 or 2 (trailing garbage warning)
25*532cf290SDick Streefland			cat $tmp1
267b76bfc8SDick Streefland			exit 0
277b76bfc8SDick Streefland		fi
28*532cf290SDick Streefland	fi
29*532cf290SDick Streefland}
30*532cf290SDick Streefland
31*532cf290SDick Streeflandtry_decompress()
32*532cf290SDick Streefland{
33*532cf290SDick Streefland	for	pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
34*532cf290SDick Streefland	do
35*532cf290SDick Streefland		pos=${pos%%:*}
36*532cf290SDick Streefland		tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
37*532cf290SDick Streefland		dump_config $tmp2
38*532cf290SDick 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
50*532cf290SDick Streefland# Prepare temp files:
51*532cf290SDick Streeflandtmp1=/tmp/ikconfig$$.1
52*532cf290SDick Streeflandtmp2=/tmp/ikconfig$$.2
53*532cf290SDick Streeflandtrap "rm -f $tmp1 $tmp2" 0
54*532cf290SDick Streefland
557b76bfc8SDick Streefland# Initial attempt for uncompressed images or objects:
567b76bfc8SDick Streeflanddump_config "$img"
571da177e4SLinus Torvalds
58*532cf290SDick Streefland# That didn't work, so retry after decompression.
59*532cf290SDick Streeflandtry_decompress '\037\213\010' xy  gunzip
60*532cf290SDick Streeflandtry_decompress 'BZh'          xy  bunzip2
61*532cf290SDick Streeflandtry_decompress '\135\0\0\0'   xxx unlzma
62*532cf290SDick Streeflandtry_decompress '\211\114\132' xy  'lzop -d'
631da177e4SLinus Torvalds
647b76bfc8SDick Streefland# Bail out:
657b76bfc8SDick Streeflandecho "$me: Cannot find kernel config." >&2
661da177e4SLinus Torvaldsexit 1
67