#!/bin/sh

export LANG=C

processimage()
{
    if ischroot; then exit 0; fi
    /bin/dd if=/boot/kernel-${1}.img of=/dev/boot bs=1M conv=fsync
    STATUS=$?
    if [ "$STATUS" != 0 ]
    then
        echo "Could not upload kernel to eMMC" >&2
        exit $STATUS
    fi
    rm -rf /lib/modules/${1}_kernel_*
}

processdtb()
{
    if ischroot; then exit 0; fi
    # Need permission to do an upload
    /usr/bin/fw_setenv upgrade_step 2
    /bin/dd if=/boot/dtb-${1}.img of=/dev/dtb bs=256k || sync && /bin/dd if=/boot/dtb-${1}.img of=/dev/dtb bs=256k
    STATUS=$?
    if [ "$STATUS" != 0 ]
    then
        echo "Could not upload DTB to eMMC" >&2
        exit $STATUS
    fi
}

processimage $1
processdtb $1
