meta-ampere: mtjade: fix flash BIOS occasional fail

Before flashing UEFI firmware, the BMC calls "obmcutil chassisoff" to
turn OFF the Host and wait 10s before checking if the Host is actually
OFF or not.
In some cases, 10s is not enough. This will make the flashing script
sees the Host is still ON and bypasses the flashing.
This commit changes to check Host state each 2s until maximum 60s to
avoid above failure.

Tested: check below steps 10 times and ensure all done successful:
  1. Request to flash SCP firmware via Redfish.
  2. Sleep 120s, when CPU is booting (did not jump to OS)
  3. Request to flash UEFI firmware via Redfish.
  4. Check if the flashing successes

Signed-off-by: Thang Tran <thuutran@amperecomputing.com>
Change-Id: I403f4b432e53b3b091315612488c50dbb595ea76
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_firmware_upgrade.sh b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_firmware_upgrade.sh
index 130682d..6f0a11f 100755
--- a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_firmware_upgrade.sh
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_firmware_upgrade.sh
@@ -31,14 +31,26 @@
 	then
 		echo "Turning the Chassis off"
 		obmcutil chassisoff
-		sleep 15
-		# Check if HOST was OFF
-		chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
-		if [ "$chassisstate_off" == 'On' ];
-		then
-			echo "Error : Failed turning the Chassis off"
-			exit
-		fi
+
+		# Wait 60s until Chassis is off
+		cnt=30
+		while [ "$cnt" -gt 0 ];
+		do
+			cnt=$((cnt - 1))
+			sleep 2
+			# Check if HOST was OFF
+			chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
+			if [ "$chassisstate_off" != 'On' ];
+			then
+				break
+			fi
+
+			if [ "$cnt" == "0" ];
+			then
+				echo "--- Error : Failed turning the Chassis off"
+				exit 1
+			fi
+		done
 	fi
 
 	if [[ $SECPRO == 1 ]]; then
@@ -73,6 +85,15 @@
 	# 226 is BMC_GPIOAC2_SPI0_PROGRAM_SEL
 	gpioset 0 226=1
 
+	# Deassert SECPRO GPIO PINs
+        if [[ $SECPRO == 1 ]]; then
+                echo "De-asserting special GPIO PINs"
+                # 3 is S0_SPECIAL_BOOT
+                gpioset 0 3=0
+                # 66 is S1_SPECIAL_BOOT
+                gpioset 0 66=0
+        fi
+
 	if [ "$chassisstate" == 'On' ];
 	then
 		sleep 5
@@ -80,20 +101,6 @@
 		obmcutil poweron
 	fi
 
-	# Deassert SECPRO GPIO PINs
-	if [[ $SECPRO == 1 ]]; then
-		chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
-		if [ "$chassisstate_off" == 'Off' ]; then
-			obmcutil poweron
-		fi
-
-		sleep 30s
-		echo "De-asserting special GPIO PINs"
-		# 3 is S0_SPECIAL_BOOT
-		gpioset 0 3=0
-		# 66 is S1_SPECIAL_BOOT
-		gpioset 0 66=0
-	fi
 }
 
 
diff --git a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_flash_bios.sh b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_flash_bios.sh
index 7852dc4..5a13da4 100755
--- a/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_flash_bios.sh
+++ b/meta-ampere/meta-jade/recipes-ampere/platform/ampere-utils/ampere_flash_bios.sh
@@ -69,14 +69,26 @@
 then
 	echo "--- Turning the Chassis off"
 	obmcutil chassisoff
-	sleep 10
-	# Check if HOST was OFF
-	chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
-	if [ "$chassisstate_off" == 'On' ];
-	then
-		echo "--- Error : Failed turning the Chassis off"
-		exit 1
-	fi
+
+	# Wait 60s until Chassis is off
+	cnt=30
+	while [ "$cnt" -gt 0 ];
+	do
+		cnt=$((cnt - 1))
+		sleep 2
+		# Check if HOST was OFF
+		chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
+		if [ "$chassisstate_off" != 'On' ];
+		then
+			break
+		fi
+
+		if [ "$cnt" == "0" ];
+		then
+			echo "--- Error : Failed turning the Chassis off"
+			exit 1
+		fi
+	done
 fi
 
 # Switch the host SPI bus to BMC"