#!/bin/bash
#
# Copyright (C) 2010-2024 Canonical
# Some of this work - Copyright (C) 2017-2019 IBM
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
FWTS="Firmware Test Suite"
OPTIONS="/tmp/options.$$"
DIALOG_CMD="/tmp/fwts.cmd.$$"
OEM_TESTS_FILE="/fwts/oem-tests.lst"
export DIALOGRC="/usr/share/fwts/fwts-live-dialogrc"

FWTS_DATE=`date +%d%m%Y`
FWTS_TIME=`date +%H%M`

#
# Check if executed as root or with sudo
#
if [ $EUID -ne 0 ]; then
	echo "`basename $0`: must be executed with sudo"
	exit 1
fi

#
# for debugging, use:
# -  WORK_DIR=./fwts/$FWTS_DATE/$FWTS_TIME
# if booting in fwts-live
# - change FWTS_AUTO_SHUTDOWN= to alter default shutdown after testing completion
#
WORK_DIR=/fwts/$FWTS_DATE/$FWTS_TIME
FWTS_AUTO_SHUTDOWN="$1"
# Change shutdown to non-destructive default to not power cycle box unknowingly
SHUTDOWN_AT_END=0

#
# set WORK_DIR to /cdrom/fwts if booting via casper
#
if [ `grep -qs boot=casper /proc/cmdline; echo $?` -eq 0 ]; then
	CASPER_DETECTED=1
	WORK_DIR=/cdrom/fwts/$FWTS_DATE/$FWTS_TIME
fi

#
# check /proc/cmdline and argv[1] for FWTS_AUTO_SHUTDOWN to toggle auto shutdown
#

# live-build v5 uses boot=live, leave the following for backward compatibility
if [ `grep -qs FWTS_AUTO_SHUTDOWN=1 /proc/cmdline; echo $?` -ne 0 ]; then
	if [ -n "${CASPER_DETECTED:+x}" ]; then
		SHUTDOWN_AT_END=0
	elif [ -n "${FWTS_AUTO_SHUTDOWN:+x}" ]; then
		SHUTDOWN_AT_END=0
	fi
fi

do_help()
{
	dialog  --backtitle "$FWTS" --title "Help" --msgbox \
		"To make selections from the menu use the following keys:\n\nUse the up/down cursor keys to navigate to the desired menu option and the <Space> bar to tick the menu selection.\n\nUse the left/right or tab keys to navigate the button options and press the <Enter> key to select." 13 60
}

#
#  Run a bunch of tests and monitor progress
#
do_test()
{

	num_tests=`fwts $1 --show-tests | wc -l`
	num_tests=$((num_tests - 1))
	if [ $num_tests -gt 0 ]; then
		fwts --force-clean $1 --show-progress-dialog --log-type plaintext,html | dialog --backtitle "$FWTS" --title "$2" --gauge "" 13 80 0
	fi
	# sleep so the progress dialog is shown for a period of time, otherwise it just flashes by and end-user cannot tell what it was
	sleep 1
}

#
#  Get user choice of tests to run
#
select_tests()
{
	while true
	do
		declare -a tests
		x=0
		#
		#  Discover available tests
		#
		fwts --batch --batch-experimental --uefitests --unsafe --utils --show-tests | grep "^ "| sort | uniq > $OPTIONS
		while read test text
		do
			((x++))
			tests[$x]=$test
			txt="${txt} ${x} \"${text}\" off"
		done < $OPTIONS
		rm $OPTIONS

		#
		# Construct and run dialog
		#
		echo dialog --backtitle '"$FWTS"' --title '"Select Tests to Run"' --help-button  --checklist '"Select from the list below the test(s) you want to run. Use up/down cursor keys, space to select and enter to start:"' 20 70 $x $txt > $DIALOG_CMD
		. $DIALOG_CMD 2> $OPTIONS
		ret=$?
		rm $DIALOG_CMD

		#
		# Scan return selections and build fwts test scenarios
		#
		x=0
		for i in `cat $OPTIONS`
		do
			((x++))
			i=${i#\"}
			i=${i%\"}
			run_tests="$run_tests ${tests[$i]}"
		done

		#
		# Handle dialog exit states:
		#
		case $ret in
		0)
			if [ $x -eq 0 ]; then
				dialog --backtitle "$FWTS" --title "No Tests Selected!" --msgbox \
			"   You did not select any tests to be run." 5 50
			else
  				fwts --force-clean $run_tests --show-progress-dialog --log-type plaintext,html | dialog  --backtitle "$FWTS" --title "$2" --gauge "" 15 80 0
				done_tests_finish
			fi
			;;
		1)
			return
			;;
		2)
			do_help
			;;
		255)
			no_tests_finish
			;;
		esac
	done
}

#
#  Get user choices to create oem-tests.lst
#
select_oem_automation()
{
	while true
	do
		declare -a tests
		x=0
		#
		#  Discover available tests
		#
		fwts --batch --batch-experimental --uefitests --unsafe --show-tests | grep "^ "| sort | uniq > $OPTIONS
		while read test text
		do
			((x++))
			tests[$x]=$test
			txt="${txt} ${x} \"${text}\" off"
		done < $OPTIONS
		rm $OPTIONS

		#
		# Construct and run dialog
		#
		echo dialog --backtitle '"$FWTS"' --title '"Select Tests for Automation"' --help-button  --checklist '"Select from the list below the test(s) for oem-tests.lst. Use up/down cursor keys, space to select and enter to start:"' 20 70 $x $txt > $DIALOG_CMD
		. $DIALOG_CMD 2> $OPTIONS
		ret=$?
		rm $DIALOG_CMD

		#
		# Scan return selections and build fwts test scenarios
		#
		x=0
		for i in `cat $OPTIONS`
		do
			((x++))
			i=${i#\"}
			i=${i%\"}
			run_tests="$run_tests ${tests[$i]}"
		done

		#
		# Handle dialog exit states:
		#
		case $ret in
		0)
			if [ $x -eq 0 ]; then
				dialog --backtitle "$FWTS" --title "No Tests Selected!" --msgbox \
			"   You did not select any tests to be run." 5 50
			else
				# remove leading space and output to oem-tests.lst
				echo "TESTS=$(echo $run_tests)" > $OEM_TESTS_FILE
				echo "POST=poweroff" >> $OEM_TESTS_FILE
				dialog --backtitle "$FWTS" --title "Auto-tests Saved!" --msgbox \
					" Auto-tests are saved to $OEM_TESTS_FILE" 5 50
				finish
			fi
			;;
		1)
			return
			;;
		2)
			do_help
			;;
		255)
			no_tests_finish
			;;
		esac
	done
}
#
#  View the results log
#
view_results()
{
	tempfile=/tmp/view_results.tmp.$$
	dialog --print-maxsize 2>| $tempfile
	term_height=`sed -n "s/^MaxSize: \([0-9]*\), \([0-9]*\)$/\1/p" $tempfile 2> /dev/null`
	term_width=` sed -n "s/^MaxSize: \([0-9]*\), \([0-9]*\)$/\2/p" $tempfile 2> /dev/null`
	term_height=$((term_height))
	term_width=$((term_width - 2))
	rm $tempfile

	dialog --backtitle "$FWTS" --title "Test Results" \
		--textbox results.log \
		$term_height $term_width
}

#
#  Shutdown or exit
#
finish()
{

	dialog --help-button --backtitle "$FWTS" --title "Choose Exit" --menu \
	"\nSelect if you want to exit to a command line or poweroff.\n\nIf you select to poweroff and this is running from a LIVE USB, the host will be powered down.\n\nIf you are running this from a VM and you choose to poweroff, then only the VM will be powered down."\
	22 70 2 \
	"Exit" "Exit to a command line" \
	"Poweroff" "Shutdown and power off" \
	2> $OPTIONS

	case $? in
	0)
		cd $WORK_DIR >& /dev/null
		case `cat $OPTIONS` in
			'Exit')
				dialog --backtitle "$FWTS" --title "EXIT to Command Line" --msgbox "\nAfter doing all you want from the command line you can enter the following to power down the host.\n\n\"sudo poweroff\" ... you will need the sudo password.\n\nIf you enter \"exit\" you will be returned back to this application where you can run more tests or select the exit paths again (if you choose Poweroff from the menu you will NOT need the sudo password)." 20 80
				exit 0
				;;
			'Poweroff')
				dialog --backtitle "$FWTS" --infobox "Shutting down and powering off..." 5 80
				sleep 2
				poweroff
				exit 0
				;;
		esac
		;;
	1|255)
		return
		;;
	2)
		do_help
		;;
	esac

}

#
#  Aborted, no tests run
#
no_tests_finish()
{
	dialog --backtitle "$FWTS" --title "Abort Testing!" --msgbox \
		"You did not select any tests to be run.\n\nPress Enter to continue." 7 50
	finish
}

#
#  Finish after running some tests
#
done_tests_finish()
{
	#
	# Dump out firmware and system data, LP: #1322543
	#
	cd $WORK_DIR
	fwts --dump > /dev/null

	dialog  --backtitle "$FWTS" --title "Testing Complete" --yesno \
"The results can be found on the USB stick in the\n
the directory: /fwts/$FWTS_DATE/$FWTS_TIME/results.log\n\n
     Do you want to view the results log now?" 9 55
	case $? in
	0)
		view_results
		;;
	1|255)
		;;
	esac

	dialog  --backtitle "$FWTS" --title "Testing Complete" --msgbox \
"The results can be found on the USB stick in the\n
the directory: /fwts/$FWTS_DATE/$FWTS_TIME/results.log\n\nPress Enter to continue." 9 55
	finish
}

#
#  Check & run OEM customised tests in /fwts/oem-tests.lst
#
check_run_oem_tests()
{
	if [ -f $OEM_TESTS_FILE ]; then
		cd $WORK_DIR >& /dev/null
		OEM_TESTS=$(echo $(grep TESTS $OEM_TESTS_FILE) | cut -d '=' -f 2)
		do_test "${OEM_TESTS}" 'Running OEM Specified Tests'
		fwts --dump > /dev/null
		POST_ACTION=$(echo $(grep POST $OEM_TESTS_FILE) | cut -d '=' -f 2)
		if [ $POST_ACTION == "poweroff" ]; then
			dialog --backtitle "$FWTS" --infobox "Shutting down and powering off..." 5 80
		elif [ $POST_ACTION == "reboot" ]; then
			dialog --backtitle "$FWTS" --infobox "Restarting system..." 5 80
		else
			dialog --backtitle "$FWTS" --infobox "Exiting to terminal..." 5 80
			POST_ACTION=""
		fi
		sleep 2
		$POST_ACTION
		exit 0
	fi
}

#
#  Here we go..
#
mkdir -p $WORK_DIR >& /dev/null
if [ $? -ne 0 ]; then
	dialog --ok-label "Finish and End" --backtitle "$FWTS" --title "Error" --msgbox "Could not create directory fwts/$FWTS_DATE/$FWTS_TIME to store test results.\n\nPress Enter to continue." 8 50
	finish
	exit 0
fi

UNAME_PLATFORM=`(uname -m) 2>/dev/null` || UNAME_PLATFORM=unknown

while true
do

	check_run_oem_tests

	case "${UNAME_PLATFORM}" in
		ppc*)
			dialog --help-button --backtitle "$FWTS" --title "Select Tests" --radiolist \
			"This will run a suite of firmware tests that will check the device tree and related subsystems. It can also find issues that can cause Linux problems.\n\nThe default below is to run just all the Batch Tests, but you can select more tests below if required.\n\nPlease select below (using cursor up/down and space) and press enter to continue."\
			22 70 4 \
			"All" "All Batch Tests" on \
			"Recommended" "Recommended Tests" off \
			"Selected" "Select Individual Tests" off \
			"Abort" "Abort Testing" off \
			2> $OPTIONS
			;;
		aarch64)
			dialog --help-button --backtitle "$FWTS" --title "Select Tests" --radiolist \
			"This will run a suite of firmware tests that will check the BIOS and ACPI tables.  It can also find issues that can cause Linux problems.\n\nThe default below is to run just all the Batch Tests, but you can select more tests below if required.\n\nPlease select below (using cursor up/down and space) and press enter to continue."\
			24 70 8 \
			"All" "All Batch Tests" on \
			"ACPI" "ACPI Tests" off \
			"UEFI" "UEFI Tests" off \
			"TPM" "TPM Tests" off \
			"EBBR" "ARM EBBR Tests" off \
			"SBBR" "ARM SBBR Tests" off \
			"Recommended" "Recommended Tests" off \
			"Recommended for IFV" "Recommended Tests for F/W Vendors" off \
			"Selected" "Select Individual Tests" off \
			"Automated" "Create Auto-Tests" off \
			"Abort" "Abort Testing" off \
			2> $OPTIONS
			;;
		*)
			dialog --help-button --backtitle "$FWTS" --title "Select Tests" --radiolist \
			"This will run a suite of firmware tests that will check the BIOS and ACPI tables.  It can also find issues that can cause Linux problems.\n\nThe default below is to run just all the Batch Tests, but you can select more tests below if required.\n\nPlease select below (using cursor up/down and space) and press enter to continue."\
			24 70 8 \
			"All" "All Batch Tests" on \
			"ACPI" "ACPI Tests" off \
			"UEFI" "UEFI Tests" off \
			"TPM" "TPM Tests" off \
			"Recommended" "Recommended Tests" off \
			"Recommended for IFV" "Recommended Tests for F/W Vendors" off \
			"Selected" "Select Individual Tests" off \
			"Automated" "Create Auto-Tests" off \
			"Abort" "Abort Testing" off \
			2> $OPTIONS
			;;

	esac

	case $? in
	0)
		cd $WORK_DIR >& /dev/null
		case `cat $OPTIONS` in
		'All')
			case "${UNAME_PLATFORM}" in
				ppc*)
					FWTS_OPTIONS="--batch"
					;;
				*)
					FWTS_OPTIONS="--batch --uefitests"
					;;
			esac
			do_test "${FWTS_OPTIONS}" 'Running Batch Tests'
			done_tests_finish
			;;
		'ACPI')
			do_test "--acpitests" 'Running ACPI Tests'
			done_tests_finish
			;;
		'UEFI')
			do_test "--uefitests" 'Running UEFI Tests'
			done_tests_finish
			;;
		'TPM')
			do_test "tpm2 tpmevlog tpmevlogdump" 'Running TPM Tests'
			done_tests_finish
			;;
		'EBBR')
			do_test "--ebbr" 'Embedded Base Boot Requirements (EBBR) Tests'
			done_tests_finish
			;;
		'SBBR')
			do_test "--sbbr" 'Server Base Boot Requirements (SBBR) Tests'
			done_tests_finish
			;;
		'Recommended')
			case "${UNAME_PLATFORM}" in
				ppc*)
					FWTS_OPTIONS="--batch"
					;;
				aarch64)
					FWTS_OPTIONS="version cpufreq aspm dmicheck klog oops --acpitests --uefitests --log-level=medium"
					;;
				*)
					FWTS_OPTIONS="version cpufreq maxfreq msr mtrr nx virt aspm dmicheck apicedge klog oops --acpitests --uefitests --log-level=medium"
					;;
			esac

			do_test "${FWTS_OPTIONS}" 'Running Recommended Tests'
			done_tests_finish
			;;
		'Recommended for IFV')
			case "${UNAME_PLATFORM}" in
				aarch64)
					FWTS_OPTIONS="version cpufreq aspm dmicheck klog oops --acpitests --uefitests --ifv"
					;;
				*)
					FWTS_OPTIONS="version cpufreq maxfreq msr mtrr nx virt aspm dmicheck apicedge klog oops --acpitests --uefitests --ifv"
					;;
			esac

			do_test "${FWTS_OPTIONS}" 'Running Recommended Tests for F/W Vendors'
			done_tests_finish
			;;
		'Selected')
			select_tests
			;;
		'Automated')
			select_oem_automation
			;;
		'Abort')
			no_tests_finish
			;;
		esac
		;;
	2)
		do_help
		;;
	1|255)
		no_tests_finish
		;;
	esac
done
