meta-ampere: mtjade: Add Host firmware update support

Enable flash_bios feature and configure to use backend script to flash
UEFI firmware via Redfish, WebUI and Rest.

Tested:
1. Flash UEFI firmware via WebUI.
2. Flash UEFI firmware via Redfish UpdateService
3. Flash UEFI firmware via Rest interface.

Signed-off-by: Thang Q. Nguyen <thang@os.amperecomputing.com>
Change-Id: I772e6a409414e374ed01e72e3c69985d0b413cb5
diff --git a/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb b/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb
index 2990d8e..0831988 100644
--- a/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb
+++ b/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb
@@ -7,13 +7,16 @@
 PROVIDES = "${PACKAGES}"
 PACKAGES = " \
         ${PN}-chassis \
+        ${PN}-flash \
         ${PN}-system \
         "
 
 PROVIDES += "virtual/obmc-chassis-mgmt"
+PROVIDES += "virtual/obmc-flash-mgmt"
 PROVIDES += "virtual/obmc-system-mgmt"
 
 RPROVIDES_${PN}-chassis += "virtual-obmc-chassis-mgmt"
+RPROVIDES_${PN}-flash += "virtual-obmc-flash-mgmt"
 RPROVIDES_${PN}-system += "virtual-obmc-system-mgmt"
 
 SUMMARY_${PN}-chassis = "Ampere Chassis"
@@ -25,3 +28,8 @@
 SUMMARY_${PN}-system = "Ampere System"
 RDEPENDS_${PN}-system = " \
         "
+
+SUMMARY_${PN}-flash = "Ampere Flash"
+RDEPENDS_${PN}-flash = " \
+        ampere-flash-utils \
+        "
diff --git a/meta-ampere/meta-common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-host-bios@.service b/meta-ampere/meta-common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-host-bios@.service
new file mode 100644
index 0000000..c344600
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-host-bios@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Ampere service for flashing the Host firmware image
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart=/usr/sbin/ampere_flash_bios.sh /tmp/images/%I/*.img
diff --git a/meta-ampere/meta-common/recipes-phosphor/flash/phosphor-software-manager_%.bbappend b/meta-ampere/meta-common/recipes-phosphor/flash/phosphor-software-manager_%.bbappend
new file mode 100644
index 0000000..5d75c71
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/flash/phosphor-software-manager_%.bbappend
@@ -0,0 +1,7 @@
+FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
+
+PACKAGECONFIG[flash_bios] = "-Dhost-bios-upgrade=enabled, -Dhost-bios-upgrade=disabled"
+
+PACKAGECONFIG_append_ = " flash_bios"
+
+SYSTEMD_SERVICE_${PN}-updater += "${@bb.utils.contains('PACKAGECONFIG', 'flash_bios', 'obmc-flash-host-bios@.service', '', d)}"
diff --git a/meta-ampere/meta-jade/conf/machine/mtjade.conf b/meta-ampere/meta-jade/conf/machine/mtjade.conf
index 5999392..2180189 100644
--- a/meta-ampere/meta-jade/conf/machine/mtjade.conf
+++ b/meta-ampere/meta-jade/conf/machine/mtjade.conf
@@ -6,8 +6,21 @@
 require conf/machine/include/obmc-bsp-common.inc
 require conf/distro/include/phosphor-isolation.inc
 
+OBMC_MACHINE_FEATURES += "\
+        obmc-phosphor-chassis-mgmt \
+        obmc-phosphor-flash-mgmt \
+        obmc-chassis-state-mgmt \
+        obmc-bmc-state-mgmt \
+        "
+
 VOLATILE_LOG_DIR = "no"
 
 OBMC_IMAGE_EXTRA_INSTALL_append = "\
                                    ampere-usbnet \
+                                   ampere-flash-utils \
                                   "
+
+PREFERRED_PROVIDER_virtual/obmc-chassis-mgmt = "packagegroup-ampere-apps"
+PREFERRED_PROVIDER_virtual/obmc-system-mgmt = "packagegroup-ampere-apps"
+PREFERRED_PROVIDER_virtual/obmc-flash-mgmt = "packagegroup-ampere-apps"
+PREFERRED_PROVIDER_virtual/phosphor-ipmi-fru-hostfw-config ?= "hostboot-inventory-config-native"
diff --git a/meta-ampere/meta-jade/recipes-ampere/flash/ampere-flash-utils.bb b/meta-ampere/meta-jade/recipes-ampere/flash/ampere-flash-utils.bb
new file mode 100644
index 0000000..9b28401
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/flash/ampere-flash-utils.bb
@@ -0,0 +1,23 @@
+
+SUMMARY = "Ampere Computing LLC Flashing Utilities"
+DESCRIPTION = "Application to support flashing utilities on Ampere platforms"
+PR = "r0"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+RDEPENDS_${PN} = "bash"
+DEPENDS = "zlib"
+
+SRC_URI += "\
+            file://ampere_flash_bios.sh \
+           "
+
+S = "${WORKDIR}"
+ROOT = "${STAGING_DIR_TARGET}"
+
+LDFLAGS += "-L ${ROOT}/usr/lib/ -lz "
+
+do_install_append() {
+    install -d ${D}/usr/sbin
+    install -m 0755 ${S}/ampere_flash_bios.sh ${D}/${sbindir}/ampere_flash_bios.sh
+}
diff --git a/meta-ampere/meta-jade/recipes-ampere/flash/ampere-flash-utils/ampere_flash_bios.sh b/meta-ampere/meta-jade/recipes-ampere/flash/ampere-flash-utils/ampere_flash_bios.sh
new file mode 100755
index 0000000..876671c
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-ampere/flash/ampere-flash-utils/ampere_flash_bios.sh
@@ -0,0 +1,103 @@
+#!/bin/bash
+#
+# Copyright (c) 2021 Ampere Computing LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#	http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+do_flash () {
+	# Check the PNOR partition available
+	HOST_MTD=$(cat /proc/mtd | grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
+	if [ -z "$HOST_MTD" ];
+	then
+		# If the PNOR partition is not available, then bind again driver
+		echo "--- Bind the ASpeed SMC driver"
+		echo 1e630000.spi > /sys/bus/platform/drivers/aspeed-smc/bind
+		sleep 2
+
+		HOST_MTD=$(cat /proc/mtd | grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
+		if [ -z "$HOST_MTD" ];
+		then
+			echo "Fail to probe Host SPI-NOR device"
+			exit 1
+		fi
+
+		echo "--- Flashing firmware to @/dev/$HOST_MTD"
+		flash_eraseall /dev/$HOST_MTD
+		flashcp -v $IMAGE /dev/$HOST_MTD
+
+		echo "--- Unbind the ASpeed SMC driver"
+		echo 1e630000.spi > /sys/bus/platform/drivers/aspeed-smc/unbind
+	else
+		echo "--- Flashing firmware to @/dev/$HOST_MTD"
+		flash_eraseall /dev/$HOST_MTD
+		flashcp -v $IMAGE /dev/$HOST_MTD
+	fi
+}
+
+
+if [ $# -eq 0 ]; then
+	echo "Usage: $(basename $0) <BIOS image file>"
+	exit 0
+fi
+
+IMAGE="$1"
+if [ ! -f $IMAGE ]; then
+	echo $IMAGE
+	echo "The image file $IMAGE does not exist"
+	exit 1
+fi
+
+# Turn off the Host if it is currently ON
+chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
+echo "--- Current Chassis State: $chassisstate"
+if [ "$chassisstate" == 'On' ];
+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
+fi
+
+# Switch the host SPI bus to BMC"
+echo "--- Switch the host SPI bus to BMC."
+gpioset 0 226=0
+
+if [[ $? -ne 0 ]]; then
+	echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state"
+	exit 1
+fi
+
+# Flash the firmware
+do_flash
+
+# Switch the host SPI bus to HOST."
+echo "--- Switch the host SPI bus to HOST."
+gpioset 0 226=1
+
+if [[ $? -ne 0 ]]; then
+	echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state"
+	exit 1
+fi
+
+if [ "$chassisstate" == 'On' ];
+then
+	sleep 5
+	echo "Turn on the Host"
+	obmcutil chassison
+fi