extract-ikconfig 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/sh
  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. gz1='\037\213\010'
  14. gz2='01'
  15. cf1='IKCFG_ST\037\213\010'
  16. cf2='0123456789'
  17. dump_config()
  18. {
  19. if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
  20. then
  21. pos=${pos%%:*}
  22. tail -c+$(($pos+8)) "$1" | zcat -q
  23. exit 0
  24. fi
  25. }
  26. # Check invocation:
  27. me=${0##*/}
  28. img=$1
  29. if [ $# -ne 1 -o ! -s "$img" ]
  30. then
  31. echo "Usage: $me <kernel-image>" >&2
  32. exit 2
  33. fi
  34. # Initial attempt for uncompressed images or objects:
  35. dump_config "$img"
  36. # That didn't work, so decompress and try again:
  37. tmp=/tmp/ikconfig$$
  38. trap "rm -f $tmp" 0
  39. for pos in `tr "$gz1\n$gz2" "\n$gz2=" < "$img" | grep -abo "^$gz2"`
  40. do
  41. pos=${pos%%:*}
  42. tail -c+$pos "$img" | zcat 2> /dev/null > $tmp
  43. dump_config $tmp
  44. done
  45. # Bail out:
  46. echo "$me: Cannot find kernel config." >&2
  47. exit 1