11da177e4SLinus Torvalds#!/bin/sh 2*7b76bfc8SDick Streefland# ---------------------------------------------------------------------- 3*7b76bfc8SDick Streefland# extract-ikconfig - Extract the .config file from a kernel image 4*7b76bfc8SDick Streefland# 5*7b76bfc8SDick Streefland# This will only work when the kernel was compiled with CONFIG_IKCONFIG. 6*7b76bfc8SDick Streefland# 7*7b76bfc8SDick Streefland# The obscure use of the "tr" filter is to work around older versions of 8*7b76bfc8SDick Streefland# "grep" that report the byte offset of the line instead of the pattern. 9*7b76bfc8SDick Streefland# 10*7b76bfc8SDick Streefland# (c) 2009, Dick Streefland <dick@streefland.net> 11*7b76bfc8SDick Streefland# Licensed under the terms of the GNU General Public License. 12*7b76bfc8SDick Streefland# ---------------------------------------------------------------------- 131da177e4SLinus Torvalds 14*7b76bfc8SDick Streeflandgz1='\037\213\010' 15*7b76bfc8SDick Streeflandgz2='01' 16*7b76bfc8SDick Streeflandcf1='IKCFG_ST\037\213\010' 17*7b76bfc8SDick Streeflandcf2='0123456789' 181da177e4SLinus Torvalds 19*7b76bfc8SDick Streeflanddump_config() 201da177e4SLinus Torvalds{ 21*7b76bfc8SDick Streefland if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"` 221da177e4SLinus Torvalds then 23*7b76bfc8SDick Streefland pos=${pos%%:*} 24*7b76bfc8SDick Streefland tail -c+$(($pos+8)) "$1" | zcat -q 25*7b76bfc8SDick Streefland exit 0 26*7b76bfc8SDick Streefland fi 27*7b76bfc8SDick Streefland} 28*7b76bfc8SDick Streefland 29*7b76bfc8SDick Streefland# Check invocation: 30*7b76bfc8SDick Streeflandme=${0##*/} 31*7b76bfc8SDick Streeflandimg=$1 32*7b76bfc8SDick Streeflandif [ $# -ne 1 -o ! -s "$img" ] 33*7b76bfc8SDick Streeflandthen 34*7b76bfc8SDick Streefland echo "Usage: $me <kernel-image>" >&2 35*7b76bfc8SDick Streefland exit 2 361da177e4SLinus Torvaldsfi 371da177e4SLinus Torvalds 38*7b76bfc8SDick Streefland# Initial attempt for uncompressed images or objects: 39*7b76bfc8SDick Streeflanddump_config "$img" 401da177e4SLinus Torvalds 41*7b76bfc8SDick Streefland# That didn't work, so decompress and try again: 42*7b76bfc8SDick Streeflandtmp=/tmp/ikconfig$$ 43*7b76bfc8SDick Streeflandtrap "rm -f $tmp" 0 44*7b76bfc8SDick Streeflandfor pos in `tr "$gz1\n$gz2" "\n$gz2=" < "$img" | grep -abo "^$gz2"` 45*7b76bfc8SDick Streeflanddo 46*7b76bfc8SDick Streefland pos=${pos%%:*} 47*7b76bfc8SDick Streefland tail -c+$pos "$img" | zcat 2> /dev/null > $tmp 48*7b76bfc8SDick Streefland dump_config $tmp 49*7b76bfc8SDick Streeflanddone 501da177e4SLinus Torvalds 51*7b76bfc8SDick Streefland# Bail out: 52*7b76bfc8SDick Streeflandecho "$me: Cannot find kernel config." >&2 531da177e4SLinus Torvaldsexit 1 54