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