File:  [ELWIX - Embedded LightWeight unIX -] / elwix / config / etc / default / rc.cfg
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Mar 11 13:59:50 2021 UTC (3 years, 9 months ago) by misho
Branches: MAIN
CVS tags: elwix2_8, elwix2_7, HEAD, ELWIX2_7, ELWIX2_6
ver 2.6

#!/bin/sh
#
# U-boot cfg nand map load/save management script
#
# $Id: rc.cfg,v 1.4 2021/03/11 13:59:50 misho Exp $

CFG_NAND_LIST="/etc/cfg.map"
CFG_NAND_MAP="/dev/ada0g"
CFG_NAND_SIZE=524288

ELWIX2="/dev/ufs/elwix2"
ELWIX="/dev/ufs/elwix"

UPGRADE_GPIO_PINS="1 2 3"

ELWIX_CFG_BACKUP_NAME="elwix_cfg_$(hostname).bin"

CFDIR="/elwix/cf"
PKG2CF="${CFDIR}/pkg"

die() { echo -n "EXIT: " >&2; echo "$@" >&2; exit 1; }

cd ${2:-/etc}

case "$1" in
	load)
		CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
		CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`

		imgupd -gs ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} | \
		      gunzip 2>&- | cpio -iud 2>&- || die "ERROR:: config load"
		;;
	save)
		CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
		CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`

		if [ ! -r ${CFG_NAND_LIST} ]; then
			echo "# Auto generated manifest file" >${CFG_NAND_LIST}
			find . >>${CFG_NAND_LIST}
			if [ $? -ne 0 -o ! -r ${CFG_NAND_LIST} ]; then
				die "ERROR:: Manifest cfg nand map - file '${CFG_NAND_LIST}' not found!"
			fi
		fi

		sed -nE -e 's/^ *([^ #]+) */\1/p' < ${CFG_NAND_LIST} | xargs find | \
			cpio -o | gzip -9 | \
			imgupd -is ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} || die "ERROR:: config save"
		;;
	erase)
		CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
		CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`

		imgupd -is ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} /dev/zero || die "ERROR:: config erase"
                echo "Config NAND was erased!"
		;;
	restore)
		CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
		CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`

		ELWIX_REMOTE_HOST=`cat /etc/myelwix`
		ping -c1 $(echo ${ELWIX_REMOTE_HOST} | cut -f3 -d '/' | cut -f2 -d '@') >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			die "ERROR:: ELWIX restore from host ${ELWIX_REMOTE_HOST} failed!!!"
		fi

		echo "ELWIX starting restore process from ${ELWIX_REMOTE_HOST}"
		fetch -q -o- --user-agent=ELWIX_Restore --allow-sslv2 \
			--no-verify-peer --no-verify-hostname \
			${ELWIX_REMOTE_HOST}/${ELWIX_CFG_BACKUP_NAME} | \
			imgupd -R -is ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} || die "ERROR:: config restore"
		echo "ELWIX restore process complete. Now rebooting system ..."
		;;
	backup)
		CFG_NAND_MAP=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("/dev/%s", $3); }'`
		CFG_NAND_SIZE=`gpart show -p | awk '(NR == 1) { disk = $4; } ($3 == disk"g") { printf("%s", $2 * 512); }'`

		ELWIX_REMOTE_HOST="/tmp/${ELWIX_CFG_BACKUP_NAME}"

		echo "ELWIX starting backup process to ${ELWIX_REMOTE_HOST}"
		imgupd -gs ${CFG_NAND_SIZE} -f ${CFG_NAND_MAP} ${ELWIX_REMOTE_HOST} || die "ERROR:: config backup"
		echo "ELWIX backup process complete."
		;;
	upgrade)
		echo "ELWIX switch to upgrade mode. Starting TFTPd ..."
		echo " You can upload now elwix_firmware.bin image to device with \"firmware\" name!!!"
		echo "  Example: put elwix_firmware.bin firmware"
		for i in ${UPGRADE_GPIO_PINS}; do
			gpioctl $i 1 >/dev/null 2>&1 || pceng "$i=0" >/dev/null 2>&1
		done
		TFTPd -wR
		;;
	pkg_save)
		echo "ELWIX syncing information about installed packages into storage"
		if mount | grep "${CFDIR}" >/dev/null 2>&1; then
			mount -uw ${CFDIR}
		fi
		cfexec -L "Locked by rc.cfg for pkg_save"
		tar -czvf /elwix/altroot/pkg/db/pkgz.tar.gz /var/db/pkg/*
		if [ $? -ne 0 ]; then
			echo "ERROR:: package save"
		else
			echo "ELWIX sync process complete."
		fi
		if mount | grep "${CFDIR}" >/dev/null 2>&1; then
			mount -ur ${CFDIR}
		fi
		cfexec -U
		;;
	pkg_load)
		echo "ELWIX loading information about installed packages from storage"
		tar -xzvpf /elwix/altroot/pkg/db/pkgz.tar.gz -C / || die "ERROR:: package load"
		echo "ELWIX load process complete."
		;;
	pkg2cf)
		echo "ELWIX setup new pkg location on CF at $PKGCF"
		if ! mount | grep "${CFDIR}" >/dev/null 2>&1; then
			echo "Skip pkg2cf setup, because CF partition isn't mounted ..."
			exit 1
		fi
		if [ ! -L /elwix/local -a ! -L /elwix/altroot/pkg/db ]; then
			mount -uw ${CFDIR}
			mkdir -p ${PKG2CF}/db
			mkdir -p ${PKG2CF}/local

			cfexec -L "Locked by rc.cfg for pkg2cf"
			if [ ! -L /elwix/altroot/pkg/db ]; then 
				cp -r /elwix/altroot/pkg/db/* ${PKG2CF}/db >/dev/null 2>&1
				rm -rf /elwix/altroot/pkg/db
				ln -s ${PKG2CF}/db /elwix/altroot/pkg/db
			fi
			if [ ! -L /elwix/local ]; then
				cp -r /elwix/local/* ${PKG2CF}/local >/dev/null 2>&1
				rm -rf /elwix/local
				ln -s ${PKG2CF}/local /elwix/local
			fi
			cfexec -U

			mount -ur ${CFDIR}
		fi
		echo "ELWIX pkg2cf setup complete."
		;;
	boot2)
		echo "ELWIX setup system boot from second image"
		if [ -c $ELWIX2 ]; then
			if mount | grep "${ELWIX2} " >/dev/null 2>&1; then
				die "You already booted from backup image! Skip changing image ..."
			fi

			IMGTYPE=$(cat ./platform | awk '{ printf("%s", $2); }')
			case $IMGTYPE in
				base)
					if ! mount $ELWIX2 /mnt; then
						die "Backup image seems like broken ..."
					fi

					awk -v elwix="$ELWIX" -v elwix2="$ELWIX2" '
						($1 == elwix) { gsub("\/", "\/", elwix); sub(elwix, elwix2, $0); }
						{ print $0; }
					' /mnt/etc/fstab > /tmp/fstab.tmp
					if [ $? -eq 0 ]; then
						cp /tmp/fstab.tmp /mnt/etc/fstab
						rm -f /tmp/fstab.tmp
					fi

					umount /mnt
					echo "ELWIX prepare backup image for next boot!"
					;;
				*)
					;;
			esac

			cfger -o /tmp/loader.conf.local.tmp -c "Edit by /etc/rc.cfg" \
				-JSs currdev /boot/loader.conf.local "\"disk0f:\""
			cfexec -L "Locked by rc.cfg for boot2"
			cp /tmp/loader.conf.local.tmp /boot/loader.conf.local
			cfexec -U
			rm -f /tmp/loader.conf.local.tmp
		else
			die "ERROR:: ELWIX has no second usable image!"
		fi
		echo "ELWIX boot2 setup complete."
		;;
	boot)
		echo "ELWIX setup system boot from main image"
		if mount | grep "${ELWIX2} " >/dev/null 2>&1; then
			mount ${ELWIX} /mnt

			cfger -o /tmp/loader.conf.local.tmp -c "Edit by /etc/rc.cfg" \
				-JSs currdev /mnt/boot/loader.conf.local "\"disk0a:\""
			cp /tmp/loader.conf.local.tmp /mnt/boot/loader.conf.local
			rm -f /tmp/loader.conf.local.tmp

			umount /mnt
		else
			die "You already booted from main image! Skip changing image ..."
		fi
		echo "ELWIX boot setup complete."
		;;
	*)
		echo "Missing parameter - available options are :"
		echo "   load | save | erase | backup | restore | upgrade | "
		echo "   pkg_load | pkg_save | pkg2cf | boot2 | boot"
		;;
esac

cd -

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>