#!/bin/sh
#
# Copyright (C) 2018 devolo AG
#

# Detect empty bootenv partition and populate it with default values.
# This is necessary for fw_setenv to work properly, delos does not know the correct default bootenv otherwise.
# (only zuse, turing, hamilton, auckland, hamming - all other devices should have a correct bootenv in the flash image)

function get_baptization_config()
{
    echo "$(baptization.config -k $1 | cut -d= -f2)"
}

function set_bootenv()
{
    fw_setenv "$1" "$2"
}

DEVICETYPE="$(get_baptization_config DeviceType)"

[ "$DEVICETYPE" = "zuse" ] || [ "$DEVICETYPE" = "turing" ] || [ "$DEVICETYPE" = "hamilton" ] || [ "$DEVICETYPE" = "auckland" ] || [ "$DEVICETYPE" = "hamming" ] || exit 0

[ -n "$(fw_printenv 2>&1 | grep '^Warning: Bad CRC')" ] || exit 0

cat <<EOF | fw_setenv -s -

bootargs            console=ttyS0,115200 root=/dev/mtdblock5 rootfstype=squashfs init=/sbin/init
bootcmd             bootm 0x9f070000
bootcmd_flash       bootm 0x9f070000
bootcmd_tftp        tftpboot 0x82000000 initRamfs; bootm 0x82000000
bootdelay           1
baudrate            115200
ethaddr             0x00:0xaa:0xbb:0xcc:0xdd:0xee
ipaddr              192.168.0.249
serverip            192.168.0.100
stdin               serial
stdout              serial
stderr              serial
ethact              eth0

EOF

exit 0