Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # ----------------------------------------------------------------------
0003 # extract-ikconfig - Extract the .config file from a kernel image
0004 #
0005 # This will only work when the kernel was compiled with CONFIG_IKCONFIG.
0006 #
0007 # The obscure use of the "tr" filter is to work around older versions of
0008 # "grep" that report the byte offset of the line instead of the pattern.
0009 #
0010 # (c) 2009,2010 Dick Streefland <dick@streefland.net>
0011 # Licensed under the terms of the GNU General Public License.
0012 # ----------------------------------------------------------------------
0013 
0014 cf1='IKCFG_ST\037\213\010'
0015 cf2='0123456789'
0016 
0017 dump_config()
0018 {
0019         if      pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
0020         then
0021                 pos=${pos%%:*}
0022                 tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
0023                 if      [ $? != 1 ]
0024                 then    # exit status must be 0 or 2 (trailing garbage warning)
0025                         cat $tmp1
0026                         exit 0
0027                 fi
0028         fi
0029 }
0030 
0031 try_decompress()
0032 {
0033         for     pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
0034         do
0035                 pos=${pos%%:*}
0036                 tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
0037                 dump_config $tmp2
0038         done
0039 }
0040 
0041 # Check invocation:
0042 me=${0##*/}
0043 img=$1
0044 if      [ $# -ne 1 -o ! -s "$img" ]
0045 then
0046         echo "Usage: $me <kernel-image>" >&2
0047         exit 2
0048 fi
0049 
0050 # Prepare temp files:
0051 tmp1=/tmp/ikconfig$$.1
0052 tmp2=/tmp/ikconfig$$.2
0053 trap "rm -f $tmp1 $tmp2" 0
0054 
0055 # Initial attempt for uncompressed images or objects:
0056 dump_config "$img"
0057 
0058 # That didn't work, so retry after decompression.
0059 try_decompress '\037\213\010' xy    gunzip
0060 try_decompress '\3757zXZ\000' abcde unxz
0061 try_decompress 'BZh'          xy    bunzip2
0062 try_decompress '\135\0\0\0'   xxx   unlzma
0063 try_decompress '\211\114\132' xy    'lzop -d'
0064 try_decompress '\002\041\114\030' xyy 'lz4 -d -l'
0065 try_decompress '\050\265\057\375' xxx unzstd
0066 
0067 # Bail out:
0068 echo "$me: Cannot find kernel config." >&2
0069 exit 1