blob: 40937248cebe31ed751a5d435434b3f783f1ab2e [file] [log] [blame]
Nicolas Pitreca8b5d92017-08-25 00:54:18 -04001#!/bin/sh
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02002# SPDX-License-Identifier: GPL-2.0-only
Nicolas Pitreca8b5d92017-08-25 00:54:18 -04003
4# XIP kernel .data segment compressor
5#
6# Created by: Nicolas Pitre, August 2017
7# Copyright: (C) 2017 Linaro Limited
8#
Nicolas Pitreca8b5d92017-08-25 00:54:18 -04009
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
15set -e
16
17VMLINUX="$1"
18XIPIMAGE="$2"
19
20DD="dd status=none"
21
22# Use "make V=1" to debug this script.
23case "$KBUILD_VERBOSE" in
24*1*)
25 set -x
26 ;;
27esac
28
29sym_val() {
30 # extract hex value for symbol in $1
Nicolas Pitre1b8837b2018-03-12 15:51:26 +010031 local val=$($NM "$VMLINUX" 2>/dev/null | sed -n "/ $1\$/{s/ .*$//p;q}")
Nicolas Pitreca8b5d92017-08-25 00:54:18 -040032 [ "$val" ] || { echo "can't find $1 in $VMLINUX" 1>&2; exit 1; }
33 # convert from hex to decimal
34 echo $((0x$val))
35}
36
37__data_loc=$(sym_val __data_loc)
38_edata_loc=$(sym_val _edata_loc)
39base_offset=$(sym_val _xiprom)
40
41# convert to file based offsets
42data_start=$(($__data_loc - $base_offset))
43data_end=$(($_edata_loc - $base_offset))
44
45# Make sure data occupies the last part of the file.
Michael Forneya670b0b2018-03-18 17:54:02 -070046file_end=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$XIPIMAGE")
Nicolas Pitreca8b5d92017-08-25 00:54:18 -040047if [ "$file_end" != "$data_end" ]; then
48 printf "end of xipImage doesn't match with _edata_loc (%#x vs %#x)\n" \
Nicolas Pitre1b8837b2018-03-12 15:51:26 +010049 $(($file_end + $base_offset)) $_edata_loc 1>&2
Nicolas Pitreca8b5d92017-08-25 00:54:18 -040050 exit 1;
51fi
52
53# be ready to clean up
Nicolas Pitre1b8837b2018-03-12 15:51:26 +010054trap 'rm -f "$XIPIMAGE.tmp"; exit 1' 1 2 3
Nicolas Pitreca8b5d92017-08-25 00:54:18 -040055
56# substitute the data section by a compressed version
57$DD if="$XIPIMAGE" count=$data_start iflag=count_bytes of="$XIPIMAGE.tmp"
58$DD if="$XIPIMAGE" skip=$data_start iflag=skip_bytes |
59gzip -9 >> "$XIPIMAGE.tmp"
60
61# replace kernel binary
62mv -f "$XIPIMAGE.tmp" "$XIPIMAGE"