18c2ecf20Sopenharmony_ci#!/bin/sh
28c2ecf20Sopenharmony_ci# ----------------------------------------------------------------------
38c2ecf20Sopenharmony_ci# extract-ikconfig - Extract the .config file from a kernel image
48c2ecf20Sopenharmony_ci#
58c2ecf20Sopenharmony_ci# This will only work when the kernel was compiled with CONFIG_IKCONFIG.
68c2ecf20Sopenharmony_ci#
78c2ecf20Sopenharmony_ci# The obscure use of the "tr" filter is to work around older versions of
88c2ecf20Sopenharmony_ci# "grep" that report the byte offset of the line instead of the pattern.
98c2ecf20Sopenharmony_ci#
108c2ecf20Sopenharmony_ci# (c) 2009,2010 Dick Streefland <dick@streefland.net>
118c2ecf20Sopenharmony_ci# Licensed under the terms of the GNU General Public License.
128c2ecf20Sopenharmony_ci# ----------------------------------------------------------------------
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cicf1='IKCFG_ST\037\213\010'
158c2ecf20Sopenharmony_cicf2='0123456789'
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cidump_config()
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	if	pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
208c2ecf20Sopenharmony_ci	then
218c2ecf20Sopenharmony_ci		pos=${pos%%:*}
228c2ecf20Sopenharmony_ci		tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
238c2ecf20Sopenharmony_ci		if	[ $? != 1 ]
248c2ecf20Sopenharmony_ci		then	# exit status must be 0 or 2 (trailing garbage warning)
258c2ecf20Sopenharmony_ci			cat $tmp1
268c2ecf20Sopenharmony_ci			exit 0
278c2ecf20Sopenharmony_ci		fi
288c2ecf20Sopenharmony_ci	fi
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_citry_decompress()
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	for	pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
348c2ecf20Sopenharmony_ci	do
358c2ecf20Sopenharmony_ci		pos=${pos%%:*}
368c2ecf20Sopenharmony_ci		tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
378c2ecf20Sopenharmony_ci		dump_config $tmp2
388c2ecf20Sopenharmony_ci	done
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci# Check invocation:
428c2ecf20Sopenharmony_cime=${0##*/}
438c2ecf20Sopenharmony_ciimg=$1
448c2ecf20Sopenharmony_ciif	[ $# -ne 1 -o ! -s "$img" ]
458c2ecf20Sopenharmony_cithen
468c2ecf20Sopenharmony_ci	echo "Usage: $me <kernel-image>" >&2
478c2ecf20Sopenharmony_ci	exit 2
488c2ecf20Sopenharmony_cifi
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci# Prepare temp files:
518c2ecf20Sopenharmony_citmp1=/tmp/ikconfig$$.1
528c2ecf20Sopenharmony_citmp2=/tmp/ikconfig$$.2
538c2ecf20Sopenharmony_citrap "rm -f $tmp1 $tmp2" 0
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci# Initial attempt for uncompressed images or objects:
568c2ecf20Sopenharmony_cidump_config "$img"
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci# That didn't work, so retry after decompression.
598c2ecf20Sopenharmony_citry_decompress '\037\213\010' xy    gunzip
608c2ecf20Sopenharmony_citry_decompress '\3757zXZ\000' abcde unxz
618c2ecf20Sopenharmony_citry_decompress 'BZh'          xy    bunzip2
628c2ecf20Sopenharmony_citry_decompress '\135\0\0\0'   xxx   unlzma
638c2ecf20Sopenharmony_citry_decompress '\211\114\132' xy    'lzop -d'
648c2ecf20Sopenharmony_citry_decompress '\002\041\114\030' xyy 'lz4 -d -l'
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci# Bail out:
678c2ecf20Sopenharmony_ciecho "$me: Cannot find kernel config." >&2
688c2ecf20Sopenharmony_ciexit 1
69