meta-fii: meta-mori: Add mori-fw-utility files

Add the mori-fw file and package in packagegroup.
Supports: CPLD, BIOS, and Bootstrap flashing.

Signed-off-by: Grant Williams <grant.williams@fii-na.com>
Change-Id: I5541af3d07c01a0602f7794c5f1b6610622e93ee
diff --git a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw.bb b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw.bb
new file mode 100644
index 0000000..272106a
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw.bb
@@ -0,0 +1,36 @@
+SUMMARY = "Phosphor OpenBMC mori Firmware Upgrade Command"
+DESCRIPTION = "Phosphor OpenBMC mori Firmware Upgrade Comman Daemon"
+
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit systemd
+inherit obmc-phosphor-systemd
+
+DEPENDS += "systemd"
+DEPENDS += "phosphor-ipmi-flash"
+RDEPENDS:${PN} += "libsystemd"
+RDEPENDS:${PN} += "bash"
+
+SRC_URI = " \
+    file://mori-fw.sh \
+    file://mori-fw-ver.service \
+    file://mori-fw-ver.sh \
+    file://mori-lib.sh \
+    "
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE:${PN} = " \
+    mori-fw-ver.service \
+    "
+
+do_install () {
+    install -d ${D}${sbindir}
+    install -d ${D}${libexecdir}/${PN}
+    install -m 0755 ${WORKDIR}/mori-fw.sh ${D}${sbindir}/mori-fw.sh
+    install -m 0755 ${WORKDIR}/mori-fw-ver.sh ${D}${libexecdir}/${PN}/mori-fw-ver.sh
+    install -m 0755 ${WORKDIR}/mori-lib.sh ${D}${libexecdir}/${PN}/mori-lib.sh
+    install -d ${D}${systemd_system_unitdir}
+    install -m 0644 ${WORKDIR}/mori-fw-ver.service ${D}${systemd_system_unitdir}
+}
diff --git a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw-ver.service b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw-ver.service
new file mode 100644
index 0000000..b0a6618
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw-ver.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Firmware Version Daemon
+
+[Service]
+RemainAfterExit=yes
+Type=oneshot
+ExecStart=/usr/libexec/mori-fw/mori-fw-ver.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw-ver.sh b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw-ver.sh
new file mode 100644
index 0000000..8055e63
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw-ver.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+# Disable check for splitting
+# shellcheck disable=SC2207
+# Provide source directive to shellcheck.
+# shellcheck source=meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
+source /usr/libexec/mori-fw/mori-lib.sh
+
+BMC_CPLD_VER_FILE="/run/cpld0.version"
+MB_CPLD_VER_FILE="/run/cpld1.version"
+ver=''
+
+function fw_rev() {
+    case $1 in
+    cpldb)
+        rsp=($(i2cget -y -f -a "${I2C_BMC_CPLD[0]}" 0x"${I2C_BMC_CPLD[1]}" 0x00 i 5))
+        ver=$(printf '%d.%d.%d.%d' "${rsp[4]}" "${rsp[3]}" "${rsp[2]}" "${rsp[1]}")
+        ;;
+    cpldm)
+        rsp=($(i2cget -y -f -a "${I2C_MB_CPLD[0]}" 0x"${I2C_MB_CPLD[1]}" 0x00 i 5))
+        ver=$(printf '%d.%d.%d.%d' "${rsp[4]}" "${rsp[3]}" "${rsp[2]}" "${rsp[1]}")
+        ;;
+    *)
+        ;;
+    esac
+}
+
+fw_rev cpldb
+echo "BMC CPLD version : ${ver}"
+echo "${ver}" > "${BMC_CPLD_VER_FILE}"
+fw_rev cpldm
+echo "MB CPLD version  : ${ver}"
+echo "${ver}" > "${MB_CPLD_VER_FILE}"
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
new file mode 100644
index 0000000..401b5bc
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-fw.sh
@@ -0,0 +1,152 @@
+#!/bin/bash
+
+# Provide source directive to shellcheck.
+# shellcheck source=meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
+# Disable check for globbing and word splitting within double quotes
+# shellcheck disable=SC2086
+source /usr/libexec/mori-fw/mori-lib.sh
+
+function fwbios() {
+  KERNEL_FIU_ID="c0000000.spi"
+  KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU"
+
+  # 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
+
+  # write to the mtd device
+  BIOS_MTD=$(grep "bios" /proc/mtd | sed -n 's/^\(.*\):.*/\1/p')
+
+  if [ ! -f "$1" ]; then
+    echo " Cannot find the" "$1" "image file"
+    return 1
+
+  fi
+  echo "Flashing BIOS @/dev/${BIOS_MTD}"
+  if [ "$(flashcp -v $1 /dev/${BIOS_MTD})" -ne  0 ]; then
+    echo "Flashing the bios failed " >&2
+    return 1
+  fi
+  wait
+
+  # 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
+
+  return 0
+}
+
+function fwbmccpld() {
+  # MB_JTAG_MUX 0:CPU 1:MB
+  # BMC_JTAG_MUX 0:GF/MB 1:BMC
+  set_gpio_ctrl MB_JTAG_MUX_SEL 0
+  set_gpio_ctrl BMC_JTAG_MUX_SEL  1
+  if [ "$(loadsvf -d /dev/jtag0 -s $1 -m 0)" -ne  0 ]; then
+    echo "BMC CPLD update failed" >&2
+    return 1
+  fi
+  wait
+
+  return 0
+}
+
+function fwmbcpld() {
+  # MB_JTAG_MUX 0:CPU 1:MB
+  # BMC_JTAG_MUX 0:GF/MB 1:BMC
+  set_gpio_ctrl MB_JTAG_MUX_SEL 1
+  set_gpio_ctrl BMC_JTAG_MUX_SEL  0
+  if [ "$(loadsvf -d /dev/jtag0 -s $1 -m 0)" -ne  0 ]; then
+    echo "Mobo CPLD update failed" >&2
+    return 1
+  fi
+  wait
+  set_gpio_ctrl MB_JTAG_MUX_SEL 0
+
+  return 0
+}
+
+function fwbootstrap() {
+  # to flash the CPU EEPROM
+  #unbind bootstrap EEPROM
+  echo ${I2C_CPU_EEPROM[0]}-00${I2C_CPU_EEPROM[1]} > /sys/bus/i2c/drivers/at24/unbind
+
+  #switch access to BMC
+  set_gpio_ctrl CPU_EEPROM_SEL 0
+
+  if [ "$(ampere_eeprom_prog -b ${I2C_CPU_EEPROM[0]} -s 0x${I2C_CPU_EEPROM[1]} -p -f $1)" -ne  0 ]; then
+    echo "CPU bootstrap EEPROM update failed" >&2
+    return 1
+  fi
+  wait
+
+  #bind bootstrap EEPROM
+  echo ${I2C_CPU_EEPROM[0]}-00${I2C_CPU_EEPROM[1]} > /sys/bus/i2c/drivers/at24/bind
+ 
+  #switch back access to CPU
+  set_gpio_ctrl CPU_EEPROM_SEL 1
+  return 0
+
+}
+
+function fwmb_pwr_seq(){
+  #$1 PS seq config file
+  if [[ ! -e "$1" ]]; then
+    echo "The file $1 does not exist"
+    return 1
+  fi
+  echo "${I2C_MB_PWRSEQ[0]}"-00"${I2C_MB_PWRSEQ[1]}" > /sys/bus/i2c/drivers/adm1266/unbind
+  if [ "$(adm1266_fw_fx $1)" -ne  0 ]; then
+
+    echo "The power seq flash failed" >&2
+    return 1
+  fi
+  echo "${I2C_MB_PWRSEQ[0]}"-00"${I2C_MB_PWRSEQ[1]}" > /sys/bus/i2c/drivers/adm1266/bind
+
+  return 0
+}
+
+if [[ ! $(which flashcp) ]]; then
+    echo "flashcp utility not installed"
+    exit 1
+fi
+if [[ ! $(which loadsvf) ]]; then
+    echo "loadsvf utility not installed"
+    exit 1
+fi
+if [[ ! -e /dev/jtag0 ]]; then
+    echo "Jtag device driver not functional"
+    exit 1
+fi
+
+case $1 in
+  bios)
+    fwbios "$2"
+    ;;
+  bmccpld)
+    fwbmccpld "$2"
+    ;;
+  mbcpld)
+    fwmbcpld "$2"
+    ;;
+  bootstrap)
+    fwbootstrap "$2"
+    ;;
+  mbseq)
+    fwmb_pwr_seq "$2"
+    ;;
+  *)
+    ;;
+esac
+ret=$?
+
+rm -f "$2"
+
+exit $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
new file mode 100644
index 0000000..744a3b9
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-fw-utility/mori-fw/mori-lib.sh
@@ -0,0 +1,52 @@
+#!/bin/bash
+
+# Disable check for usage of the definitions within mori-lib.sh
+#shellcheck disable=SC2034
+
+# get_gpio_num
+# Dynamically obtains GPIO number from chip base and I2C expanders
+# line-name
+function get_gpio_num() {
+    #shellcheck disable=SC2207
+    CHIP_PIN=($(gpiofind "$1" | awk '{print substr ($1, 9 ), $2 }'))
+    #shellcheck disable=SC2128
+    if [ -z "$CHIP_PIN" ]; then
+        echo "Could not find GPIO with name: $1"
+        return 1
+    fi
+
+    if [ "${CHIP_PIN[0]}" -gt 7 ]; then
+        BUS_ADDR=$(gpiodetect | grep gpiochip"${CHIP_PIN[0]}" | awk '{print substr($2, 2, length($2) - 2)}')
+        GPIO_BASE=$(cat /sys/bus/i2c/devices/"$BUS_ADDR"/gpio/*/base)
+        echo "$((GPIO_BASE+CHIP_PIN[1]))"
+    else
+        echo "$((CHIP_PIN[0]*32+CHIP_PIN[1]))"
+    fi
+}
+
+# set_gpio_ctrl
+# line-name, high(1)/low(0)
+function set_gpio_ctrl() {
+    #shellcheck disable=SC2046
+    gpioset $(gpiofind "$1")="$2"
+}
+
+# get_gpio_ctrl
+# line-name
+function get_gpio_ctrl() {
+    GPIO_NUM=$(get_gpio_num "$1")
+    echo "$GPIO_NUM" > /sys/class/gpio/export
+    cat /sys/class/gpio/gpio"$GPIO_NUM"/value
+    echo "$GPIO_NUM" > /sys/class/gpio/unexport
+}
+
+# Start definitions
+
+# I2C Definitions 
+# The array is (<bus> <address>), where address is in hexadecimal.
+I2C_BMC_CPLD=(13 76)
+I2C_MB_CPLD=(0 76)
+I2C_FANCTRL=(35 2c)
+I2C_BMC_PWRSEQ=(48 59)
+I2C_MB_PWRSEQ=(40 40)
+I2C_CPU_EEPROM=(19 50)
diff --git a/meta-fii/meta-mori/recipes-phosphor/image/obmc-phosphor-image.bbappend b/meta-fii/meta-mori/recipes-phosphor/image/obmc-phosphor-image.bbappend
index 5694467..6eda748 100644
--- a/meta-fii/meta-mori/recipes-phosphor/image/obmc-phosphor-image.bbappend
+++ b/meta-fii/meta-mori/recipes-phosphor/image/obmc-phosphor-image.bbappend
@@ -33,6 +33,7 @@
 OBMC_IMAGE_EXTRA_INSTALL:append:mori = " estoraged"
 OBMC_IMAGE_EXTRA_INSTALL:append:mori = " phosphor-pid-control"
 OBMC_IMAGE_EXTRA_INSTALL:append:mori = " phosphor-logging"
+OBMC_IMAGE_EXTRA_INSTALL:append:mori = " mori-fw"
 
 # Required for phosphor-ipmi-ssif
 OBMC_IMAGE_EXTRA_INSTALL:append:mori = " virtual-obmc-host-ipmi-hw"