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