#!/bin/sh

# Fixup script for bogus U-Boot environment in early manufactured
# Ikebana units.

# Early manufacturing set up the "bootreco" environment variable to an
# incorrect value, preventing the use of the redundant recovery
# console image.
#
# The bogus "bootreco" value is:
#
#   run setrecoargs addcommonargs;
#     mmc dev 0 4 && mmc read 0x88000000 0 0x10000 && bootm 0x88000000 &&
#     mmc dev 0 5 && mmc read 0x88000000 0 0x10000 && bootm 0x88000000
#
# For safety we are conservative and only save the correct bootreco
# value if the current one matches the known bogus value. A regexp is
# used to check so as to allow for differences in whitespace; it is
# obtained by passing the (single-line) bogus value through
#
#   sed -e 's/[[:space:]]*\(;\|&&\)[[:space:]]*/[[:space:]]*\1[[:space:]]*/g'
#     -e 's/[[:space:]]\+/[[:space:]]\\+/g'
#
bad_bootreco_re="run[[:space:]]\+setrecoargs[[:space:]]\+addcommonargs[[:space:]]*;[[:space:]]*mmc[[:space:]]\+dev[[:space:]]\+0[[:space:]]\+4[[:space:]]*&&[[:space:]]*mmc[[:space:]]\+read[[:space:]]\+0x88000000[[:space:]]\+0[[:space:]]\+0x10000[[:space:]]*&&[[:space:]]*bootm[[:space:]]\+0x88000000[[:space:]]*&&[[:space:]]*mmc[[:space:]]\+dev[[:space:]]\+0[[:space:]]\+5[[:space:]]*&&[[:space:]]*mmc[[:space:]]\+read[[:space:]]\+0x88000000[[:space:]]\+0[[:space:]]\+0x10000[[:space:]]*&&[[:space:]]*bootm[[:space:]]\+0x88000000"

# The correct bootreco value
good_bootreco="run setrecoargs addcommonargs; mmc dev 0 4 && mmc read 0x88000000 0 0x10000 && bootm 0x88000000; mmc dev 0 5 && mmc read 0x88000000 0 0x10000 && bootm 0x88000000"

if fw_printenv -n bootreco | grep -q "^${bad_bootreco_re}\$"; then
    # we set the value twice so that it also makes it to the redundant U-Boot environment
    echo "Fixing up incorrect bootreco value in U-Boot environment"
    fw_setenv bootreco "$good_bootreco"
    fw_setenv bootreco "$good_bootreco"
fi

# As this is run from a post-install script always exit with a zero status
exit 0
