#!/bin/bash

cat <<EOF > mainline.its
/dts-v1/;

/ {
    description = "Fedora Remix";
    #address-cells = <1>;
    images {
        kernel@1{
            description = "tegra_defconfig";
            data = /incbin/("arch/arm/boot/zImage");
            type = "kernel_noload";
            arch = "arm";
            os = "linux";
            compression = "none";
            load = <0>;
            entry = <0>;
        };
        fdt@1{
            description = "tegra124-nyan-big";
            data = /incbin/("arch/arm/boot/dts/tegra124-nyan-big.dtb");
            type = "flat_dt";
            arch = "arm";
            compression = "none";
        };
    };
    configurations {
        default = "conf@1";
        conf@1{
            description = "tegra124-nyan-big";
            kernel = "kernel@1";
            fdt = "fdt@1";
        };
    };
};
EOF

:<<COMMENT
        fdt@2{
            description = "tegra124-nyan-blaze";
            data = /incbin/("arch/arm/boot/dts/tegra124-nyan-blaze.dtb");
            type = "flat_dt";
            arch = "arm";
            compression = "none";
        };

        conf@2{
            description = "tegra124-nyan-blaze";
            kernel = "kernel@1";
            fdt = "fdt@2";
        };

COMMENT

# Create the uImage binary file
mkimage -vf mainline.it{s,b}

cat <<EOF > /tmp/kcmdline
console=tty1 consoleblank=0 debug verbose root=PARTUUID=83999adc-bd67-417e-bd9c-5e7d0aac6aa0 rootwait rw
EOF

# Pack and sign the kernel
vbutil_kernel --pack         ./mainline-kernel.img    \
              --keyblock     /boot/fedora-22.keyblock \
              --signprivate  /boot/fedora-22.vbprivk  \
              --config       /tmp/kcmdline            \
              --vmlinuz      ./mainline.itb           \
              --arch arm  --version 1

vbutil_kernel --verbose \
              --verify ./mainline-kernel.img

sudo cp mainline-kernel.img /boot/.
sudo dd if=mainline-kernel.img of=/dev/mmcblk1p2
sudo make modules_install
sudo make firmware_install
sudo cgpt add -i1 -P5  /dev/mmcblk1
sudo cgpt add -i2 -P10 /dev/mmcblk1
sync
read -p "Press Enter to reboot" line junk
sudo reboot
