deflate_xip_data.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # XIP kernel .data segment compressor
  4. #
  5. # Created by: Nicolas Pitre, August 2017
  6. # Copyright: (C) 2017 Linaro Limited
  7. #
  8. # This script locates the start of the .data section in xipImage and
  9. # substitutes it with a compressed version. The needed offsets are obtained
  10. # from symbol addresses in vmlinux. It is expected that .data extends to
  11. # the end of xipImage.
  12. set -e
  13. VMLINUX="$1"
  14. XIPIMAGE="$2"
  15. DD="dd status=none"
  16. # Use "make V=1" to debug this script.
  17. case "$KBUILD_VERBOSE" in
  18. *1*)
  19. set -x
  20. ;;
  21. esac
  22. sym_val() {
  23. # extract hex value for symbol in $1
  24. local val=$($NM "$VMLINUX" 2>/dev/null | sed -n "/ $1\$/{s/ .*$//p;q}")
  25. [ "$val" ] || { echo "can't find $1 in $VMLINUX" 1>&2; exit 1; }
  26. # convert from hex to decimal
  27. echo $((0x$val))
  28. }
  29. __data_loc=$(sym_val __data_loc)
  30. _edata_loc=$(sym_val _edata_loc)
  31. base_offset=$(sym_val _xiprom)
  32. # convert to file based offsets
  33. data_start=$(($__data_loc - $base_offset))
  34. data_end=$(($_edata_loc - $base_offset))
  35. # Make sure data occupies the last part of the file.
  36. file_end=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$XIPIMAGE")
  37. if [ "$file_end" != "$data_end" ]; then
  38. printf "end of xipImage doesn't match with _edata_loc (%#x vs %#x)\n" \
  39. $(($file_end + $base_offset)) $_edata_loc 1>&2
  40. exit 1;
  41. fi
  42. # be ready to clean up
  43. trap 'rm -f "$XIPIMAGE.tmp"; exit 1' 1 2 3
  44. # substitute the data section by a compressed version
  45. $DD if="$XIPIMAGE" count=$data_start iflag=count_bytes of="$XIPIMAGE.tmp"
  46. $DD if="$XIPIMAGE" skip=$data_start iflag=skip_bytes |
  47. $KGZIP -9 >> "$XIPIMAGE.tmp"
  48. # replace kernel binary
  49. mv -f "$XIPIMAGE.tmp" "$XIPIMAGE"