^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # ----------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # extract-ikconfig - Extract the .config file from a kernel image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # This will only work when the kernel was compiled with CONFIG_IKCONFIG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # The obscure use of the "tr" filter is to work around older versions of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # "grep" that report the byte offset of the line instead of the pattern.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # (c) 2009,2010 Dick Streefland <dick@streefland.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # Licensed under the terms of the GNU General Public License.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # ----------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) cf1='IKCFG_ST\037\213\010'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) cf2='0123456789'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) dump_config()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) pos=${pos%%:*}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if [ $? != 1 ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) then # exit status must be 0 or 2 (trailing garbage warning)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) cat $tmp1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) try_decompress()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) pos=${pos%%:*}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) dump_config $tmp2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) # Check invocation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) me=${0##*/}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) img=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if [ $# -ne 1 -o ! -s "$img" ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) echo "Usage: $me <kernel-image>" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) exit 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # Prepare temp files:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) tmp1=/tmp/ikconfig$$.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) tmp2=/tmp/ikconfig$$.2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) trap "rm -f $tmp1 $tmp2" 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) # Initial attempt for uncompressed images or objects:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dump_config "$img"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) # That didn't work, so retry after decompression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) try_decompress '\037\213\010' xy gunzip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) try_decompress '\3757zXZ\000' abcde unxz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) try_decompress 'BZh' xy bunzip2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) try_decompress '\135\0\0\0' xxx unlzma
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) try_decompress '\211\114\132' xy 'lzop -d'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) try_decompress '\002\041\114\030' xyy 'lz4 -d -l'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) # Bail out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) echo "$me: Cannot find kernel config." >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) exit 1