deflate_xip_data.sh 1.8 KB

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