Skip to content

Commit 7b76bfc

Browse files
streefrostedt
authored andcommitted
kconfig: simplification of scripts/extract-ikconfig
I've rewritten the extract-ikconfig script to extract the kernel configuration from a kernel compiled with CONFIG_IKCONFIG. The main motivation for the rewrite was to remove the dependency on the external C program binoffset.c, which is compiled on the initial run. The binoffset executable is invoked with a relative path, which means that the old script can only be run from the top of the kernel tree, and only when you have write permission in the scripts directory. The new script uses tr/grep/tail/zcat only, and can be invoked from anywhere. The binoffset.c program has been removed. This script requires GNU grep 2.5 (released 2002-03-13) or higher, because the -o option was introduced in that version. Signed-off-by: Dick Streefland <dick@streefland.net> LKML-Reference: <20091006203540.GA14634@streefland.net> Tested-by: Steven Rostedt <rostedt@goodmis.org> Cc: Sam Ravnborg <sam@ravnborg.org> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 0eca52a commit 7b76bfc

4 files changed

Lines changed: 44 additions & 248 deletions

File tree

‎Documentation/dontdiff‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ autoconf.h*
6868
bbootsect
6969
bin2c
7070
binkernel.spec
71-
binoffset
7271
bootsect
7372
bounds.h
7473
bsetup

‎scripts/.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ kallsyms
66
pnmtologo
77
bin2c
88
unifdef
9-
binoffset
109
ihex2fw

‎scripts/binoffset.c‎

Lines changed: 0 additions & 163 deletions
This file was deleted.

‎scripts/extract-ikconfig‎

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,53 @@
11
#!/bin/sh
2-
# extracts .config info from a [b]zImage file
3-
# uses: binoffset (new), dd, zcat, strings, grep
4-
# $arg1 is [b]zImage filename
5-
6-
binoffset="./scripts/binoffset"
7-
test -e $binoffset || cc -o $binoffset ./scripts/binoffset.c || exit 1
8-
9-
IKCFG_ST="0x49 0x4b 0x43 0x46 0x47 0x5f 0x53 0x54"
10-
IKCFG_ED="0x49 0x4b 0x43 0x46 0x47 0x5f 0x45 0x44"
11-
dump_config() {
12-
file="$1"
13-
14-
start=`$binoffset $file $IKCFG_ST 2>/dev/null`
15-
[ "$?" != "0" ] && start="-1"
16-
if [ "$start" -eq "-1" ]; then
17-
return
18-
fi
19-
end=`$binoffset $file $IKCFG_ED 2>/dev/null`
20-
[ "$?" != "0" ] && end="-1"
21-
if [ "$end" -eq "-1" ]; then
22-
return
23-
fi
24-
25-
start=`expr $start + 8`
26-
size=`expr $end - $start`
27-
28-
dd if="$file" ibs=1 skip="$start" count="$size" 2>/dev/null | zcat
29-
30-
clean_up
31-
exit 0
32-
}
33-
34-
35-
usage()
36-
{
37-
echo " usage: extract-ikconfig [b]zImage_filename"
38-
}
39-
40-
clean_up()
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+
#
10+
# (c) 2009, Dick Streefland <dick@streefland.net>
11+
# Licensed under the terms of the GNU General Public License.
12+
# ----------------------------------------------------------------------
13+
14+
gz1='\037\213\010'
15+
gz2='01'
16+
cf1='IKCFG_ST\037\213\010'
17+
cf2='0123456789'
18+
19+
dump_config()
4120
{
42-
if [ "$TMPFILE" != "" ]; then
43-
rm -f $TMPFILE
21+
if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
22+
then
23+
pos=${pos%%:*}
24+
tail -c+$(($pos+8)) "$1" | zcat -q
25+
exit 0
4426
fi
4527
}
4628

47-
if [ $# -lt 1 ]
29+
# Check invocation:
30+
me=${0##*/}
31+
img=$1
32+
if [ $# -ne 1 -o ! -s "$img" ]
4833
then
49-
usage
50-
exit 1
34+
echo "Usage: $me <kernel-image>" >&2
35+
exit 2
5136
fi
5237

53-
TMPFILE=`mktemp -t ikconfig-XXXXXX` || exit 1
54-
image="$1"
55-
56-
# vmlinux: Attempt to dump the configuration from the file directly
57-
dump_config "$image"
58-
59-
GZHDR1="0x1f 0x8b 0x08 0x00"
60-
GZHDR2="0x1f 0x8b 0x08 0x08"
61-
62-
ELFHDR="0x7f 0x45 0x4c 0x46"
63-
64-
# vmlinux.gz: Check for a compressed images
65-
off=`$binoffset "$image" $GZHDR1 2>/dev/null`
66-
[ "$?" != "0" ] && off="-1"
67-
if [ "$off" -eq "-1" ]; then
68-
off=`$binoffset "$image" $GZHDR2 2>/dev/null`
69-
[ "$?" != "0" ] && off="-1"
70-
fi
71-
if [ "$off" -eq "0" ]; then
72-
zcat <"$image" >"$TMPFILE"
73-
dump_config "$TMPFILE"
74-
elif [ "$off" -ne "-1" ]; then
75-
(dd ibs="$off" skip=1 count=0 && dd bs=512k) <"$image" 2>/dev/null | \
76-
zcat >"$TMPFILE"
77-
dump_config "$TMPFILE"
78-
79-
# check if this is simply an ELF file
80-
else
81-
off=`$binoffset "$image" $ELFHDR 2>/dev/null`
82-
[ "$?" != "0" ] && off="-1"
83-
if [ "$off" -eq "0" ]; then
84-
dump_config "$image"
85-
fi
86-
fi
87-
88-
echo "ERROR: Unable to extract kernel configuration information."
89-
echo " This kernel image may not have the config info."
90-
91-
clean_up
38+
# Initial attempt for uncompressed images or objects:
39+
dump_config "$img"
40+
41+
# That didn't work, so decompress and try again:
42+
tmp=/tmp/ikconfig$$
43+
trap "rm -f $tmp" 0
44+
for pos in `tr "$gz1\n$gz2" "\n$gz2=" < "$img" | grep -abo "^$gz2"`
45+
do
46+
pos=${pos%%:*}
47+
tail -c+$pos "$img" | zcat 2> /dev/null > $tmp
48+
dump_config $tmp
49+
done
50+
51+
# Bail out:
52+
echo "$me: Cannot find kernel config." >&2
9253
exit 1

0 commit comments

Comments
 (0)