#!/bin/sh
# my-mkimage
# This will pad kernel to 64k boundary and append ramdisk afterwards.
# we have to be fancy because u-boot mkimage is going to add 64 byte header,
# plus another 12 bytes at the beginning of the ramdisk image? ...
# and i only know basic arithmetic.. ;)
#
# NOTE: The string we use for the image name cannot be changed on the
#       WRTU54G-TM because the bootloader has a stupid vendor string check!
# Copyright (C) 2011 Scott Nicholas <neutronscott@scottn.us>
# Released under GPL. See http://www.gnu.org/licenses/gpl-3.0.txt
[ $# -ne 3 ] && {
	echo usage: $0 kernel ramdisk.gz output.bin
	exit
}

OLDSIZE=$(stat -c%s $1)
NEWSIZE=$(((OLDSIZE / 65536 + 1) * 65536 - 64 - 12))

## i suck at integer maths?
if [ "$NEWSIZE" -lt "$OLDSIZE" ]; then
  NEWSIZE=$((NEWSIZE + 65536))
fi

dd if=$1 of=aligned.kernel bs=$NEWSIZE conv=sync >/dev/null 2>&1
dd if=$2 of=aligned.ramdisk bs=64k conv=sync >/dev/null 2>&1

printf "### ramdisk starts at 0x%x\n" "$((NEWSIZE+64+12))"
echo

#### for normal gzip kernel
./mkimage -A mips -O linux -T multi -C gzip -a 0x80002000 -e 0x80006270 \
-n "ADM8668 Linux Kernel(2.4.31)" -d aligned.kernel:aligned.ramdisk $3

#### for lzma-loader
#./mkimage -A mips -O linux -T multi -C none -a 0x80400000 -e 0x80400000 \
#-n "ADM8668 Linux Kernel(2.4.31)" -d aligned.kernel:aligned.ramdisk $3

rm aligned.kernel aligned.ramdisk
