blob: 37f30d36c944645988c722d81e854744a5a18f39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#!/bin/sh
Dick Streefland7b76bfc2009-10-06 22:35:40 +02002# ----------------------------------------------------------------------
3# extract-ikconfig - Extract the .config file from a kernel image
4#
5# This will only work when the kernel was compiled with CONFIG_IKCONFIG.
6#
7# The obscure use of the "tr" filter is to work around older versions of
8# "grep" that report the byte offset of the line instead of the pattern.
9#
10# (c) 2009, Dick Streefland <dick@streefland.net>
11# Licensed under the terms of the GNU General Public License.
12# ----------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Dick Streefland7b76bfc2009-10-06 22:35:40 +020014gz1='\037\213\010'
15gz2='01'
16cf1='IKCFG_ST\037\213\010'
17cf2='0123456789'
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Dick Streefland7b76bfc2009-10-06 22:35:40 +020019dump_config()
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Dick Streefland7b76bfc2009-10-06 22:35:40 +020021 if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
22 then
23 pos=${pos%%:*}
24 tail -c+$(($pos+8)) "$1" | zcat -q
25 exit 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 fi
27}
28
Dick Streefland7b76bfc2009-10-06 22:35:40 +020029# Check invocation:
30me=${0##*/}
31img=$1
32if [ $# -ne 1 -o ! -s "$img" ]
Linus Torvalds1da177e2005-04-16 15:20:36 -070033then
Dick Streefland7b76bfc2009-10-06 22:35:40 +020034 echo "Usage: $me <kernel-image>" >&2
35 exit 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070036fi
37
Dick Streefland7b76bfc2009-10-06 22:35:40 +020038# Initial attempt for uncompressed images or objects:
39dump_config "$img"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dick Streefland7b76bfc2009-10-06 22:35:40 +020041# That didn't work, so decompress and try again:
42tmp=/tmp/ikconfig$$
43trap "rm -f $tmp" 0
44for pos in `tr "$gz1\n$gz2" "\n$gz2=" < "$img" | grep -abo "^$gz2"`
45do
46 pos=${pos%%:*}
47 tail -c+$pos "$img" | zcat 2> /dev/null > $tmp
48 dump_config $tmp
49done
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Dick Streefland7b76bfc2009-10-06 22:35:40 +020051# Bail out:
52echo "$me: Cannot find kernel config." >&2
Linus Torvalds1da177e2005-04-16 15:20:36 -070053exit 1