extract-ikconfig 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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,2010 Dick Streefland <dick@streefland.net>
  11. # Licensed under the terms of the GNU General Public License.
  12. # ----------------------------------------------------------------------
  13. cf1='IKCFG_ST\037\213\010'
  14. cf2='0123456789'
  15. dump_config()
  16. {
  17. if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"`
  18. then
  19. pos=${pos%%:*}
  20. tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null
  21. if [ $? != 1 ]
  22. then # exit status must be 0 or 2 (trailing garbage warning)
  23. cat $tmp1
  24. exit 0
  25. fi
  26. fi
  27. }
  28. try_decompress()
  29. {
  30. for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
  31. do
  32. pos=${pos%%:*}
  33. tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null
  34. dump_config $tmp2
  35. done
  36. }
  37. # Check invocation:
  38. me=${0##*/}
  39. img=$1
  40. if [ $# -ne 1 -o ! -s "$img" ]
  41. then
  42. echo "Usage: $me <kernel-image>" >&2
  43. exit 2
  44. fi
  45. # Prepare temp files:
  46. tmp1=/tmp/ikconfig$$.1
  47. tmp2=/tmp/ikconfig$$.2
  48. trap "rm -f $tmp1 $tmp2" 0
  49. # Initial attempt for uncompressed images or objects:
  50. dump_config "$img"
  51. # That didn't work, so retry after decompression.
  52. try_decompress '\037\213\010' xy gunzip
  53. try_decompress '\3757zXZ\000' abcde unxz
  54. try_decompress 'BZh' xy bunzip2
  55. try_decompress '\135\0\0\0' xxx unlzma
  56. try_decompress '\211\114\132' xy 'lzop -d'
  57. try_decompress '\002\041\114\030' xyy 'lz4 -d -l'
  58. # Bail out:
  59. echo "$me: Cannot find kernel config." >&2
  60. exit 1