#
# mk-images.ppc
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

makeBootImages() {
    echo "Building boot images for kernel $kernelvers"
    FAKEARCH=""

    if [ "$KERNELARCH" = "ppc64" ]; then
	mkdir -p $TOPDESTPATH/ppc/ppc64
	echo "Building $KERNELARCH initrd"
	makeinitrd --initrdto $TOPDESTPATH/ppc/ppc64/ramdisk.image.gz \
	    --initrdsize 8192 \
	    --loaderbin loader \
	    --modules "$INITRDMODS spufs viocd gpio_mdio"

	cp $KERNELROOT/boot/vmlinuz-* $TOPDESTPATH/ppc/ppc64/vmlinuz
        sed -e "s/%BITS%/64/" -e "s/%PRODUCT%/$PRODUCT/" -e "s/%VERSION%/$VERSION/" \
	    $BOOTDISKDIR/yaboot.conf.in > $TOPDESTPATH/ppc/ppc64/yaboot.conf

    cat << __EOT__ >> $TOPDESTPATH/.treeinfo
[images-$KERNELARCH]
kernel = ppc/ppc64/vmlinuz
initrd = ppc/ppc64/ramdisk.image.gz
__EOT__

	if [ -x $IMGPATH/usr/bin/mkzimage -a -r $IMGPATH/usr/share/ppc64-utils/zImage.stub ]; then
		mkdir -p $TOPDESTPATH/images/netboot
		pushd $TOPDESTPATH/ppc/ppc64
		cp $IMGPATH/usr/share/ppc64-utils/zImage.lds $TOPDESTPATH/ppc/ppc64/zImage.lds
		$IMGPATH/usr/bin/mkzimage $TOPDESTPATH/ppc/ppc64/vmlinuz no no $TOPDESTPATH/ppc/ppc64/ramdisk.image.gz $IMGPATH/usr/share/ppc64-utils/zImage.stub $TOPDESTPATH/images/netboot/ppc64.img
		rmdir $TOPDESTPATH/images/netboot || :
		rm -f $TOPDESTPATH/ppc/ppc64/zImage.lds
		popd
	if [ -f $TOPDESTPATH/images/netboot/ppc64.img ]; then
		echo "zimage = images/netboot/ppc64.img" >> $TOPDESTPATH/.treeinfo
	fi
	elif [ -x $IMGPATH/usr/sbin/wrapper -a -r $IMGPATH/usr/$LIBDIR/kernel-wrapper/wrapper.a ]; then
		mkdir -p $TOPDESTPATH/images/netboot
		$IMGPATH/usr/sbin/wrapper -o $TOPDESTPATH/images/netboot/ppc64.img	\
					  -i $TOPDESTPATH/ppc/ppc64/ramdisk.image.gz	\
					  -D $IMGPATH/usr/$LIBDIR/kernel-wrapper		\
					     $TOPDESTPATH/ppc/ppc64/vmlinuz
		rmdir $TOPDESTPATH/images/netboot || :
		if [ -f $TOPDESTPATH/images/netboot/ppc64.img ]; then
			echo "zimage = images/netboot/ppc64.img" >> $TOPDESTPATH/.treeinfo
		fi
	fi

    echo >> $TOPDESTPATH/.treeinfo


    elif [ "$KERNELARCH" = "ppc" ]; then
	FAKEARCH="ppc"
	mkdir -p $TOPDESTPATH/ppc/ppc32
	mkdir -p $TOPDESTPATH/ppc/mac

        echo "Building ppc initrd"
        makeinitrd --initrdto $TOPDESTPATH/ppc/ppc32/ramdisk.image.gz \
            --initrdsize 8192 \
            --loaderbin loader \
            --modules "$INITRDMODS"

        cp $KERNELROOT/boot/vmlinuz-* $TOPDESTPATH/ppc/ppc32/vmlinuz
        sed -e "s/%BITS%/32/" -e "s/%PRODUCT%/$PRODUCT/" -e "s/%VERSION%/$VERSION/" \
	    $BOOTDISKDIR/yaboot.conf.in > $TOPDESTPATH/ppc/ppc32/yaboot.conf

    cat << __EOT__ >> $TOPDESTPATH/.treeinfo
[images-$KERNELARCH]
kernel = ppc/ppc32/vmlinuz
initrd = ppc/ppc32/ramdisk.image.gz
__EOT__
	if [ -x $IMGPATH/usr/bin/mkzimage -a -r $IMGPATH/usr/share/ppc64-utils/zImage.stub ]; then
		mkdir -p $TOPDESTPATH/images/netboot
		pushd $TOPDESTPATH/ppc/ppc32
		cp $IMGPATH/usr/share/ppc64-utils/zImage.lds $TOPDESTPATH/ppc/ppc32/zImage.lds
		$IMGPATH/usr/bin/mkzimage $TOPDESTPATH/ppc/ppc32/vmlinuz no no $TOPDESTPATH/ppc/ppc32/ramdisk.image.gz $IMGPATH/usr/share/ppc64-utils/zImage.stub $TOPDESTPATH/images/netboot/ppc32.img
		rmdir $TOPDESTPATH/images/netboot || :
		rm -f $TOPDESTPATH/ppc/ppc32/zImage.lds
		popd
	if [ -f $TOPDESTPATH/images/netboot/ppc32.img ]; then
		echo "zimage = images/netboot/ppc32.img" >> $TOPDESTPATH/.treeinfo
	fi
	elif [ -x $IMGPATH/usr/sbin/wrapper -a -r $IMGPATH/usr/$LIBDIR/kernel-wrapper/wrapper.a ]; then
		mkdir -p $TOPDESTPATH/images/netboot
		$IMGPATH/usr/sbin/wrapper -o $TOPDESTPATH/images/netboot/ppc32.img	\
					  -i $TOPDESTPATH/ppc/ppc32/ramdisk.image.gz	\
					  -D $IMGPATH/usr/$LIBDIR/kernel-wrapper		\
					     $TOPDESTPATH/ppc/ppc32/vmlinuz
		rmdir $TOPDESTPATH/images/netboot || :
		if [ -f $TOPDESTPATH/images/netboot/ppc32.img ]; then
			echo "zimage = images/netboot/ppc32.img" >> $TOPDESTPATH/.treeinfo
		fi
	fi
    echo >> $TOPDESTPATH/.treeinfo

    else
        echo "Unknown kernel arch: $KERNELARCH"
    fi
}

doPostImages() {
    mkdir -p $TOPDESTPATH/etc
    mkdir -p $TOPDESTPATH/ppc/chrp
	
    # Create ofboot.b and bootinfo.txt files, and yaboot binaries for Mac and CHRP
    cp $BOOTDISKDIR/bootinfo.txt $TOPDESTPATH/ppc/bootinfo.txt
    cp $IMGPATH/usr/lib/anaconda-runtime/boot/efika.forth $TOPDESTPATH/ppc/efika.forth

    if [ -d $TOPDESTPATH/ppc/mac ]; then
	cp $BOOTDISKDIR/ofboot.b $TOPDESTPATH/ppc/mac/ofboot.b
	cp $IMGPATH/usr/lib/yaboot/yaboot $TOPDESTPATH/ppc/mac/yaboot
    fi

    if [ -d $TOPDESTPATH/ppc/chrp ]; then
	cp $IMGPATH/usr/lib/yaboot/yaboot $TOPDESTPATH/ppc/chrp/yaboot
	$IMGPATH/usr/lib/yaboot/addnote $TOPDESTPATH/ppc/chrp/yaboot
    fi

    # IBM firmware can't handle boot scripts properly, so for biarch installs
    # we use a yaboot.conf which asks the user to select 32-bit or 64-bit kernel. 
    if [ -r $TOPDESTPATH/ppc/ppc32/yaboot.conf -a -r $TOPDESTPATH/ppc/ppc64/yaboot.conf ]; then
        # Both kernels exist. Copy the biarch yaboot.conf into place.
        sed -e "s/%BITS%/32/" -e "s/%PRODUCT%/$PRODUCT/" -e "s/%VERSION%/$VERSION/" \
	    $BOOTDISKDIR/yaboot.conf.3264 > $TOPDESTPATH/etc/yaboot.conf
    else
	# Copy the one that exists, assuming one does exist
	cp $TOPDESTPATH/ppc/ppc??/yaboot.conf $TOPDESTPATH/etc
    fi

    if [ -z "$BOOTISO" ]; then
	return
    fi

    # Copy it all into the isopath for the boot CD
    mkdir -p $TOPDESTPATH/isopath
    cp -r $TOPDESTPATH/{ppc,etc} $TOPDESTPATH/isopath

    # We want the ppc32 prep image in the boot.iso too.
    if [ -d $TOPDESTPATH/images/netboot ]; then
	mkdir -p $TOPDESTPATH/isopath/images
	cp -r $TOPDESTPATH/images/netboot $TOPDESTPATH/isopath/images
	rm -f $TOPDESTPATH/isopath/images/ppc64.img
    fi

    if [ -r $TOPDESTPATH/isopath/images/netboot/ppc32.img ]; then
	PREPBOOT="-prep-boot images/netboot/ppc32.img"
    fi

    if [ -d $TOPDESTPATH/isopath/ppc/mac ]; then
	MACBOOT="-hfs-volid $VERSION -hfs-bless $TOPDESTPATH/isopath/ppc/mac"
    fi

    # Create the boot.iso
    mkisofs -o $TOPDESTPATH/images/$BOOTISO -chrp-boot -U $PREPBOOT \
	-part -hfs -T -r -l -J -A "$PRODUCT $VERSION" -sysid PPC \
	-V "PBOOT" -volset "$VERSION" -volset-size 1 -volset-seqno 1 \
	$MACBOOT \
	-map $BOOTDISKDIR/mapping -magic $BOOTDISKDIR/magic \
	-no-desktop -allow-multidot -graft-points $TOPDESTPATH/isopath \
	images/install.img=$TOPDESTPATH/images/install.img
    implantisomd5 $TOPDESTPATH/images/$BOOTISO
    rm -rf $TOPDESTPATH/isopath
}
