#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-gpiointrd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup gpio interrupt monitoring for FBY2... "

runsv /etc/sv/gpiointrd > /dev/null 2>&1 &

echo "done."


#!/bin/sh
exec /usr/local/bin/ipmbd 13 5


#! /bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          ipmbd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Provides ipmb message tx/rx service
#
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

SPB_FRU_FILE="/tmp/fruid_spb.bin"

echo "Starting IPMB Rx/Tx Daemon"

kernel_ver=$(get_kernel_ver)
if [ $kernel_ver == 5 ]; then
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-1/new_device
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-3/new_device
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-5/new_device
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-7/new_device
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-13/new_device
fi

ulimit -q 1024000
runsv /etc/sv/ipmbd_1 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_3 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_5 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_7 > /dev/null 2>&1 &

#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) != "0" && $(get_slot_type 1) != "4" ]]; then
  sv stop ipmbd_1
elif [ $(is_server_prsnt 1) == "0" ]; then
  sv stop ipmbd_1
fi
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 2) == "1" && $(get_slot_type 2) != "0" && $(get_slot_type 2) != "4" ]]; then
  sv stop ipmbd_3
elif [ $(is_server_prsnt 2) == "0" ]; then
  sv stop ipmbd_3
fi
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 3) == "1" && $(get_slot_type 3) != "0" && $(get_slot_type 3) != "4" ]]; then
  sv stop ipmbd_5
elif [ $(is_server_prsnt 3) == "0" ]; then
  sv stop ipmbd_5
fi
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 4) == "1" && $(get_slot_type 4) != "0" && $(get_slot_type 4) != "4" ]]; then
  sv stop ipmbd_7
elif [ $(is_server_prsnt 4) == "0" ]; then
  sv stop ipmbd_7
fi

runsv /etc/sv/ipmbd_13 > /dev/null 2>&1 &


#!/bin/sh

# PLATFORM MAY CHOOSE TO OVERRIDE THIS FILE
echo -n "Starting mTerm console server..."
runsv /etc/sv/mTerm1 > /dev/null 2>&1 &
runsv /etc/sv/mTerm2 > /dev/null 2>&1 &
runsv /etc/sv/mTerm3 > /dev/null 2>&1 &
runsv /etc/sv/mTerm4 > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          enable-aen
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

# enables OEM AENs if Broadcom NIC
q=$(fw-util nic --version | grep -i broadcom)
if [ -z "$q" ]
then
      # non Broadcom
      echo "Enable standard AENs:(Link Status, Config Req, Driver Status)" >> /dev/kmsg
      /usr/local/bin/ncsi-util 8 0 0 0 0 0 0 0 7
else
      # Broadcom
      echo "Enable standard & Broadcom OEM AENs " >> /dev/kmsg
      /usr/local/bin/ncsi-util 8 0 0 0 0 0 7 0 7
fi

echo "done."


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

gpio_set MEZZ_PRSNTID_A_SEL_N F4 1
gpio_set MEZZ_PRSNTID_B_SEL_N F5 1

if [[ $(gpio_get MEZZ_PRSNTA2_N L0) == "0" && $(gpio_get MEZZ_PRSNTB2_N L1) == "1" ]]; then
  val=0
  for i in {1..3}; do
    val=$(i2cget -y -f 12 0x20 0 b)
    val=$((val & 0x07))  # NIC_AUX_POWER_EN=1,NIC_MAIN_POWER_EN=1,NIC_PWRGOOD=1
    [ $val -eq 7 ] && break
    usleep 100000
  done

  if [ $val -ne 7 ]; then
    i2cset -y -f 12 0x20 6 0xffff w
    i2cset -y -f 12 0x20 2 0xffff w
    usleep 200000
    i2cset -y -f 12 0x20 6 0xfe b  # NIC_AUX_POWER_EN=1
    for i in {1..10}; do  # wait for NIC_PWRGOOD=1
      val=$(i2cget -y -f 12 0x20 0 b)
      val=$((val & 0x05))
      [ $val -eq 5 ] && break
      usleep 100000
    done
    i2cset -y -f 12 0x20 6 0xfc b  # NIC_MAIN_POWER_EN=1
    sleep 1
  fi
fi


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

if [ "$#" -eq 1 ]; then
  case $1 in
    slot1)
      SLOT=(1)
      ;;
    slot2)
      SLOT=(2)
      ;;
    slot3)
      SLOT=(3)
      ;;
    slot4)
      SLOT=(4)
      ;;
    *)
      exit 1
      ;;
  esac
else
  SLOT=(1 2 3 4)
fi

TL_PRODUCT_ID0=39
TL_PRODUCT_ID1=30
ND_PRODUCT_ID0=44
ND_PRODUCT_ID1=4E

max_retry=3

for (( i=0; i<${#SLOT[@]}; i++ )); do
  slot_id=${SLOT[$i]}
  slot_12v=$(is_slot_12v_on $slot_id)
  if [ $(is_server_prsnt $slot_id) != "1" ]; then
    server_type=3
  elif [[ "$slot_12v" == "1" && $(get_slot_type $slot_id) == "0" ]]; then
    j=0
    while [ ${j} -lt ${max_retry} ]; do
      # Use standard IPMI command 'get-device-id' to read product id of server board
      output=$(/usr/bin/bic-util slot$slot_id 0x18 0x01)
      # if the command fails and the number of retry times reach max retry, continue to next slot
      if [ $(echo $output | wc -c) == 45 ]; then
        product_id_l=`echo "$output" | cut -c 28-29`
        product_id_h=`echo "$output" | cut -c 31-32`
        if [[ "$product_id_l" == "$TL_PRODUCT_ID0" && "$product_id_h" == "$TL_PRODUCT_ID1" ]]; then
          #TL
          server_type=0
        elif [[ "$product_id_l" == "$ND_PRODUCT_ID0" && "$product_id_h" == "$ND_PRODUCT_ID1" ]]; then
          #ND
          server_type=4
          RES=$(/usr/bin/bic-util slot$slot_id 0xE0 0x2A 0x15 0xA0 0x00 0x01)
          postcode=$(echo $RES| awk '{print $7$6$5$4;}')
          # Since server may not power on, skip if post code empty
          if [ -n "$postcode" ]; then
            /usr/bin/kv set "slot${slot_id}_last_postcode" "$postcode" 2> /dev/null
          fi
        else
          #unknown
          server_type=3
        fi
        break
      fi
      sleep 1
      j=$(($j+1))
    done
  elif [[ "$slot_12v" == "1" || ! -f "/tmp/server_type$slot_id.bin" ]]; then
    server_type=3
  else
    continue
  fi

  echo "Slot$slot_id Server Type: $server_type"
  echo $server_type > /tmp/server_type$slot_id.bin
done


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

kernel_ver=$(get_kernel_ver)

if [ "$#" -eq 1 ]; then
  case $1 in
    slot1)
      SLOT=(1)
      VOL_EN=(O4)
      VOL_EN_NAME=(P12V_STBY_SLOT1_EN)
      if [ $kernel_ver == 5 ]; then
        ADC_VALUE=(in13_input)
      else
        ADC_VALUE=(adc12_value)
      fi
      ;;
    slot2)
      SLOT=(2)
      VOL_EN=(O5)
      VOL_EN_NAME=(P12V_STBY_SLOT2_EN)
      if [ $kernel_ver == 5 ]; then
        ADC_VALUE=(in14_input)
      else
        ADC_VALUE=(adc13_value)
      fi
      ;;
    slot3)
      SLOT=(3)
      VOL_EN=(O6)
      VOL_EN_NAME=(P12V_STBY_SLOT3_EN)
      if [ $kernel_ver == 5 ]; then
        ADC_VALUE=(in15_input)
      else
        ADC_VALUE=(adc14_value)
      fi
      ;;
    slot4)
      SLOT=(4)
      VOL_EN=(O7)
      VOL_EN_NAME=(P12V_STBY_SLOT4_EN)
      if [ $kernel_ver == 5 ]; then
        ADC_VALUE=(in16_input)
      else
        ADC_VALUE=(adc15_value)
      fi
      ;;
    *)
      exit 1
      ;;
  esac
else
  SLOT=(1 2 3 4)
  VOL_EN=(O4 O5 O6 O7)
  VOL_EN_NAME=(P12V_STBY_SLOT1_EN P12V_STBY_SLOT2_EN P12V_STBY_SLOT3_EN P12V_STBY_SLOT4_EN)
  if [ $kernel_ver == 5 ]; then
      ADC_VALUE=(in13_input in14_input in15_input in16_input)
  else
      ADC_VALUE=(adc12_value adc13_value adc14_value adc15_value)
  fi
fi

sku=0
for (( i=0; i<${#SLOT[@]}; i++ )); do
  slot_id=${SLOT[$i]}
  if [ $(is_server_prsnt $slot_id) != "1" ]; then
    sku=3
  elif [ $(gpio_get ${VOL_EN_NAME[$i]} ${VOL_EN[$i]}) == "1" ]; then
    if [ $kernel_ver == 5 ]; then
      slot_vol=`cat /sys/devices/platform/iio-hwmon/hwmon/hwmon*/"${ADC_VALUE[$i]}" | cut -d \  -f 1 | awk '{printf ("%.3f\n",$1/1000)}'`
    else
      slot_vol=`cat /sys/devices/platform/ast_adc.0/"${ADC_VALUE[$i]}" | cut -d \  -f 1 | awk '{printf ("%.3f\n",$1)}'`
    fi

    if [[ `awk -v a=1.9 -v b=$slot_vol 'BEGIN{print(a>b)?"1":"0"}'` == 1 &&  `awk -v a=$slot_vol -v b=1.5 'BEGIN{print(a>b)?"1":"0"}'` == 1 ]]; then
      sku=2
      y=$(($slot_id*2-1))
      i2cset -y $y 0x40 0x5 0xa w
    elif [[ `awk -v a=1.4 -v b=$slot_vol 'BEGIN{print(a>b)?"1":"0"}'` == 1 && `awk -v a=$slot_vol -v b=1.0 'BEGIN{print(a>b)?"1":"0"}'` == 1 ]]; then
      sku=1
      y=$(($slot_id*2-1))
      i2cset -y $y 0x70 0x5 c
      usleep 10000
      i2cset -y $y 0x40 0x5 0xa w
    elif [[ `awk -v a=0.95 -v b=$slot_vol 'BEGIN{print(a>b)?"1":"0"}'` == 1 && `awk -v a=$slot_vol -v b=0.55 'BEGIN{print(a>b)?"1":"0"}'` == 1 ]]; then
      sku=4
    else
      sku=0
    fi
  elif [ ! -f "/tmp/slot$slot_id.bin" ]; then
    sku=3
  else
    continue
  fi

  echo "Slot$slot_id Type: $sku"
  echo $sku > /tmp/slot$slot_id.bin
done


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          eth0_mac_fixup.sh
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description:  Fixup the MAC address for eth0 based on wedge EEPROM
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

mac=$(weutil  | grep '^Local MAC'  | cut -d' ' -f3 )

if [ -n "$mac" ]; then
    ifconfig eth0 hw ether $mac
    # compare the 'ethaddr' from u-boot env
    ethaddr=$(fw_printenv ethaddr  | cut -d'=' -f2 )
    if [ "$ethaddr" != "$mac" ]; then
        fw_setenv "ethaddr" "$mac"
    fi
fi


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

LPS_PATH=/mnt/data/power/por/last_state

prog="$0"

usage() {
    echo "Usage: $prog <slot#> <command> [command options]"
    echo
    echo "Commands:"
    echo "  status: Get the current 1S server power status"
    echo
    echo "  on: Power on 1S server if not powered on already"
    echo "    options:"
    echo "      -f: Re-do power on sequence no matter if 1S server has "
    echo "          been powered on or not."
    echo
    echo "  off: Power off 1S server ungracefully"
    echo
    echo
}

do_status() {
    if [ $(is_server_prsnt $slot) == "0" ]; then
      echo  "The given slot is Empty"
      return 0
    fi

    if [[ $(get_slot_type $slot) == "1" || $(get_slot_type $slot) == "2" ]]; then
      echo "The given slot type is not server"
      return 0
    fi

    echo -n "1S Server power for slot#$slot is "
    if [ $(fby2_is_server_on $slot) -eq 1 ] ; then
        echo "on"
    else
        echo "off"
    fi
    return 0
}

do_on() {

    if [ $(is_server_prsnt $slot) == "0" ]; then
      echo "The given slot is Empty"
      return 0
    fi

    if [[ $(get_slot_type $slot) == "1" || $(get_slot_type $slot) == "2" ]]; then
      echo "The given slot type is not server"
      return 0
    fi

    local force opt
    force=0
    while getopts "f" opt; do
        case $opt in
            f)
                force=1
                ;;
            *)
                usage
                exit -1
                ;;

        esac
    done
    echo -n "Power on slot#$slot server ..."
    if [ $force -eq 0 ]; then
        # need to check if 1S Server is on or not
        if [ $(fby2_is_server_on $slot) -eq 1 ]; then
            echo " Already on. Skip!"
            return 1
        fi
    fi

    # TODO: State the power state change
    echo "on $(date +%s)" > $LPS_PATH

    # disable GPIOD pass-though function
    devmem_set_bit $(scu_addr 7c) 21

    # first make sure, GPIO is high
    gpio_set $gpio_name $gpio 1
    # generate the power on pulse
    gpio_set $gpio_name $gpio 0
    sleep 1
    gpio_set $gpio_name $gpio 1
    sleep 1
    # Turn on the power LED
    /usr/local/bin/power_led.sh $slot on

    # enable GPIOD pass-though function
    devmem_set_bit $(scu_addr 70) 21

    echo " Done"
    return 0
}

do_off() {
    if [ $(is_server_prsnt $slot) == "0" ]; then
      echo "The given slot is Empty"
      return 0
    fi

    if [[ $(get_slot_type $slot) == "1" || $(get_slot_type $slot) == "2" ]]; then
      echo "The given slot type is no server"
      return 0
    fi

    echo -n "Power off slot#$slot server ..."

    #TODO: State the power state change
    echo "off $(date +%s)" > $LPS_PATH

    # disable GPIOD pass-though function
    devmem_set_bit $(scu_addr 7c) 21

    # first make sure, GPIO is high
    gpio_set $gpio_name $gpio 1
    sleep 1
    gpio_set $gpio_name $gpio 0
    sleep 5
    gpio_set $gpio_name $gpio 1
    # Turn off the power LED
    /usr/local/bin/power_led.sh $slot off

    # enable GPIOD pass-though function
    devmem_set_bit $(scu_addr 70) 21

    echo " Done"
    return 0
}

# Slot1: GPIOD1(25), Slot2: GPIOD3(27), Slot3: GPIOD5(29), Slot4: GPIOD7(31)
slot=$1

case $slot in
  1)
    gpio=D1
    gpio_name=PWR_SLOT1_BTN_N
    ;;
  2)
    gpio=D3
    gpio_name=PWR_SLOT2_BTN_N
    ;;
  3)
    gpio=D5
    gpio_name=PWR_SLOT3_BTN_N
    ;;
  4)
    gpio=D7
    gpio_name=PWR_SLOT4_BTN_N
    ;;
  *)
    gpio=D1
    gpio_name=PWR_SLOT1_BTN_N
    ;;
esac

command="$2"
shift
shift

case "$command" in
    status)
        do_status $@
        ;;
    on)
        do_on $@
        ;;
    off)
        do_off $@
        ;;
    *)
        usage
        exit -1
        ;;
esac

exit $?


#! /bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          usbcons
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Creates a virtual USB serial device and starts a console
#                    on it.
#
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME="FC Switcher"
DESC="FC Failover Daemon"

# source function library
. /etc/init.d/functions

. /usr/local/fbpackages/utils/ast-functions

STOPPER=
ACTION="$1"

case "$ACTION" in
  start)
    if [ "$(wedge_board_type)" = "LC" ]; then
      # Ability to prevent this from starting by editing cmdline in u-boot.
      # Keeping this here until I get gadget switching working properly. (t4906522)
      /usr/local/bin/watch-fc.sh > /dev/null 2>&1 &
      echo "$NAME."
    else
      echo 'skipping watch-fc.sh: only necessary on six-pack line cards.'
    fi
    ;;
  stop)
    echo -n "Stopping $DESC: "
    killall watch-fc.sh
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    killall watch-fc.sh
    if [ "$(wedge_board_type)" = "LC" ]; then
      sleep 1
      /usr/local/bin/watch-fc.sh > /dev/null 2>&1 &
      echo "$NAME."
    else
      echo 'skipping watch-fc.sh: only necessary on six-pack line cards.'
    fi
    ;;
  status)
    status watch-fc.sh
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


#!/bin/sh
. /usr/local/fbpackages/utils/ast-functions

#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

echo "Hot service is triggered, re-init process... "

function device_config() {
      SLOT_N=$1
      SLOT_B=$2

      case $SLOT_N in
          1)
            I2C_REG=0x1e78a084
          ;;
          3)
            I2C_REG=0x1e78a184
          ;;
      esac

      # Delete devices on I2C bus 1 and bus 5 if there are device cards
      # EEPROM, 0xA2
      i2c_remove_device $SLOT_B 0x51
      # Inlet temp sensor, 0x9A
      i2c_remove_device $SLOT_B 0x4d
      # outlet temp sensor, 0x9C
      i2c_remove_device $SLOT_B 0x4e
      # I2C mux, 0xE2
      i2c_remove_device $SLOT_B 0x71
      # I2C GPIO, 0x40
      i2c_remove_device $SLOT_B 0x20
      # Voltage sensor, 0x80
      i2c_remove_device $SLOT_B 0x40

      #if [ $(is_server_prsnt $(($SLOT_N+1))) == "1" ] && [ $(get_slot_type $(($SLOT_N+1))) == "0" ] ; then
        if [ $(is_server_prsnt $SLOT_N) == "1" ] && [ $(get_slot_type $SLOT_N) != "0" ] && [ $(get_slot_type $SLOT_N) != "4" ] ; then

           devmem $I2C_REG w 0xFFF77304

           if [ $(get_slot_type $SLOT_N) == "2" ]; then
              # I2C mux, 0xE2
              i2c_add_device $SLOT_B 0x71 pca9551
              # I2C GPIO, 0x40
              i2c_add_device $SLOT_B 0x20 pca9551
           fi

           # EEPROM, 0xA2
           i2c_add_device $SLOT_B 0x51 24c128
           # Inlet temp sensor, 0x9A
           i2c_add_device $SLOT_B 0x4d tmp75
           # outlet temp sensor, 0x9C
           i2c_add_device $SLOT_B 0x4e tmp75
           # Voltage sensor, 0x80
           i2c_add_device $SLOT_B 0x40 ina230
        else

           devmem $I2C_REG w 0xFFF5E700
        fi
      #fi
}

function set_sysconfig() {
      SLOT_N=$1
      SLOT_B=$2

      case $SLOT_N in
          1)
            device_config $SLOT_N $SLOT_B
          ;;
          2)
            # Detect invalid configuration and then power-down the 12V for that slot(e.g. GP/CF plugged in to slot#2/slot#4)
            if [ $(is_server_prsnt $SLOT_N) == "1" ] && [ $(get_slot_type $SLOT_N) != "0" ] ; then
                  gpio_set P12V_STBY_SLOT2_EN O5 0
                  logger -p user.crit "Invalid configuration on SLOT$SLOT_N"

                  if [ $(is_server_prsnt $(($SLOT_N-1))) == "1" ] && [ $(get_slot_type $(($SLOT_N-1))) != "0" ] ; then
                     gpio_set P12V_STBY_SLOT1_EN O4 0
                  fi
            fi
          ;;
          3)
            device_config $SLOT_N $SLOT_B
          ;;
          4)
            # Detect invalid configuration and then power-down the 12V for that slot(e.g. GP/CF plugged in to slot#2/slot#4)
            if [ $(is_server_prsnt $SLOT_N) == "1" ] && [ $(get_slot_type $SLOT_N) != "0" ] ; then
                  gpio_set P12V_STBY_SLOT4_EN O7 0
                  logger -p user.crit "Invalid configuration on SLOT$SLOT_N"
                  if [ $(is_server_prsnt $(($SLOT_N-1))) == "1" ] && [ $(get_slot_type $(($SLOT_N-1))) != "0" ] ; then
                     gpio_set P12V_STBY_SLOT3_EN O6 0
                  fi
            fi
          ;;
      esac
}

SLOT=$1
OPTION=$2

case $SLOT in
    slot1)
      SLOT_NUM=1
      PE_BUFF_OE_0=B4
      PE_BUFF_OE_1=B5
      ;;
    slot2)
      SLOT_NUM=2
      PE_BUFF_OE_0=B4
      PE_BUFF_OE_1=B5
      ;;
    slot3)
      SLOT_NUM=3
      PE_BUFF_OE_0=B6
      PE_BUFF_OE_1=B7
      ;;
    slot4)
      SLOT_NUM=4
      PE_BUFF_OE_0=B6
      PE_BUFF_OE_1=B7
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      echo "Usage: $N {slot1|slot2|slot3|slot4} {start}"
      exit 1
      ;;
esac

SLOT_BUS=$(get_slot_bus $SLOT_NUM)

case $OPTION in
    start)
      if [ $(is_server_prsnt $SLOT_NUM) == 0 ]; then
        exit 1
      fi
      logger -p user.crit "Start to re-init for slot$SLOT_NUM insertion"
      echo "start to re-init for slot$SLOT_NUM insertion"

      # Delay 1 second for slot_type voltage ready
      sleep 2

      # System Configuration
      echo "reset system configuration for $SLOT $OPTION"
      /usr/local/bin/check_slot_type.sh $SLOT

      # Remove Service for new device/server
      # REST API
      sv stop restapi

      # Sensor
      sv stop sensord
      /usr/bin/kv del $SLOT'.*'
      set_sysconfig $SLOT_NUM $SLOT_BUS

      # GPIO
      sv stop gpiod

      # IPMB
      sv stop ipmbd_$SLOT_BUS

      # Console
      sv stop mTerm$SLOT_NUM

      # Delay to wait for sv stop
      sleep 3

      # Restart Service for new device/server
      if [[ $(is_server_prsnt $SLOT_NUM) == "1" ]]; then
        if [[ $(get_slot_type $SLOT_NUM) == "0" || $(get_slot_type $SLOT_NUM) == "4" ]]; then
          echo "restart mTerm for $SLOT $OPTION"
          sv start mTerm$SLOT_NUM

          echo "restart ipmbd for $SLOT $OPTION"
          sv start ipmbd_$SLOT_BUS
          rm -rf /tmp/fruid_$SLOT*
          /usr/local/bin/bic-cached -s $SLOT_NUM > /dev/null 2>&1 &
        fi
      fi

      # Server Type recognition restart
      echo "restart server type recognition for $SLOT"
      /usr/local/bin/check_server_type.sh $SLOT

      # Setup fan config
      /etc/init.d/setup-fan.sh

      echo "restart gpiod for $SLOT $OPTION"
      sv start gpiod

      echo "restart sensord for $SLOT $OPTION"
      sv start sensord

      echo "restart rest-api for $SLOT $OPTION"
      sv start restapi

      if [ $(is_date_synced) == "0" ]; then
        /usr/local/bin/sync_date.sh
      fi

      logger -p user.crit "Finish re-init for slot$SLOT_NUM insertion"
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      echo "Usage: $N {slot1|slot2|slot3|slot4} {start}"
      exit 1
      ;;
esac

echo "done."


#!/bin/sh
#
# Copyright 2004-present Facebook. All rights reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Displays values onto the debug header LEDs."
    echo "Hex and decimal accepted."
    echo "Usage: $0 <value>"
}

. /usr/local/fbpackages/utils/ast-functions

# Function to set the less significant hex digit
display_lower() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Set the pins to the correct operating mode.
    # The relevant pins are GPIOG[0...3].
    # For GPIO bank G, SCU84[0..3] must be 0.
    devmem_clear_bit $(scu_addr 84) 0
    devmem_clear_bit $(scu_addr 84) 1
    devmem_clear_bit $(scu_addr 84) 2
    devmem_clear_bit $(scu_addr 84) 3

    # Now set the GPIOs to the right binary values
    gpio_set LED_POSTCODE_0 G0 $bit0
    gpio_set LED_POSTCODE_1 G1 $bit1
    gpio_set LED_POSTCODE_2 G2 $bit2
    gpio_set LED_POSTCODE_3 G3 $bit3
}

# Function to set the more significant hex digit
display_upper() {
    local bit0=$(expr $1 % 2)
    local bit1=$(expr $1 / 2 % 2)
    local bit2=$(expr $1 / 4 % 2)
    local bit3=$(expr $1 / 8 % 2)

    # Set the pins to the correct operating mode.
    # The relevant pins are GPIOP[4...7].

    gpio_set LED_POSTCODE_4 P4 $bit0
    gpio_set LED_POSTCODE_5 P5 $bit1
    gpio_set LED_POSTCODE_6 P6 $bit2
    gpio_set LED_POSTCODE_7 P7 $bit3
}

# Check number of parameters
if [ $# -ne 1 ]
then
    usage
    exit 1
fi

# Make sure input is actually numeric
DEC_VALUE=$(printf "%d" $1 2>/dev/null)
if [ $? -eq 1 ]
then
    echo "Unable to parse input as numeric value."
    exit 1
fi

# Make sure input is within proper range
if [ $DEC_VALUE -lt 0 ] || [ $DEC_VALUE -gt 255 ]
then
    echo "Value $DEC_VALUE is outside of displayable range 0 - 0xff (255)."
    exit 1
fi

# Get upper/lower decimal values
LOWER_DEC_VALUE=$(expr $DEC_VALUE % 16)
UPPER_DEC_VALUE=$(expr $DEC_VALUE / 16)

# Display the results
display_lower $LOWER_DEC_VALUE
display_upper $UPPER_DEC_VALUE


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

DEF_PWR_ON=1
TO_PWR_ON=
POWER_ON_SLOT=
RETRY=0

check_por_config()
{

  TO_PWR_ON=-1

  POR=`kv get slot${1}_por_cfg persistent`
  # Check if the file/key doesn't exist
  if [ -z "${POR}" ]; then
    TO_PWR_ON=$DEF_PWR_ON
  else
    # Case ON
    if [ $POR == "on" ]; then
      TO_PWR_ON=1;

    # Case OFF
    elif [ $POR == "off" ]; then
      TO_PWR_ON=0;

    # Case LPS
    elif [ $POR == "lps" ]; then

      # Check if the file/key doesn't exist
      LS=`kv get /pwr_server${1}_last_state persistent`
      if [ -z "${LS}" ]; then
        TO_PWR_ON=$DEF_PWR_ON
      else
        if [ $LS == "on" ]; then
          TO_PWR_ON=1;
        elif [ $LS == "off" ]; then
          TO_PWR_ON=0;
        fi
      fi
    fi
  fi
}

# Check whether it is fresh power on reset
if [ $(is_bmc_por) -eq 1 ]; then
  # Remove all GP/GPv2 info
  for i in {2..13};
  do
    kv del sys_config/fru2_m2_${i}_info persistent > /dev/null 2>&1
    kv del sys_config/fru4_m2_${i}_info persistent > /dev/null 2>&1
  done

  /usr/local/bin/sync_date.sh

  # Disable clearing of PWM block on WDT SoC Reset
  devmem_clear_bit $(scu_addr 9c) 17

  check_por_config 1
  if [ $TO_PWR_ON -eq 1 ] && [ $(is_server_prsnt 1) == "1" ] && [ $(get_slot_type 1) == "0" ] ; then
    POWER_ON_SLOT+=(1)
    power-util slot1 on &
  fi

  check_por_config 2
  if [ $TO_PWR_ON -eq 1 ] && [ $(is_server_prsnt 2) == "1" ] && [ $(get_slot_type 2) == "0" ] ; then
    POWER_ON_SLOT+=(2)
    power-util slot2 on &
  fi

  check_por_config 3
  if [ $TO_PWR_ON -eq 1 ] && [ $(is_server_prsnt 3) == "1" ] && [ $(get_slot_type 3) == "0" ] ; then
    POWER_ON_SLOT+=(3)
    power-util slot3 on &
  fi

  check_por_config 4
  if [ $TO_PWR_ON -eq 1 ] && [ $(is_server_prsnt 4) == "1" ] && [ $(get_slot_type 4) == "0" ] ; then
    POWER_ON_SLOT+=(4)
    power-util slot4 on &
  fi

  # Wait for all slots finish power-up
  while [ $RETRY -lt 10 ]
  do
    FINISH=1
    for i in ${POWER_ON_SLOT[@]}
    do
      status=$(power-util slot$i status)
      if [ ${status:(-3)} == "OFF" ]; then
        FINISH=0
      fi
    done
    if [ $FINISH -eq 1 ]; then
      break
    fi
    RETRY=$(($RETRY+1))
    sleep 1
  done

  if [ $(is_date_synced) == "0" ]; then
    # Time sync with RC Server
    echo "Start time sync with server"
    sh /usr/local/bin/time-sync.sh &
  fi
fi


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "Usage: $1 <slot#> <on | off>"
    exit -1
}

. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin

set -e

if [ $# != 2 ]; then
    usage $0
fi

# Slot#1: GPIOM0(96),Slot#2: GPIOM1(97),Slot#3: GPIOM2(98),Slot#4: GPIOM3(99)
if [ $1 = "1" ]; then
    gpio=M0
    name=PWR1_LED
elif [ $1 = "2" ]; then
    gpio=M1
    name=PWR2_LED
elif [ $1 = "3" ]; then
    gpio=M2
    name=PWR3_LED
elif [ $1 = "4" ]; then
    gpio=M3
    name=PWR4_LED
else
    usage $0
fi

if [ $2 = "on" ]; then
    val=1
elif [ $2 = "off" ]; then
    val=0
else
    usage $0
fi

gpio_set $name $gpio $val


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

#TODO: Add logic to control mux based on front panel switch
echo -n "Set USB Mux to given slot ... "

# USB_MUX_SEL signals: GPIOE4(36), GPIOE5(37)
slot=$1

case $slot in
  1)
    gpio_set FM_USB_SW0 E4 0
    gpio_set FM_USB_SW1 E5 0
    gpio_set USB_MUX_EN_R_N AB3 0
    ;;
  2)
    gpio_set FM_USB_SW0 E4 E4 1
    gpio_set FM_USB_SW1 E5 E5 0
    gpio_set USB_MUX_EN_R_NAB3 0
    ;;
  3)
    gpio_set FM_USB_SW0 E4 E4 0
    gpio_set FM_USB_SW1 E5 E5 1
    gpio_set USB_MUX_EN_R_N AB3 0
    ;;
  4)
    gpio_set FM_USB_SW0 E4 E4 1
    gpio_set FM_USB_SW1 E5 E5 1
    gpio_set USB_MUX_EN_R_N AB3 0
    ;;
  *)
    gpio_set USB_MUX_EN_R_N AB3 1
    ;;
esac

echo "Done"


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

# Check OCP NIC
sh /usr/local/bin/check_ocp_nic.sh > /dev/NULL

# Get and Set slot type
sh /usr/local/bin/check_slot_type.sh > /dev/NULL


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          setup-por
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Determine BMC Power On Reset flag
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

store_clear_por


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

# Get server type
sh /usr/local/bin/check_server_type.sh > /dev/NULL


# This file contains definitions for the different system configurations.
# We should probably move some more of the definitions to this file at some point.

echo -n "Setup System Configuration .."

. /usr/local/fbpackages/utils/ast-functions

if [ $(is_server_prsnt 2) == "1" ] ; then
   if [ $(get_slot_type 2) != "0" ] ; then
      gpio_set P12V_STBY_SLOT2_EN O5 0
      logger -p user.crit "Invalid configuration on SLOT2"
      if [ $(is_server_prsnt 1) == "1" ] && [ $(get_slot_type 1) != "0" ] ; then
         gpio_set P12V_STBY_SLOT1_EN O4 0
      fi
   fi
else
   if [ $(is_server_prsnt 1) == "1" ] && [ $(get_slot_type 1) != "0" ] ; then
      gpio_set P12V_STBY_SLOT1_EN O4 0
   fi
fi

if [ $(is_server_prsnt 4) == "1" ] ; then
   if [ $(get_slot_type 4) != "0" ] ; then
      gpio_set P12V_STBY_SLOT4_EN O7 0
      logger -p user.crit "Invalid configuration on SLOT4"
      if [ $(is_server_prsnt 3) == "1" ] && [ $(get_slot_type 3) != "0" ] ; then
         gpio_set P12V_STBY_SLOT3_EN O6 0
      fi
   fi
else
   if [ $(is_server_prsnt 3) == "1" ] && [ $(get_slot_type 3) != "0" ] ; then
      gpio_set P12V_STBY_SLOT3_EN O6 0
   fi
fi

# Enable buffer to pass through signal by system configuration
if [ $(is_server_prsnt 1) == "1" ] && [ $(is_server_prsnt 2) == "1" ] ; then
   if [ $(get_slot_type 1) != "0" ] && [ $(get_slot_type 2) == "0" ] ; then
      gpio_set CLK_BUFF1_PWR_EN_N J0 0
      gpio_set PE_BUFF_OE_0_N B4 0
      gpio_set PE_BUFF_OE_1_N B5 0
   else
      gpio_set PE_BUFF_OE_0_N B4 1
      gpio_set PE_BUFF_OE_1_N B5 1
   fi
else
   gpio_set PE_BUFF_OE_0_N B4 1
   gpio_set PE_BUFF_OE_1_N B5 1
fi

if [ $(is_server_prsnt 3) == "1" ] && [ $(is_server_prsnt 4) == "1" ] ; then
   if [ $(get_slot_type 3) != "0" ] && [ $(get_slot_type 4) == "0" ] ; then
      gpio_set CLK_BUFF2_PWR_EN_N J1 0
      gpio_set PE_BUFF_OE_2_N B6 0
      gpio_set PE_BUFF_OE_3_N B7 0
   else
      gpio_set PE_BUFF_OE_2_N B6 1
      gpio_set PE_BUFF_OE_3_N B7 1
   fi
else
   gpio_set PE_BUFF_OE_2_N B6 1
   gpio_set PE_BUFF_OE_3_N B7 1
fi

# create devices on I2C bus 1 and bus 5
# Bus1,  Glacier Point
if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) != "0" && $(get_slot_type 1) != "4" ]]; then

   devmem 0x1e78a084 w 0xFFF77304

   if [ $(get_slot_type 1) == "2" ]; then
      # I2C mux, 0xE2
      echo pca9551 0x71 > /sys/class/i2c-dev/i2c-1/device/new_device
      # I2C GPIO, 0x40
      echo pca9551 0x20 > /sys/class/i2c-dev/i2c-1/device/new_device
   fi

   # EEPROM, 0xA2
   echo 24c128 0x51 > /sys/class/i2c-dev/i2c-1/device/new_device
   # Inlet temp sensor, 0x9A
   echo tmp75 0x4d > /sys/class/i2c-dev/i2c-1/device/new_device
   # outlet temp sensor, 0x9C
   echo tmp75 0x4e > /sys/class/i2c-dev/i2c-1/device/new_device
   # Voltage sensor, 0x80
   echo ina230 0x40 > /sys/class/i2c-dev/i2c-1/device/new_device
else
   devmem 0x1e78a084 w 0xFFF5E700
fi

# Bus5,  Glacier Point
if [[ $(is_server_prsnt 3) == "1" && $(get_slot_type 3) != "0" && $(get_slot_type 3) != "4" ]]; then

   devmem 0x1e78a184 w 0xFFF77304

   if [ $(get_slot_type 3) == "2" ]; then
      # I2C mux, 0xE2
      echo pca9551 0x71 > /sys/class/i2c-dev/i2c-5/device/new_device
      # I2C GPIO, 0x40
      echo pca9551 0x20 > /sys/class/i2c-dev/i2c-5/device/new_device
   fi

   # EEPROM, 0xA2
   echo 24c128 0x51 > /sys/class/i2c-dev/i2c-5/device/new_device
   # Inlet temp sensor, 0x9A
   echo tmp75 0x4d > /sys/class/i2c-dev/i2c-5/device/new_device
   # outlet temp sensor, 0x9C
   echo tmp75 0x4e > /sys/class/i2c-dev/i2c-5/device/new_device
   # Voltage sensor, 0x80
   echo ina230 0x40 > /sys/class/i2c-dev/i2c-5/device/new_device
else
   devmem 0x1e78a184 w 0xFFF5E700
fi

echo "done"


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

# The T2 chip can prefer different input voltages, depending, presumably
# of manufacturing variations.  We need to determine whether it wants
# 0.95V or 1.025V, reset the T2 to reduce total power usage, set the
# outgoing voltage on the first buck converter, and bring T2 up out of
# reset.

. /usr/local/fbpackages/utils/ast-functions

kernel_ver=$(get_kernel_ver)

# read the T2 ROV after the GPIOs are enabled
t2_rov() {
    local val0 val1 val2
    # Note that the values are *not* read in order.
    if [ $kernel_ver == 5 ]; then
        val0=$(cat /sys/class/gpio/gpio850/value 2>/dev/null)
        val1=$(cat /sys/class/gpio/gpio848/value 2>/dev/null)
        val2=$(cat /sys/class/gpio/gpio849/value 2>/dev/null)
    else
        val0=$(cat /sys/class/gpio/gpio58/value 2>/dev/null)
        val1=$(cat /sys/class/gpio/gpio56/value 2>/dev/null)
        val2=$(cat /sys/class/gpio/gpio57/value 2>/dev/null)
    fi
    echo $((val0 | (val1 << 1) | (val2 << 2)))
}

rov=$(t2_rov)

# target_volts come from the data sheet and 18mV of loss and
# some fudging based on actual measurements to get either 1.025V
# or 0.95V at T2
if [ $rov -eq 1 ]; then
    target_volts=0x5a
elif [ $rov -eq 2 ]; then
    target_volts=0x65
else
    echo "Unrecognized T2 ROV value $rov, setting failed."
    exit 1
fi
target_volts=$(( $target_volts * 1 ))  # normalize to decimal

# We shouldn't have to rmmod pmbus, because it hasn't been loaded yet,
# but if the script is rerun after the system is up, it may be necessary.
if [ $kernel_ver == 4 ]; then
    rmmod pmbus
    reload=$?
fi

# Get current voltage value
cur_volts=$(i2cget -y 1 0x60 0x8b w)
cur_volts=$(( $cur_volts * 1 ))  # normalize to decimal

# Only bounce the T2 if we actually need to modify the voltage
if [ $cur_volts -ne $target_volts ]; then
    # Set values before turning out output;  we're using "PCIE, then MCS"
    gpio_set SYSTEM_ID3_LED_N F2 1
    gpio_set SYSTEM_ID4_LED_N F3 1
    gpio_set SMB_TEMP_SCL_R C0 0
    # T2 is in reset;  note that this may cause NMI messages on the uServer,
    # which shouldn't be up anyway when this is first run.

    # Set the requested value to the current value to avoid rapid shifts
    i2cset -y 1 0x60 0x21 $cur_volts w
    # Enable the requested voltage
    i2cset -y 1 0x60 0xd2 0x5a
    i2cset -y 1 0x60 0xd3 0x5a
    sleep 1

    # Set the target voltage
    i2cset -y 1 0x60 0x21 $target_volts w

    sleep 1

    # Let T2 come out of reset
    gpio_set SMB_TEMP_SCL_R C0 1
    echo "T2 ROV value set based on $rov."
    sleep 2
    gpio_set SYSTEM_ID3_LED_N F2 0
    gpio_set SYSTEM_ID4_LED_N F3 0
else
    echo "T2 ROV already correctly set."
fi

if [ $kernel_ver == 4 ]; then
    # Bring back pmbus if necessary
    if [ $reload -eq 0 ]; then
        modprobe pmbus
    fi
fi


#!/bin/bash

SLOT=$1
SLOT_NUM=${SLOT#"slot"}

PWR_UTIL_PID=$(ps | grep power-util | grep "$SLOT" | awk '{print $1}')
HSV_REINIT_PID=$(ps | grep hotservice-reinit.sh | grep "$SLOT" | awk '{print $1}')

PWR_UTIL_LOCK="/var/run/power-util_$SLOT_NUM.lock"
BIC_CACHED_LOCK="/var/run/bic-cached_$SLOT_NUM.lock"

if [ ! -z "$PWR_UTIL_PID" ]; then
  logger -p user.info "slot-removal-precheck.sh: kill power-util $SLOT, pid: $PWR_UTIL_PID"
  kill -9 "$PWR_UTIL_PID"
fi

if [ ! -z "$HSV_REINIT_PID" ]; then
  logger -p user.info "slot-removal-precheck.sh: kill hotservice-reinit.sh $SLOT, pid: $HSV_REINIT_PID"
  kill -9 "$HSV_REINIT_PID"
fi

# check the lock file to find out the flock are occupied by which process
# then kill the processes to free the flock
for lock_file in "$PWR_UTIL_LOCK" "$BIC_CACHED_LOCK"
do
  if [ -f "$lock_file" ]; then
    for lock_pid in $(fuser "$lock_file")
    do
      cmdline=$(cat < /proc/"$lock_pid"/cmdline | tr '\000' ' ')
      logger -p user.info "slot-removal-precheck.sh: kill $cmdline, pid: $lock_pid, lock_file: $lock_file"
      kill -9 "$lock_pid"
    done

    rm "$lock_file"
  fi
done


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# Sync BMC's date with one of the four servers
sync_date()
{
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    if  kv get time_sync ; then
      # ND Server Time Sync by IPMI command
      output=$(kv get time_sync)
      if [ ${#output} == 11 ] ; then
        echo Syncing up BMC time with server...
        date -s @$((16#$(echo "$output" | awk '{print $4$3$2$1}')))
        test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
        echo 1 > /tmp/sync_date
      fi
    else
      for i in 1 2 3 4
      do
        if [[ $(is_server_prsnt $i) == "1" && $(get_slot_type $i) == "0" ]] ; then
          # Use standard IPMI command 'get-sel-time' to read RTC time
          s_type=$(get_server_type $i)
          if [ "$s_type" == "0" ] ; then
            output=$(/usr/local/bin/me-util slot$i 0x28 0x48)
          elif [ "$s_type" == "2" ] ; then
            output=$(/usr/bin/bic-util slot$i 0x28 0x48)
          fi
          # if the command fails, continue to next slot
          [ ${#output} != 12 ] && continue
          echo Syncing up BMC time with server$i...
          date -s @$((16#$(echo "$output" | awk '{print $4$3$2$1}')))
          test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
          echo 1 > /tmp/sync_date
          break
        fi
      done
    fi
  else
    test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
    echo 1 > /tmp/sync_date
  fi
}

sync_date


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          time-sync
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Time Sync With Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

time_sync_success=0
time_sync_with_IMC=0

# Sync BMC's date with one of the four servers
sync_date()
{
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    sleep 60    #wait for BIOS writes RTC time to internal memory
    max_retry=6
    default_time_h=5A   #default time high byte 2018-01-01
    default_time_l=4A   #default time low byte 2018-01-01
    for i in 1 2 3 4
    do
      if [[ $(is_server_prsnt $i) == "1" && $(get_slot_type $i) == "0" && $(get_server_type $i) == "1" ]] ; then
        retry_times=0
        time_sync_with_IMC=1
        while [ ${retry_times} -lt ${max_retry} ]
        do
          # Use IMC command 'get-rtc-time' to read RTC time
          output=$(/usr/bin/bic-util slot$i 0xe0 0x02 0x15 0xa0 0x00 0x10 0xb8 0xe3 0xa9 0x05 0x0 0x0 0x0 0x0 0x0)
          # if the command fails and the number of retry times reach max retry, continue to next slot
          if [ $(echo $output | wc -c) == 36 ] ; then
            time_h=$((16#$(echo "$output" | cut -c 34-35)))
            time_l=$((16#$(echo "$output" | cut -c 31-32)))

            if [ "$time_h" -gt $((16#${default_time_h})) ] ; then
              echo Syncing up BMC time with server$i...
              date -s @$((16#$(echo $output | awk '{print $12$11$10$9}')))
              test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
              time_sync_success=1
              break
            fi

            if [[ "$time_h" -ge $((16#${default_time_h})) && "$time_l" -ge $((16#${default_time_l})) ]] ; then
              echo Syncing up BMC time with server$i...
              date -s @$((16#$(echo $output | awk '{print $12$11$10$9}')))
              test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
              time_sync_success=1
              break
            fi
          fi
          retry_times=$(($retry_times+1))
          sleep 5
        done
      fi
      if [ $time_sync_success == 1 ] ; then
        break
      fi
    done
    # This log only appears in the RC system
    if [[ $time_sync_success == 0 && $time_sync_with_IMC == 1 ]] ; then
      logger -p user.crit "Time sync with IMC failed, using 2018/01/01 as default"
    fi
  fi
  sts=$(ifconfig eth0 | grep -i "inet addr")
  if [ "$sts" == "" ]; then    #No ipv4 ip
    kill -9 `cat /var/run/dhclient.eth0.pid`
    #if dhcleint need to restart, make it non-demonized so that dhclient will continuely re-access ipv4 address
    dhclient -d -pf /var/run/dhclient.eth0.pid eth0 > /dev/null 2>&1 &
  fi
}

sync_date


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

usage() {
    echo "$0 <connect | disconnect>"
}

. /usr/local/fbpackages/utils/ast-functions

if [ $# -ne 1 ]; then
    usage
    exit 1
fi

if [ "$1" == "connect" ]; then
    VALUE=1
elif [ "$1" == "disconnect" ]; then
    VALUE=0
else
    usage
    exit 1
fi

gpio_set DEBUG_UART_SEL_0 E0 $VALUE


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

. /usr/local/fbpackages/utils/ast-functions

MAC1LINK=A0   # GPIOA0
FAB0_PRES=C4  # GPIOC4
FAB1_PRES=C5  # GPIOC5

while true; do
  # fabN_pres: active low.
  if [ $(gpio_get SMB_MEZZ_NIC_CLK_R $FAB0_PRES) = 0 ]; then
    gpio_set BMC_READY_N $MAC1LINK 0
  elif [ $(gpio_get SMB_MEZZ_NIC_DAT_R $FAB1_PRES) = 0 ]; then
    gpio_set BMC_READY_N $MAC1LINK 1
  fi
  sleep 1
done


#!/bin/bash
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

mac=$(i2cdump -y 0x0 0x49 s 0xd4 | grep '^00: d4'| awk '{ print $3":"$4":"$5":"$6":"$7":"$8 }') 2>/dev/null

if [ -n "$mac" ]; then
    echo $mac
else
    echo "Cannot find out the microserver MAC" 1>&2
fi


#!/bin/sh
. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup sensor monitoring for fby2... "

# Check for the slots present and run sensord for those slots only.
SLOTS=
  if [ $(is_server_prsnt 1) == "1" ]; then
    if [ $(is_slot_12v_on 1) == "1" ]; then
      if [[ $(get_slot_type 1) != "0" || $(is_bic_ready 1) == "1" ]]; then
        SLOTS="$SLOTS slot1"
      fi
    else
      SLOTS="$SLOTS slot1"
    fi
  fi

  if [ $(is_server_prsnt 2) == "1" ]; then
    if [ $(is_slot_12v_on 2) == "1" ]; then
      if [[ $(get_slot_type 2) != "0" || $(is_bic_ready 2) == "1" ]]; then
        SLOTS="$SLOTS slot2"
      fi
    else
      SLOTS="$SLOTS slot2"
    fi
  fi

  if [ $(is_server_prsnt 3) == "1" ]; then
    if [ $(is_slot_12v_on 3) == "1" ]; then
      if [[ $(get_slot_type 3) != "0" || $(is_bic_ready 3) == "1" ]]; then
        SLOTS="$SLOTS slot3"
      fi
    else
      SLOTS="$SLOTS slot3"
    fi
  fi

  if [ $(is_server_prsnt 4) == "1" ]; then
    if [ $(is_slot_12v_on 4) == "1" ]; then
      if [[ $(get_slot_type 4) != "0" || $(is_bic_ready 4) == "1" ]]; then
        SLOTS="$SLOTS slot4"
      fi
    else
      SLOTS="$SLOTS slot4"
    fi
  fi

SLOTS="$SLOTS spb nic"
exec /usr/local/bin/sensord $SLOTS


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          sensor-setup
# Required-Start:    power-on
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions
. /usr/local/bin/i2c-utils.sh

check_i2c_device_bind() {
   # $1 - driver name
   # $2 - device path in <bus>-00<addr> format.
   chk_driver_dir="/sys/bus/i2c/drivers/$1"

   if [ ! -L "$chk_driver_dir"/"$2" ]; then
      # device not found in driver folder
      return 1
   fi

   # bind success
   return 0
}

init_northdome_inlet_temp_type() {
   key_name="inlet_temp_type"
   dev_uid_1="9-004e"
   dev_name_1="tmp421"
   dev_driver_1="$(i2c_driver_map $dev_name_1)"
   dev_uid_2="9-0048"
   dev_name_2="tmp75"
   dev_driver_2="$(i2c_driver_map $dev_name_2)"
   retry_count=3
   val="unknow"

   while [ "$retry_count" -gt "0" ]
   do
      check_i2c_device_bind $dev_driver_1 $dev_uid_1 && val=$dev_name_1 && break
      check_i2c_device_bind $dev_driver_2 $dev_uid_2 && val=$dev_name_2 && break

      # both sensors not  successful binding, rebind again
      i2c_bind_driver $dev_driver_1 $dev_uid_1
      i2c_bind_driver $dev_driver_2 $dev_uid_2

      retry_count=$((retry_count-1))
   done

   kv set "$key_name" "$val"
}

init_northdome_outlet_temp_type() {
   key_name="outlet_temp_type"
   dev_uid_1="9-004f"
   dev_name_1="tmp421"
   dev_driver_1="$(i2c_driver_map $dev_name_1)"
   dev_uid_2="9-0049"
   dev_name_2="tmp75"
   dev_driver_2="$(i2c_driver_map $dev_name_2)"
   retry_count=3
   val="unknow"

   while [ "$retry_count" -gt "0" ]
   do
      check_i2c_device_bind $dev_driver_1 $dev_uid_1 && val=$dev_name_1 && break
      check_i2c_device_bind $dev_driver_2 $dev_uid_2 && val=$dev_name_2 && break

      # both sensors not  successful binding, rebind again
      i2c_bind_driver $dev_driver_1 $dev_uid_1
      i2c_bind_driver $dev_driver_2 $dev_uid_2

      retry_count=$((retry_count-1))
   done

   kv set "$key_name" "$val"
}

# Eventually, this will be used to configure the various (mostly
# i2c-based) sensors, once we have a kernel version that supports
# doing this more dynamically.
#
# For now, we're using it to install the lm75 and pmbus module so that it
# can detect the fourth temperature sensor, which is located
# on the uServer, which doesn't get power until power-on executes.
#
# Similarly, the pmbus sensor seems to have an easier time of
# detecting the NCP4200 buck converters after poweron.  This has not
# been carefully explored.
kernel_ver=$(get_kernel_ver)
if [ $kernel_ver == 4 ]; then
    modprobe lm75
    modprobe pmbus

    # Enable the ADC inputs;  adc0 - adc15 are connected to various voltage sensors
    # adc12 - adc15 are for SLOT_TYPE

    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

    # setup ADC channels

    ADC_PATH="/sys/devices/platform/ast_adc.0"
    # For FBY2 tyep V:
    # channel 0:  r1:  5.36K; r2:  2.0K; v2: 0mv
    # channel 1:  r1:  15.8K; r2:  2.0K; v2: 0mv
    # channel 2:  r1:  2.87K; r2:  2.0K; v2: 0mv
    # channel 3:  r1:  15.8K; r2:  2.0K; v2: 0mv
    # channel 4:  r1:  15.8K; r2:  2.0K; v2: 0mv
    # channel 5:  r1:  15.8K; r2:  2.0K; v2: 0mv
    # channel 6:  r1:  15.8K; r2:  2.0K; v2: 0mv
    # channel 7:  r1:  2.87K; r2:  2.0K; v2: 0mv
    # channel 8:  r1:     1K; r2:  0.0K; v2: 0mv
    # channel 9:  r1:     1K; r2:  0.0K; v2: 0mv
    # channel 10: r1:  1.69K; r2:  2.0K; v2: 0mv
    # channel 11: r1:    665; r2:  2.0K; v2: 0mv
    # channel 12: r1:     1K; r2:    0K; v2: 0mv
    # channel 13: r1:     1K; r2:    0K; v2: 0mv
    # channel 14: r1:     1K; r2:    0K; v2: 0mv
    # channel 15: r1:     1K; r2:    0K; v2: 0mv

    config_adc() {
        channel=$1
        r1=$2
        r2=$3
        v2=$4
        echo $r1 > ${ADC_PATH}/adc${channel}_r1
        echo $r2 > ${ADC_PATH}/adc${channel}_r2
        echo $v2 > ${ADC_PATH}/adc${channel}_v2
        echo 1 > ${ADC_PATH}/adc${channel}_en
    }

    config_adc 0  5360  2000 0
    config_adc 1 15800  2000 0
    config_adc 2  2870  2000 0
    config_adc 3 15800  2000 0
    config_adc 4 15800  2000 0
    config_adc 5 15800  2000 0
    config_adc 6 15800  2000 0
    config_adc 7  2870  2000 0
    config_adc 8  	 0  1000 0
    config_adc 9  	 0  1000 0
    config_adc 10 1690  2000 0
    config_adc 11  665  2000 0
    config_adc 12    0  1000 0
    config_adc 13    0  1000 0
    config_adc 14    0  1000 0
    config_adc 15    0  1000 0
fi

gpio_export MB_HSC_RSENSE_SRC GPIOA2

spb_type=$(get_spb_type)

# ADM1278 Configuration
if [ $spb_type == "1" ]; then
   # Yosemite V2.50 Platform
   # Clear PEAK_PIN & PEAK_IOUT register
   i2cset -y -f 10 0x40 0xd0 0x0000 w
   i2cset -y -f 10 0x40 0xda 0x0000 w

   #
   i2cset -y -f 10 0x40 0xd4 0x3f1c w
   i2cset -y -f 10 0x40 0xd5 0x0400 w
   i2cset -y -f 10 0x40 0xd8 0x0003 w

   # calibrtion to get HSC to trigger 60A based on EE team input
   # 0xe18 = 3608 (dec)
   #(3608*10-20475)/ (800*0.3)= 65.0208A
   # 65.0208* 0.92259 = 59.9875A

   # Get ADM1278 Rsense source, low: main source, high: 2nd source
   src=$(gpio_get MB_HSC_RSENSE_SRC GPIOA2)
   if [ $src == "0" ]; then
      i2cset -y -f 10 0x40 0x4a 0x0e18 w
   else
      i2cset -y -f 10 0x40 0x4a 0x0d9f w
   fi
elif [ $spb_type == "2" ]; then
   # FBND Rome & GPv2 Platform
   # Clear PEAK_PIN & PEAK_IOUT register
   i2cset -y -f 10 0x40 0xd0 0x0000 w
   i2cset -y -f 10 0x40 0xda 0x0000 w

   #
   i2cset -y -f 10 0x40 0xd4 0x3d1c w
   i2cset -y -f 10 0x40 0xd5 0x0400 w

   # calibrtion to get HSC to trigger 63A based on EE team input
   # 0xe64 = 3686 (dec)
   #(3684*10-20475)/ (800*0.3)= 68.1875A
   # 68.1875* 0.92355 = 62.9746A
   i2cset -y -f 10 0x40 0x4a 0x0e64 w
elif [ $spb_type == "3" ]; then
   # FBND Milan Platform
   if [ $(gpio_get HSC_SELECT A1) == "0" ]; then
      # ADM1278
      echo "HSC Type: ADM1278"
      # Clear PEAK_PIN & PEAK_IOUT register
      i2cset -y -f 10 0x40 0xd0 0x0000 w
      i2cset -y -f 10 0x40 0xda 0x0000 w

      #
      i2cset -y -f 10 0x40 0xd4 0x3d1c w
      i2cset -y -f 10 0x40 0xd5 0x0400 w

      # calibrtion to get HSC to trigger 63A based on EE team input
      # 0x0de9 = 3561 (dec)
      #(3561*10-20475)/ (800*0.3)= 63.0625A
      # 63.0625 * 0.9989997 = 62.9994A
      i2cset -y -f 10 0x40 0x4a 0x0de9 w
   else
      # LTC4282
      echo "HSC Type: LTC4282"

      # ILIM_ADJUST (0x11)
      # bit[7-5]: 000b
      # bit[4-3]: 10b
      # bit[2]: 0b
      # bit[1]: 1b
      # bit[0]: 0b
      i2cset -y -f 10 0x41 0x11 0x12 b

      i2cset -y -f 10 0x41 0x02 0x04 b
      # Set VSENSE MAX for OC Warning
      # (75 * 0.04)/(255* 0.001* 0.1875)= 62.745098039 A
      i2cset -y -f 10 0x41 0x03 0x20 b
      i2cset -y -f 10 0x41 0x0d 0x4B b

      # FET_BAD_FAULT_TIME set to 80
      i2cset -y -f 10 0x41 0x06 0x50 b

      # Clear PEAK_PIN & PEAK_IOUT register
      i2cset -y -f 10 0x41 0x44 0x0000 w
      i2cset -y -f 10 0x41 0x4A 0x0000 w
   fi
else
   # Yosemite V2 Platform
   # Clear PEAK_PIN & PEAK_IOUT register
   i2cset -y -f 10 0x40 0xd0 0x0000 w
   i2cset -y -f 10 0x40 0xda 0x0000 w

   #
   i2cset -y -f 10 0x40 0xd4 0x3f1c w
   i2cset -y -f 10 0x40 0xd5 0x0400 w

   # calibrtion to get HSC to trigger 50A based on EE team input
   # 0xd14 = 3348 (dec)
   #(3348*10-20475)/ (800*0.3)= 54.1875A
   # 54.1875* 0.92190 = 49.9554A
   i2cset -y -f 10 0x40 0x4a 0x0d14 w
fi

if [ $kernel_ver == 4 ]; then
  rmmod adm1275
  modprobe adm1275
fi

# init inlet & outlet temp sensor type cache
if [ "$spb_type" -eq "3" ]; then
   init_northdome_inlet_temp_type
   init_northdome_outlet_temp_type
fi


#!/bin/sh
#
# Copyright 2017-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

SENSOR_UTIL="/usr/local/bin/sensor-util"
SENSORDUMP_FILE="/mnt/data/sensordump"

logger -t "gpiointrd" -p daemon.crit "Sensordump is generated at $SENSORDUMP_FILE"

echo "Sensor dump log created at " > $SENSORDUMP_FILE  && date >> $SENSORDUMP_FILE && echo $'\n\n' >> $SENSORDUMP_FILE && echo "Senor Dump" >> $SENSORDUMP_FILE && $SENSOR_UTIL all >> $SENSORDUMP_FILE
exit 0


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
### BEGIN INIT INFO
# Provides:          setup-bic-cached
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Set Cachcing for Bridge IC info
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

function init_class2_sdr() {
  /usr/local/bin/bic-cached -s slot1
  /usr/local/bin/bic-cached -f slot1 > /dev/null 2>&1 &
}

function init_class1_sdr() {
  # -f means `dump fru` only
  if [[ $(is_sb_bic_ready 1) == "1" ]]; then
    /usr/local/bin/bic-cached -s slot1
    /usr/local/bin/bic-cached -f slot1 > /dev/null 2>&1 &
  fi

  if [[ $(is_sb_bic_ready 2) == "1" ]]; then
    /usr/local/bin/bic-cached -s slot2
    /usr/local/bin/bic-cached -f slot2 > /dev/null 2>&1 &
  fi

  if [[ $(is_sb_bic_ready 3) == "1" ]]; then
    /usr/local/bin/bic-cached -s slot3
    /usr/local/bin/bic-cached -f slot3 > /dev/null 2>&1 &
  fi

  if [[ $(is_sb_bic_ready 4) == "1" ]]; then
    /usr/local/bin/bic-cached -s slot4
    /usr/local/bin/bic-cached -f slot4 > /dev/null 2>&1 &
  fi
}

echo -n "Setup Caching for Bridge IC info.."
bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  init_class2_sdr
elif [ $bmc_location -eq 14 ] || [ $bmc_location -eq 7 ]; then
  #The BMC of class1
  init_class1_sdr
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi

echo "done."


#!/bin/bash
. /usr/local/fbpackages/utils/ast-functions

BOARD_ID=$(get_bmc_board_id)
ME_UTIL="/usr/local/bin/me-util"
FRUID_UTIL="/usr/local/bin/fruid-util"
FW_UTIL="/usr/bin/fw-util"
SLOT_NAME=$1
INTERFACE="PECI_INTERFACE"
PCIE_INTERFACE="PECI_INTERFACE"

function is_numeric {
  if [ $(echo "$1" | grep -cE "^\-?([[:xdigit:]]+)(\.[[:xdigit:]]+)?$") -gt 0 ]; then
    return 1
  else
    return 0
  fi
}

case $SLOT_NAME in
    slot1)
      SLOT_NUM=1
      ;;
    slot2)
      SLOT_NUM=2
      ;;
    slot3)
      SLOT_NUM=3
      ;;
    slot4)
      SLOT_NUM=4
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      if [ "$BOARD_ID" -eq 9 ]; then
        echo "Usage: $N {slot1}"
      else
        echo "Usage: $N {slot1|slot2|slot3|slot4}"
      fi
      exit 1
      ;;
esac

if [ "$BOARD_ID" -eq 9 ] && [ "$SLOT_NAME" != "slot1" ]; then
  # class2 checking
  echo "$SLOT_NAME not supported"
  exit 1
fi

# File format autodump<slot_id>.pid (See pal_is_crashdump_ongoing()
# function definition)
PID=$$
PID_FILE="/var/run/autodump$SLOT_NUM.pid"

# check if running auto dump
[ -r $PID_FILE ] && OLDPID=`cat $PID_FILE` || OLDPID=''

# Set current pid
echo $PID > $PID_FILE

# kill previous autodump if exist
if [ ! -z "$OLDPID" ] && (grep "autodump" /proc/$OLDPID/cmdline &> /dev/null) ; then
  # Check if 2nd dump is running
  if [ ! -z "$(ps | grep '{autodump.sh}' | grep "${SLOT_NAME}" | grep "second\|dwr")" ]; then
    echo "$OLDPID" > $PID_FILE
    echo "2nd DUMP or Demoted Warm Reset DUMP is running. exit."
    exit 1
  fi

  LOG_FILE="/tmp/crashdump/"
  LOG_ARCHIVE="/mnt/data/acd_uncompleted_${SLOT_NAME}.tar.gz"

  tar zcf $LOG_ARCHIVE -C `dirname $LOG_FILE` `basename $LOG_FILE` && \
  rm -rf $LOG_FILE && \

  echo "kill pid $OLDPID..."
  kill -s 9 "$OLDPID"
  ps | grep 'crashdump' | grep "$SLOT_NAME" | awk '{print $1}'| xargs kill -s 9 &> /dev/null
fi
unset OLDPID

# Set crashdump timestamp
# pal_is_crashdump_ongoing() need the crashdump timestamp
sys_runtime=$(awk '{print $1}' /proc/uptime)
sys_runtime=$(printf "%0.f" "$sys_runtime")
/usr/bin/kv set fru${SLOT_NUM}_crashdump $((sys_runtime+1200))

CRASHDUMP_FILE="/tmp/crashdump/"
CRASHDUMP_LOG_ARCHIVE="/mnt/data/acd_crashdump_$SLOT_NAME.tar.gz"
LOG_MSG_PREFIX=""

DWR=0
SECOND_DUMP=0
DELAY_SEC=30
POWER_OFF=0

while test $# -gt 1
do
  case "$2" in
  --dwr)
    DWR=1
    ;;
  --now)
    DELAY_SEC=0
    ;;
  --second)
    SECOND_DUMP=1
    ;;
  --power_off)
    POWER_OFF=1
    ;;
  *)
    echo "unknown argument $2"
    ;;
  esac
  shift
done

if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
  echo "ACD Dump after System Reset or Demoted Warm Reset"
fi

if [ "$DELAY_SEC" != "0" ]; then
  echo "ACD Dump will start after ${DELAY_SEC}s..."

  sleep ${DELAY_SEC}
fi

echo "ACD for $SLOT_NAME Started"
logger -t "ipmid" -p daemon.crit "ACD for FRU: $SLOT_NUM started"

crashdump $SLOT_NUM

# only second/dwr dump need to rename accordingly
if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
  # rename the archieve file based on whether dump in DWR mode or not
  if [ "$?" == "2" ]; then
    CRASHDUMP_LOG_ARCHIVE="/mnt/data/acd_crashdump_dwr_$SLOT_NAME.tar.gz"
    LOG_MSG_PREFIX="DWR "
  else
    CRASHDUMP_LOG_ARCHIVE="/mnt/data/acd_crashdump_second_$SLOT_NAME.tar.gz"
    LOG_MSG_PREFIX="SECOND_DUMP "
  fi
fi

tar zcf $CRASHDUMP_LOG_ARCHIVE -C `dirname $CRASHDUMP_FILE` `basename $CRASHDUMP_FILE` && \
rm -rf $CRASHDUMP_FILE && \
logger -t "ipmid" -p daemon.crit "${LOG_MSG_PREFIX}ACD for FRU: $SLOT_NUM is generated at $CRASHDUMP_LOG_ARCHIVE"
cp -f "$CRASHDUMP_LOG_ARCHIVE" /tmp
echo "ACD for $SLOT_NAME Completed"

# Remove current pid file
rm $PID_FILE

echo "${LOG_MSG_PREFIX}ACD Dump Stored in $CRASHDUMP_LOG_ARCHIVE"

if [ "$POWER_OFF" = "1" ]; then
  logger -t "ipmid" -p daemon.crit "Dump completed, power off FRU: $SLOT_NUM"
  /usr/local/bin/power-util "slot$SLOT_NUM" off
fi

exit 0


#!/bin/bash

ME_UTIL="/usr/local/bin/me-util"
PECI_UTIL="/usr/bin/bic-util"
CMD_DIR="/usr/local/fbpackages/crashdump"
INTERFACE=$3
SENSOR_HISTORY=180
SLOT=$1

# read command from stdin
function execute_via_me {
  # if ME_UTIL cannot be executed, return
  [ -x $ME_UTIL ] || \
    return

  # Adapt command to ME-UTIL format
  cat | \
    sed 's/^0x/0xb8 0x40 0x57 0x01 0x00 0x/g' $1 | \
      $ME_UTIL $SLOT --file /dev/stdin | \
        sed 's/^57 01 00 //g'
}

# read command from stdin
function execute_via_peci {
  # if PECI_UTIL cannot be executed, return
  [ -x $PECI_UTIL ] || \
    return

  cat | \
    sed 's/^0x/0xe0 0x29 0x9c 0x9c 0x00 0x/g' $1 | \
      $PECI_UTIL $SLOT --file /dev/stdin | \
        sed 's/^9C 9C 00 //g'
}

function execute_cmd {
  if [ "$INTERFACE" == "ME_INTERFACE" ]; then
    cat | execute_via_me
  else
    cat | execute_via_peci
  fi
}

# ARG: Bus[8], Device[5], Function[3], Register[12]
function PCI_Config_Addr {
  # BUS
  RESULT=$((($1 & 0xFF) << 20))
  # DEV
  RESULT=$((RESULT | (($2 & 0x1F) << 15)))
  # FUN
  RESULT=$((RESULT | (($3 & 0x7) << 12)))
  # REG
  RESULT=$((RESULT | (($4 & 0xFFF) << 0)))
  printf "0x%x 0x%x 0x%x 0x%x" \
    $((RESULT & 0xFF)) \
    $(((RESULT >> 8) & 0xFF)) \
    $(((RESULT >> 16) & 0xFF)) \
    $(((RESULT >> 24) & 0xFF))
  return 0
}

function print_bus_device {
  MAX_FUN=0
  MAX_DEV=0
  BUS=$(printf "0x%02x" $1)
  if [ "$BUS" == "0x17" ] || [ "$BUS" == "0x65" ]; then
    MAX_FUN=1
  elif [ "$BUS" == "0x18" ]; then
    MAX_DEV=7
  elif [ "$BUS" == "0x66" ]; then
    MAX_DEV=3
  fi
  BUSM=$(($1 >> 4))
  BUSL=$((($1 & 0xF) << 4))
  for (( DEV=0; DEV<=$MAX_DEV; DEV++ )); do
    DEVM=$((DEV >> 1))
    DEVL=$(((DEV & 0x1) << 3))
    for (( FUN=0; FUN<=$MAX_FUN; FUN++ )); do
      printf "echo \"BUS 0x%02x, Dev 0x%02x, Fun 0x%02x, Reg: 0x100 ~ 0x147\"\n" $1 $DEV $FUN
      BYTE1=$(((DEVL | FUN) << 4 | 0x1))
      BYTE2=$((BUSL | DEVM))
      for REG in {0..68..4}; do
        printf "0x30 0x06 0x05 0x61 0x00 0x%02x 0x%02x 0x%02x 0x0%x\n" $REG $BYTE1 $BYTE2 $BUSM
      done
      if [ "$BUS" == "0x18" ] || [ "$BUS" == "0x66" ]; then
        printf "echo \"BUS 0x%02x, Dev 0x%02x, Fun 0x%02x, Reg: 0x1B0 ~ 0x1BB\"\n" $1 $DEV $FUN
        for REG in {176..184..4}; do
          printf "0x30 0x06 0x05 0x61 0x00 0x%02x 0x%02x 0x%02x 0x0%x\n" $REG $BYTE1 $BYTE2 $BUSM
        done
      fi
    done
  done
}

function find_end_device {
  echo "Find Root Port Bus $1 Dev $2 Fun $3..."
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 $(PCI_Config_Addr $1 $2 $3 0x18)" | execute_cmd)
  echo "$RES"
  CC=$(echo $RES| awk '{print $1;}')
  SECONDARY_BUS=0x$(echo $RES| awk '{print $3;}')
  SUBORDINATE_BUS=0x$(echo $RES| awk '{print $4;}')
  if [ "$CC" == "40" ] && [ "$SECONDARY_BUS" != "0x00" ] && [ "$SUBORDINATE_BUS" != "0x00" ]; then
    for (( BUS=$SECONDARY_BUS; BUS<=$SUBORDINATE_BUS; BUS++ )); do
      printf "End Device on Bus 0x%02x\n" $BUS
      echo "$(print_bus_device $BUS)" | execute_cmd
    done
  fi
}

function pcie_dump {

  # CPU and PCH
  [ -r $CMD_DIR/crashdump_pcie ] && \
    cat $CMD_DIR/crashdump_pcie | execute_cmd

  # Buses
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 0xcc 0x20 0x04 0x00" | execute_cmd)
  echo "Get CPUBUSNO: $RES"

  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')

  # Success
  if [ "$CC" == "40" ]; then
    # CPU root port - Bus 0x16/0x64, Dev 0~3, Fun:0
    # Root port buses
    ROOT_BUSES=$(echo $RES | awk '{printf "0x%s 0x%s", $3, $4;}')
    ROOT_BUS=$(echo $RES | awk '{printf "0x%s", $2;}')
    echo ROOT_BUSES=$ROOT_BUSES
    for ROOT in $ROOT_BUSES; do
      for DEV in {0..3}; do
        find_end_device $ROOT $DEV 0
      done
    done

    # CPU root port - Bus 0,Dev 0, Fun:0
    echo ROOT_BUS=$ROOT_BUS
    find_end_device $ROOT_BUS 0 0

    # PCH root port - Bus 0,Dev 0x1B, Fun:0
    find_end_device $ROOT_BUS 0x1B 0

    # PCH root port - Bus 0,Dev 0x1D, Fun:0
    find_end_device $ROOT_BUS 0x1D 0

    # PCH root port - Bus 0,Dev 0x1D, Fun:4
    find_end_device $ROOT_BUS 0x1D 4
  fi
}

function dwr_dump {

  echo
  echo DWR assert check:
  echo =================
  echo

  # Get biosscratchpad7[26]: DWR assert check
  RES=$(echo "0x30 0x06 0x05 0x61 0x00 0xbc 0x20 0x04 0x00" | execute_cmd)

  echo "< Get biosscratchpad7[26]: DWR assert check >"
  echo "$RES"
  echo

  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')
  DWR=0x$(echo $RES| awk '{print $5;}')

  # Success
  if [ "$CC" == "40" ]; then
    if [ $((DWR & 0x04)) == 4 ]; then
      echo "Auto Dump in DWR mode"
      return 2
    else
      echo "Auto Dump in non-DWR mode"
      return 1
    fi
  else
    echo "get DWR mode fail"
    return 0
  fi
}

function print_dump_help {
  echo "$0 <slot#> coreid <INTERFACE> ==> for CPU CoreID"
  echo "$0 <slot#> msr    <INTERFACE> ==> for CPU MSR"
  echo "$0 <slot#> sensors            ==> for sensor history"
  echo "$0 <slot#> threshold          ==> for sensor threshold"
  echo "$0 <slot#> pcie   <INTERFACE> ==> for PCIe"
  echo "$0 <slot#> dwr    <INTERFACE> ==> for DWR check"
  exit 1
}

if [ "$#" -eq 1 ] && [ $1 = "time" ]; then
  now=$(date)
  echo "Crash Dump generated at $now"
  exit 0
fi

if [ "$#" -eq 2 ]; then
  if [ "$2" != "sensors" ] && [ "$2" != "threshold" ]; then
    print_dump_help
  fi
elif [ "$#" -eq 3 ]; then
  if [ "$2" != "coreid" ] && [ "$2" != "msr" ] && [ "$2" != "pcie" ] && [ "$2" != "dwr" ]; then
    print_dump_help
  fi
  if [ "$INTERFACE" != "ME_INTERFACE" ] && [ "$INTERFACE" != "PECI_INTERFACE" ]; then
    print_dump_help
  fi
else
  print_dump_help
fi

echo ""

if [ "$2" = "coreid" ]; then
  [ -r $CMD_DIR/crashdump_coreid ] && \
      cat $CMD_DIR/crashdump_coreid | execute_cmd
elif [ "$2" = "msr" ]; then
  [ -r $CMD_DIR/crashdump_msr ] && \
      cat $CMD_DIR/crashdump_msr | execute_cmd
elif [ "$2" = "sensors" ]; then
  /usr/local/bin/sensor-util $1 --history $SENSOR_HISTORY \
   && /usr/local/bin/sensor-util bmc --history $SENSOR_HISTORY \
   && /usr/local/bin/sensor-util nic --history $SENSOR_HISTORY
elif [ "$2" = "threshold" ]; then
  /usr/local/bin/sensor-util $1 --threshold \
   && /usr/local/bin/sensor-util bmc --threshold \
   && /usr/local/bin/sensor-util nic --threshold
elif [ "$2" = "pcie" ]; then
  pcie_dump
elif [ "$2" = "dwr" ]; then
  dwr_dump
  exit $?
fi

exit 0


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
### BEGIN INIT INFO
# Provides:          setup-front-paneld
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start front panel control daemon
### END INIT INFO

echo -n "Setup Front Panel Daemon.."
runsv /etc/sv/front-paneld > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-fan
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     5
# Default-Stop:
# Short-Description: Set fan speed
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

default_fsc_config_path="/etc/fsc-config.json"

KV_CMD=/usr/bin/kv

function init_class1_fsc(){
  sys_config=$(/usr/local/bin/show_sys_config | grep -i "config:" | awk -F ": " '{print $3}')
  target_fsc_config=""
  config_type=""
  last_config_type=""
  if [ "$sys_config" = "A" ]; then
    config_type="1"
    target_fsc_config="/etc/FSC_CLASS1_PVT_type1.json"
  elif [ "$sys_config" = "B" ]; then
    get_1ou_type
    if [ "$type_1ou" = "EDSFF_1U" ]; then
      echo "use EDSFF_1U fan table"
      config_type="EDSFF_1U"
      target_fsc_config="/etc/FSC_CLASS1_DVT_EDSFF_1U.json"
    else
      config_type="10"
      target_fsc_config="/etc/FSC_CLASS1_EVT_type10.json"
    fi
  elif [ "$sys_config" = "D" ]; then
    get_2ou_type
    config_type="15"
    if ([ $type_2ou == "0x00" ] || [ $type_2ou == "0x03" ]); then
      echo "use Config D GPv3 fan table"
      target_fsc_config="/etc/FSC_CLASS1_CONFIG_D_GPV3.json"
    elif [ "$type_2ou" == "0x06" ]; then
      # boot with last fan table, check and reload it after recieve host post complete code
      last_config_type="$($KV_CMD get sled_system_conf persistent)"
      case "$last_config_type" in
        "Type_DPB")
          config_type="DPB"
          target_fsc_config="/etc/FSC_CLASS1_DVT_DP_HBA.json"
          ;;
        "Type_DPF")
          config_type="DPF"
          target_fsc_config="/etc/FSC_CLASS1_DP_FAVA.json"
          ;;
        *)
          config_type="DP"
          target_fsc_config="/etc/FSC_CLASS1_EVT_DP.json"
          ;;
      esac
      echo "use $config_type fan table"
    else
      target_fsc_config="/etc/FSC_CLASS1_type15.json"
    fi
  else
    config_type="15"
    target_fsc_config="/etc/FSC_CLASS1_type15.json"
  fi

  cp ${target_fsc_config} ${default_fsc_config_path}
  $KV_CMD set sled_system_conf "Type_${config_type}" persistent
}

function init_class2_fsc(){
  cpld_bus_num="4"
  exp_board=$(get_2ou_board_type $cpld_bus_num)
  if [ "$exp_board" == "0x02" ]; then # E1.S board
    ln -s /etc/FSC_CLASS2_PVT_SPE_config.json ${default_fsc_config_path}
    echo -n "Type_8" > /mnt/data/kv_store/sled_system_conf
  elif [ "$exp_board" == "0x04" ]; then #CWC board
    ln -s /etc/FSC_CLASS2_DVT_CWC.json ${default_fsc_config_path}
    echo -n "Type_17" > /mnt/data/kv_store/sled_system_conf
  else
    ln -s /etc/FSC_CLASS2_EVT_config.json ${default_fsc_config_path}
    echo -n "Type_17" > /mnt/data/kv_store/sled_system_conf
  fi

}

function get_1ou_type(){
  for i in $(seq 1 4)
  do
    if [[ $(is_sb_bic_ready $i) == "1" ]]; then
      output=$(bic-util slot$i 0xE0 2 0x9c 0x9c 0 0x5 0xe0 0xa0 0x9c 0x9c 0 | awk '{print $7" "$11}')
      if [[ ${output} == "00 07" ]]; then
        type_1ou=EDSFF_1U
        break
      elif [[ ${output} == "C1 " ]]; then
        break
      fi
    fi
  done
}

function get_2ou_type(){
  for i in {1..4..2}
  do
    I2C_NUM=$(($i+3))
    type_2ou=$(get_2ou_board_type $I2C_NUM)
    if [ $type_2ou != "0xff" ]; then
      break
    fi
  done
}

function start_sled_fsc() {
  echo "Setup fan speed..."
  /usr/local/bin/fan-util --set 70

  bmc_location=$(get_bmc_board_id)
  if [ $bmc_location -eq 9 ]; then
    #The BMC of class2
    init_class2_fsc
  elif [ $bmc_location -eq 14 ] || [ $bmc_location -eq 7 ]; then
    #The BMC of class1
    init_class1_fsc
  else
    echo -n "Is board id correct(id=$bmc_location)?..."
    exit -1
  fi

  echo -n "Setup fscd for fby3..."
  runsv /etc/sv/fscd > /dev/null 2>&1 &
  echo "Done."
}

function reload_sled_fsc() {
  bmc_location=$(get_bmc_board_id)
  if [ $bmc_location -eq 9 ]; then
    exp_board=$(get_2ou_board_type 4)

    if [ $exp_board == "0x04" ]; then
      run_fscd=true
    else
      #The BMC of class2 need to check the present status from BB BIC
      slot1_prsnt=$(bic-util slot1 0xe0 0x2 0x9c 0x9c 0x0 0x10 0xe0 0x41 0x9c 0x9c 0x0 0x0 27 | awk '{print $12}')
      slot3_prsnt=$(bic-util slot1 0xe0 0x2 0x9c 0x9c 0x0 0x10 0xe0 0x41 0x9c 0x9c 0x0 0x0 28 | awk '{print $12}')
      if [ "$slot1_prsnt" == "01" ] || [ "$slot3_prsnt" == "01" ]; then
        run_fscd=false
      else
        run_fscd=true
      fi
    fi
  else
    cnt=`get_all_server_prsnt`
    run_fscd=false

    #Config A and B or Config D
    sys_config=$(cat /mnt/data/kv_store/sled_system_conf)
    if [[ "$sys_config" =~ ^(Type_(1|10|EDSFF_1U))$ ]]; then
      if [ $cnt -eq 4 ]; then
        run_fscd=true
      fi
    else
      if [ $cnt -eq 2 ]; then
        run_fscd=true
      elif [ $cnt -eq 1 ]; then
        # DP system only has one slot
        if [ "$sys_config" = "Type_DP" ] || [ "$sys_config" = "Type_DPB" ] || [ "$sys_config" = "Type_DPF" ]; then
          run_fscd=true
        fi
      fi
    fi
  fi

  fscd_status=$(cat /etc/sv/fscd/supervise/stat)
  if [ $run_fscd = false ]; then
    if [ "$fscd_status" == "run" ]; then
      sleep 1 && /usr/bin/sv stop fscd
      echo "slot is pulled out, stop fscd."
      /usr/local/bin/fan-util --set 100
    else
      echo "fscd is already stopped."
    fi
  else
    #if fscd has been running, there is no need to restart
    if ! [ "$fscd_status" == "run" ]; then
      sleep 1 && /usr/bin/sv restart fscd
    fi
  fi
}

case "$1" in
  start)
    start_sled_fsc
    #if one of the blades is not present, stop fscd
    reload_sled_fsc
  ;;

  reload)
    #if one of the blades is pulled out, stop fscd
    reload_sled_fsc
  ;;

  *)
    echo "Usage: /etc/init.d/setup-fan.sh {start|reload}"
  ;;
esac

exit 0


#!/bin/sh
. /usr/local/fbpackages/utils/ast-functions

SLOTS=

function init_class2_gpiod() {
  SLOTS="$SLOTS slot1"
}

function init_class1_gpiod() {
  if [[ $(is_server_prsnt 1) == "1" ]]; then
    SLOTS="$SLOTS slot1"
  fi

  if [[ $(is_server_prsnt 2) == "1" ]]; then
    SLOTS="$SLOTS slot2"
  fi

  if [[ $(is_server_prsnt 3) == "1" ]]; then
    SLOTS="$SLOTS slot3"
  fi

  if [[ $(is_server_prsnt 4) == "1" ]]; then
    SLOTS="$SLOTS slot4"
  fi
}

bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  init_class2_gpiod
elif [ $bmc_location -eq 14 ] || [ $bmc_location -eq 7 ]; then
  #The BMC of class1
  init_class1_gpiod
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi

exec /usr/local/bin/gpiod $SLOTS


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          setup-sensord
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup gpio monitoring for fby3... "

runsv /etc/sv/gpiod > /dev/null 2>&1 &

echo "done."


#! /bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          server-init
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: Provides a service to reinitialize the server
#
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

# stop the service first
sv stop gpiod
sv stop sensord

slot_num=$1
PID=$$
PID_FILE="/var/run/server-init.${slot_num}.pid"
bus=$((slot_num-1))

# check if the script is running
[ -r $PID_FILE ] && OLDPID=`cat $PID_FILE` || OLDPID=''

# Set the current pid
echo $PID > $PID_FILE

# kill the previous running script if it is existed
if [ ! -z "$OLDPID" ] && (grep "server-init" /proc/$OLDPID/cmdline &> /dev/null) ; then
  echo "kill pid $OLDPID..."
  kill -s 9 $OLDPID
  ps | grep 'bic-cached' | grep "slot${slot_num}" | awk '{print $1}'| xargs kill -s 9 &> /dev/null
  ps | grep 'power-util' | grep "slot${slot_num}" | awk '{print $1}'| xargs kill -s 9 &> /dev/null
  ps | grep 'setup-fan' | awk '{print $1}'| xargs kill -s 9 &> /dev/null
fi
unset OLDPID

if [ $(is_server_prsnt $slot_num) == "0" ]; then
  disable_server_12V_power $slot_num
  gpio_set FM_BMC_SLOT${slot_num}_ISOLATED_EN_R 0
  rm -f /tmp/*fruid_slot${slot_num}*
  rm -f /tmp/*sdr_slot${slot_num}*
  rm -f /tmp/cache_store/slot${slot_num}_vr*
  rm -f /tmp/cache_store/fru${slot_num}_2ou_board_type
  rm -f /tmp/cache_store/slot${slot_num}_is_m2_exp_prsnt
  rm -f /tmp/cache_store/slot${slot_num}_1ou_card_type
  for pcie_info_file in $(ls /mnt/data/kv_store/sys_config/fru${slot_num}_pcie_*)
  do
    /usr/bin/kv del ${pcie_info_file:19} persistent > /dev/null 2>&1
  done
  set_nic_power

  # reload fscd
  /etc/init.d/setup-fan.sh reload
else
  /usr/bin/sv start ipmbd_${bus} > /dev/null 2>&1
  /usr/local/bin/power-util "slot${slot_num}" 12V-on
  /usr/local/bin/bic-cached -s "slot${slot_num}"
  /usr/local/bin/bic-cached -f "slot${slot_num}"
  /usr/bin/fw-util "slot${slot_num}" --version > /dev/null
  /etc/init.d/setup-sic.sh "slot${slot_num}" > /dev/null

  # reload fscd
  /etc/init.d/setup-fan.sh start
fi

# start the services again
sv start gpiod
sv start sensord


#!/bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
### BEGIN INIT INFO
# Provides:          setup-gpiointrd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start gpiointrd daemon
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

echo -n "Setup gpiointrd for fby3..."
runsv /etc/sv/gpiointrd > /dev/null 2>&1 &

bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  #gpiointrd only supports the hot service and the hot service is not supported on class 2.
  #stop it
  sv stop gpiointrd
fi

echo "done."


#! /bin/sh
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          ipmbd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Provides ipmb message tx/rx service
#
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions

function init_class1_ipmb(){
  for slot_num in $(seq 1 4); do
    bus=$((slot_num-1))
    echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-${bus}/new_device
    runsv /etc/sv/ipmbd_${bus} > /dev/null 2>&1 &
    slot_present=$(gpio_get PRSNT_MB_BMC_SLOT${slot_num}_BB_N)
    if [[ "$slot_present" == "1" ]]; then
      sv stop ipmbd_${bus}
      disable_server_12V_power ${slot_num}
    else
      enable_server_i2c_bus ${slot_num}
    fi
  done

  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-9/new_device
  runsv /etc/sv/ipmbd_9 > /dev/null 2>&1 &

  # add BMC slave address to NIC I2C bus for MCTP
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-8/new_device
}

function init_class2_ipmb(){
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-0/new_device
  runsv /etc/sv/ipmbd_0 > /dev/null 2>&1 &

  # add BMC slave address to NIC I2C bus for MCTP
  echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-8/new_device
}

echo -n "Starting IPMB Rx/Tx Daemon.."

ulimit -q 1024000

bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  init_class2_ipmb
elif [ $bmc_location -eq 14 ] || [ $bmc_location -eq 7 ]; then
  #The BMC of class1
  init_class1_ipmb
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi

echo "Done."


#!/bin/sh

. /usr/local/fbpackages/utils/ast-functions
BOARD_ID=$(get_bmc_board_id)

# PLATFORM MAY CHOOSE TO OVERRIDE THIS FILE
echo -n "Starting mTerm console server..."
runsv /etc/sv/mTerm1 > /dev/null 2>&1 &

if [ $BOARD_ID -eq 14 ] || [ $BOARD_ID -eq 7 ]; then
  runsv /etc/sv/mTerm2 > /dev/null 2>&1 &
  runsv /etc/sv/mTerm3 > /dev/null 2>&1 &
  runsv /etc/sv/mTerm4 > /dev/null 2>&1 &
fi

echo "done."


#!/bin/bash
#This is workaround for IPV4 lost There is slim chance ipv4 lost when AC cycle.
#DHCP Client can't get OFFER package from server.
#This script is for reinitial ipv4 to prevnet it lost.

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

check_eth0_ipv4() {
  for i in {1..10};
  do
    ip=$(ifconfig eth0 |grep "inet addr")
    if [[ -z "$ip" ]]; then
      pid=$(cat /var/run/dhclient.eth0.pid)
      kill $pid
      dhclient -d -pf /var/run/dhclient.eth0.pid eth0 > /dev/null 2>&1 &
      sleep 3
    else
      break;
    fi
  done
}

echo "check eth0 ipv4..."
check_eth0_ipv4 &


#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

KEYDIR=/mnt/data/kv_store
DEF_PWR_ON=1
TO_PWR_ON=

check_por_config() {
  TO_PWR_ON=-1

  # Check if the file/key doesn't exist
  if [ ! -f "${KEYDIR}/slot${1}_por_cfg" ]; then
    TO_PWR_ON=$DEF_PWR_ON
  else
    POR=`cat ${KEYDIR}/slot${1}_por_cfg`

    if [ $POR == "on" ]; then
      TO_PWR_ON=1;
    elif [ $POR == "lps" ]; then
      # Check if the file/key doesn't exist
      if [ ! -f "${KEYDIR}/pwr_server${1}_last_state" ]; then
        TO_PWR_ON=$DEF_PWR_ON
      else
        LS=`cat ${KEYDIR}/pwr_server${1}_last_state`
        if [ $LS == "on" ]; then
          TO_PWR_ON=1;
        fi
      fi
    fi
  fi
}

function init_4u_gpv3_bic() {
  GPV3_PRESENT=$(/usr/sbin/i2cget -y 4 0x50 0x04)

  if [ $((GPV3_PRESENT & 0x03)) -eq 0 ]; then
    retry=3

    while [ $retry -gt 0 ]; do
      res=$(/usr/bin/bic-util slot1-2U-top 0xe0 0x70 0x9c 0x9c 0x0 0x1 0x0)

      if [[ ${res:0:8} == "9C 9C 00" ]]; then
        logger "Init top gpv3 position"
        break
      else
        retry=$((retry - 1))
        sleep 1
      fi
    done

    if [ $retry -eq 0 ]; then
      logger "Fail to init top gpv3 position"
    fi
  fi

  if [ $((GPV3_PRESENT & 0x0C)) -eq 0 ]; then
    retry=3

    while [ $retry -gt 0 ]; do
      res=$(/usr/bin/bic-util slot1-2U-bot 0xe0 0x70 0x9c 0x9c 0x0 0x1 0x1)

      if [[ ${res:0:8} == "9C 9C 00" ]]; then
        logger "Init bot gpv3 position"
        break
      else
        retry=$((retry - 1))
        sleep 1
      fi
    done

    if [ $retry -eq 0 ]; then
      logger "Fail to init bot gpv3 position"
    fi
  fi
}

function init_2u_gpv3_bic() {
  retry=3
  while [ $retry -gt 0 ]; do
    res=$(/usr/bin/bic-util slot1 0xe0 0x2 0x9c 0x9c 0x0 0x15 0xe0 0x70 0x9c 0x9c 0x0 0x1 0xff)

    if [[ ${res:0:29} == "9C 9C 00 15 E4 70 00 9C 9C 00" ]]; then
      logger "Init gpv3 position"
      break
    else
      retry=$((retry - 1))
      sleep 1
    fi
  done

  if [ $retry -eq 0 ]; then
    logger "Fail to init gpv3 position"
  fi
}

function init_class2_server() {
  EXP_BOARD_TYPE=$(get_2ou_board_type 4)
  if [ $EXP_BOARD_TYPE == "0x04" ]; then
    init_4u_gpv3_bic
  elif [ $EXP_BOARD_TYPE == "0x00" ] || [ $EXP_BOARD_TYPE == "0x03" ]; then
    init_2u_gpv3_bic
  fi

  if [ $(is_bmc_por) -eq 1 ]; then
    devmem_clear_bit $(scu_addr 9c) 17

    check_por_config 1
    if [ $TO_PWR_ON -eq 1 ] ; then
      power-util slot1 on
    fi
  fi

  show_sys_config -r > /dev/null
}

function init_class1_server() {
  # Check whether it is fresh power on reset
  if [ $(is_bmc_por) -eq 1 ]; then
    # Disable clearing of PWM block on WDT SoC Reset
    devmem_clear_bit $(scu_addr 9c) 17

    check_por_config 1
    if [ $(is_sb_bic_ready 1) == "1" ] && [ $TO_PWR_ON -eq 1 ] ; then
      power-util slot1 on
    fi

    check_por_config 2
    if [ $(is_sb_bic_ready 2) == "1" ] && [ $TO_PWR_ON -eq 1 ] ; then
      power-util slot2 on
    fi

    check_por_config 3
    if [ $(is_sb_bic_ready 3) == "1" ] && [ $TO_PWR_ON -eq 1 ] ; then
      power-util slot3 on
    fi

    check_por_config 4
    if [ $(is_sb_bic_ready 4) == "1" ] && [ $TO_PWR_ON -eq 1 ] ; then
      power-util slot4 on
    fi
  fi
}

if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
  date -s "2018-01-01 00:00:00"
fi

/usr/local/bin/sync_date.sh

bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  init_class2_server
elif [ $bmc_location -eq 14 ] || [ $bmc_location -eq 7 ]; then
  #The BMC of class1
  init_class1_server
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi