blob: 3b42f255e2baa85738240c6bd847e4fe21a378f5 [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#
Dick Streefland532cf292010-10-23 00:02:44 +020010# (c) 2009,2010 Dick Streefland <dick@streefland.net>
Dick Streefland7b76bfc2009-10-06 22:35:40 +020011# 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 +020014cf1='IKCFG_ST\037\213\010'
15cf2='0123456789'
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Dick Streefland7b76bfc2009-10-06 22:35:40 +020017dump_config()
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Dick Streefland7b76bfc2009-10-06 22:35:40 +020019 if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
20 then
21 pos=${pos%%:*}
Dick Streefland532cf292010-10-23 00:02:44 +020022 tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
23 if [ $? != 1 ]
24 then # exit status must be 0 or 2 (trailing garbage warning)
25 cat $tmp1
26 exit 0
27 fi
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 fi
29}
30
Dick Streefland532cf292010-10-23 00:02:44 +020031try_decompress()
32{
33 for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
34 do
35 pos=${pos%%:*}
36 tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
37 dump_config $tmp2
38 done
39}
40
Dick Streefland7b76bfc2009-10-06 22:35:40 +020041# Check invocation:
42me=${0##*/}
43img=$1
44if [ $# -ne 1 -o ! -s "$img" ]
Linus Torvalds1da177e2005-04-16 15:20:36 -070045then
Dick Streefland7b76bfc2009-10-06 22:35:40 +020046 echo "Usage: $me <kernel-image>" >&2
47 exit 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070048fi
49
Dick Streefland532cf292010-10-23 00:02:44 +020050# Prepare temp files:
51tmp1=/tmp/ikconfig$$.1
52tmp2=/tmp/ikconfig$$.2
53trap "rm -f $tmp1 $tmp2" 0
54
Dick Streefland7b76bfc2009-10-06 22:35:40 +020055# Initial attempt for uncompressed images or objects:
56dump_config "$img"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dick Streefland532cf292010-10-23 00:02:44 +020058# That didn't work, so retry after decompression.
Dick Streeflandab94e462011-01-24 00:44:57 +010059try_decompress '\037\213\010' xy gunzip
60try_decompress '\3757zXZ\000' abcde unxz
61try_decompress 'BZh' xy bunzip2
62try_decompress '\135\0\0\0' xxx unlzma
63try_decompress '\211\114\132' xy 'lzop -d'
Alex Pilon99b2cdd2015-04-14 00:38:33 -040064try_decompress '\002\041\114\030' xyy 'lz4 -d -l'
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Dick Streefland7b76bfc2009-10-06 22:35:40 +020066# Bail out:
67echo "$me: Cannot find kernel config." >&2
Linus Torvalds1da177e2005-04-16 15:20:36 -070068exit 1