#!/bin/bash
#
# U-Boot injection script.
#
# This script installs U-Boot SPL, MLO, BL1, IMX, whatever images into
# the target image during setup time as well as on first boot.
#
# It also serves as our generic hook into things we need to do to fix
# up the build.

set -x

diskname=$1
loopname=$2
flavor=raspberrypi3_aarch64
is_firstboot=

if [ "$is_firstboot" ]; then
	# We're not inside the image build, but in the first boot of an
	# installed system

	diskname=$(df / --output=source | tail -n1 | sed 's/[0-9]*$//;s/p$//')
	is_firstboot=1
	if [ -d /boot/efi ]; then
		is_efi=1
	fi

	cd /
fi

(

if [ -d EFI ]; then
	# We're on an EFI build, so kiwi doesn't copy u-boot over.
	is_efi=1
fi

#==========================================
# Convert GPT to MBR if necessary
#------------------------------------------
if [ "$is_efi" ]; then
	# The RPi firmware can only read MBR
	force_mbr=1

	if [ "$force_mbr" ]; then
		# The target system doesn't support GPT, so let's move it to
		# MBR partition layout instead.
		#
		# Also make sure to set the ESP partition to type 0xc so that
		# broken firmware (Rpi) detects it as FAT.

		cat > gdisk.tmp <<-'EOF'
			x
			r
			g
			t
			1
			c
			w
			y
		EOF
		losetup /dev/loop3 $diskname
		dd if=/dev/loop3 of=mbrid.bin bs=1 skip=440 count=4
		if [ -e /usr/src/packages/KIWIROOT-oem/usr/sbin/gdisk ]; then
			/usr/src/packages/KIWIROOT-oem/usr/sbin/gdisk /dev/loop3 < gdisk.tmp
		else
			/usr/sbin/gdisk /dev/loop3 < gdisk.tmp
		fi
		dd of=/dev/loop3 if=mbrid.bin bs=1 seek=440 count=4
		losetup -d /dev/loop3
		rm -f mbrid.bin
		rm -f gdisk.tmp
	fi
fi

#==========================================
# adjust Raspberry Pi partition table
#------------------------------------------
if [ ! "$is_efi" ]; then
	echo -n > gdisk.tmp
	if [ ! "$is_firstboot" ]; then
		# Set the name of the first partition to "vcboot" and mark
		# the second partition as bootable (checked by U-Boot)
		cat >> gdisk.tmp <<-'EOF'
			c
			1
			vcboot
			x
			a
			2
			2
			64
			m
		EOF
	fi

	# Convert GPT to hybrid GPT
	cat >> gdisk.tmp <<-'EOF'
		x
		r
		h
		1 2 3
		n
		c
		n
		82
		y
		83
		n
		w
		y
	EOF

	if [ "$is_firstboot" ]; then
		/usr/sbin/gdisk /dev/mmcblk0 < gdisk.tmp
	else
		losetup /dev/loop3 $diskname
		if [ -e /usr/src/packages/KIWIROOT-oem/usr/sbin/gdisk ]; then
			/usr/src/packages/KIWIROOT-oem/usr/sbin/gdisk /dev/loop3 < gdisk.tmp
		else
			/usr/sbin/gdisk /dev/loop3 < gdisk.tmp
		fi
		losetup -d /dev/loop3
	fi
	rm -f gdisk.tmp
fi

#==========================================
# set certain dirs as nocow
#------------------------------------------
function nocow() {
  [ -d $1 ] && chattr +C $i
}
for i in var/lib/mariadb var/lib/mysql var/lib/pgsql var/lib/libvirt/images; do
  nocow $i
done

#==========================================
# copy Raspberry Pi firmware to EFI partition
#------------------------------------------
if [ "$is_efi" -a ! "$is_firstboot" ]; then
	echo "RPi EFI system, installing firmware on ESP"
	LINE=$(kpartx -asv $diskname | head -n1)
	PART=$(echo "$LINE" | awk '{print $3}')
	ROOTPART=${PART/%p1/p3}
	mkdir -p ./mnt-pi
	mkdir -p ./mnt-pi-root
	mount /dev/mapper/$PART ./mnt-pi
	mount /dev/mapper/$ROOTPART ./mnt-pi-root
	if [ -d mnt-pi-root/boot/vc ]; then
		( cd mnt-pi-root/boot/vc; tar c . ) | ( cd ./mnt-pi/; tar x )
	else
		echo "ERROR: No RPi firmware directory found"
	fi
	if [ -d mnt-pi-root/boot/dtb ]; then
		( cd mnt-pi-root/boot/; tar c dtb/* ) | ( cd ./mnt-pi/; tar x )
	fi
	umount ./mnt-pi
	umount ./mnt-pi-root
	rmdir ./mnt-pi
	rmdir ./mnt-pi-root
	# "kpartx -dv $diskname" does not work if $diskname
	# is longer than 64 characters
	LOOPDEV=$(echo "/dev/$PART" | sed 's/p[0-9][0-9]*$//')
	kpartx -dv $LOOPDEV
	losetup -d $LOOPDEV
fi

)

if [ "$is_firstboot" ]; then
	# On second boot, the rootfs is no longer tmpfs and dracut would interpret the
	# command line argument, remove it again from the config
	for file in /etc/sysconfig/bootloader /etc/default/grub; do
		[ -e "$file" ] && sed -i -e 's/rootflags=size=100%//' $file
	done

	# Fix up grub2 efi installation on 32bit arm (shouldn't hurt elsewhere)
	if grep -q boot/efi /etc/fstab; then
		mkdir -p /boot/efi
		mount /boot/efi
		/sbin/update-bootloader --reinit
	fi
else
	# Install a startup.nsh file so we boot automatically via the EFI shell
	if [ -f boot/grub2/*efi/grub.efi -o -f ./EFI/BOOT/boot*.efi ];then
		echo "EFI system, installing startup.nsh"
		LINE=$(kpartx -asv $diskname | head -n1)
		PART=$(echo "$LINE" | awk '{print $3}')
		mkdir ./mnt
		mount /dev/mapper/$PART ./mnt
	        if [ -f boot/grub2/arm-efi/grub.efi ]; then
			echo "bootarm" > mnt/startup.nsh
		else
			echo "bootaa64" > mnt/startup.nsh
		fi
		umount ./mnt
		rmdir ./mnt
		# "kpartx -dv $diskname" does not work if $diskname
		# is longer than 64 characters
		LOOPDEV=$(echo "/dev/$PART" | sed 's/p[0-9][0-9]*$//')
		kpartx -dv $LOOPDEV
		losetup -d $LOOPDEV
	fi
fi
