| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #!/bin/sh | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 2 | # ---------------------------------------------------------------------- | 
 | 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 Streefland | 532cf29 | 2010-10-23 00:02:44 +0200 | [diff] [blame] | 10 | # (c) 2009,2010 Dick Streefland <dick@streefland.net> | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 11 | # Licensed under the terms of the GNU General Public License. | 
 | 12 | # ---------------------------------------------------------------------- | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 14 | cf1='IKCFG_ST\037\213\010' | 
 | 15 | cf2='0123456789' | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 |  | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 17 | dump_config() | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 19 | 	if	pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"` | 
 | 20 | 	then | 
 | 21 | 		pos=${pos%%:*} | 
| Dick Streefland | 532cf29 | 2010-10-23 00:02:44 +0200 | [diff] [blame] | 22 | 		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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | 	fi | 
 | 29 | } | 
 | 30 |  | 
| Dick Streefland | 532cf29 | 2010-10-23 00:02:44 +0200 | [diff] [blame] | 31 | try_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 Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 41 | # Check invocation: | 
 | 42 | me=${0##*/} | 
 | 43 | img=$1 | 
 | 44 | if	[ $# -ne 1 -o ! -s "$img" ] | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | then | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 46 | 	echo "Usage: $me <kernel-image>" >&2 | 
 | 47 | 	exit 2 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | fi | 
 | 49 |  | 
| Dick Streefland | 532cf29 | 2010-10-23 00:02:44 +0200 | [diff] [blame] | 50 | # Prepare temp files: | 
 | 51 | tmp1=/tmp/ikconfig$$.1 | 
 | 52 | tmp2=/tmp/ikconfig$$.2 | 
 | 53 | trap "rm -f $tmp1 $tmp2" 0 | 
 | 54 |  | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 55 | # Initial attempt for uncompressed images or objects: | 
 | 56 | dump_config "$img" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  | 
| Dick Streefland | 532cf29 | 2010-10-23 00:02:44 +0200 | [diff] [blame] | 58 | # That didn't work, so retry after decompression. | 
 | 59 | try_decompress '\037\213\010' xy  gunzip | 
 | 60 | try_decompress 'BZh'          xy  bunzip2 | 
 | 61 | try_decompress '\135\0\0\0'   xxx unlzma | 
 | 62 | try_decompress '\211\114\132' xy  'lzop -d' | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
| Dick Streefland | 7b76bfc | 2009-10-06 22:35:40 +0200 | [diff] [blame] | 64 | # Bail out: | 
 | 65 | echo "$me: Cannot find kernel config." >&2 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | exit 1 |