#!/bin/sh
#
# If specified, the first argument is the compressed image load address. Defaults
# to 0x42000000 for mx283
#
# We need to support Device Tree appended to zImage, so we create an
# uncompressed uImage.
#
# The zImage gets loaded to 0x42000000 and then decompresses to 40008000

RDIR=`/bin/pwd`
cd "`dirname $0`"
SDIR=`/bin/pwd`
cd "$RDIR"

if [ -n "$1" ]; then
	LOADADDR="$1"
else
	. $SDIR/.config
	if [ "$CONFIG_ARCH_MXS" = "y" ]; then
		LOADADDR="0x42000000"
	else
		#default
		LOADADDR="0x42000000"
	fi
fi

echo "build_image: using LOADADDR=$LOADADDR"

# The kernel.mk file in the build system copies
# the zImage from arch/arm/boot/ and appends the Device Tree binary
# if enabled via V_DEVTREE.
if [ -x "$SDIR/../u_boot/u-boot/tools/mkimage" ]; then
	MKIMAGE="$SDIR/../u_boot/u-boot/tools/mkimage"
elif [ -x "$SDIR/mkimage" ]; then
	MKIMAGE="$SDIR/mkimage"
else
	echo "Couldn't find the mkimage binary" 1>&2
	exit 1
fi

# Check for zImage. Some kernels dump it into the top level dir,
# some leave it under /usr/arch/arm
if [ -f "$SDIR/zImage" ]; then
	ZIMAGE="$SDIR/zImage";
elif [ -f "$SDIR/arch/arm/boot/zImage" ]; then
	ZIMAGE="$SDIR/arch/arm/boot/zImage";
else
	echo "Can't find zImage" 1>&2
	exit 1
fi

$MKIMAGE -A arm -O linux -T kernel -C none -a $LOADADDR -e $LOADADDR -n "Netcomm Linux Kernel" -d "$ZIMAGE" ./uImage
