#!/bin/sh
#Mod by Vasiliks 2014 for Neutrino
LOG(){
echo "$1"
echo "$1" >>/tmp/mboot.log
echo "$1" >/tmp/media/sda1/MBoot.log
}
GOTERROR(){
echo "$1"
echo "$1" >>/tmp/mboot.log
echo "$1" >/tmp/media/sda1/MBoot.log
touch /tmp/mboot.error
exit 1
}
echo "##### setIMG.sh `date` #####" >/tmp/media/sda1/MBoot.log
#IMpath="/usr/lib/enigma2/python/Plugins/Extensions/ImageManager"
#/Mboot
BIN=/Mboot
[ -f /tmp/mboot.ok ] && rm -rf /tmp/mboot.ok
[ -f /tmp/mboot.error ] && rm -rf /tmp/mboot.error
[ -f /tmp/mboot.log ] && rm -rf /tmp/mboot.log
#checking parameters
if [ -z $1 ]; then
GOTERROR "usage: setIMG.sh [NAND|USB sd[abcd][1-9]]"
else
if [ -z $2 ]; then
if ! [ $1 == "NAND" ]; then
GOTERROR "For boot from USB use: setIMG.sh USB sd[abcd][1-9]"
fi
fi
fi
sdXY=`echo $2 | sed "s;/dev/;;"`
echo "Active partition: '$sdXY'"
# do we realy need to change anything?
if `cat /proc/cmdline | grep -q "/dev/$sdXY"`; then
if `echo $1 | grep -q 'USB'`; then
LOG "Restart current soft from $sdXY"
touch /tmp/mboot.ok
sync
exit 0
fi
fi
if `cat /proc/cmdline | grep -q '/dev/mtdblock6'`; then
if `echo $1 | grep -q 'NAND'`; then
LOG "Restart current soft from NAND"
touch /tmp/mboot.ok
sync
exit 0
fi
fi
# checking box type
if `cat /proc/stb/info/model | grep -q spark`; then
LOG "spark detected
"
MyBootCMD="nboot.i 80000000 0 18000000;bootm 80000000"
MyBootARGS=""
MyMBoot_bootcmd='nboot.i 80000000 0 18400000;run MBoot_bootargs;bootm 80000000;set bootargs ${bootargs_enigma2};nboot.i 80000000 0 18000000;bootm 80000000;reset'
MyMBoot_bootargs='setenv bootargs "console=ttyAS0,115200 root=/dev/'$sdXY' rw init=/bin/devinit coprocessor_mem=4m@0x40000000,4m@0x40400000 printk=1 nwhwconf=device:eth0,hwaddr:${ethaddr} rw ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:SPARK:eth0:off bigphysarea=6000 stmmaceth=msglvl:0,phyaddr:2,watchdog:5000 rootdelay=9"'
[ -e /tmp/fw_env.config ] || ln -sf $BIN/fw_env.config.spark /tmp/fw_env.config
else
GOTERROR "unknown box detected
"
exit 1
fi
if `echo $1 | grep -q 'USB'`; then
if ! `echo $2 | grep -q 'sd[abcd][1-9]'`; then
GOTERROR "Error, got incorrect parameter
($1)"
exit 1
fi
MBoot_bootargs=$MyMBoot_bootargs
MBoot_bootcmd=$MyMBoot_bootcmd
# mounting partition
# unmouting partition if already mounted
if `mount | grep '/tmp/MBOOT' | grep -q /dev/$sdXY`; then
LOG "unmounting /tmp/MBOOT folder"
umount -l /tmp/MBOOT 2>/dev/null
fi
if `mount | grep '/tmp/MBOOT' | grep -q /dev/$sdXY`; then
GOTERROR "ERROR, partition cannot be unmounted
"
exit 1
fi
# if there is no our catalog, it is necessary to create
if [ ! -d /tmp/MBOOT ]; then
LOG "Creating /tmp/MBOOT folder"
mkdir /tmp/MBOOT
fi
# if we partition is unmounted, let's check the consistency
if ! `mount | grep -q /dev/$sdXY`; then
LOG "checking partition consistency"
e2fsck -p /dev/$sdXY
fi
# mounting a suitable partition
LOG "mounting partition"
mount /dev/$sdXY /tmp/MBOOT
if ! `mount | grep -q /dev/$sdXY`; then
GOTERROR "ERROR: Mount partitions, finish work"
exit 1
fi
if [ ! -e /tmp/MBOOT/boot/uImage ]; then
GOTERROR "ERROR: Mounted partition contains the / boot / uImage. I finish work!!!"
exit 1
fi
# some improvements
sed -i "s/\$1\$K\$Oy86o0YspthTr2IXvUm751//" /tmp/MBOOT/etc/passwd
# programming 2nd kernel
LOG "Erasing 2nd kernel space in flash..."
echo "ErASE nAnd.." >/dev/vfd
$BIN/flash_erase /dev/mtd5 0x400000 0x20
[ $? -gt 0 ] && GOTERROR "error erasing nand"
LOG "Writing 2nd kernel to NAND..."
echo "FLASH 2nd kernel to nAnd.." >/dev/vfd
nandwrite -s 0x400000 -p /dev/mtd5 /tmp/MBOOT/boot/uImage
[ $? -gt 0 ] && GOTERROR "error writing nand"
else # boot from NAND
MBoot_bootargs=$MyBootARGS
MBoot_bootcmd=$MyBootCMD
fi
# checking environment
[ -e $BIN/fw_printenv ] || ln -sf $BIN/fw_setenv $BIN/fw_printenv
myENV=`$BIN/fw_printenv`
RET=$?
if [ $RET -eq 0 ]; then
LOG "Valid fw_env detected
"
else
GOTERROR "fw_env misconfigured
"
fi
LOG "Writing bootargs..."
LOG $MBoot_bootargs #
# MBoot_bootargs
$BIN/fw_setenv MBoot_bootargs "$MBoot_bootargs"
[ $? -gt 0 ] && GOTERROR "error writing MBoot_bootargs"
# MBoot_bootcmd
$BIN/fw_setenv bootcmd "$MBoot_bootcmd"
[ $? -gt 0 ] && GOTERROR "error writing MBoot_bootcmd"
# To check if all required written correctly
myENV=`$BIN/fw_printenv`
if ! `echo $myENV | grep -q "bootcmd="`;then
[ $? -gt 0 ] && GOTERROR "error no bootcmd found after flashing"
fi
if ! `echo $myENV | grep -q "bootargs="`;then
$BIN/fw_setenv MBoot_ON
[ $? -gt 0 ] && GOTERROR "error no bootargs found after flashing"
fi
# cleaning trashes
LOG "cleaning up..."
if `echo $myENV | grep -q "MBoot_ON"`;then
$BIN/fw_setenv MBoot_ON
[ $? -gt 0 ] && GOTERROR "error removing MBoot_ON"
fi
if `echo $myENV | grep -q "MBoot_sda_NO"`;then
$BIN/fw_setenv MBoot_sda_NO
[ $? -gt 0 ] && GOTERROR "error removing MBoot_sda_NO"
fi
if `echo $myENV | grep -q "MBoot_bootcmd"`;then
$BIN/fw_setenv MBoot_bootcmd
[ $? -gt 0 ] && GOTERROR "error removing MBoot_bootcmd"
fi
if `echo $myENV | egrep -q "bootargsusb|menu_|bootcmdusb|bootargshub|bootcmdhub|bootargside|bootcmdide"`; then
for i in `cat /proc/partitions | grep sd[abcd]. | awk '{print $4}' | sed 's/sd.//'`
do
$BIN/fw_setenv "menu_$i"
[ $? -gt 0 ] && GOTERROR "error removing menu_$i"
$BIN/fw_setenv "bootargsusb$i"
[ $? -gt 0 ] && GOTERROR "error removing bootargsusb$i"
$BIN/fw_setenv "bootcmdusb$i"
[ $? -gt 0 ] && GOTERROR "error removing bootcmdusb$i"
$BIN/fw_setenv "bootargshub$i"
[ $? -gt 0 ] && GOTERROR "error removing bootargshub$i"
$BIN/fw_setenv "bootcmdhub$i"
[ $? -gt 0 ] && GOTERROR "error removing bootcmdhub$i"
$BIN/fw_setenv "bootargside$i"
[ $? -gt 0 ] && GOTERROR "error removing bootargside$i"
$BIN/fw_setenv "bootcmdide$i"
[ $? -gt 0 ] && GOTERROR "error removing bootcmdide$i"
done
fi
touch /tmp/mboot.ok
sync
reboot