meta-fii: meta-mori: Add mori-boot and mori-cmd

Summary of Changes:
 - Add basic mori.sh tool
 - Add mori-boot for host and switch initialization

Signed-off-by: Charles Boyer <Charles.Boyer@fii-usa.com>
Change-Id: I2cee6a2b6a15a1e2f9a53baf331fb452f73f080d
diff --git a/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot.bb b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot.bb
new file mode 100644
index 0000000..bceca8d
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot.bb
@@ -0,0 +1,30 @@
+SUMMARY = "Phosphor OpenBMC Mori System Power Control Service"
+DESCRIPTION = "Phosphor OpenBMC Mori System Power Control 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"
+RDEPENDS:${PN} += "libsystemd"
+RDEPENDS:${PN} += "bash"
+
+SRC_URI = " \
+    file://init_once.sh \
+    file://host-powerctrl.service \
+    "
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE:${PN} = " \
+    host-powerctrl.service \
+    "
+
+do_install () {
+    install -d ${D}${libexecdir}/${PN}
+    install -m 0755 ${WORKDIR}/init_once.sh ${D}${libexecdir}/${PN}
+    install -d ${D}${systemd_unitdir}/system/
+    install -m 0644 ${WORKDIR}/host-powerctrl.service ${D}${systemd_unitdir}/system
+}
diff --git a/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot/host-powerctrl.service b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot/host-powerctrl.service
new file mode 100644
index 0000000..8a67ffb
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot/host-powerctrl.service
@@ -0,0 +1,16 @@
+[Unit]
+Description = Host Power Control Sequence
+Wants=ssifbridge.service
+After=ssifbridge.service
+Wants=mori-bios-verify.service
+After=mori-bios-verify.service
+
+[Service]
+Restart=no
+RemainAfterExit=true
+Type=oneshot
+ExecStart=/usr/libexec/mori-boot/init_once.sh
+StandardOutput=syslog
+
+[Install]
+WantedBy=sysinit.target
diff --git a/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot/init_once.sh b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot/init_once.sh
new file mode 100644
index 0000000..1c59aa0
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-boot/init_once.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+# 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
+
+function set_mux_default(){
+    # set all mux route to CPU before power on host
+    # BMC_CPU_DDR_I2C_SEL
+    set_gpio_ctrl CPU_DDR_SEL 1
+    # BMC_CPU_EEPROM_I2C_SEL
+    set_gpio_ctrl CPU_EEPROM_SEL 1
+    # BMC_CPU_PMBUS_SEL
+    set_gpio_ctrl CPU_VRD_SEL 1
+
+    # CPU Host UART Mux
+    set_gpio_ctrl S0_UART0_BMC_SEL 0
+    # SCP Host UART Mux
+    set_gpio_ctrl S0_UART1_BMC_SEL 0
+
+    # LED control
+    # LED_BMC_LIVE
+    set_gpio_ctrl LED_BMC_ALIVE 1
+}
+
+set_mux_default
+set_gpio_ctrl HPM_STBY_RST_N 1
+sleep 5
+set_gpio_ctrl S0_BMC_OK 1
+
+# Create /run/openbmc for system power files
+if [[ ! -d "/run/openbmc" ]]; then
+  mkdir "/run/openbmc"
+fi
+
+echo "BMC initialization complete"
diff --git a/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-cmd.bb b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-cmd.bb
new file mode 100644
index 0000000..34d9e04
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-cmd.bb
@@ -0,0 +1,19 @@
+SUMMARY = "Phosphor OpenBMC Mori System Command"
+DESCRIPTION = "Phosphor OpenBMC Mori System Command Daemon"
+
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+RDEPENDS:${PN} += "bash"
+
+SRC_URI = " \
+    file://mori.sh \
+    "
+
+SYSTEMD_PACKAGES = "${PN}"
+
+do_install () {
+    install -d ${D}${sbindir}
+    install -m 0755 ${WORKDIR}/mori.sh ${D}${sbindir}/mori.sh
+}
diff --git a/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-cmd/mori.sh b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-cmd/mori.sh
new file mode 100644
index 0000000..d9ad531
--- /dev/null
+++ b/meta-fii/meta-mori/recipes-mori/mori-sys-utility/mori-cmd/mori.sh
@@ -0,0 +1,266 @@
+#!/bin/bash
+# help information
+
+# 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
+
+function usage_rst() {
+  echo " mori rst [parameter]"
+  echo "        hotswap  --> reset the whole mori node"
+  echo "        system   --> reset the host"
+  echo "        btn      --> trigger a power button event"
+  echo "        shutdown --> send out shutdown signal to CPU"
+  echo "        display  --> "
+}
+
+function usage_led() {
+  echo " mori led 'attn'/'boot' [parameter]"
+  echo "        on --> turn led on"
+  echo "        off --> turn led off"
+  echo "        toggle --> toggle led"
+  echo "        status --> get status of led"
+}
+
+function usage_uart() {
+  echo " mori uart [parameter]"
+  echo "        host --> show CPU console"
+  echo "        mpro --> show Mpro console"
+  echo "        swhost --> change to CPU console to ttyS1"
+  echo "        swmpro --> change to CPU 0 Mpro console to ttyS3"
+  echo "        swhosthr --> change CPU console to header"
+  echo "        swmprohr --> change Mpro console to header"
+  echo "        display  --> "
+}
+
+function usage() {
+  echo " mori BMC console system utilities"
+  echo " mori [optional] [parameter]"
+  echo "   rst   --> reset traget device"
+  echo "   fw    --> get version"
+  echo "   uart  --> control the uart mux"
+  echo "   led   --> control the leds"
+}
+
+function reset() {
+  case $1 in
+    hotswap)
+      # Virtual AC reset
+      echo "mori.sh rst hotswap occurred"
+      set_gpio_ctrl HOTSWAP 1
+      ;;
+    system)
+      # S0 system reset
+      set_gpio_ctrl S0_SYSRESET 0
+      sleep 1
+      set_gpio_ctrl S0_SYSRESET 1
+      ;;
+    btn)
+      # virtual power button on
+      set_gpio_ctrl POWER_OUT 0
+      sleep 1
+      set_gpio_ctrl POWER_OUT 1
+      ;;
+    shutdown)
+      # request host shutdown
+      set_gpio_ctrl S0_SHD_REQ  0
+      sleep 3
+      set_gpio_ctrl S0_SHD_REQ  1
+      ;;
+    forceOff)
+      # virtual power button off
+      set_gpio_ctrl POWER_OUT 0
+      sleep 6
+      set_gpio_ctrl POWER_OUT 1
+      ;;
+    display)
+      echo "Virtual AC Reset:     GPIO$(get_gpio_num HOTSWAP)" "State:$(get_gpio_ctrl HOTSWAP)"
+      echo "Virtual Power Button: GPIO$(get_gpio_num POWER_OUT)" "State:$(get_gpio_ctrl POWER_OUT)"
+      echo "S0 System Reset:      GPIO$(get_gpio_num S0_SYSRESET)" "State:$(get_gpio_ctrl S0_SYSRESET)"
+      echo "S0 Shutdown Request:  GPIO$(get_gpio_num S0_SHD_REQ)" "State:$(get_gpio_ctrl S0_SHD_REQ)"
+      ;;
+    *)
+      usage_rst
+    ;;
+  esac
+}
+
+function fw_rev() {
+  BMC_CPLD_VER_FILE="/run/cpld0.version"
+  MB_CPLD_VER_FILE="/run/cpld1.version"
+
+  cmd=$(cat ${BMC_CPLD_VER_FILE})
+  echo " BMC_CPLD: " "${cmd}"
+  cmd=$(cat ${MB_CPLD_VER_FILE})
+  echo " MB_CPLD: " "${cmd}"
+
+  major=$(ipmitool mc info | grep "Firmware Revision" | awk '{print $4}')
+  cmd=$(ipmitool mc info | tail -4 | tr -s '\t' ' ' | tr -s '\n' ' ')
+
+  for hex in $cmd; do
+    minor="${hex:2}$minor";
+  done
+
+  minor=$(echo "obase=10; ibase=16; ${minor^^}" | bc)
+  echo " BMC        : " "${major}"."${minor}"
+
+  #BMC PWR Sequencer
+  i2cset -y -f -a "${I2C_BMC_PWRSEQ[0]}" 0x"${I2C_BMC_PWRSEQ[1]}" 0xfe 0x0000 w
+  cmd=$(i2cget -y -f -a "${I2C_BMC_PWRSEQ[0]}" 0x"${I2C_BMC_PWRSEQ[1]}" 0xfe i 2 | awk '{print substr($0,3)}')
+  echo " BMC PowerSequencer : ${cmd}"
+  #only display with smbios exists
+  if [[ -e /var/lib/smbios/smbios2 ]]; then
+    cmd=$(busctl introspect xyz.openbmc_project.Smbios.MDR_V2 \
+            /xyz/openbmc_project/inventory/system/chassis/motherboard/bios | grep Version | awk '{print $4}')
+    echo " Bios: $cmd"
+  fi
+
+  adm1266_ver  | grep REVISION
+
+}
+
+function uartmux() {
+  case $1 in
+    host)
+      if [ "$(tty)" ==  "/dev/ttyS0" ]; then
+        echo "Couldn't redirect to the host console within BMC local console"
+      else
+        echo "Entering Console use 'shift ~~..' to quit"
+        obmc-console-client -c /etc/obmc-console/server.ttyS1.conf
+      fi
+      ;;
+    mpro)
+      if [ "$(tty)" ==  "/dev/ttyS0" ]; then
+        echo "Couldn't redirect to the Mpro console within BMC local console"
+      else
+        echo "Entering Console use 'shift ~~..' to quit"
+        obmc-console-client -c /etc/obmc-console/server.ttyS3.conf
+      fi
+      ;;
+    swhost)
+      set_gpio_ctrl S0_UART0_BMC_SEL 1
+      ;;
+    swmpro)
+      set_gpio_ctrl S0_UART1_BMC_SEL 1
+      ;;
+    swhosthr)
+      set_gpio_ctrl S0_UART0_BMC_SEL 0
+      ;;
+    swmprohr)
+      set_gpio_ctrl S0_UART1_BMC_SEL 0
+      ;;
+    display)
+      if [ "$(get_gpio_ctrl S0_UART0_BMC_SEL)" -eq 1 ]; then
+        echo " CPU host to BMC console"
+      else
+        echo " CPU host to header"
+      fi
+
+      if [ "$(get_gpio_ctrl S0_UART1_BMC_SEL)" -eq 1 ]; then
+        echo " Mpro host to BMC console"
+      else
+        echo " Mpro host to header"
+      fi
+      ;;
+    *)
+      usage_uart
+      ;;
+  esac
+}
+
+function ledtoggle() {
+  case $1 in
+    boot)
+      cmd=$(get_gpio_ctrl SYS_BOOT_STATUS_LED)
+      case $2 in
+        on)
+          #turn on LED
+          set_gpio_ctrl SYS_BOOT_STATUS_LED 1
+          ;;
+        off)
+          #turn off LED
+          set_gpio_ctrl SYS_BOOT_STATUS_LED 0
+          ;;
+        toggle)
+          #toggle off LED
+          if [[ $cmd -eq 1 ]]; then
+            set_gpio_ctrl SYS_BOOT_STATUS_LED 0
+          fi
+
+          #toggle on LED
+          if [[ $cmd -eq 0 ]]; then
+            set_gpio_ctrl SYS_BOOT_STATUS_LED 1
+          fi
+          ;;
+        status)
+          #displayLED status
+          if [[ $cmd -eq 1 ]]; then
+            echo "on"
+          else
+            echo "off"
+          fi
+          ;;
+        *)
+          usage_led
+          ;;
+        esac
+      ;;
+    attn)
+      cmd=$(get_gpio_ctrl SYS_ERROR_LED)
+      case $2 in
+        on)
+          #turn on LED
+          set_gpio_ctrl SYS_ERROR_LED 1
+          ;;
+        off)
+          #turn off LED
+          set_gpio_ctrl SYS_ERROR_LED 0
+          ;;
+        toggle)
+          #toggle off LED
+          if [[ $cmd -eq 1 ]]; then
+            set_gpio_ctrl SYS_ERROR_LED 0
+          fi
+
+          #toggle on LED
+          if [[ $cmd -eq 0 ]]; then
+            set_gpio_ctrl SYS_ERROR_LED 1
+          fi
+          ;;
+        status)
+          #displayLED status
+          if [[ $cmd -eq 1 ]]; then
+            echo "on"
+          else
+            echo "off"
+          fi
+          ;;
+        *)
+          usage_led
+          ;;
+        esac
+      ;;
+    *)
+      usage_led
+      ;;
+    esac
+}
+
+case $1 in
+  rst)
+    reset "$2"
+    ;;
+  fw)
+    fw_rev
+    ;;
+  uart)
+    uartmux "$2"
+    ;;
+  led)
+    ledtoggle "$2" "$3"
+    ;;
+  *)
+    usage
+    ;;
+esac
+
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 6eda748..80eae00 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,8 @@
 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-cmd"
+OBMC_IMAGE_EXTRA_INSTALL:append:mori = " mori-boot"
 OBMC_IMAGE_EXTRA_INSTALL:append:mori = " mori-fw"
 
 # Required for phosphor-ipmi-ssif