meta-fii: meta-mori: Add BIOS accessing library functions

The BMC accesses the BIOS SPI during flashing, but the BMC will
potentially need to access it while the host is running. Add library
functions that performs a handshake to request access to it. Update
current BIOS SPI access to use the functions. Also fix shellcheck
error.

Tested: Flashed BMC image on unit. Ensured that BIOS flash is still
successful.

Change-Id: Icc91802647a6967694fca3e57bbc56f2c1db9189
Signed-off-by: Kyle Nieman <kyle.nieman@fii-na.com>
diff --git a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw.sh b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw.sh
index a0f1154..4b0ab09 100644
--- a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw.sh
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw.sh
@@ -7,23 +7,13 @@
 source /usr/libexec/mori-fw/mori-lib.sh
 
 function fwbios() {
-  KERNEL_FIU_ID="c0000000.spi"
-  KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU"
   ret=0
   if [ ! -f "$1" ]; then
     echo " Cannot find the" "$1" "image file"
     return 1
   fi
 
-  # switch the SPI mux from Host to BMC
-  set_gpio_ctrl FM_BIOS_FLASH_SPI_MUX_R_SEL 1
-
-  # rescan the spi bus
-  if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
-    echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
-    sleep 1
-  fi
-  echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/bind
+  setup_bios_access
 
   # write to the mtd device
   BIOS_MTD=$(grep "hnor" /proc/mtd | sed -n 's/^\(.*\):.*/\1/p')
@@ -34,11 +24,7 @@
     ret=1
   fi
 
-  # switch the SPI mux from BMC to Host
-  if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
-    echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
-  fi
-  set_gpio_ctrl FM_BIOS_FLASH_SPI_MUX_R_SEL 0
+  cleanup_bios_access
 
   return $ret
 }
diff --git a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
index 9c443bf..45303c6 100644
--- a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
@@ -33,7 +33,7 @@
             if [[ $(gpiofind "$1" | cut -d " " -f 1) == "$X" ]]; then
                 # Used to select the correct GPIO_BASE value
                 #shellcheck disable=SC2086
-                GPIO_BASE_DIR=$(echo ${GPIO_BASE_DIR} | cut -d " " -f $count)
+                GPIO_BASE_DIR=("$(echo ${GPIO_BASE_DIR} | cut -d " " -f $count)")
                 break
             fi
             count=$((count-1))
@@ -85,6 +85,48 @@
   fi
 }
 
+function cleanup_bios_access() {
+  # Run only if setup_bios_access was previously ran
+  if Does_File_Exist /run/bios_access &> /dev/null ; then
+    # switch the SPI mux from BMC to Host
+    if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
+      echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
+    fi
+    set_gpio_ctrl FM_BIOS_FLASH_SPI_MUX_R_SEL 0
+
+    # Indicate to host that BMC is finished accessing SPI
+    set_gpio_ctrl S0_BMC_SPI_NOR_ACCESS 0
+
+    rm /run/bios_access
+  fi
+}
+
+function setup_bios_access() {
+  # Run only if setup_bios_access was not previously ran without cleanup
+  if ! Does_File_Exist /run/bios_access &> /dev/null ; then
+    echo "BMC is accessing BIOS" > /run/bios_access
+
+    # rescan the spi bus
+    if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
+      echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
+      usleep 100
+    fi
+
+    # Wait until the host is finished accessing the SPI
+    while [[ $(get_gpio_ctrl S0_SOC_SPI_NOR_ACCESS) == 1 ]]
+    do
+      sleep 1
+    done
+    # Indicate to host that BMC is accessing SPI
+    set_gpio_ctrl S0_BMC_SPI_NOR_ACCESS 1
+
+    # switch the SPI mux from Host to BMC
+    set_gpio_ctrl FM_BIOS_FLASH_SPI_MUX_R_SEL 1
+
+    echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/bind
+  fi
+}
+
 # Start definitions
 
 # I2C Definitions
@@ -101,3 +143,7 @@
 # File Path Definition
 # File path used to prevent hotswapping
 RST_LOCK_FILE="/etc/FW_FLASH_ONGOING"
+
+# Device name and driver path used for BIOS SPI
+KERNEL_FIU_ID="c0000000.spi"
+KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU"