1
0

extract-vmlinux 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # ----------------------------------------------------------------------
  4. # extract-vmlinux - Extract uncompressed vmlinux from a kernel image
  5. #
  6. # Inspired from extract-ikconfig
  7. # (c) 2009,2010 Dick Streefland <dick@streefland.net>
  8. #
  9. # (c) 2011 Corentin Chary <corentin.chary@gmail.com>
  10. #
  11. # ----------------------------------------------------------------------
  12. me=${0##*/}
  13. check_vmlinux()
  14. {
  15. if file "$1" | grep -q 'Linux kernel.*boot executable' ||
  16. readelf -h "$1" > /dev/null 2>&1
  17. then
  18. cat "$1"
  19. echo "$me: Extracted vmlinux using '$2' from offset $3" >&2
  20. exit 0
  21. fi
  22. }
  23. try_decompress()
  24. {
  25. # The obscure use of the "tr" filter is to work around older versions of
  26. # "grep" that report the byte offset of the line instead of the pattern.
  27. # Try to find the header ($1) and decompress from here
  28. for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"`
  29. do
  30. pos=${pos%%:*}
  31. tail -c+$pos "$img" | $3 > $tmp 2> /dev/null
  32. check_vmlinux $tmp "$3" $pos
  33. done
  34. }
  35. # Check invocation:
  36. img=$1
  37. if [ $# -ne 1 -o ! -s "$img" ]
  38. then
  39. echo "Usage: $me <kernel-image>" >&2
  40. exit 2
  41. fi
  42. # Prepare temp files:
  43. tmp=$(mktemp /tmp/vmlinux-XXX)
  44. trap "rm -f $tmp" 0
  45. # That didn't work, so retry after decompression.
  46. try_decompress '\037\213\010' xy gunzip
  47. try_decompress '\3757zXZ\000' abcde unxz
  48. try_decompress 'BZh' xy bunzip2
  49. try_decompress '\135\0\0\0' xxx unlzma
  50. try_decompress '\211\114\132' xy 'lzop -d'
  51. try_decompress '\002!L\030' xxx 'lz4 -d'
  52. try_decompress '(\265/\375' xxx unzstd
  53. # Finally check for uncompressed images or objects:
  54. check_vmlinux "$img" cat 0
  55. # Bail out:
  56. echo "$me: Cannot find vmlinux." >&2