#! /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
#
. /usr/local/fbpackages/utils/ast-functions

HSC_DET_ADM1278=0
HSC_DET_LTC4282=1
HSC_DET_MP5990=2
HSC_DET_ADM1276=3

function create_new_dev() {
  local dev_name=$1
  local addr=$2
  local bus=$3

  echo $dev_name $addr > /sys/class/i2c-dev/i2c-${bus}/device/new_device
}

function init_class1_dev(){
  hsc_detect=$(get_hsc_bb_detect)
  VENDOR_TI="0x55"
  KEY_TEMP_VENDOR="tmp_vendor"

  if [ $hsc_detect -eq $HSC_DET_ADM1278 ]; then
    for i in $(seq 1 5)
    do
      #enable the register of the temperature of hsc
      /usr/sbin/i2cset -y 11 0x40 0xd4 0x1c 0x3f i 2> /dev/null
      RET=$?
      if [ "$RET" = 0 ]; then
        break
      fi
    done
  elif [ $hsc_detect -eq $HSC_DET_LTC4282 ] || [ $hsc_detect -eq $HSC_DET_ADM1276 ]; then
    if [ $hsc_detect -eq $HSC_DET_LTC4282 ]; then
      create_new_dev "ltc4282" 0x40 11
    fi
    vendor=$(/usr/sbin/i2cget -f -y 12 0x4c 0xfe)
    if [ "$vendor" = "$VENDOR_TI" ]; then
      /usr/bin/kv set $KEY_TEMP_VENDOR "TI"
      echo "$vendor"
      create_new_dev "tmp401" 0x4c 12
    else
      echo "$vendor"
      /usr/bin/kv set $KEY_TEMP_VENDOR "non-TI"
      create_new_dev "lm90" 0x4c 12
    fi
  fi

  #create the device of the inlet/outlet temp.
  create_new_dev "lm75" 0x4e 12
  create_new_dev "lm75" 0x4f 12

  #create the device of bmc/bb fru.
  create_new_dev "24c128" 0x51 11
  create_new_dev "24c128" 0x54 11

  local VENDOR_MPS="0x4d5053"
  local chip=""

  #create the device of medusa board
  vendor=$(/usr/sbin/i2cget -y 11 0x44 0x99 i 2> /dev/null | tail -n 1)
  if [ "$vendor" == "$VENDOR_MPS" ]; then
    chip="mp5920"
  else
    chip="ltc4282"
  fi

  create_new_dev $chip 0x44 11

  # /mnt/data/kv_store checking, and create one if not exist
  if [ ! -d /mnt/data/kv_store ]; then
    /bin/mkdir -p /mnt/data/kv_store
  fi
  echo -n $chip > /mnt/data/kv_store/bb_hsc_conf

  # check nic power to see if it's need to be set to standby mode
  set_nic_power
}

function init_class2_dev(){
  #create the device of the outlet temp.
  create_new_dev "lm75" 0x4f 2

  #create the device of bmc/nic fru.
  create_new_dev "24c128" 0x51 10
  create_new_dev "24c128" 0x54 10
}

#create the device of mezz card
create_new_dev "tmp421" 0x1f 8
create_new_dev "24c32" 0x50 8

echo -n "Setup devs for fby3..."

bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  init_class2_dev
elif [ $bmc_location -eq 14 ] || [ $bmc_location -eq 7 ]; then
  #The BMC of class1
  init_class1_dev
else
  echo -n "Is board id correct(id=$bmc_location)?..."
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
#

### BEGIN INIT INFO
# Provides:          gpio-setup
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description:  Set up GPIO pins as appropriate
### END INIT INFO

# This file contains definitions for the GPIO pins that were not otherwise
# defined in other files.  We should probably move some more of the
# definitions to this file at some point.

# The commented-out sections are generally already defined elsewhere,
# and defining them twice generates errors.

# The exception to this is the definition of the GPIO H0, H1, and H2
# pins, which seem to adversely affect the rebooting of the system.
# When defined, the system doesn't reboot cleanly.  We're still
# investigating this.

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

echo -n "Set up GPIO pins....."

# GPIOF0-BOARD_BMC_ID0_R
# GPIOF1-BOARD_BMC_ID1_R
# GPIOF2-BOARD_BMC_ID2_R
# GPIOF3-BOARD_BMC_ID3_R
devmem_clear_bit $(scu_addr 80) 24
devmem_clear_bit $(scu_addr 80) 25
devmem_clear_bit $(scu_addr 80) 26
devmem_clear_bit $(scu_addr 80) 27
devmem_clear_bit $(scu_addr 90) 30
gpio_export BOARD_BMC_ID0_R GPIOF0
gpio_export BOARD_BMC_ID1_R GPIOF1
gpio_export BOARD_BMC_ID2_R GPIOF2
gpio_export BOARD_BMC_ID3_R GPIOF3

BOARD_ID=$(get_bmc_board_id)

# GPIOA0-BMC_READY_R
devmem_clear_bit $(scu_addr 80) 0
gpio_export BMC_READY_R GPIOA0
gpio_set BMC_READY_R 0

# GPIOA3-FM_NIC_WAKE_BMC_N
devmem_clear_bit $(scu_addr 80) 3
gpio_export FM_NIC_WAKE_BMC_N GPIOA3

# GPIOE0-P3V3_NIC_FAULT_N
# GPIOE1-NIC_POWER_BMC_EN_R
# GPIOE2-P12V_NIC_FAULT_N
# GPIOE5-EMMC_RST_N_R
devmem_clear_bit $(scu_addr 80) 16
devmem_clear_bit $(scu_addr 80) 17
devmem_clear_bit $(scu_addr 80) 18
devmem_clear_bit $(scu_addr 80) 21
devmem_clear_bit $(scu_addr 8C) 12
devmem_clear_bit $(scu_addr 8C) 13
devmem_clear_bit $(scu_addr 8C) 14
gpio_export P3V3_NIC_FAULT_N GPIOE0
gpio_export NIC_POWER_BMC_EN_R GPIOE1
gpio_export P12V_NIC_FAULT_N GPIOE2
gpio_export EMMC_RST_N_R GPIOE5
gpio_set NIC_POWER_BMC_EN_R 1
gpio_set EMMC_RST_N_R 1

# GPIOF4-SMB_RST_PRIMARY_BMC_N_R
devmem_clear_bit $(scu_addr 80) 28
gpio_export SMB_RST_PRIMARY_BMC_N_R GPIOF4

# GPIOG3-SMB_RST_SECONDARY_BMC_N_R
devmem_clear_bit $(scu_addr 84) 3
gpio_export SMB_RST_SECONDARY_BMC_N_R GPIOG3

# GPIOM4-FM_PWRBRK_PRIMARY_R
devmem_clear_bit $(scu_addr 84) 28
gpio_export FM_PWRBRK_PRIMARY_R GPIOM4
gpio_set FM_PWRBRK_PRIMARY_R 0

# GPIOM5-OCP_NIC_PRSNT_BMC_N
devmem_clear_bit $(scu_addr 84) 29
gpio_export OCP_NIC_PRSNT_BMC_N GPIOM5

# GPIOQ7-FM_BMC_TPM_PRSNT_N
devmem_clear_bit $(scu_addr 2C) 29
gpio_export FM_BMC_TPM_PRSNT_N GPIOQ7

# GPIOAB0-PWRGD_NIC_BMC
devmem_clear_bit $(scu_addr A8) 0
gpio_export PWRGD_NIC_BMC GPIOAB0

# GPIOAB3-RST_BMC_WDRST2_R
devmem_set_bit $(scu_addr A8) 3
gpio_export RST_BMC_WDRST2_R GPIOAB3

# GPIOAC4-EMMC_PRESENT_N
devmem_clear_bit $(scu_addr AC) 4
gpio_export EMMC_PRESENT_N GPIOAC4

###NIC EXP BMC###
if [ $BOARD_ID -eq 9 ]; then
  # GPIOB0-SMB_MUX_ALT_N
  gpio_export SMB_MUX_ALT_N GPIOB0

  # GPIOS0-PWROK_STBY_BMC
  devmem_clear_bit $(scu_addr 8C) 0
  gpio_export PWROK_STBY_BMC GPIOS0

  # GPIOZ0-FM_BMC_ISOLATED_EN_R
  # GPIOZ1-FM_BMC_ISOLATED_UART_EN_R
  devmem_clear_bit $(scu_addr A4) 16
  devmem_clear_bit $(scu_addr A4) 17
  gpio_export FM_BMC_ISOLATED_EN_R GPIOZ0
  gpio_export FM_BMC_ISOLATED_UART_EN_R GPIOZ1
  gpio_set FM_BMC_ISOLATED_EN_R 1
  gpio_set FM_BMC_ISOLATED_UART_EN_R 0

###Baseboard BMC###
elif [ $BOARD_ID -eq 14 ] || [ $BOARD_ID -eq 7 ]; then
  # GPIOA1-SMB_HOTSWAP_BMC_ALERT_N_R
  devmem_clear_bit $(scu_addr 80) 1
  gpio_export SMB_HOTSWAP_BMC_ALERT_N_R GPIOA1

  # GPIOA2-FM_HSC_BMC_FAULT_N_R
  devmem_clear_bit $(scu_addr 80) 15
  gpio_export FM_HSC_BMC_FAULT_N_R GPIOA2

  # GPIOB0-SMB_BMC_SLOT1_ALT_N
  # GPIOB1-SMB_BMC_SLOT2_ALT_N
  # GPIOB2-SMB_BMC_SLOT3_ALT_N
  # GPIOB3-SMB_BMC_SLOT4_ALT_N
  gpio_export SMB_BMC_SLOT1_ALT_N GPIOB0
  gpio_export SMB_BMC_SLOT2_ALT_N GPIOB1
  gpio_export SMB_BMC_SLOT3_ALT_N GPIOB2
  gpio_export SMB_BMC_SLOT4_ALT_N GPIOB3

  # GPIOB4-PRSNT_MB_BMC_SLOT1_BB_N
  # GPIOB5-PRSNT_MB_BMC_SLOT2_BB_N
  # GPIOB6-PRSNT_MB_BMC_SLOT3_BB_N
  # GPIOB7-PRSNT_MB_BMC_SLOT4_BB_N
  devmem_clear_bit $(scu_addr 80) 13
  devmem_clear_bit $(scu_addr 80) 14
  gpio_export PRSNT_MB_BMC_SLOT1_BB_N GPIOB4
  gpio_export PRSNT_MB_BMC_SLOT2_BB_N GPIOB5
  gpio_export PRSNT_MB_BMC_SLOT3_BB_N GPIOB6
  gpio_export PRSNT_MB_BMC_SLOT4_BB_N GPIOB7

  # GPIOE3-ADAPTER_BUTTON_BMC_CO_N_R
  # GPIOE4-RST_BMC_USB_HUB_N_R
  devmem_clear_bit $(scu_addr 80) 19
  devmem_clear_bit $(scu_addr 80) 20
  gpio_export ADAPTER_BUTTON_BMC_CO_N_R GPIOE3
  gpio_export RST_BMC_USB_HUB_N_R GPIOE4
  gpio_set RST_BMC_USB_HUB_N_R 1

  # GPIOG4-HSC_BB_BMC_DETECT2
  # GPIOG6-HSC_BB_BMC_DETECT1
  devmem_clear_bit $(scu_addr 84) 4
  devmem_clear_bit $(scu_addr 84) 6
  devmem_clear_bit $(scu_addr 94) 12
  gpio_export HSC_BB_BMC_DETECT2 GPIOG4
  gpio_export HSC_BB_BMC_DETECT1 GPIOG6

  # GPIOI5-HSC_BB_BMC_DETECT0
  gpio_export HSC_BB_BMC_DETECT0 GPIOI5

  # GPIOI0-FAN0_BMC_CPLD_EN_R
  # GPIOI1-FAN1_BMC_CPLD_EN_R
  # GPIOI2-FAN2_BMC_CPLD_EN_R
  # GPIOI3-FAN3_BMC_CPLD_EN_R
  devmem_clear_bit $(scu_addr 90) 6
  gpio_export FAN0_BMC_CPLD_EN_R GPIOI0
  gpio_export FAN1_BMC_CPLD_EN_R GPIOI1
  gpio_export FAN2_BMC_CPLD_EN_R GPIOI2
  gpio_export FAN3_BMC_CPLD_EN_R GPIOI3
  gpio_set FAN0_BMC_CPLD_EN_R 1
  gpio_set FAN1_BMC_CPLD_EN_R 1
  gpio_set FAN2_BMC_CPLD_EN_R 1
  gpio_set FAN3_BMC_CPLD_EN_R 1

  # GPIOJ4-RST_PCIE_RESET_SLOT1_N
  # GPIOJ5-RST_PCIE_RESET_SLOT2_N
  # GPIOJ6-RST_PCIE_RESET_SLOT3_N
  # GPIOJ7-RST_PCIE_RESET_SLOT4_N
  devmem_clear_bit $(scu_addr 84) 12
  devmem_clear_bit $(scu_addr 84) 13
  devmem_clear_bit $(scu_addr 84) 14
  devmem_clear_bit $(scu_addr 84) 15
  devmem_clear_bit $(scu_addr 94) 8
  devmem_clear_bit $(scu_addr 94) 9
  gpio_export RST_PCIE_RESET_SLOT1_N GPIOJ4
  gpio_export RST_PCIE_RESET_SLOT2_N GPIOJ5
  gpio_export RST_PCIE_RESET_SLOT3_N GPIOJ6
  gpio_export RST_PCIE_RESET_SLOT4_N GPIOJ7

  # GPIOL0-AC_ON_OFF_BTN_SLOT1_N
  # GPIOL1-AC_ON_OFF_BTN_BMC_SLOT2_N_R
  # GPIOL2-AC_ON_OFF_BTN_SLOT3_N
  # GPIOL3-AC_ON_OFF_BTN_BMC_SLOT4_N_R
  devmem_clear_bit $(scu_addr 84) 16
  devmem_clear_bit $(scu_addr 84) 17
  devmem_clear_bit $(scu_addr 84) 18
  devmem_clear_bit $(scu_addr 84) 19
  gpio_export AC_ON_OFF_BTN_SLOT1_N GPIOL0
  gpio_export AC_ON_OFF_BTN_SLOT2_N GPIOL1
  gpio_export AC_ON_OFF_BTN_SLOT3_N GPIOL2
  gpio_export AC_ON_OFF_BTN_SLOT4_N GPIOL3

  # GPIOL4-FAST_PROCHOT_BMC_N_R
  devmem_clear_bit $(scu_addr 84) 20
  gpio_export FAST_PROCHOT_BMC_N_R GPIOL4

  # GPIOL5-USB_BMC_EN_R
  devmem_clear_bit $(scu_addr 84) 21
  gpio_export USB_BMC_EN_R GPIOL5
  gpio_set USB_BMC_EN_R 1

  # GPIOM0-HSC_FAULT_SLOT1_N
  # GPIOM1-HSC_FAULT_BMC_SLOT2_N_R
  # GPIOM2-HSC_FAULT_SLOT3_N
  # GPIOM3-HSC_FAULT_BMC_SLOT4_N_R
  devmem_clear_bit $(scu_addr 84) 24
  devmem_clear_bit $(scu_addr 84) 25
  devmem_clear_bit $(scu_addr 84) 26
  devmem_clear_bit $(scu_addr 84) 27
  gpio_export HSC_FAULT_SLOT1_N GPIOM0
  gpio_export HSC_FAULT_BMC_SLOT2_N_R GPIOM1
  gpio_export HSC_FAULT_SLOT3_N GPIOM2
  gpio_export HSC_FAULT_BMC_SLOT4_N_R GPIOM3

  # GPION4-DUAL_FAN0_DETECT_BMC_N_R
  # GPION5-DUAL_FAN1_DETECT_BMC_N_R
  devmem_clear_bit $(scu_addr 88) 4
  devmem_clear_bit $(scu_addr 88) 5
  gpio_export DUAL_FAN0_DETECT_BMC_N_R GPION4
  gpio_export DUAL_FAN1_DETECT_BMC_N_R GPION5

  # GPIOP0-SLOT1_ID0_DETECT_BMC_N
  # GPIOP1-SLOT1_ID1_DETECT_BMC_N
  # GPIOP2-SLOT2_ID0_DETECT_BMC_N
  # GPIOP3-SLOT2_ID1_DETECT_BMC_N
  # GPIOP4-SLOT3_ID0_DETECT_BMC_N
  # GPIOP5-SLOT3_ID1_DETECT_BMC_N
  # GPIOP6-SLOT4_ID0_DETECT_BMC_N
  # GPIOP7-SLOT4_ID1_DETECT_BMC_N
  devmem_clear_bit $(scu_addr 88) 16
  devmem_clear_bit $(scu_addr 88) 17
  devmem_clear_bit $(scu_addr 88) 18
  devmem_clear_bit $(scu_addr 88) 19
  devmem_clear_bit $(scu_addr 88) 20
  devmem_clear_bit $(scu_addr 88) 21
  devmem_clear_bit $(scu_addr 88) 22
  devmem_clear_bit $(scu_addr 88) 23
  gpio_export SLOT1_ID0_DETECT_BMC_N GPIOP0
  gpio_export SLOT1_ID1_DETECT_BMC_N GPIOP1
  gpio_export SLOT2_ID0_DETECT_BMC_N GPIOP2
  gpio_export SLOT2_ID1_DETECT_BMC_N GPIOP3
  gpio_export SLOT3_ID0_DETECT_BMC_N GPIOP4
  gpio_export SLOT3_ID1_DETECT_BMC_N GPIOP5
  gpio_export SLOT4_ID0_DETECT_BMC_N GPIOP6
  gpio_export SLOT4_ID1_DETECT_BMC_N GPIOP7

  # GPIOQ6-USB_OC_N
  devmem_clear_bit $(scu_addr 2C) 1
  gpio_export USB_OC_N GPIOQ6

  # GPIOS0-PWROK_STBY_BMC_SLOT1
  # GPIOS1-PWROK_STBY_BMC_SLOT2
  # GPIOS2-PWROK_STBY_BMC_SLOT3
  # GPIOS3-PWROK_STBY_BMC_SLOT4
  devmem_clear_bit $(scu_addr 8C) 0
  devmem_clear_bit $(scu_addr 8C) 1
  devmem_clear_bit $(scu_addr 8C) 2
  devmem_clear_bit $(scu_addr 8C) 3
  gpio_export PWROK_STBY_BMC_SLOT1 GPIOS0
  gpio_export PWROK_STBY_BMC_SLOT2 GPIOS1
  gpio_export PWROK_STBY_BMC_SLOT3 GPIOS2
  gpio_export PWROK_STBY_BMC_SLOT4 GPIOS3

  # GPIOZ0-FM_BMC_SLOT1_ISOLATED_EN_R
  # GPIOZ1-FM_BMC_SLOT2_ISOLATED_EN_R
  # GPIOZ2-FM_BMC_SLOT3_ISOLATED_EN_R
  # GPIOZ3-FM_BMC_SLOT4_ISOLATED_EN_R
  devmem_clear_bit $(scu_addr A4) 16
  devmem_clear_bit $(scu_addr A4) 17
  devmem_clear_bit $(scu_addr A4) 18
  devmem_clear_bit $(scu_addr A4) 19
  gpio_export FM_BMC_SLOT1_ISOLATED_EN_R GPIOZ0
  gpio_export FM_BMC_SLOT2_ISOLATED_EN_R GPIOZ1
  gpio_export FM_BMC_SLOT3_ISOLATED_EN_R GPIOZ2
  gpio_export FM_BMC_SLOT4_ISOLATED_EN_R GPIOZ3
  gpio_set FM_BMC_SLOT1_ISOLATED_EN_R 0
  gpio_set FM_BMC_SLOT2_ISOLATED_EN_R 0
  gpio_set FM_BMC_SLOT3_ISOLATED_EN_R 0
  gpio_set FM_BMC_SLOT4_ISOLATED_EN_R 0

  # GPIOAB1-OCP_DEBUG_BMC_PRSNT_N
  devmem_clear_bit $(scu_addr A8) 1
  gpio_export OCP_DEBUG_BMC_PRSNT_N GPIOAB1

  # These pins are added in DVT
  # GPIOI4-FM_RESBTN_SLOT3_BMC_N
  # GPIOI6-FM_RESBTN_SLOT4_BMC_N
  # GPIOAC2-FM_RESBTN_SLOT1_BMC_N
  # GPIOAC3-FM_RESBTN_SLOT2_BMC_N
  devmem_clear_bit $(scu_addr AC) 2
  devmem_clear_bit $(scu_addr AC) 3
  gpio_export FM_RESBTN_SLOT3_BMC_N GPIOI4
  gpio_export FM_RESBTN_SLOT4_BMC_N GPIOI6
  gpio_export FM_RESBTN_SLOT1_BMC_N GPIOAC2
  gpio_export FM_RESBTN_SLOT2_BMC_N GPIOAC3

else
  echo "Is board id correct(id=$BOARD_ID)?"
fi

gpio_set BMC_READY_R 1


#! /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 Vishay IC 450/451
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: setup Vout and OCP dynamically
#
### END INIT INFO

. /usr/local/fbpackages/utils/ast-functions
LOG="/tmp/setup-sic.log"
FEXP_INTF="0x05"
REXP_INTF="0x15"

function run_cmd() {
  echo "bic-util $1" >> $LOG
  /usr/bin/bic-util $1 >> $LOG
}

function init_vishay_ic(){
  local slot=$1
  local intf=$2

  #SiC450
  #OCP: 48A
  run_cmd "$slot 0xe0 0x02 0x9c 0x9c 0x0 $intf 0x18 0x52 0x13 0x28 0x00 0x46 0x60 0xf8"

  #Offset Vout 70mV
  run_cmd "$slot 0xe0 0x02 0x9c 0x9c 0x0 $intf 0x18 0x52 0x13 0x28 0x00 0x22 0x09 0x00"

  #Clear fault flag
  run_cmd "$slot 0xe0 0x02 0x9c 0x9c 0x0 $intf 0x18 0x52 0x13 0x28 0x00 0x03 0x0f"

  #SiC451
  #Offset Vout 50mV
  run_cmd "$slot 0xe0 0x02 0x9c 0x9c 0x0 $intf 0x18 0x52 0x13 0x26 0x00 0x22 0x06 0x00"

  #Clear fault flag
  run_cmd "$slot 0xe0 0x02 0x9c 0x9c 0x0 $intf 0x18 0x52 0x13 0x26 0x00 0x03"
}

function check_class1_config() {
  local slot=$1
  local val=$2
  local i2c_num=$(get_cpld_bus ${slot:4:1})
  local type_2ou=$(get_2ou_board_type $i2c_num)

  if [[ $((val & 0x08)) = "0" && $type_2ou == "0x06" ]]; then
    # waive 2OU present if 2OU type is dp riser card (0x06)
    val=$((val | 0x08))
  fi

  if [ $val = 0 ]; then
    echo "2OU:" >> $LOG
    init_vishay_ic $slot $REXP_INTF
    echo "1OU:" >> $LOG
    init_vishay_ic $slot $FEXP_INTF
  elif [ $val = 4 ]; then
    echo "1OU is not present" >> $LOG
    echo "2OU:" >> $LOG
    init_vishay_ic $slot $REXP_INTF
  elif [ $val = 8 ]; then
    echo "2OU is not present" >> $LOG
    echo "1OU:" >> $LOG
    init_vishay_ic $slot $FEXP_INTF
  else
    echo "1OU and 2OU are not present" >> $LOG
  fi
}

function init_class1_sic(){
  for slot_num in $(seq 1 4); do
    slot_present=$(gpio_get PRSNT_MB_BMC_SLOT${slot_num}_BB_N)
    slot="slot${slot_num}"
    if [[ "$slot_present" == "1" ]]; then
      echo "$slot is not present" >> $LOG
    elif [[ $(is_sb_bic_ready ${slot_num}) == "0" ]]; then
      echo "$slot bic is not ready" >> $LOG
    else
      val=$(get_m2_prsnt_sts $slot)
      echo "$slot" >> $LOG
      check_class1_config $slot $val
    fi
    echo "" >> $LOG
  done
}

function check_4u_gpv3_type() {
  echo "Check 4U GPv3 type" >> $LOG
  # get the top configure
  cmd="slot1 0xe0 0x02 0x9c 0x9c 0x0 0x15 0xe0 0x02 0x9c 0x9c 0x0 0x45 0xe0 0x66 0x9c 0x9c 0x0 0x0"
  run_cmd "$cmd"
  top_cfg=$(cut -d' ' -f2 <<<"$(cat $LOG | tail -n 1)")
  echo "top_cfg result: $top_cfg" >> $LOG

  echo "" >> $LOG

  # get the bot configure
  cmd="slot1 0xe0 0x02 0x9c 0x9c 0x0 0x15 0xe0 0x02 0x9c 0x9c 0x0 0x40 0xe0 0x66 0x9c 0x9c 0x0 0x0"
  run_cmd "$cmd"
  bot_cfg=$(cut -d' ' -f2 <<<"$(cat $LOG | tail -n 1)")
  echo "bot_cfg result: $bot_cfg" >> $LOG

  if ! [ "$bot_cfg" == "$top_cfg" ]; then
    echo "BMC doesn't support multi config" >> $LOG
    echo "Use TOP_CFG ${top_cfg} by default" >> $LOG
  fi

  echo $top_cfg
}

function check_2u_gpv3_type() {
  echo "Check 2U GPv3 type" >> $LOG

  cmd="slot1 0xe0 0x02 0x9c 0x9c 0x0 0x15 0xe0 0x66 0x9c 0x9c 0x0 0x0"
  run_cmd "$cmd"

  cfg=$(cut -d' ' -f11 <<<"$(cat $LOG | tail -n 1)")
  echo -n "result: " >> $LOG
  echo $cfg | tee -a $LOG
}

function init_class2_sic(){
  local slot="slot1"
  local cfg=""

  val=$(get_m2_prsnt_sts $slot)
  echo "$slot" >> $LOG
  if [ $val = 0 ] || [ $val = 4 ]; then
    echo "2OU:" >> $LOG
    # 0h = GPv3 with BRCM
    # 1h = 2OU EXP w/o PCIe Switch
    # 2h = SPE
    # 3h = GPv3 w/ MCHP
    # 4h = CWC w/ MCHP
    # 5h = TBD
    # 6h = DPv1, class 1
    # 7h = DPv2, class 1
    EXP_BOARD_TYPE=$(get_2ou_board_type 4) #only slot1
    if [ $EXP_BOARD_TYPE == "0x01" ]; then
      init_vishay_ic $slot $REXP_INTF
    else
      # GPv3, CWC, and SPE
      echo "no need to initialize it because it's done by BIC or no VRs" >> $LOG
      # check if it's 2U or 4U GPv3
      if [ $EXP_BOARD_TYPE == "0x04" ]; then
        cfg=$(check_4u_gpv3_type)
      elif [ $EXP_BOARD_TYPE == "0x03" ]; then
        cfg=$(check_2u_gpv3_type)
      fi

      [[ ! -z "$cfg" ]] && /usr/bin/kv set "gpv3_cfg" "$cfg" create
    fi
  else
    echo "2OU is not present" >> $LOG
  fi
}

echo "" > $LOG
if [[ $1 =~ slot ]]; then
  val=$(get_m2_prsnt_sts $1)
  echo "$1" >> $LOG
  check_class1_config $1 $val
  exit
fi

echo -n "Setup VishayIC for fby3..."
bmc_location=$(get_bmc_board_id)
if [ $bmc_location -eq 9 ]; then
  #The BMC of class2
  init_class2_sic
elif [ $bmc_location -eq 14 ] ||  [ $bmc_location -eq 7 ]; then
  #The BMC of class1
  init_class1_sic
else
  echo -n "Is board id correct(id=$bmc_location)?..."
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
#
. /usr/local/fbpackages/utils/ast-functions

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

function get_sel_time_from_server {
  local BOARD_ID=$(get_bmc_board_id)
  local output=""
  local snum=0

  if [ $BOARD_ID -eq 9 ]; then
    output="$(/usr/local/bin/me-util slot1 0x28 0x48)"
    [ ${#output} == 12 ] && snum=1
  else
    for i in $(seq 1 4)
    do
      if [[ $(is_server_prsnt $i) == "1" ]] ; then
        # Use standard IPMI command 'get-sel-time' to read RTC time
        output=$(/usr/local/bin/me-util slot$i 0x28 0x48)
        if [ ${#output} == 12 ]; then
          snum=$i
          break
        fi
      fi
    done
  fi

  # return IPMI "Get sel time" command output, or return empty string if command failed
  [ ${#output} == 12 ] && echo $output || echo ""

  return $snum
}

function do_sync {
  local sel_time=$1
  date -s @$((16#$(echo "$sel_time" | awk '{print $4$3$2$1}')))
  test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
  echo 1 > /tmp/sync_date
}

function server_sync {
  output="$(/usr/bin/kv get time_sync)"
  if [ -n "${output}" ] ; then
    if [ ${#output} == 11 ] ; then
      echo Syncing up BMC time with server...
      do_sync "$output"
    else
      echo Syncing up BMC time with IPMI command failed
      logger -p user.info "Time sync with IPMI command failed"
    fi
  else
    output=$(get_sel_time_from_server)
    num=$?
    if [ -n "$output" ]; then
      echo Syncing up BMC time with server$num...
      do_sync "$output"
    else
      echo Syncing up BMC time with server failed
      logger -p user.info "Time sync with server failed"
    fi
  fi
}

function update_ntp_config_from_kv() {
  local ntp_server
  ntp_server=$(/usr/bin/kv get ntp_server persistent)
  if [ -z "$ntp_server" ]; then
    return
  fi

  sed -i "s/.*# REPLACE WITH KV.*/server $ntp_server iburst # REPLACE WITH KV/" /etc/ntp.conf
  /etc/init.d/ntpd restart
}

# Sync BMC's date with one of the four servers
function sync_date {
  local NTP_DELAY_COUNT

  update_ntp_config_from_kv

  NTP_DELAY_COUNT=0
  while [ $NTP_DELAY_COUNT -lt 30 ];
  do
    if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
      server_sync
      break
    fi
  done
}

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

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

cmd=$1
fru=$2
dev=$3
busid=
exp=
retry=10
BOARD_ID=$(get_bmc_board_id)

function show_usage() {
  if [ "$BOARD_ID" -eq 9 ]; then
    #The BMC of class2
    echo "Usage: usb-util.sh [ bind | unbind ] [ slot1-2U | slot1-2U-exp | slot1-2U-top | slot1-2U-bot ] [ pesw-uart, uart0, uart1, uart2, dev0, dev1, ..., dev11 ]"
    echo "Usage: usb-util.sh bus-id [ slot1-2U | slot1-2U-exp | slot1-2U-top | slot1-2U-bot ] [ pesw-uart, uart0, uart1, uart2, dev0, dev1, ..., dev11 ]"
  else
    #The BMC of class1
    echo "Usage: usb-util.sh [ bind | unbind ] [ slot1-2U | slot3-2U ] [ dev0, dev1, ..., dev11 ]"
    echo "Usage: usb-util.sh bus-id [ slot1-2U | slot3-2U  ] [ dev0, dev1, ..., dev11 ]"
  fi
}

function check_file_exist() {
    local found=0
    for i in $(seq 0 $retry)
    do
        ls /sys/bus/usb/devices/ | grep -x $busid
        if [ $? -eq 0 ]; then
        # if usb path exist
            found=1
            break
        fi
        sleep 2
    done

    if [ $found -eq 0 ]; then
        echo "bus id: $busid not found"
        exit -1
    fi
}

function get_2u_gpv3_bus_id() {
    if [ $exp != "0x00" ] && [ $exp != "0x03" ]; then
        echo "command not supported by this platform"
        exit -1
    fi

    case $dev in
    pesw-uart)
        busid="1-1.3.4.2.4"
    ;;

    uart0)
        busid="1-1.3.1"
    ;;

    uart1)
        busid="1-1.3.2.2"
    ;;

    uart2)
        busid="1-1.3.2.4"
    ;;

    dev0)
        busid="1-1.3.4.1"
    ;;

    dev1)
        busid="1-1.3.3.1"
    ;;

    dev2)
        busid="1-1.3.4.2.1"
    ;;

    dev3)
        busid="1-1.3.3.2.1"
    ;;

    dev4)
        busid="1-1.3.4.2.2"
    ;;

    dev5)
        busid="1-1.3.3.2.2"
    ;;

    dev6)
        busid="1-1.3.4.3"
    ;;

    dev7)
        busid="1-1.3.3.3"
    ;;

    dev8)
        busid="1-1.3.4.2.3"
    ;;

    dev9)
        busid="1-1.3.3.2.3"
    ;;

    dev10)
        busid="1-1.3.4.4"
    ;;

    dev11)
        busid="1-1.3.3.2.4"
    ;;

    *)
        echo "dev:$dev is not supported by $fru"
        exit -1
    ;;
    esac
}

function get_2u_gpv3_bus_id_by_fru() {
    if [ $exp != "0x00" ] && [ $exp != "0x03" ]; then
        echo "command not supported by this platform"
        exit -1
    fi

    if [ "$fru" = "slot1-2U" ]; then
      case $dev in
        dev0) # a
        busid="1-1.1.3.4.1"
      ;;
        dev1) # b
        busid="1-1.1.3.3.1"
      ;;
        dev2) # c
        busid="1-1.1.3.4.2.1"
      ;;
        dev3) # d
        busid="1-1.1.3.3.2.1"
      ;;
        dev4) # e
        busid="1-1.1.3.4.2.2"
      ;;
        dev5) # f
        busid="1-1.1.3.3.2.2"
      ;;
        dev6) # g
        busid="1-1.1.3.4.3"
      ;;
        dev7) # h
        busid="1-1.1.3.3.3"
      ;;
        dev8) # i
        busid="1-1.1.3.4.2.3"
      ;;
        dev9) # j
        busid="1-1.1.3.3.2.3"
      ;;
        dev10) # k
        busid="1-1.1.3.4.4"
      ;;
        dev11) # l
        busid="1-1.1.3.3.2.4"
      ;;
        *)
        echo "dev:$dev is not supported by $fru"
        exit -1
      ;;
      esac
    elif [ "$fru" = "slot3-2U" ]; then
      case $dev in
        dev0) # a
        busid="1-1.3.3.4.1"
      ;;
        dev1) # b
        busid="1-1.3.3.3.1"
      ;;
        dev2) # c
        busid="1-1.3.3.4.2.1"
      ;;
        dev3) # d
        busid="1-1.3.3.3.2.1"
      ;;
        dev4) # e
        busid="1-1.3.3.4.2.2"
      ;;
        dev5) # f
        busid="1-1.3.3.3.2.2"
      ;;
        dev6) # g
        busid="1-1.3.3.4.3"
      ;;
        dev7) # h
        busid="1-1.3.3.3.3"
      ;;
        dev8) # i
        busid="1-1.3.3.4.2.3"
      ;;
        dev9) # j
        busid="1-1.3.3.3.2.3"
      ;;
        dev10) # k
        busid="1-1.3.3.4.4"
      ;;
        dev11) # l
        busid="1-1.3.3.3.2.4"
      ;;
        *)
        echo "dev:$dev is not supported by $fru"
        exit -1
      ;;
      esac
    else
      echo "The $fru is invalid"
      exit -1;
    fi

}

function get_cwc_bus_id() {
    if [ $exp != "0x04" ]; then
        echo "command not supported by this platform"
        exit -1
    fi

    case $dev in
    pesw-uart)
        busid="1-1.3.2.2"
    ;;

    *)
        echo "dev:$dev is not supported by $fru"
        exit -1
    ;;
    esac
}

function get_top_gpv3_bus_id() {
    if [ $exp != "0x04" ]; then
        echo "command not supported by this platform"
        exit -1
    fi

    case $dev in
    pesw-uart)
        busid="1-1.3.1.4.2.4"
    ;;

    uart0)
        busid="1-1.3.1.1"
    ;;

    uart1)
        busid="1-1.3.1.2.2"
    ;;

    uart2)
        busid="1-1.3.1.2.4"
    ;;

    dev0)
        busid="1-1.3.1.4.1"
    ;;

    dev1)
        busid="1-1.3.1.3.1"
    ;;

    dev2)
        busid="1-1.3.1.4.2.1"
    ;;

    dev3)
        busid="1-1.3.1.3.2.1"
    ;;

    dev4)
        busid="1-1.3.1.4.2.2"
    ;;

    dev5)
        busid="1-1.3.1.3.2.2"
    ;;

    dev6)
        busid="1-1.3.1.4.3"
    ;;

    dev7)
        busid="1-1.3.1.3.3"
    ;;

    dev8)
        busid="1-1.3.1.4.2.3"
    ;;

    dev9)
        busid="1-1.3.1.3.2.3"
    ;;

    dev10)
        busid="1-1.3.1.4.4"
    ;;

    dev11)
        busid="1-1.3.1.3.2.4"
    ;;

    *)
        echo "dev:$dev is not supported by $fru"
        exit -1
    ;;
    esac
}

function get_bot_gpv3_bus_id() {
    if [ $exp != "0x04" ]; then
        echo "command not supported by this platform"
        exit -1
    fi

    case $dev in
    pesw-uart)
        busid="1-1.3.4.4.2.4"
    ;;

    uart0)
        busid="1-1.3.4.1"
    ;;

    uart1)
        busid="1-1.3.4.2.2"
    ;;

    uart2)
        busid="1-1.3.4.2.4"
    ;;

    dev0)
        busid="1-1.3.4.4.1"
    ;;

    dev1)
        busid="1-1.3.4.3.1"
    ;;

    dev2)
        busid="1-1.3.4.4.2.1"
    ;;

    dev3)
        busid="1-1.3.4.3.2.1"
    ;;

    dev4)
        busid="1-1.3.4.4.2.2"
    ;;

    dev5)
        busid="1-1.3.4.3.2.2"
    ;;

    dev6)
        busid="1-1.3.4.4.3"
    ;;

    dev7)
        busid="1-1.3.4.3.3"
    ;;

    dev8)
        busid="1-1.3.4.4.2.3"
    ;;

    dev9)
        busid="1-1.3.4.3.2.3"
    ;;

    dev10)
        busid="1-1.3.4.4.4"
    ;;

    dev11)
        busid="1-1.3.4.3.2.4"
    ;;

    *)
        echo "dev:$dev is not supported by $fru"
        exit -1
    ;;
    esac
}

function bind_usb() {
    if [ -z $fru ] || [ -z $dev ]; then
        show_usage
        exit -1
    fi

    if [ "$fru" = "slot3-2U" ]; then
        exp=$(get_2ou_board_type 6)
    else
        exp=$(get_2ou_board_type 4)
    fi

    case $fru in
    slot1-2U)
        if [ "$BOARD_ID" -eq 9 ]; then
            get_2u_gpv3_bus_id
        else
            get_2u_gpv3_bus_id_by_fru
        fi
    ;;

    slot1-2U-exp)
        get_cwc_bus_id
    ;;

    slot1-2U-top)
        get_top_gpv3_bus_id
    ;;

    slot1-2U-bot)
        get_bot_gpv3_bus_id
    ;;

    slot3-2U)
        get_2u_gpv3_bus_id_by_fru
    ;;

    *)
        show_usage
        exit -1
    ;;
    esac

    if [ $cmd == "unbind" ] ; then
        usbip unbind --busid $busid
    else
        if [ "$fru" = "slot3-2U" ]; then
            hub=$(/usr/bin/bic-util slot3 --get_gpio | grep 16 | awk '{print $3;}') # get gpio value
        else
            hub=$(/usr/bin/bic-util slot1 --get_gpio | grep 16 | awk '{print $3;}') # get gpio value
        fi

        if [ $hub -eq 0 ]; then
            if [ "$fru" = "slot3-2U" ]; then
                /usr/bin/bic-util slot3 --set_gpio 16 1 > /dev/null 2>&1
            else
                /usr/bin/bic-util slot1 --set_gpio 16 1 > /dev/null 2>&1
            fi
            retry=15
            echo "Initializing USB devs..."
            sleep 10
        fi

        check_file_exist

        modprobe usbip-core
        modprobe usbip-host
        usbipd -D
        sleep 1
        usbip bind --busid $busid
    fi
}

function get_bus_id() {
    if [ "$fru" = "slot3-2U" ]; then
        exp=$(get_2ou_board_type 6)
    else
        exp=$(get_2ou_board_type 4)
    fi

    case $fru in
    slot1-2U)
        if [ "$BOARD_ID" -eq 9 ]; then
            get_2u_gpv3_bus_id
        else
            get_2u_gpv3_bus_id_by_fru
        fi
    ;;

    slot1-2U-exp)
        get_cwc_bus_id
    ;;

    slot1-2U-top)
        get_top_gpv3_bus_id
    ;;

    slot1-2U-bot)
        get_bot_gpv3_bus_id
    ;;

    slot3-2U)
        get_2u_gpv3_bus_id_by_fru
    ;;

    *)
        show_usage
        exit -1
    ;;
    esac

    echo "$fru $dev usb bus id is $busid"
}

case $cmd in
    bind)
        bind_usb
    ;;

    unbind)
        bind_usb
    ;;

    bus-id)
        get_bus_id
    ;;

    *)
        show_usage
        exit -1
    ;;
esac


#!/bin/sh
. /usr/local/fbpackages/utils/ast-functions
echo -n "Setup sensor monitoring for FBY3... "
SLOTS=

function get_bus_num() {
  local bus=0
  case $1 in
    slot1)
      bus=4
    ;;
    slot2)
      bus=5
    ;;
    slot3)
      bus=6
    ;;
    slot4)
      bus=7
    ;;
  esac

  echo $bus
}

read_system_conf() {
  slot_num=${1#"slot"}
  val=$(get_m2_prsnt_sts "$1")
  i2c_bus=$(get_bus_num $1)

  system_type="Type_"
  #0 = 2ou and 1ou are present
  #4 = 2ou is present
  #8 = 1ou is present
  #other = not present
  if [ "$val" -eq 0 ]; then
    exp_board=$(get_2ou_board_type $i2c_bus)

    if [ "$exp_board" == "0x04" ] || [ "$exp_board" == "0x00" ] || [ "$exp_board" == "0x03" ]; then
      system_type=${system_type}17
    else
      system_type=${system_type}10
    fi
  elif [ "$val" -eq 4 ]; then
    cpld_bus=$(get_cpld_bus "$slot_num")
    type_2ou=$(get_2ou_board_type "$cpld_bus")
    if [ "$type_2ou" = "0x06" ]; then
      # DP slot
      dp_type=$(/usr/bin/kv get "sled_system_conf" persistent)
      if [ "$dp_type" = "Type_DPB" ]; then
        system_type=${system_type}DPB
      elif [ "$dp_type" = "Type_DPF" ]; then
        system_type=${system_type}DPF
      else
        system_type=${system_type}DP
      fi
    elif [ "$type_2ou" == "0x03" ] ; then # Config D GPv3
        system_type=${system_type}15
    else
      system_type=${system_type}1
    fi
  elif [ "$val" -eq 8 ]; then
    system_type=${system_type}10
  else
    system_type=${system_type}1
  fi

  /usr/bin/kv set "${1}_system_conf" "$system_type" persistent
}

init_class2_sensord() {
  SLOTS="slot1 bmc nic"
  read_system_conf "slot1"

  board=$(get_2ou_board_type 4)
  if [ "$board" = "0x04" ]; then
    SLOTS="$SLOTS slot1-2U-exp slot1-2U-top slot1-2U-bot"
  fi
}

init_class1_sensord() {
  # Check for the slots present and run sensord for those slots only.
  if [ "$(is_server_prsnt 1)" = "1" ]; then
    if [ "$(is_slot_12v_on 1)" = "1" ]; then
      SLOTS="$SLOTS slot1"
      read_system_conf "slot1"
    fi
  fi

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

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

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

  SLOTS="$SLOTS bmc nic"
}

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

exec /usr/local/bin/sensord $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

# Call "fw-util slotX --version" once to store vr information before sensor monitoring
echo "Get MB FW version... "

bmc_location=$(get_bmc_board_id)
if ! [ $bmc_location -eq 9 ]; then
  if [[ $(is_sb_bic_ready 1) == "1" ]]; then
    /usr/bin/fw-util slot1 --version > /dev/null
  fi

  if [[ $(is_sb_bic_ready 2) == "1" ]]; then
    /usr/bin/fw-util slot2 --version > /dev/null
  fi

  if [[ $(is_sb_bic_ready 3) == "1" ]]; then
    /usr/bin/fw-util slot3 --version > /dev/null
  fi

  if [[ $(is_sb_bic_ready 4) == "1" ]]; then
    /usr/bin/fw-util slot4 --version > /dev/null
  fi

  # Clear PEAK_PIN & PEAK_IOUT register
  /usr/sbin/i2cset -y 11 0x40 0xd0 0x0000 w
  /usr/sbin/i2cset -y 11 0x40 0xda 0x0000 w
else
  /usr/bin/fw-util slot1 --version > /dev/null
fi

echo -n "Setup sensor monitoring for FBYV3... "
runsv /etc/sv/sensord > /dev/null 2>&1 &

echo "done."


#! /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
#

### 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

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

init_class1_ipmb() {
  for slot_num in {1..4..2}; 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
      usleep 50000
      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 &
}

echo -n "Starting IPMB Rx/Tx Daemon.."
sleep 10
ulimit -q 1024000

init_class1_ipmb

echo "Done."


#!/bin/bash

dump_sensor_history()
{
  l_SLOT=$1
  l_SENSOR_HISTORY=180

  /usr/local/bin/sensor-util "$l_SLOT" --history $l_SENSOR_HISTORY \
   && /usr/local/bin/sensor-util bmc --history $l_SENSOR_HISTORY \
   && /usr/local/bin/sensor-util nic --history $l_SENSOR_HISTORY
}

dump_sensor_threshold()
{
  l_SLOT=$1

  /usr/local/bin/sensor-util "$l_SLOT" --threshold \
   && /usr/local/bin/sensor-util bmc --threshold \
   && /usr/local/bin/sensor-util nic --threshold
}

SLOT_NAME=$1

case $SLOT_NAME in
  slot1)
    SLOT_NUM=1
    ;;
  slot2)
    SLOT_NUM=2
    ;;
  slot3)
    SLOT_NUM=3
    ;;
  slot4)
    SLOT_NUM=4
    ;;
  *)
    N=${0##*/}
    echo "Usage: $N {slot1|slot2|slot3|slot4}"
    exit 1
    ;;
esac

# File format autodump<slot_id>.pid (See pal_is_crashdump_ongoing()
# function definition)
PID_FILE="/var/run/autodump${SLOT_NUM}.pid"
DUMP_DIR='/var/lib/amd-ras'
CURRENT_INDEX_FILE="$DUMP_DIR/fru${SLOT_NUM}_current_index"
INFODUMP_FILE="$DUMP_DIR/fru${SLOT_NUM}_crashdump_info"
DUMP_UTIL='/usr/bin/amd-ras'
FRUID_UTIL="/usr/local/bin/fruid-util"
FW_UTIL="/usr/bin/fw-util"

# check crashdump binary file exists
if [ ! -f $DUMP_UTIL ]; then
  logger -t "ipmid" -p daemon.crit "AMD ADDC crashdump not supported"
  exit 1
fi

# check if auto crashdump is already running
if [ -f $PID_FILE ]; then
  logger -t "ipmid" -p daemon.warning "Another crashdump for $SLOT_NAME is runnung"
  exit 1
else
  touch $PID_FILE
fi

# Set crashdump timestamp
# this timestamp is used by pal_is_crashdump_ongoing() and it can
# prevent the power control when crashdump is ongoing
sys_runtime=$(awk '{print $1}' /proc/uptime)
sys_runtime=$(printf "%0.f" "$sys_runtime")
kv set "fru${SLOT_NUM}_crashdump" "$((sys_runtime+600))"

# check number of crashdump file exists
[ -r $CURRENT_INDEX_FILE ] && PREVIOUS_INDEX=$(cat $CURRENT_INDEX_FILE) || PREVIOUS_INDEX=0

CURT_DTIME=$(date +"%Y%m%d-%H%M%S")
DUMP_FILE="$DUMP_DIR/fru${SLOT_NUM}_ras-error${PREVIOUS_INDEX}.cper"
LOG_ARCHIVE="/mnt/data/fru${SLOT_NUM}_ras-error${PREVIOUS_INDEX}_${CURT_DTIME}.tar.gz"

logger -t "ipmid" -p daemon.crit "Crashdump for FRU: $SLOT_NUM started"

#HEADER LINE for the dump
echo "Crash Dump generated at $(date)" > "$INFODUMP_FILE"
{
  # Get BMC version & hostname
  echo "Get BMC version info: "
  "$FW_UTIL" bmc --version
  uname -a
  cat /etc/issue

  # Get fw info

  echo "Get firmware version info: "
  "$FW_UTIL" "$SLOT_NAME" "--version"

  # Get FRUID info
  echo "Get FRUID Info:"
  "$FRUID_UTIL" "$SLOT_NAME"

  # Sensors & sensor thresholds
  echo "Sensor history at dump:"
  dump_sensor_history "$SLOT_NAME"
  echo "Sensor threshold at dump:"
  dump_sensor_threshold "$SLOT_NAME"
} >> "$INFODUMP_FILE"

RAS_STATUS=$2
NUM_OF_PROC=$3
TARGET_CPU=$4
CPUID_EAX=$5
CPUID_EBX=$6
CPUID_ECX=$7
CPUID_EDX=$8

$DUMP_UTIL --fru "$SLOT_NUM" --ras "$RAS_STATUS" --ncpu "$NUM_OF_PROC" --tcpu "$TARGET_CPU"  --bid 0 --cid "$CPUID_EAX" "$CPUID_EBX" "$CPUID_ECX" "$CPUID_EDX"

[ -r $CURRENT_INDEX_FILE ] && CURRENT_INDEX=$(cat $CURRENT_INDEX_FILE) || CURRENT_INDEX=0
if [ "$PREVIOUS_INDEX" == "$CURRENT_INDEX" ]; then
  DUMP_FILE=
fi

IFS=" " read -r -a LOGS <<< "$(basename "$INFODUMP_FILE") $(basename "$DUMP_FILE")"
tar zcf "$LOG_ARCHIVE" -C "$DUMP_DIR" "${LOGS[@]}" && rm -rf "$DUMP_FILE"
if [ -f "$LOG_ARCHIVE" ]; then
  logger -t "ipmid" -p daemon.crit "Crashdump for FRU: $SLOT_NUM is generated at $LOG_ARCHIVE"
fi

# Remove current pid file
rm $PID_FILE
exit 0


#! /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
#

### 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

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

init_class1_ipmb() {
  for slot_num in {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 &
    if [ "$slot_num" -eq 1 ] || [ "$slot_num" -eq 3 ]; then
      slot_present=$(gpio_get PRSNT_MB_BMC_SLOT"${slot_num}"_BB_N)
    else
      slot_present=$(gpio_get PRSNT_MB_SLOT"${slot_num}"_BB_N)
    fi
    if [ "$slot_present" = "1" ]; then
      usleep 50000
      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 &
}

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

echo -n "Starting IPMB Rx/Tx Daemon.."
sleep 10
ulimit -q 1024000

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_ipmb
elif [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_ipmb
else
  echo -n "Is board id correct(id=$bmc_location)?..."
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
#

# shellcheck disable=SC1091,SC2039
. /usr/local/fbpackages/utils/ast-functions

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

echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-8/new_device

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

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

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO

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

# shellcheck disable=SC1091
. /usr/local/fbpackages/utils/ast-functions

DEF_PWR_ON=1
TO_PWR_ON=
DEBUG_ENABLE=false

check_por_config() {
  TO_PWR_ON=0

  if POR=$(kv get "slot${1}_por_cfg" persistent); then
    if [ "$POR" = "lps" ]; then
      if LS=$(kv get "pwr_server${1}_last_state" persistent); then
        if [ "$LS" = "on" ]; then
          TO_PWR_ON=1;
        fi
      else
        TO_PWR_ON=$DEF_PWR_ON
      fi
    elif [ "$POR" = "on" ]; then
      TO_PWR_ON=1;
    fi
  else
    TO_PWR_ON=$DEF_PWR_ON
  fi
}

update_ntp_config_from_kv() {
  local ntp_server
  ntp_server=$(/usr/bin/kv get ntp_server persistent)
  if [ -z "$ntp_server" ]; then
    return
  fi

  sed -i "s/.*# REPLACE WITH KV.*/server $ntp_server iburst # REPLACE WITH KV/" /etc/ntp.conf
  /etc/init.d/ntpd restart
}

is_ntp_time_synced() {
  local str
  local ret

  str=$(/usr/sbin/ntpq -np | grep '^\*')
  ret=$?

  if [ "$DEBUG_ENABLE" = true ] ; then
    echo "=================================="
    echo "ret: $ret"
    echo "----------------------------------"
    echo "$str"
    echo "=================================="
  fi

  # ret == 0, synced
  # ret == 1, not synced
  return $ret
}

ntp_sync_delay() {
    local NTP_DELAY_COUNT

    NTP_DELAY_COUNT=0
    echo "[$(/bin/date)] ntp is enabled, ntp_server: $(/usr/bin/kv get ntp_server persistent)"
    while [ $NTP_DELAY_COUNT -lt 30 ];
    do
      if is_ntp_time_synced ; then
        break
      fi
      ((NTP_DELAY_COUNT++))
      [ "$DEBUG_ENABLE" = true ] && echo "[$(/bin/date)] system time not synced, count: ${NTP_DELAY_COUNT}"
      sleep 1
    done
    if is_ntp_time_synced ; then
      echo "[$(/bin/date)] system time synced after $NTP_DELAY_COUNT sec"
    else
      echo "[$(/bin/date)] system time not synced after $NTP_DELAY_COUNT sec"
    fi
}

# 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 70)" 13

  # Wait for NTP sync
  update_ntp_config_from_kv
  ntp_sync_delay

  # check POR config for each slot
  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 3
  if [ "$(is_sb_bic_ready 3)" = "1" ] && [ $TO_PWR_ON -eq 1 ]; then
    power-util slot3 on
  fi

fi


#!/bin/bash

exp_addr=0x44

if [ $exp_addr = 0x44 ]; then
  addr=0x40
  exp_frs=(0x00 0x00 0x11 0x00 0x00 0x00 0x00 0x00)
else
  addr=0x44
  exp_frs=(0x00 0x00 0x10 0x00 0x00 0x00 0x00 0x00)
fi
exp_idcode="0x61 0x2b 0xc0 0x43"
exp_fbs=(0x02 0x20)
retcode=0

slot=$1

check_busy() {
  dev=$1

  for _ in {1..30}; do
    usleep 100000
    busy=$(i2ctransfer -y "$bus" w4@"$dev" 0xf0 0x00 0x00 0x00 r1 2>/dev/null)
    if [[ "$busy" != "" && $((busy & 0x80)) = 0 ]]; then
      break
    fi
  done
  if [[ "$busy" == "" || $((busy & 0x80)) != 0 ]]; then
    printf "Failed\n"
    exit 1
  fi
}

case $slot in
  slot1)
    bus=4
    ;;
  slot2)
    bus=5
    ;;
  slot3)
    bus=6
    ;;
  slot4)
    bus=7
    ;;
  *)
    N=${0##*/}
    echo "Usage: $N {slot1|slot2|slot3|slot4}"
    exit 1
    ;;
esac

# Check if already programmed
idcode=$(i2ctransfer -y $bus w4@$exp_addr 0xe0 0x00 0x00 0x00 r4 2>/dev/null)
if [ "$idcode" = "$exp_idcode" ]; then
  addr=$exp_addr
  i2ctransfer -y $bus w3@$addr 0x74 0x08 0x00 >/dev/null 2>&1
  frs=$(i2ctransfer -y $bus w4@$addr 0xe7 0x00 0x00 0x00 r8 2>/dev/null)
  fbs=$(i2ctransfer -y $bus w4@$addr 0xfb 0x00 0x00 0x00 r2 2>/dev/null)
  i2ctransfer -y $bus w3@$addr 0x26 0x00 0x00 >/dev/null 2>&1
  if [[ "$frs" = "${exp_frs[*]}" && "$fbs" = "${exp_fbs[*]}" ]]; then
    echo "Already programmed!"
    echo "Feature Rows: $frs"
    echo "FEABITS: $fbs"
    exit 0
  fi
fi

# Check the IDCODE
idcode=$(i2ctransfer -y $bus w4@$addr 0xe0 0x00 0x00 0x00 r4)
echo "IDCODE: $idcode"
if [ "$idcode" != "$exp_idcode" ]; then
  echo "Unexpected IDCODE!"
  exit 1
fi

# Check the STATUS
read -r -a sts <<< "$(i2ctransfer -y $bus w4@$addr 0x3c 0x00 0x00 0x00 r4)"
echo "Status Register: ${sts[*]}"
if [[ $((sts[2] & 0x70)) != 0 || $((sts[3] & 0x20)) != 0 ]]; then
  echo "Unexpected Status!"
  exit 1
fi

printf "Enable XPROGRAM mode..."
if ! i2ctransfer -y $bus w3@$addr 0x74 0x08 0x00 >/dev/null; then
  printf "Failed\n"
  exit 1
fi
printf "OK\n"

frs=$(i2ctransfer -y $bus w4@$addr 0xe7 0x00 0x00 0x00 r8)
echo "Feature Rows before programming: $frs"

fbs=$(i2ctransfer -y $bus w4@$addr 0xfb 0x00 0x00 0x00 r2)
echo "FEABITS before programming: $fbs"

printf "Erase the Feature Rows..."
i2ctransfer -y $bus w4@$addr 0x0e 0x02 0x00 0x00 >/dev/null
addr=0x40  # address become to default
check_busy $addr
printf "OK\n"

printf "Program Feature Rows..."
if ! i2ctransfer -y $bus w4@$addr 0x46 0x02 0x00 0x00 >/dev/null; then
  printf "Failed\n"
  exit 1
fi

i2ctransfer -y $bus w12@$addr 0xe4 0x00 0x00 0x00 "${exp_frs[@]}" >/dev/null
addr=$exp_addr
check_busy $addr
frs=$(i2ctransfer -y $bus w4@$addr 0xe7 0x00 0x00 0x00 r8)

i2ctransfer -y $bus w6@$addr 0xf8 0x00 0x00 0x00 "${exp_fbs[@]}" >/dev/null
check_busy $addr
fbs=$(i2ctransfer -y $bus w4@$addr 0xfb 0x00 0x00 0x00 r2)

if [[ "$frs" = "${exp_frs[*]}" && "$fbs" = "${exp_fbs[*]}" ]]; then
  printf "OK\n"
else
  printf "Failed\n"
  retcode=1
fi
echo "Feature Rows after programming: $frs"
echo "FEABITS after programming: $fbs"

printf "Exit the programming mode..."
if ! i2ctransfer -y $bus w3@$addr 0x26 0x00 0x00 >/dev/null; then
  printf "Failed\n"
  exit 1
fi
printf "OK\n"
exit $retcode


#!/bin/bash
#
# 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

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

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

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 "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_sdr
elif [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_sdr
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi

echo "done."


#!/bin/bash

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

BOARD_ID=$(get_bmc_board_id)
SLOT_NAME=$1

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

if [ "$BOARD_ID" = "$BMC_ID_CLASS2" ] && [ "$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"
DUMP_DIR='/tmp/crashdump/output'
PARSE_FILE="$DUMP_DIR/bafi_${SLOT_NAME}.log"
DUMP_FILE="$DUMP_DIR/crashdump_${SLOT_NAME}.log"
DUMP_UTIL='/usr/bin/crashdump'
PARSE_UTIL='/usr/bin/bafi'
LOG_ARCHIVE="/mnt/data/crashdump_${SLOT_NAME}.tar.gz"
ACD=true

if [ ! -f $DUMP_UTIL ]; then
  ACD=false
  DUMP_UTIL="/usr/bin/dump.sh $SLOT_NAME"
  mkdir -p $DUMP_DIR
fi

# 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 [ -n "$OLDPID" ] && (grep "autodump" /proc/$OLDPID/cmdline &> /dev/null); then
  echo "kill pid $OLDPID..."
  kill -s 9 $OLDPID
  if [ "$ACD" = true ]; then
    pkill -KILL -f "$DUMP_UTIL $SLOT_NUM --type IERR" >/dev/null 2>&1
    pkill -KILL -f "$PARSE_UTIL .*${SLOT_NAME}" >/dev/null 2>&1
  else
    pkill -KILL -f "$DUMP_UTIL" >/dev/null 2>&1
    pkill -KILL -f "me-util $SLOT_NAME" >/dev/null 2>&1
    pkill -KILL -f "bic-util $SLOT_NAME" >/dev/null 2>&1

    { echo -n "(uncompleted) Auto Dump End at "; date; } >> "$DUMP_FILE"
    tar zcf "/mnt/data/crashdump_uncompleted_${SLOT_NAME}.tar.gz" -C $DUMP_DIR "$(basename "$DUMP_FILE")"
  fi
fi
unset OLDPID

# Set crashdump timestamp
# this timestamp is used by pal_is_crashdump_ongoing() and it can
# prevent the power control when crashdump is ongoing
sys_runtime=$(awk '{print $1}' /proc/uptime)
sys_runtime=$(printf "%0.f" "$sys_runtime")
kv set "fru${SLOT_NUM}_crashdump" "$((sys_runtime+600))"

DELAY_SEC=30

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

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

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

LOGS=("$(basename "$DUMP_FILE")")
if [ "$ACD" = true ]; then
  $DUMP_UTIL $SLOT_NUM --type IERR

  # shellcheck disable=SC2012
  LOG_FILE=$(ls $DUMP_DIR/crashdump_"${SLOT_NAME}"* -t1 |head -n 1)
  $PARSE_UTIL "$LOG_FILE" > "$PARSE_FILE" 2>&1

  LOGS=("$(basename "$LOG_FILE")" "$(basename "$PARSE_FILE")")
else
  { echo -n "Auto Dump Start at "; date; } > "$DUMP_FILE"
  { $DUMP_UTIL coreid; $DUMP_UTIL msr; $DUMP_UTIL pcie; } >> "$DUMP_FILE" 2>&1
  { echo -n "Auto Dump End at "; date; } >> "$DUMP_FILE"
fi

tar zcf "$LOG_ARCHIVE" -C $DUMP_DIR "${LOGS[@]}"
logger -t "ipmid" -p daemon.crit "Crashdump for FRU: $SLOT_NUM is generated at $LOG_ARCHIVE"

# Remove current pid file
rm $PID_FILE

echo "Auto Dump for $SLOT_NAME Stored in $LOG_ARCHIVE"
exit 0


#!/bin/bash

ME_UTIL="/usr/local/bin/me-util"
PECI_UTIL="/usr/bin/bic-util"
CMD_DIR="/usr/bin"
INTERFACE="PECI_INTERFACE"
SENSOR_UTIL="/usr/local/bin/sensor-util"
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' | \
    $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 0x15 0xa0 0x00 0x/g' | \
    $PECI_UTIL "$SLOT" --file /dev/stdin | sed 's/^15 A0 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] (unit: bits)
# OUT: PCI config address
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=4
  BUS=$(printf "0x%02x" "$1")
  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
    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
      print_bus_device $BUS | execute_cmd
    done
  fi
}

function pcie_dump {
  # CPU and PCH
  [ -r $CMD_DIR/crashdump_pcie ] && execute_cmd < $CMD_DIR/crashdump_pcie

  # 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 print_dump_help {
  N=${0##*/}
  echo "Usage:"
  echo "$N <slot#> coreid <INTERFACE> ==> for CPU CoreID"
  echo "$N <slot#> msr    <INTERFACE> ==> for CPU MSR"
  echo "$N <slot#> pcie   <INTERFACE> ==> for PCIe"
  echo "$N <slot#> sensors            ==> for sensor history"
  echo "$N <slot#> threshold          ==> for sensor threshold"
  exit 1
}

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

if [ "$#" -eq 2 ] || [ "$#" -eq 3 ]; then
  if [ "$#" -eq 3 ]; then
    if [ "$3" != "ME_INTERFACE" ] && [ "$3" != "PECI_INTERFACE" ]; then
      print_dump_help
    fi
    INTERFACE="$3"
  fi

  case "$2" in
    coreid)
      [ -r $CMD_DIR/crashdump_coreid ] && execute_cmd < $CMD_DIR/crashdump_coreid
      ;;
    msr)
      [ -r $CMD_DIR/crashdump_msr ] && execute_cmd < $CMD_DIR/crashdump_msr
      ;;
    pcie)
      pcie_dump
      ;;
    sensors)
      "$SENSOR_UTIL" "$SLOT" --history "$SENSOR_HISTORY" && \
      "$SENSOR_UTIL" bmc --history "$SENSOR_HISTORY" && \
      "$SENSOR_UTIL" nic --history "$SENSOR_HISTORY"
      ;;
    threshold)
      "$SENSOR_UTIL" "$SLOT" --threshold && \
      "$SENSOR_UTIL" bmc --threshold && \
      "$SENSOR_UTIL" nic --threshold
      ;;
    *)
      print_dump_help
      ;;
  esac
else
  print_dump_help
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/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
#

### 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

# shellcheck disable=SC1091,SC2039
. /usr/local/fbpackages/utils/ast-functions

TYPE_DUAL_FAN=0
TYPE_SINGEL_FAN=1
GET_FAN_RPM="/usr/bin/bic-util slot1 0xe0 0x2 ""$IANA_ID"" 0x10 0xe0 0x52 ""$IANA_ID" # for config D
default_fsc_config_path="/etc/fsc-config.json"
sys_config=$(/usr/local/bin/show_sys_config | grep -i "config:" | awk -F ": " '{print $3}')
bmc_location=$(get_bmc_board_id)

#For config A B C, we get the Baseboard revision from BB CPLD through i2cget.
CLASS1_GET_BOARD_REVID="/usr/sbin/i2cget -y 12 0x0f 0x08"
#For config D, we get the Baseboard revision from BB BPLD. command: bic-util slot1 IPMI Send request message to BIC, IANA, BB bic, IPMI Master write read, CPLD i2cbus addr register
CLASS2_GET_BOARD_REVID="/usr/bin/bic-util slot1 0xe0 0x2 ""$IANA_ID"" 0x10 0x18 0x52 0x1 0x1e 0x1 0x8 "

check_dvt_fan() {
  local fan_type=$1
  if [ "$fan_type" = "$TYPE_DUAL_FAN" ]; then
    logger -t "fan_check" -p daemon.crit "Fan slot $i is dual fan, please install single fan in DVT system"
  fi
}

check_evt_fan() {
  local fan_type=$1
  if [ "$fan_type" = "$TYPE_DUAL_FAN" ]; then
    if [ "$sys_config" = "A" ]; then
      logger -t "fan_check" -p daemon.crit "Fan slot $i is dual fan, please install single fan on 1U system"
    fi
  else
    if [[ "$sys_config" = "B" || "$sys_config" = "D" ]]; then
      logger -t "fan_check" -p daemon.crit "Fan slot $i is single fan, please install dual fan on 2U system"
    fi
  fi
}

check_fan_type() {
  if [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
    # check CC of from BIC, if there is single fan TACH 1, 3, 5, 7 will not turn CC "00"
    if [[ "$($GET_FAN_RPM 1 | awk '{print $7;}')" = "00" && "$($GET_FAN_RPM 3 | awk '{print $7;}')" = "00" ]]; then
      fan0_type=$TYPE_DUAL_FAN
    else
      fan0_type=$TYPE_SINGEL_FAN
    fi
    if [[ "$($GET_FAN_RPM 5 | awk '{print $7;}')" = "00" && "$($GET_FAN_RPM 7 | awk '{print $7;}')" = "00" ]]; then
      fan1_type=$TYPE_DUAL_FAN
    else
      fan1_type=$TYPE_SINGEL_FAN
    fi
  else
    if [[ "$(cat /sys/class/hwmon/hwmon*/fan2_input)" = "0" && "$(cat /sys/class/hwmon/hwmon*/fan4_input)" = "0" ]]; then
      fan0_type=$TYPE_SINGEL_FAN
    else
      fan0_type=$TYPE_DUAL_FAN
    fi
    if [[ "$(cat /sys/class/hwmon/hwmon*/fan6_input)" = "0" && "$(cat /sys/class/hwmon/hwmon*/fan8_input)" = "0" ]]; then
      fan1_type=$TYPE_SINGEL_FAN
    else
      fan1_type=$TYPE_DUAL_FAN
    fi
  fi
  fan_type=("$fan0_type" "$fan1_type")
  for ((i=0; i < ${#fan_type[@]}; i++))
  do
    if [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
      BOARD_REVID=$($CLASS1_GET_BOARD_REVID)
      if [ "$((0x${BOARD_REVID#*x}))" -ge "$DVT_REVID" ]; then # for revision id >= 5 means the stage is DVT or after DVT
        check_dvt_fan "${fan_type[$i]}"
      else
        check_evt_fan "${fan_type[$i]}"
      fi
    elif [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
      BOARD_REVID=$($CLASS2_GET_BOARD_REVID | awk '{print $8;}')
      if [ "$((0x${BOARD_REVID#*x}))" -ge "$DVT_REVID" ]; then
        check_dvt_fan "${fan_type[$i]}"
      else
        check_evt_fan "${fan_type[$i]}"
      fi
    else
      logger -t "fan_check" -p daemon.crit "Cannot get the location of BMC"
    fi
  done
}

init_class1_fsc() {
  target_fsc_config=""
  config_type=""
  server_type=$(get_server_type 1)
  if [ "$sys_config" = "A" ]; then
    if [[ $server_type -eq 4 ]]; then
      config_type="GL"
      target_fsc_config="/etc/FSC_CLASS1_GL_config.json"
    else
      config_type="1"
      target_fsc_config="/etc/FSC_CLASS1_type1_config.json"
    fi
  elif [ "$sys_config" = "B" ]; then
    if [[ $server_type -eq 2 ]]; then
      board_type_1ou=$(get_1ou_board_type slot1)
      case $board_type_1ou in
      8)
        config_type="8"
        target_fsc_config="/etc/FSC_CLASS1_type8_config.json"
        ;;
      2)
        config_type="HD"
        target_fsc_config="/etc/FSC_CLASS1_HD_RBF_config.json"
        ;;
      *)
        config_type="HD"
        target_fsc_config="/etc/FSC_CLASS1_HD_config.json"
        ;;
      esac
    else
      config_type="DPV2"
      target_fsc_config="/etc/FSC_CLASS1_DPV2_config.json"
    fi
  elif [ "$sys_config" = "C" ]; then
    if [[ $server_type -eq 4 ]]; then
      config_type="GL"
      target_fsc_config="/etc/FSC_CLASS1_GL_config.json"
    else
      board_type_1ou=$(get_1ou_board_type slot1)
      case $board_type_1ou in
        4)
          config_type="VF"
          target_fsc_config="/etc/FSC_CLASS1_type3_10_config.json"
          ;;
        *)
          logger -t "fan_check" -p daemon.crit "Fail to get server type, run with the default fan config"
          config_type="1"
          target_fsc_config="/etc/FSC_CLASS1_type1_config.json"
          ;;
      esac
    fi
  else
    config_type="1"
    target_fsc_config="/etc/FSC_CLASS1_type1_config.json"
  fi

  ### Greatlakes does not have formal fan table, run default fan table(fix 70% PWM)
    # Todo: After get the Greatlakes formal fan table, need to Check the server type
  if [[ $server_type -eq 4 ]]; then
    config_type="GL"
    target_fsc_config="/etc/FSC_CLASS1_GL_config.json"
  fi

  ln -s ${target_fsc_config} ${default_fsc_config_path}
  $KV_CMD set sled_system_conf "Type_${config_type}" persistent
}

init_class2_fsc() {
  ln -s /etc/FSC_CLASS2_config.json ${default_fsc_config_path}
  $KV_CMD set sled_system_conf "Type_17" persistent
}

start_sled_fsc() {
  check_fan_type

  echo "Setup fscd for fby35..."
  #Wait for sensord ready
  for _ in {1..5}; do
    sleep 3
    if [ "$($KV_CMD get bmc_sensor231)" != "" ]; then
      break
    fi
  done

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

  #boost fans if one of the blades is not present
  reload_sled_fsc
}

reload_sled_fsc() {
  if [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
    #The BMC of class2 need to check the present status from BB BIC
    slot1_prsnt=$(bic-util slot1 0xe0 0x2 "$IANA_ID" 0x10 0xe0 0x41 "$IANA_ID" 0x0 15 | awk '{print $12}')
    slot3_prsnt=$(bic-util slot1 0xe0 0x2 "$IANA_ID" 0x10 0xe0 0x41 "$IANA_ID" 0x0 3 | awk '{print $12}')
    if [ "$slot1_prsnt" = "01" ] || [ "$slot3_prsnt" = "01" ]; then
      run_fscd=false
    else
      run_fscd=true
    fi
  else
    cnt=$(get_all_server_prsnt)

    #Check number of slots
    sys_config="$($KV_CMD get sled_system_conf persistent)"
    if [[ "$sys_config" =~ ^(Type_(1|10|VF))$ && "$cnt" -eq 4 ]]; then
      run_fscd=true
    elif [[ "$sys_config" =~ ^(Type_(DPV2|HD|GL))$ && "$cnt" -eq 2 ]]; then
      run_fscd=true
    elif [[ "$sys_config" == "Type_8" && "$cnt" -eq 1 ]]; then
      run_fscd=true
    else
      run_fscd=false
    fi
  fi

  if [ $run_fscd = false ]; then
    echo "mismatched system config, boost fans!"
    $KV_CMD set fan_mode_event 2 # boost_mode
  else
    $KV_CMD del fan_mode_event 2>/dev/null
  fi

  fscd_status=$(cat /etc/sv/fscd/supervise/stat)
  if [ "$fscd_status" != "run" ]; then
    sleep 1 && /usr/bin/sv restart fscd
  fi
}

start_fscd() {
  if [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
    init_class1_fsc
  elif [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
    init_class2_fsc
  else
    echo -n "Is board id correct(id=$bmc_location)?..."
    exit 255
  fi

  (start_sled_fsc &)
}

case "$1" in
  start)
    echo "Setup fan speed..."
    /usr/local/bin/fan-util --set 70
    start_fscd
  ;;

  reload)
    #boost fans if one of the blades is not present
    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=

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

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 "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_gpiod
elif [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; 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 fby35... "

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

echo "done."


#! /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
#

### 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

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

clear_all_vr_cache(){
  sb_vr_list=("c0h" "c4h" "ech" "c2h" "c6h" "c8h" "cch" "d0h" "96h" "9ch" "9eh" "8ah" "8ch" "8eh")
  rbf_vr_list=("b0h" "b4h" "c8h")
  for vr in "${sb_vr_list[@]}"; do
    $KV_CMD del "slot${slot_num}_vr_${vr}_crc"
    $KV_CMD del "slot${slot_num}_vr_${vr}_new_crc" persistent
  done
  for vr in "${rbf_vr_list[@]}"; do
    $KV_CMD del "slot${slot_num}_1ou_vr_${vr}_crc"
    $KV_CMD del "slot${slot_num}_1ou_vr_${vr}_new_crc" persistent
  done
}

# stop the service first
sv stop gpiod

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 [ -n "$OLDPID" ] && (grep "server-init" /proc/$OLDPID/cmdline 1> /dev/null 2>&1) ; then
  echo "kill pid $OLDPID..."
  kill -s 9 "$OLDPID"
  pkill -KILL -f "bic-cached -. slot${slot_num}" >/dev/null 2>&1
  pkill -KILL -f "power-util slot${slot_num}" >/dev/null 2>&1
  pkill -KILL -f "setup-fan" >/dev/null 2>&1
fi
unset OLDPID

prot_bus=$((slot_num+3))
if [ "$(is_server_prsnt "$slot_num")" = "0" ]; then
  disable_server_12V_power "$slot_num"
  gpio_set_value "FM_BMC_SLOT${slot_num}_ISOLATED_EN_R" 0
  rm -f "/tmp/"*"fruid_slot${slot_num}"*
  rm -f "/tmp/"*"sdr_slot${slot_num}"*
  $KV_CMD del "slot${slot_num}_cpld_new_ver"
  $KV_CMD del "slot${slot_num}_prot_ver"
  $KV_CMD del "slot${slot_num}_is_m2_exp_prsnt"
  $KV_CMD del "slot${slot_num}_get_1ou_type"
  $KV_CMD del "fru${slot_num}_2ou_board_type"
  $KV_CMD del "fru${slot_num}_sb_type"
  $KV_CMD del "fru${slot_num}_sb_rev_id"
  $KV_CMD del "fru${slot_num}_is_prot_prsnt"
  clear_all_vr_cache
  i2c_device_delete "${prot_bus}" 0x50 > /dev/null 2>&1
  set_nic_power
else
  /usr/bin/sv start "mctpd_${slot_num}" > /dev/null 2>&1
  /usr/bin/sv start ipmbd_${bus} > /dev/null 2>&1
  /usr/local/bin/power-util "slot${slot_num}" 12V-on
  i2c_device_add "${prot_bus}" 0x50 24c32 > /dev/null 2>&1
  set_vf_gpio "$slot_num"
  /usr/local/bin/bic-cached -s "slot${slot_num}"
  /usr/local/bin/bic-cached -f "slot${slot_num}"
  $KV_CMD set "slot${slot_num}_sdr_thresh_update" 1
  /usr/bin/fw-util "slot${slot_num}" --version > /dev/null
fi

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

# start the services again
sv start gpiod


#!/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 fby35..."
runsv /etc/sv/gpiointrd > /dev/null 2>&1 &

echo "done."


#! /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
#

### 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

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

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

echo -n "Starting IPMB Rx/Tx Daemon.."
sleep 10
ulimit -q 1024000

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_ipmb
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi

echo "Done."


#!/bin/sh
exec /usr/local/bin/mctpd asti3c 0 7ec80010000


#!/bin/sh
exec /usr/local/bin/mctpd asti3c 1 7ec80010000


#!/bin/sh
exec /usr/local/bin/mctpd asti3c 2 7ec80010000


#!/bin/sh
exec /usr/local/bin/mctpd asti3c 3 7ec80010000


#!/bin/sh
exec /usr/local/bin/mctpd smbus 8 0x10


#! /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
#

# shellcheck disable=SC1091,SC2039
. /usr/local/fbpackages/utils/ast-functions

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

init_class1_mctp() {
  for slot_num in {1..4}; do
    bus=$((slot_num))
    runsv /etc/sv/mctpd_${bus} > /dev/null 2>&1 &
    if [ "$slot_num" -eq 1 ] || [ "$slot_num" -eq 3 ]; then
      slot_present=$(gpio_get PRSNT_MB_BMC_SLOT"${slot_num}"_BB_N)
    else
      slot_present=$(gpio_get PRSNT_MB_SLOT"${slot_num}"_BB_N)
    fi
    if [ "$slot_present" = "1" ]; then
      usleep 50000
      sv stop mctpd_${bus}
      disable_server_12V_power "${slot_num}"
    else
      enable_server_i2c_bus "${slot_num}"
    fi
  done
}

init_class2_mctp() {
  runsv /etc/sv/mctpd_1 > /dev/null 2>&1 &
}

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_mctp
elif [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_mctp
else
  echo -n "setup_mctpd: Is board id correct(id=$bmc_location)?..."
fi

echo slave-mqueue 0x1010 > /sys/bus/i2c/devices/i2c-8/new_device

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

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 "$BMC_ID_CLASS1" ]; 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/sh

# Address of SRAM that is used to store "reboot by command" flag.
SRAM_BMC_REBOOT_BY_CMD="0x10015C04"

# Set 0x101 as flag for healthd logging reboot cause.
val=$(devmem "$SRAM_BMC_REBOOT_BY_CMD" 32 2>/dev/null)
devmem "$SRAM_BMC_REBOOT_BY_CMD" 32 $((val | 0x101)) 2>/dev/null


#!/bin/sh

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

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

MAX_RETRY=600         # wait 10 mins before give up
NOT_READY_DAEMON=""

total_daemon="sensord ipmid fscd front-paneld gpiod gpiointrd ncsid healthd ipmbd_9"

sensord_list="flag_sensord_monitor flag_sensord_health"
ipmid_list="flag_ipmid"
front_paneld_list="flag_front_sys_status_led"
gpiod_list="flag_gpiod_server_pwr "
gpiointrd_list="flag_gpiointrd flag_gpiod_fru_miss"
ncsid_list="flag_ncsid"
healthd_list="flag_healthd_wtd flag_healthd_hb_led flag_healthd_crit_proc flag_healthd_cpu flag_healthd_mem flag_healthd_bmc_timestamp"
ipmbd_9_list="flag_ipmbd_rx_9 flag_ipmbd_res_9 flag_ipmbd_req_9"

check_daemon_flag() {
  flag_list=$1

  for flag_name in $flag_list
  do
    result="$($KV_CMD get "$flag_name")"
    if [ "$result" != "1" ] || [ "$result" = "" ]; then
      echo "$flag_name"
      break
    fi
  done
}

check_daemon_status() {
  daemon_name=$1

  #check slot present & BIC ready
  if [ "$daemon_name" = "mctpd_1" ]; then
    if [ "$(is_sb_bic_ready 1)" != "1" ]; then
      return
    fi
  elif [ "$daemon_name" = "mctpd_2" ]; then
    if [ "$(is_sb_bic_ready 2)" != "1" ]; then
      return
    fi
  elif [ "$daemon_name" = "mctpd_3" ]; then
    if [ "$(is_sb_bic_ready 3)" != "1" ]; then
      return
    fi
  elif [ "$daemon_name" = "mctpd_4" ]; then
    if [ "$(is_sb_bic_ready 4)" != "1" ]; then
      return
    fi
  fi

  daemon_status="$(sv status "$daemon_name" | grep 'run')"
  if [ "$daemon_status" = "" ]; then
    echo "$daemon_name"
  else
    # check ready flag of each thread
    if [ "$daemon_name" = "sensord" ]; then
      not_ready="$(check_daemon_flag "$sensord_list" "$daemon_name")"
    elif [ "$daemon_name" = "ipmid" ]; then
      not_ready="$(check_daemon_flag "$ipmid_list" "$daemon_name")"
    elif [ "$daemon_name" = "front-paneld" ]; then
      not_ready="$(check_daemon_flag "$front_paneld_list" "$daemon_name")"
    elif [ "$daemon_name" = "gpiod" ]; then
      not_ready="$(check_daemon_flag "$gpiod_list" "$daemon_name")"
    elif [ "$daemon_name" = "gpiointrd" ]; then
      not_ready="$(check_daemon_flag "$gpiointrd_list" "$daemon_name")"
    elif [ "$daemon_name" = "ncsid" ]; then
      not_ready="$(check_daemon_flag "$ncsid_list" "$daemon_name")"
    elif [ "$daemon_name" = "healthd" ]; then
      not_ready="$(check_daemon_flag "$healthd_list" "$daemon_name")"
    elif [ "$daemon_name" = "ipmbd_9" ]; then
      not_ready="$(check_daemon_flag "$ipmbd_9_list" "$daemon_name")"
    else
      return
    fi
    echo "$not_ready"
  fi
}

check_bmc_ready() {
  retry=0

  while [ "$retry" -lt "$MAX_RETRY" ]
  do

    for daemon in $total_daemon
    do
      result="$(check_daemon_status "$daemon")"

      # daemon not ready
      if [ "$result" = "$daemon" ]; then
        NOT_READY_DAEMON=$daemon
        break
      elif [ "$result" != "" ]; then
        NOT_READY_DAEMON=$daemon
        NOT_READY_FLAG=$result
        break
      else
        NOT_READY_DAEMON=
      fi
    done

    # all daemons are ready
    if [ "$NOT_READY_DAEMON" = "" ]; then
      break
    else
      sleep 1
      retry=$((retry+1))
    fi
  done

  if [ "$NOT_READY_DAEMON" = "" ]; then
    "$KV_CMD" set bmc_ready_flag 1
    logger -s -p user.info -t ready-flag "BMC is ready"
  else
    logger -s -p user.info -t ready-flag "daemon: $NOT_READY_DAEMON ($NOT_READY_FLAG) is not ready"
  fi
}

echo "check bmc ready..."
check_bmc_ready &


#!/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/bash
#
# Copyright 2023-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
#

# shellcheck source=common/recipes-utils/openbmc-utils/files/openbmc-utils.sh
. /usr/local/bin/openbmc-utils.sh

FRUS=(slot1 slot2 slot3 slot4 bmc)

clear_vr_cache() {
  local fru=$1
  local list
  local cache

  if list=$(ls /mnt/data/kv_store/"${fru}"_*vr_*_crc 2>/dev/null); then
    mapfile list <<< "$list"
    for cache in "${list[@]}"; do
      /usr/bin/kv del "$(basename "$cache")" persistent
    done
  fi
}

if [ "$(is_bmc_por)" -eq 1 ]; then
  for i in "${FRUS[@]}"; do
    clear_vr_cache "$i"
  done
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

### BEGIN INIT INFO
# Provides:          power-on
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on Server
### END INIT INFO

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

# shellcheck disable=SC1091
. /usr/local/fbpackages/utils/ast-functions

DEF_PWR_ON=1
TO_PWR_ON=

check_por_config() {
  TO_PWR_ON=0

  if POR=$(kv get "slot${1}_por_cfg" persistent); then
    if [ "$POR" = "lps" ]; then
      if LS=$(kv get "pwr_server${1}_last_state" persistent); then
        if [ "$LS" = "on" ]; then
          TO_PWR_ON=1;
        fi
      else
        TO_PWR_ON=$DEF_PWR_ON
      fi
    elif [ "$POR" = "on" ]; then
      TO_PWR_ON=1;
    fi
  else
    TO_PWR_ON=$DEF_PWR_ON
  fi
}

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

  show_sys_config -r > /dev/null
}

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 70)" 13

    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
}

/usr/local/bin/sync_date.sh

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_server
elif [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_server
else
  echo "Is board id correct(id=$bmc_location)?..."
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
#

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

MEDUSA_HSC_BUS="11"
LTC4282_ADDR="0x44"
ADM1272_ADDR="0x1f"
LTC4287_ADDR="0x11"
LTC4282_CONTROL_REG="0x00"
LTC4282_ADJUST_REG="0x11"
LTC4287_CONTROL_REG="0x47"
MEDUSA_48V_IO_EXP_ADDR="0x49"
LTC2992_ADDR="6e"
INA238_ADDR_PSU="44"
INA238_ADDR_GND="41"

function read_dev() {
  local bus=$1
  local addr=$2
  local reg=$3
  local val

  for _ in {1..3}; do
    if val=$(/usr/sbin/i2cget -f -y "$bus" "$addr" "$reg" 2>/dev/null); then
      echo "$val"
      return 0
    fi
  done

  return 1
}

function init_48V_medusa() {
  local medusa_adc_addr=("$INA238_ADDR_PSU" "$INA238_ADDR_GND")
  local medusa_adc_devs=("ina238" "ina238")
  local medusa_dev_addr=""
  local chip=""

  for i in "${!medusa_adc_addr[@]}"; do
    if read_dev "$MEDUSA_HSC_BUS" 0x"${medusa_adc_addr["$i"]}" 0 >/dev/null; then
      medusa_dev_addr="$MEDUSA_HSC_BUS"-00"${medusa_adc_addr["$i"]}"
      if [ ! -L "${SYSFS_I2C_DEVICES}/$medusa_dev_addr/driver" ]; then
        chip="${medusa_adc_devs["$i"]}"
        i2c_device_add "$MEDUSA_HSC_BUS" 0x"${medusa_adc_addr["$i"]}" "${medusa_adc_devs["$i"]}"
      fi
    else
      chip="ltc2992"
    fi
  done

  kv set medusa_adc_chip "$chip"
}

function init_class1_dev() {
  #create the device of the inlet/outlet temp.
  i2c_device_add 12 0x4e lm75
  i2c_device_add 12 0x4f lm75

  #create the device of bmc/bb fru.
  i2c_device_add 11 0x51 24c128
  i2c_device_add 11 0x54 24c128

  local medusa_addr=""
  local chip=""
  local load_driver=false

  # Get register from different address to determine HSC chip

  if read_dev $MEDUSA_HSC_BUS $LTC4287_ADDR 0x1 >/dev/null; then
    if curr_val=$(read_dev $MEDUSA_HSC_BUS $LTC4287_ADDR $LTC4287_CONTROL_REG); then
      set_oc_auto_retry=$((curr_val | (0x7 << 3)))
      /usr/sbin/i2cset -y $MEDUSA_HSC_BUS $LTC4287_ADDR $LTC4287_CONTROL_REG "$set_oc_auto_retry"
    fi
    chip="ltc4287"
    medusa_addr=$LTC4287_ADDR
    load_driver=true
  elif read_dev $MEDUSA_HSC_BUS $ADM1272_ADDR 0x1 >/dev/null; then
    chip="adm1272"
    medusa_addr=$ADM1272_ADDR
    load_driver=true
  elif read_dev $MEDUSA_HSC_BUS $LTC4282_ADDR 0 >/dev/null; then
    if curr_val=$(read_dev $MEDUSA_HSC_BUS $LTC4282_ADDR $LTC4282_CONTROL_REG); then
      set_oc_auto_retry=$((curr_val | (0x1 << 2)))
      /usr/sbin/i2cset -y $MEDUSA_HSC_BUS $LTC4282_ADDR $LTC4282_CONTROL_REG "$set_oc_auto_retry"
    fi
    chip="ltc4282"
    medusa_addr=$LTC4282_ADDR
    load_driver=true
  else
    load_driver=false
  fi

  if [ "$load_driver" = true ]; then
    i2c_device_add $MEDUSA_HSC_BUS $medusa_addr $chip
    if [ "$chip" == "ltc4282" ]; then
      if curr_val=$(read_dev $MEDUSA_HSC_BUS $LTC4282_ADDR $LTC4282_ADJUST_REG); then
        set_12bit_mode=$((curr_val & ~0x1))
        /usr/sbin/i2cset -f -y $MEDUSA_HSC_BUS $LTC4282_ADDR $LTC4282_ADJUST_REG "$set_12bit_mode"
      fi
    fi
  fi

  kv set medusa_hsc_conf $chip persistent

  # check nic power to see if it's need to be set to standby mode
  set_nic_power
}

function init_class2_dev() {
  #create the device of the outlet temp.
  i2c_device_add 2 0x4f lm75
  i2c_device_add 12 0x4f lm75

  #create the device of bmc/nic fru.
  i2c_device_add 11 0x51 24c128
  i2c_device_add 11 0x54 24c128
}

function init_exp_dev() {
  for i in {1..4}; do
    bmc_location=$(get_bmc_board_id)
    if [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
      if [ "$(is_server_prsnt "$i")" == "0" ]; then
        continue
      fi
      enable_server_i2c_bus "$i"
    fi
    cpld_bus=$(get_cpld_bus "$i")
    type_2ou=$(get_2ou_board_type "$cpld_bus")
    # check DPv2 x8 present
    prsnt_x8=$((type_2ou & 0x7))
    if [ "$prsnt_x8" -eq "7" ]; then
      i2c_device_add "$cpld_bus" 0x51 24c128
    fi
    # EEPROM on PRoT module
    if [ "$(is_prot_prsnt "$i")" == "1" ]; then
      i2c_device_add "$cpld_bus" 0x50 24c32
    fi
    # IO-exp on VF2
    set_vf_gpio "$i"
  done
}

echo "Setup devs for fby35..."

#create the device of mezz card
i2c_device_add 8 0x1f tmp421
i2c_device_add 8 0x50 24c32

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_dev
elif [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_dev
else
  echo -n "Is board id correct(id=$bmc_location)?..."
fi

# Check if the Medusa IO expender exist
if read_dev $MEDUSA_HSC_BUS $MEDUSA_48V_IO_EXP_ADDR 0 >/dev/null; then
  init_48V_medusa
fi

init_exp_dev

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
#
. /usr/local/fbpackages/utils/ast-functions

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

function get_sel_time_from_server {
  local BOARD_ID=$(get_bmc_board_id)
  local output=""
  local snum=0

  if [ "$BOARD_ID" -eq "$BMC_ID_CLASS2" ]; then
    output="$(/usr/local/bin/me-util slot1 0x28 0x48)"
    [ ${#output} == 12 ] && snum=1
  else
    for i in $(seq 1 4)
    do
      if [[ $(is_server_prsnt "$i") == "1" ]] ; then
        # Use standard IPMI command 'get-sel-time' to read RTC time
        output=$(/usr/local/bin/me-util "slot$i" 0x28 0x48)
        if [ ${#output} == 12 ]; then
          snum=$i
          break
        fi
      fi
    done
  fi

  # return IPMI "Get sel time" command output, or return empty string if command failed
  [ ${#output} == 12 ] && echo "$output" || echo ""

  return "$snum"
}

function do_sync {
  local sel_time=$1
  date -s @$((16#$(echo "$sel_time" | awk '{print $4$3$2$1}')))
  test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
  kv set date_sync 1
}

# Sync BMC's date with one of the four servers
function sync_date {
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    output="$(/usr/bin/kv get time_sync)"
    if [ -n "${output}" ] ; then
      # Server Time Sync by IPMI command
      if [ ${#output} == 11 ] ; then
        echo Syncing up BMC time with server...
        do_sync "$output"
      fi
    else
      output=$(get_sel_time_from_server)
      num=$?
      if [ -n "$output" ]; then
        echo Syncing up BMC time with server$num...
        do_sync "$output"
      fi
    fi
  else
    test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
    kv set date_sync 1
  fi
}

sync_date


#!/bin/sh
exec /usr/local/bin/pldmd --bus 0


#!/bin/sh
exec /usr/local/bin/pldmd --bus 1


#!/bin/sh
exec /usr/local/bin/pldmd --bus 2


#!/bin/sh
exec /usr/local/bin/pldmd --bus 3


#! /bin/sh
#
# Copyright 2022-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
#

# shellcheck disable=SC1091,SC2039
. /usr/local/fbpackages/utils/ast-functions

init_class1_pldm() {
  for slot_num in {1..4}; do
    bus=$((slot_num))
    runsv /etc/sv/pldmd_${bus} > /dev/null 2>&1 &
  done
}

init_class2_pldm() {
  runsv /etc/sv/pldmd_1 > /dev/null 2>&1 &
}

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

ulimit -q 1024000

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_pldm
elif [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_pldm
else
  echo -n "setup-pldmd: Is board id correct(id=$bmc_location)?..."
fi

echo "done."


#!/bin/bash

# shellcheck source=meta-facebook/meta-fby35/recipes-fby35/plat-utils/files/ast-functions
. /usr/local/fbpackages/utils/ast-functions

SLOTS=

init_class2_sensord() {
  SLOTS=(slot1 bmc nic)
}

init_class1_sensord() {
  sys_config="$($KV_CMD get sled_system_conf persistent)"
  if [[ "$sys_config" == "Type_8" ]]; then
    SLOTS=(slot1)
  elif [[ "$sys_config" =~ ^(Type_(DPV2|HD|GL))$ ]]; then
    SLOTS=(slot1 slot3)
  else
    SLOTS=(slot1 slot2 slot3 slot4)
  fi

  SLOTS+=(bmc nic)
}

bmc_location=$(get_bmc_board_id)
if [ "$bmc_location" -eq "$BMC_ID_CLASS2" ]; then
  #The BMC of class2
  init_class2_sensord
elif [ "$bmc_location" -eq "$BMC_ID_CLASS1" ]; then
  #The BMC of class1
  init_class1_sensord
else
  /bin/echo -n "Is board id correct(id=$bmc_location)?..."
fi

SENSORD_HOTSWAP_SUPPORT=1 exec /usr/local/bin/sensord "${SLOTS[@]}"