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

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

exec /usr/local/bin/sensor-svcd


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

### BEGIN INIT INFO
# Provides:          sensor-svcd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: dbus based sensor service
### END INIT INFO

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

# Call "fw-util mb --version" once before sensor monitoring to store vr information
echo "Get MB FW version... "
/usr/bin/fw-util mb --version > /dev/null

echo -n "Setup sensor svcd for FBTP... "

runsv /etc/sv/sensor-svcd > /dev/null 2>&1 &

echo "done."


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

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

# setup ADC channels

ADC_PATH="/sys/devices/platform/ast_adc.0"
# channel 0: r1: 2.87K; r2:  2.0K; v2: 0mv
# channel 1: r1: 5.36K; r2:  2.0K; v2: 0mv
# channel 2: r1: 15.8K; r2:  2.0K; v2: 0mv
# channel 3: r1:  1.0K; r2:    NA; v2: 0mv # No voltage divider
# channel 4: r1:  1.0K; r2:    NA; v2: 0mv # No voltage divider
# channel 5: r1: 2.87K; r2:  2.0K; v2: 0mv
# channel 6: r1: 5.36K; r2:  2.0K; v2: 0mv
# channel 7: r1:  200K; r2:  100K; v2: 0mv

config_adc() {
    channel=$1
    r1=$2
    r2=$3
    v2=$4
    echo $r1 > ${ADC_PATH}/adc${channel}_r1
    echo $r2 > ${ADC_PATH}/adc${channel}_r2
    echo $v2 > ${ADC_PATH}/adc${channel}_v2
    echo 1 > ${ADC_PATH}/adc${channel}_en
}
config_adc 0  287 200 0
config_adc 1  536 200 0
config_adc 2 1580 200 0
config_adc 3    0   1 0
config_adc 4    0   1 0
config_adc 5  287 200 0
config_adc 6  536 200 0
config_adc 7  400 200 0


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

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

echo -n "Setup Caching for Bridge IC info.."
if [ $(is_server_prsnt 1) == "1" ]; then
  /usr/local/bin/bic-cached 1 > /dev/null 2>&1 &
fi

echo "done."


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

### BEGIN INIT INFO
# Provides:          setup_bios_util
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup bios-util config
### END INIT INFO

echo -n "Setup bios-util config... "

bios_util_config_path="/usr/local/fbpackages/bios-util/bios_support.json"
bc_type5_config_path="/usr/local/fbpackages/bios-util/BIOS_UTIL_BCT5_v2.json"
bc_type7_config_path="/usr/local/fbpackages/bios-util/BIOS_UTIL_BCT7_v2.json"
IOM_M2=1      # IOM type: M.2
IOM_IOC=2     # IOM type: IOC

sh /usr/local/bin/check_pal_sku.sh > /dev/NULL
  PAL_SKU=$(($? & 0x3))
  if [ "$PAL_SKU" == "$IOM_M2" ]; then
    cp ${bc_type5_config_path} ${bios_util_config_path}
  elif [ "$PAL_SKU" == "$IOM_IOC" ]; then
  	cp ${bc_type7_config_path} ${bios_util_config_path}
  else
  	echo "Setup bios-util: System type is unknown! Please confirm the system type."
  fi

echo "done."


#!/bin/sh

rm -f ../bios-util/*.pyc
rm -rf ./*.pyc


#!/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-consoled
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup console history buffering
### END INIT INFO

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

echo -n "Setup console  buffering..."
  if [ $(is_server_prsnt 1) == "1" ] ; then
    /usr/local/bin/consoled server --buffer
  fi

echo "done."


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

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

echo -n "Setup Caching for SCC Expander info.."
#To do... get exp heartbeat

if [ $(is_server_prsnt 1) == "1" ]; then
  /usr/local/bin/exp-cached 1 > /dev/null 2>&1 &
fi

echo "done."


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

get_sku()
{
  # * SCC_RMT_TYPE_0  GPIOF7  47
  # 1: single server mode (Type 7 Headnode) ; 0: dual server mode (Type5)
  SCC_RMT=`cat /sys/class/gpio/gpio47/value`
  get_iom_location
  SLOTID=$?
  get_iom_type
  IOM_TYPE=$?

  if [ $IOM_TYPE -eq 1 ]; then
    echo "System: Type5"
    if [ $SLOTID -eq 1 ]; then
      echo "IOM Location: Side A"
    elif [ $SLOTID -eq 2 ]; then
      echo "IOM Location: Side B"
    else
      echo "IOM Location: Unknown"
    fi
  elif [ $IOM_TYPE -eq 2 ]; then
    echo "System: Type7"
  else
    echo "System: Unknown"
  fi

  PAL_SKU=$((($SCC_RMT << 6) | ($SLOTID << 4) | $IOM_TYPE))

  return $PAL_SKU
}

get_iom_location()
{
  # * SLOTID_0  GPIOG0  48 Strap pin for IOM location
  # * SLOTID_1  GPIOG1  49 Strap pin for IOM location
  # SLOTID_[0:1]: 01=IOM_A; 10=IOM_B
  SLOTID_0=`cat /sys/class/gpio/gpio48/value`
  SLOTID_1=`cat /sys/class/gpio/gpio49/value`
  SLOTID=$((($SLOTID_0 << 1) | $SLOTID_1))

  return $SLOTID
}

get_iom_type()
{
  # * IOM_TYPE0  GPIOJ4  76
  # * IOM_TYPE1  GPIOJ5  77
  # * IOM_TYPE2  GPIOJ6  78
  # * IOM_TYPE3  GPIOJ7  79
  IOM_TYPE_0=`cat /sys/class/gpio/gpio76/value`
  IOM_TYPE_1=`cat /sys/class/gpio/gpio77/value`
  IOM_TYPE_2=`cat /sys/class/gpio/gpio78/value`
  IOM_TYPE_3=`cat /sys/class/gpio/gpio79/value`
  IOM_TYPE=$((($IOM_TYPE_0 << 3) | ($IOM_TYPE_1 << 2) | ($IOM_TYPE_2 << 1) | $IOM_TYPE_3))

  return $IOM_TYPE
}

get_sku
PAL_SKU=$?
echo -n "Platform SKU: $PAL_SKU ("
i=0
while [ "${i}" -lt 8 ]
do
  tmp_sku_bit=$((($PAL_SKU >> i) & 1))
  sku_bit=$tmp_sku_bit$sku_bit
  i=$(($i+1))
done
  echo -n $sku_bit
echo ")"
echo "<SKU[6:0] = {SCC_RMT_TYPE_0, IOM_SLOTID0, IOM_SLOTID1, IOM_TYPE0, IOM_TYPE1, IOM_TYPE2, IOM_TYPE3}>"

exit $PAL_SKU


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

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

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

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

    # In FBTTN LED is controlled by H0~H7
    gpio_set LED_POSTCODE_0 $bit0
    gpio_set LED_POSTCODE_1 $bit1
    gpio_set LED_POSTCODE_2 $bit2
    gpio_set LED_POSTCODE_3 $bit3
}

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

    # In FBTTN LED is controlled by H0~H7

    gpio_set LED_POSTCODE_4 $bit0
    gpio_set LED_POSTCODE_5 $bit1
    gpio_set LED_POSTCODE_6 $bit2
    gpio_set LED_POSTCODE_7 $bit3
}

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

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

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

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

# Display the results
display_lower $LOWER_DEC_VALUE
display_upper $UPPER_DEC_VALUE


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

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

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

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

# Check power policy
# $1: slot number
check_por_config()
{
  TO_PWR_ON=-1
  slot_num=$1

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

    # Case ON
    if [ "$PWR_POLICY" == "on" ]; then
      TO_PWR_ON=1;

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

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

      # Check if the file/key doesn't exist
      if [ ! -f "${KEYDIR}/pwr_server${slot_num}_last_state" ]; then
        TO_PWR_ON=$DEF_PWR_ON
      else
        LS=`cat ${KEYDIR}/pwr_server${slot_num}_last_state`
        if [ "$LS" == "on" ]; then
          TO_PWR_ON=1;
        elif [ "$LS" == "off" ]; then
          TO_PWR_ON=0;
        fi
      fi
    fi
    logger -s -p user.info -t power-on "Power Policy: $PWR_POLICY, Last Power State: $LS"
  fi
}

# Check if BIC is ready or not
check_bic_ready()
{
  retry=0
  while [ "$retry" != "5" ]
  do
    if [ "$(gpio_get BIC_READY_N)" == "0" ]; then
      logger -s -p user.warn -t power-on "BIC is Ready!"
      return
    else
      logger -s -p user.info -t power-on "BIC is NOT Ready!!! Retry: $retry"
      retry=$((retry+1))
      sleep 1
    fi
  done
}

# Sync BMC's date with one of the four servers
sync_date()
{
  retry=0
  if [ "$(is_server_prsnt)" == "1" ] ; then
    while [ "$retry" != "5" ]
    do
      # Use standard IPMI command 'get-sel-time' to read RTC time
      output=$(/usr/local/bin/me-util server 0x28 0x48)
      # Sync BMC time only when ME returns correct response length
      if [ "$(echo $output | wc -c)" == "12" ] ; then
        col1=$(echo $output | cut -d' ' -f1 | sed 's/^0*//')
        col2=$(echo $output | cut -d' ' -f2 | sed 's/^0*//')
        col3=$(echo $output | cut -d' ' -f3 | sed 's/^0*//')
        col4=$(echo $output | cut -d' ' -f4 | sed 's/^0*//')
        # create the integer from the hex bytes returned
        val=$((0x$col4 << 24 | 0x$col3 << 16 | 0x$col2 << 8 | 0x$col1))
        # create the timestamp required for busybox's date command
        ts=$(date -d @$val +"%Y.%m.%d-%H:%M:%S")
        # set the command
        logger -s -p user.info -t power-on "Syncing up BMC time with server..."
        logger -s -p user.info -t power-on "`date $ts`"
        return
      else
        retry=$((retry+1))
        logger -s -p user.info -t power-on "Get SEL time from ME failed, syncing up BMC time with server failed for $retry times"
        sleep 1
      fi
    done
  fi
}

is_server_12v_off="1"

# Check Mono Lake is  present or not:
# If Mono Lake absent, turn off Mono Lake HSC 12V and IOM 3V3
is_server_12v_off="1"
if [ "$(is_server_prsnt)" == "0" ]; then
  logger -s -p user.warn -t power-on "The Mono Lake is absent, turn off Mono Lake HSC 12V and IOM 3V3."
  gpio_set COMP_PWR_EN 0
  gpio_set IOM_FULL_PWR_EN 0
  is_server_12v_off="0"
fi

# Check whether it is fresh power on reset
if [ "$(is_bmc_por)" == "1" ] && [ "$is_server_12v_off" == "1" ]; then
  logger -s -p user.warn -t power-on "BMC POR and Server is Present"
  # Disable clearing of PWM block on WDT SoC Reset
  devmem_clear_bit $(scu_addr 9c) 17

  # For fbttn remote SCC PWR sequence
  sh /usr/local/bin/check_pal_sku.sh > /dev/NULL
  PAL_SKU=$(($(($? >> 6)) & 0x1))
  if [ "$PAL_SKU" == "1" ]; then  # type 7, always power-on SCC B
    gpio_set SCC_RMT_FULL_PWR_EN 1
    gpio_tolerance_fun SCC_RMT_FULL_PWR_EN GPIOF1
  fi

  # Turn on the IOM_FULL_PWR_EN to power on the M.2 and IOM 3v3
  gpio_set IOM_FULL_PWR_EN 1
  logger -s -p user.warn -t power-on "Turning on IOM 3v3(IOM_FULL_PWR_EN)"
  # For fbttn MonoLake PWR sequence
  if [ "$(gpio_get IOM_FULL_PGOOD)" == "1" ]; then
    logger -s -p user.warn -t power-on "BMC full power good is enable"
    # set ML board power enable (12V on)
    gpio_set COMP_PWR_EN 1
    logger -s -p user.warn -t power-on "Turning Server 12V-on"
    sleep 3  # waiting for ME ready
    check_bic_ready
    sync_date
    check_por_config 1
    if [ "$TO_PWR_ON" == "1" ] && [ "$(is_server_prsnt)" == "1" ] ; then
        power-util server on
    fi
  else
    logger -s -p user.warn -t power-on "IOM full power good is disable"
  fi
else
  logger -s -p user.warn -t power-on "BMC Rebooted or Server is not Present"
  sync_date
fi


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

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

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

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

set -e

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

# Slot#1: GPIOA3(3)
if [ $1 = "1" ]; then
    gpio="SYS_PWR_LED"
else
    usage $0
fi

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

gpio_set $gpio $val


#!/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
# exported in other files.  We should probably move some more of the
# definitions to this file at some point.

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

# This is a workaround for MAC1LINK
devmem 0x1e660050 w 0xA954F

# BMC_TO_EXP_RESET: A2 (2)
# To use GPIOA2, SCU80[2], and SCU80[15] must be 0
devmem_clear_bit $(scu_addr 80) 2
devmem_clear_bit $(scu_addr 80) 15

gpio_export BMC_TO_EXP_RESET GPIOA2
# set GPIOA2 WDT reset tolerance
gpio_tolerance_fun BMC_TO_EXP_RESET GPIOA2

# SYS_PWR_LED: A3 (3)
# To use GPIOA3, SCU80[3] must be 0
devmem_clear_bit $(scu_addr 80) 3

gpio_export SYS_PWR_LED GPIOA3
gpio_set SYS_PWR_LED 1

# PWRBTN_OUT_N: D2 (26)
# To use GPIOD2, SCU90[1], SCU8C[9], and SCU70[21] must be 0
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 8C) 9
devmem_clear_bit $(scu_addr 70) 21

gpio_export PWRBTN_OUT_N GPIOD2

# COMP_PWR_BTN_N: D3 (27)
# To use GPIOD3, SCU90[1], SCU8C[9], and SCU70[21] must be 0
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 8C) 9
devmem_clear_bit $(scu_addr 70) 21

gpio_export COMP_PWR_BTN_N GPIOD3
gpio_set COMP_PWR_BTN_N 1
# set GPIOD3 WDT reset tolerance
gpio_tolerance_fun COMP_PWR_BTN_N GPIOD3

# RSTBTN_OUT_N: D4 (28)
# To use GPIOD4, SCU90[1], SCU8C[10], and SCU70[21] must be 0
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 8C) 10
devmem_clear_bit $(scu_addr 70) 21

gpio_export RSTBTN_OUT_N GPIOD4

# SYS_RESET_N_OUT: D5 (29)
# To use GPIOD5, SCU90[1], SCU8C[10], and SCU70[21] must be 0
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 8C) 10
devmem_clear_bit $(scu_addr 70) 21

gpio_export SYS_RESET_N_OUT GPIOD5
gpio_set SYS_RESET_N_OUT 1
# set GPIOD5 WDT reset tolerance
gpio_tolerance_fun SYS_RESET_N_OUT GPIOD5

# M2_1_PRESENT_N: E0 (32)
# To use GPIOE0, SCU80[16], SCU8C[12], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 16
devmem_clear_bit $(scu_addr 8C) 12
devmem_clear_bit $(scu_addr 70) 22

gpio_export M2_1_PRESENT_N GPIOE0

# M2_2_PRESENT_N: E1 (33)
# To use GPIOE1, SCU80[17], SCU8C[12], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 17
devmem_clear_bit $(scu_addr 8C) 12
devmem_clear_bit $(scu_addr 70) 22

gpio_export M2_2_PRESENT_N GPIOE1

# M2_1_FAULT_N: E2 (34)
# To use GPIOE2, SCU80[18], SCU8C[13], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 18
devmem_clear_bit $(scu_addr 8C) 13
devmem_clear_bit $(scu_addr 70) 22

gpio_export M2_1_FAULT_N GPIOE2

# M2_2_FAULT_N: E3 (35)
# To use GPIOE3, SCU80[19], SCU8C[13], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 19
devmem_clear_bit $(scu_addr 8C) 13
devmem_clear_bit $(scu_addr 70) 22

gpio_export M2_2_FAULT_N GPIOE3

# BMC_EXT1_LED_B: E4 (36)
# To use GPIOE4, SCU80[20], SCU8C[14], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 20
devmem_clear_bit $(scu_addr 8C) 14
devmem_clear_bit $(scu_addr 70) 22

gpio_export BMC_EXT1_LED_B GPIOE4
gpio_set BMC_EXT1_LED_B 0

# BMC_EXT1_LED_Y: E5 (37)
# To use GPIOE5, SCU80[21], SCU8C[14], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 21
devmem_clear_bit $(scu_addr 8C) 14
devmem_clear_bit $(scu_addr 70) 22

gpio_export BMC_EXT1_LED_Y GPIOE5
gpio_set BMC_EXT1_LED_Y 1

# BMC_EXT2_LED_B: E6 (38)
# To use GPIOE6, SCU80[22], SCU8C[15], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 22
devmem_clear_bit $(scu_addr 8C) 15
devmem_clear_bit $(scu_addr 70) 22

gpio_export BMC_EXT2_LED_B GPIOE6
gpio_set BMC_EXT2_LED_B 0

# BMC_EXT2_LED_Y: E7 (39)
# To use GPIOE7, SCU80[23], SCU8C[15], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 23
devmem_clear_bit $(scu_addr 8C) 15
devmem_clear_bit $(scu_addr 70) 22

gpio_export BMC_EXT2_LED_Y GPIOE7
gpio_set BMC_EXT2_LED_Y 1

# Because SCC power sequence is controlled by HW on EVT.
# So we just configure SCC PWR pins to GPIO and does not export their GPIO node.
# SCC PWR pins: GPIOF0, GPIOF1, and GPIOF4
# SCC_LOC_FULL_PWR_EN: F0 (40), PS
# To use GPIOF0, LHCR[0], SCU90[30], and SCU80[24] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 24

# SCC_RMT_FULL_PWR_EN: F1 (41)
# To use GPIOF1, LHCR[0], SCU90[30], and SCU80[25] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 25

gpio_export SCC_RMT_FULL_PWR_EN GPIOF1

# EXP_SPARE_0: F2 (42), EXP_SPARE_1: F3 (43)
# To use GPIOF2, LHCR[0], SCU90[30], and SCU80[26] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 26
# To use GPIOF3, LHCR[0], SCU90[30], and SCU80[27] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 27

gpio_export EXP_SPARE_0 GPIOF2
gpio_export EXP_SPARE_1 GPIOF3

# SCC_STBY_PWR_EN: F4 (44), PS
# To use GPIOF4, LHCR[0], SCU90[30], and SCU80[28] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 28

# DPB_MISC_ALERT: F5 (45)
# To use GPIOF5, LHCR[0], SCU90[30], and SCU80[29] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 29

gpio_export DPB_MISC_ALERT GPIOF5

# P12V_A_PGOOD: F6 (46)
# To use GPIOF6, LHCR[0], and SCU80[30] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 80) 30

gpio_export P12V_A_PGOOD GPIOF6

# SCC_RMT_TYPE_0: F7 (47)
# To use GPIOF7, LHCR[0], SCU90[30], and SCU80[31] must be 0
devmem_clear_bit $(lpc_addr A0) 0
devmem_clear_bit $(scu_addr 90) 30
devmem_clear_bit $(scu_addr 80) 31

gpio_export SCC_RMT_TYPE_0 GPIOF7

# SLOTID_0: G0 (48), SLOTID_1: G1 (49)
# To use GPIOG0, SCU84[0] must be 0
devmem_clear_bit $(scu_addr 84) 0
# To use GPIOG1, SCU84[1] must be 0
devmem_clear_bit $(scu_addr 84) 1

gpio_export SLOTID_0 GPIOG0
gpio_export SLOTID_1 GPIOG1

# COMP_SPARE_0: G2 (50), COMP_SPARE_1: G3 (51))
# To use GPIOG2, SCU84[2] must be 0
devmem_clear_bit $(scu_addr 84) 2
# To use GPIOG3, SCU84[3] must be 0
devmem_clear_bit $(scu_addr 84) 3

gpio_export COMP_SPARE_0 GPIOG2
gpio_export COMP_SPARE_1 GPIOG3

# BIC_READY_N: G7 (55)
# To use GPIOG7, SCU94[12], and SCU84[7] must be 0
devmem_clear_bit $(scu_addr 94) 12
devmem_clear_bit $(scu_addr 84) 7

gpio_export BIC_READY_N GPIOG7

# LED_POSTCODE_0: H0 (56)
# To use GPIOH0, SCU94[5], and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 94) 5
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_0 GPIOH0
gpio_set LED_POSTCODE_0 0

# LED_POSTCODE_1: H1 (57)
# To use GPIOH1, SCU94[5], and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 94) 5
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_1 GPIOH1
gpio_set LED_POSTCODE_1 0

# LED_POSTCODE_2: H2 (58)
# To use GPIOH2, SCU94[6], and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 94) 6
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_2 GPIOH2
gpio_set LED_POSTCODE_2 0

# LED_POSTCODE_3: H3 (59)
# To use GPIOH3, SCU94[6], and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 94) 6
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_3 GPIOH3
gpio_set LED_POSTCODE_3 0

# LED_POSTCODE_4: H4 (60)
# To use GPIOH4, SCU94[7], and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 94) 7
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_4 GPIOH4
gpio_set LED_POSTCODE_4 0

# LED_POSTCODE_5: H5 (61)
# To use GPIOH5, SCU94[7], and SCU90[7] must be 0
devmem_clear_bit $(scu_addr 94) 7
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_5 GPIOH5
gpio_set LED_POSTCODE_5 0

# LED_POSTCODE_6: H6 (62)
# To use GPIOH6, SCU90[7] must be 0
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_6 GPIOH6
gpio_set LED_POSTCODE_6 0

# LED_POSTCODE_7: H7 (63)
# To use GPIOH7, SCU90[7] must be 0
devmem_clear_bit $(scu_addr 90) 7

gpio_export LED_POSTCODE_7 GPIOH7
gpio_set LED_POSTCODE_7 0

# BOARD_REV_0: J0 (72), BOARD_REV_1: J1 (73), BOARD_REV_2: J2 (74)
# To use GPIOJ0, SCU84[8] must be 0
devmem_clear_bit $(scu_addr 84) 8
# To use GPIOJ1, SCU84[9] must be 0
devmem_clear_bit $(scu_addr 84) 9
# To use GPIOJ2, SCU84[10] must be 0
devmem_clear_bit $(scu_addr 84) 10

gpio_export BOARD_REV_0 GPIOJ0
gpio_export BOARD_REV_1 GPIOJ1
gpio_export BOARD_REV_2 GPIOJ2

# PCIE_WAKE_N: J3 (75)
# To use GPIOJ3, SCU84[11] must be 0
devmem_clear_bit $(scu_addr 84) 11

gpio_export PCIE_WAKE_N GPIOJ3

# IOM_TYPE0: J4 (76)
# To use GPIOJ4, SCU84[12] must be 0
# To use GPIOJ4, SCU94[8] must be 0
devmem_clear_bit $(scu_addr 84) 12
devmem_clear_bit $(scu_addr 94) 8

gpio_export IOM_TYPE0 GPIOJ4

# IOM_TYPE1: J5 (77)
# To use GPIOJ5, SCU84[13] must be 0
# To use GPIOJ5, SCU94[8] must be 0
devmem_clear_bit $(scu_addr 84) 13
devmem_clear_bit $(scu_addr 94) 8

gpio_export IOM_TYPE1 GPIOJ5

# IOM_TYPE2: J6 (78)
# To use GPIOJ6, SCU84[14] must be 0
# To use GPIOJ6, SCU94[9] must be 0
devmem_clear_bit $(scu_addr 84) 14
devmem_clear_bit $(scu_addr 94) 9

gpio_export IOM_TYPE2 GPIOJ6

# IOM_TYPE3: J7 (79)
# To use GPIOJ7, SCU84[15] must be 0
# To use GPIOJ7, SCU94[9] must be 0
devmem_clear_bit $(scu_addr 84) 15
devmem_clear_bit $(scu_addr 94) 9

gpio_export IOM_TYPE3 GPIOJ7

# BMC_RMT_HEARTBEAT(TACH): O0 (112)
# To use GPIOO0, SCU88[8] must be 0
devmem_clear_bit $(scu_addr 88) 8

gpio_export BMC_RMT_HEARTBEAT GPIOO0

# BMC_LOC_HEARTBEAT: O1 (113)
# To use GPIOO1, SCU88[9] must be 0
devmem_clear_bit $(scu_addr 88) 9

gpio_export BMC_LOC_HEARTBEAT GPIOO1
gpio_set BMC_LOC_HEARTBEAT 1

# FLA0_WP_N: O2 (114)
# To use GPIOO1, SCU88[10] must be 0
devmem_clear_bit $(scu_addr 88) 10

gpio_export FLA0_WP_N GPIOO2

# ENCL_FAULT_LED: O3 (115)
# To use GPIOO1, SCU88[11] must be 0
devmem_clear_bit $(scu_addr 88) 11

gpio_export ENCL_FAULT_LED GPIOO3
gpio_set ENCL_FAULT_LED 0

# SCC_LOC_HEARTBEAT: O4 (116)
# To use GPIOO4, SCU88[12] must be 0
devmem_clear_bit $(scu_addr 88) 12

gpio_export SCC_LOC_HEARTBEAT GPIOO4

# SCC_RMT_HEARTBEAT: O5 (117)
# To use GPIOO5, SCU88[13] must be 0
devmem_clear_bit $(scu_addr 88) 13

gpio_export SCC_RMT_HEARTBEAT GPIOO5

# COMP_POWER_FAIL_N: O6 (118)
# To use GPIOO6, SCU88[14] must be 0
devmem_clear_bit $(scu_addr 88) 14

gpio_export COMP_POWER_FAIL_N GPIOO6

# COMP_PWR_EN: O7 (119), PS
# To use GPIOO7, SCU88[15] must be 0
devmem_clear_bit $(scu_addr 88) 15

gpio_export COMP_PWR_EN GPIOO7
# set GPIOO7 WDT reset tolerance
gpio_tolerance_fun COMP_PWR_EN GPIOO7

# Disable GPIOP internal pull down
devmem_set_bit $(scu_addr 8C) 31

# DEBUG_GPIO_BMC_6: P0 (120)
# To use GPIOP0, SCU88[16] must be 0
devmem_clear_bit $(scu_addr 88) 16

gpio_export DEBUG_GPIO_BMC_6 GPIOP0
gpio_set DEBUG_GPIO_BMC_6 1
# set GPIOP0 WDT reset tolerance
gpio_tolerance_fun DEBUG_GPIO_BMC_6 GPIOP0

# COMP_FAST_THROTTLE_N: P2 (122)
# To use GPIOP2, SCU88[18] must be 0
devmem_clear_bit $(scu_addr 88) 18

# TODO: Disable the CPU Throttle PIN to fix the leakage issue from BMC to ML
gpio_export COMP_FAST_THROTTLE_N GPIOP2

# DEBUG_PWR_BTN_N: P3 (123)
# To use GPIOP3, SCU88[19] must be 0
devmem_clear_bit $(scu_addr 88) 19

gpio_export DEBUG_PWR_BTN_N GPIOP3

# DEBUG_GPIO_BMC_2: P4 (124)
# To use GPIOP4, SCU88[20] must be 0
# To use GPIOP4, SCU90[28] must be 0
devmem_clear_bit $(scu_addr 88) 20
devmem_clear_bit $(scu_addr 90) 28

gpio_export DEBUG_GPIO_BMC_2 GPIOP4
gpio_set DEBUG_GPIO_BMC_2 1
# set GPIOP4 WDT reset tolerance
gpio_tolerance_fun DEBUG_GPIO_BMC_2 GPIOP4

# DEBUG_GPIO_BMC_3: P5 (125)
# To use GPIOP4, SCU88[20] must be 0
# To use GPIOP5, SCU88[21] must be 0
devmem_clear_bit $(scu_addr 88) 21
devmem_clear_bit $(scu_addr 90) 28

gpio_export DEBUG_GPIO_BMC_3 GPIOP5

# DEBUG_GPIO_BMC_4: P6 (126)
# To use GPIOP4, SCU88[20] must be 0
# To use GPIOP6, SCU88[22] must be 0
devmem_clear_bit $(scu_addr 88) 22
devmem_clear_bit $(scu_addr 90) 28

gpio_export DEBUG_GPIO_BMC_4 GPIOP6

# DEBUG_GPIO_BMC_5: P7 (127)
# To use GPIOP4, SCU88[20] must be 0
# To use GPIOP7, SCU88[23] must be 0
devmem_clear_bit $(scu_addr 88) 23
devmem_clear_bit $(scu_addr 90) 28

gpio_export DEBUG_GPIO_BMC_5 GPIOP7

# DB_PRSNT_BMC_N: Q6 (134)
# To use GPIOQ6, SCU2C[1] must be 0
devmem_clear_bit $(scu_addr 2C) 1

gpio_export DB_PRSNT_BMC_N GPIOQ6

# EXP_UART_EN: S0 (144)
# To use GPIOS0, SCU8C[0] must be 0
devmem_clear_bit $(scu_addr 8C) 0

gpio_export EXP_UART_EN GPIOS0
gpio_set EXP_UART_EN 0

# UART_SEL_IN: S1 (145)
# To use GPIOS1, SCU8C[1] must be 0
devmem_clear_bit $(scu_addr 8C) 1

gpio_export UART_SEL_IN GPIOS1

# BMC_GPIOS4: S4 (148)
# To use GPIOS4, SCU8C[4] must be 0
devmem_clear_bit $(scu_addr 8C) 4

gpio_export BMC_GPIOS4 GPIOS4

# BMC_GPIOS5: S5 (149)
# To use GPIOS5, SCU8C[5] must be 0
devmem_clear_bit $(scu_addr 8C) 5

gpio_export BMC_GPIOS5 GPIOS5

# BMC_GPIOS6: S6 (150)
# To use GPIOS6, SCU8C[6] must be 0
devmem_clear_bit $(scu_addr 8C) 6

gpio_export BMC_GPIOS6 GPIOS6

# BMC_GPIOS7: S7 (151)
# To use GPIOS7, SCU8C[7] must be 0
devmem_clear_bit $(scu_addr 8C) 7

gpio_export BMC_GPIOS7 GPIOS7

# Disable Z0~AB1 Function 3-4
# To use NOR Function, SCU90[31] must be 0
devmem_clear_bit $(scu_addr 90) 31

# DEBUG_RST_BTN_N: Z0 (200)
# To use GPIOZ0, SCUA4[16], and SCU70[19] must be 0
devmem_clear_bit $(scu_addr A4) 16
devmem_clear_bit $(scu_addr 70) 19

gpio_export DEBUG_RST_BTN_N GPIOZ0

#Z1 = 1, Z2 = 0 to make IOM LED defualt OFF
# BMC_ML_PWR_YELLOW_LED: Z1 (201)
# To use GPIOZ1, SCUA4[17], and SCU70[19] must be 0
devmem_clear_bit $(scu_addr A4) 17
devmem_clear_bit $(scu_addr 70) 19

gpio_export BMC_ML_PWR_YELLOW_LED GPIOZ1
gpio_set BMC_ML_PWR_YELLOW_LED 1

# BMC_ML_PWR_BLUE_LED: Z2 (202)
# To use GPIOZ2, SCUA4[18], and SCU70[19] must be 0
devmem_clear_bit $(scu_addr A4) 18
devmem_clear_bit $(scu_addr 70) 19

gpio_export BMC_ML_PWR_BLUE_LED GPIOZ2
gpio_set BMC_ML_PWR_BLUE_LED 0

# PCIE_COMP_TO_IOM_RST_4: Z3 (203)
# To use GPIOZ3, SCUA4[19] must be 0
devmem_clear_bit $(scu_addr A4) 19

gpio_export PCIE_COMP_TO_IOM_RST_4 GPIOZ3

# BMC_GPIOZ4: Z4 (204)
# To use GPIOZ4, SCUA4[20] must be 0
devmem_clear_bit $(scu_addr A4) 20

gpio_export BMC_GPIOZ4 GPIOZ4

# BMC_GPIOZ5: Z5 (205)
# To use GPIOZ5, SCUA4[21] must be 0
devmem_clear_bit $(scu_addr A4) 21

gpio_export BMC_GPIOZ5 GPIOZ5

# BMC_GPIOZ6: Z6 (206)
# To use GPIOZ6, SCUA4[22] must be 0
devmem_clear_bit $(scu_addr A4) 22

gpio_export BMC_GPIOZ6 GPIOZ6

# BMC_GPIOZ7: Z7 (207)
# To use GPIOZ7, SCUA4[23] must be 0
devmem_clear_bit $(scu_addr A4) 23

gpio_export BMC_GPIOZ7 GPIOZ7

# COMP_RST_BTN_N: AA0 (208)
# To use GPIOAA0, SCUA4[24] must be 0
devmem_clear_bit $(scu_addr A4) 24

gpio_export COMP_RST_BTN_N GPIOAA0
gpio_set COMP_RST_BTN_N 1

# CONN_A_PRSNTA: AA1 (209)
# To use GPIOAA1, SCUA4[25] must be 0
devmem_clear_bit $(scu_addr A4) 25

gpio_export CONN_A_PRSNTA GPIOAA1

# CONN_B_PRSNTB_N: AA2 (210)
# To use GPIOAA2, SCUA4[26] must be 0
devmem_clear_bit $(scu_addr A4) 26

gpio_export CONN_B_PRSNTB_N GPIOAA2

# FM_TPM_PRSNT_N: AA3 (211)
# To use GPIOAA3, SCUA4[27] must be 0
devmem_clear_bit $(scu_addr A4) 27

gpio_export FM_TPM_PRSNT_N GPIOAA3

# BMC_SELF_HW_RST: AA4 (212) For TPM; don't need to implement

# IOM_FULL_PWR_EN: AA7 (215), PS
# To use GPIOAA7, SCUA4[31] must be 0
devmem_clear_bit $(scu_addr A4) 31

gpio_export IOM_FULL_PWR_EN GPIOAA7
# set GPIOAA7 WDT reset tolerance
gpio_tolerance_fun IOM_FULL_PWR_EN GPIOAA7

# IOM_LOC_INS_N: AB0 (216)
# To use GPIOAB0, SCUA8[0] must be 0
devmem_clear_bit $(scu_addr A8) 0

gpio_export IOM_LOC_INS_N GPIOAB0

# IOM_FULL_PGOOD: AB1 (217)
# To use GPIOAB1, SCUA8[1] must be 0
devmem_clear_bit $(scu_addr A8) 1

gpio_export IOM_FULL_PGOOD GPIOAB1

# BMC_SELF_HW_RST: AB2 (218)
# Set GPIOAB2 to funtion2:WDTRST1 , SCUA8[2] must be 1, and SCU94[1:0] must be 0
devmem_set_bit $(scu_addr A8) 2
devmem_clear_bit $(scu_addr 94) 0
devmem_clear_bit $(scu_addr 94) 1

# === PCA9555 GPIOs ===
# ML_INS_N: P00 (472)
pca9555_gpio_export ML_INS_N 0
# FAN_0_INS_N: P01 (473)
pca9555_gpio_export FAN_0_INS_N 1
# FAN_1_INS_N: P02 (474)
pca9555_gpio_export FAN_1_INS_N 2
# FAN_2_INS_N: P03 (475)
pca9555_gpio_export FAN_2_INS_N 3
# FAN_3_INS_N: P04 (476)
pca9555_gpio_export FAN_3_INS_N 4
# ANOTHER_IOM_INS_N: P05 (477)
pca9555_gpio_export ANOTHER_IOM_INS_N 5
# SCC_A_INS: P06 (478)
pca9555_gpio_export SCC_A_INS 6
# SCC_B_INS: P07 (479)
pca9555_gpio_export SCC_B_INS 7

# SCC_TYPE_0: P10 (480)
pca9555_gpio_export SCC_TYPE_0 8
# SCC_TYPE_1: P11 (481)
pca9555_gpio_export SCC_TYPE_1 9
# SCC_TYPE_2: P12 (482)
pca9555_gpio_export SCC_TYPE_2 10
# SCC_TYPE_3: P13 (483)
pca9555_gpio_export SCC_TYPE_3 11
# SCC_STBY_PGOOD: P14 (484)
pca9555_gpio_export SCC_STBY_PGOOD 12
# SCC_FULL_PGOOD: P15 (485)
pca9555_gpio_export SCC_FULL_PGOOD 13
# ML_PGOOD: P16 (486)
pca9555_gpio_export ML_PGOOD 14
# DRAWER_CLOSED_N: P17 (487)
pca9555_gpio_export DRAWER_CLOSED_N 15


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

# Get platform SKU
sh /usr/local/bin/check_pal_sku.sh > /dev/NULL
PAL_SKU=$?
echo $PAL_SKU > /tmp/system.bin

# Record the platform SKU in syslog
IOM_TYPE=$(($PAL_SKU & 0xF))
IOM_SLOTID=$(($(($PAL_SKU >> 4)) & 0x3))

if [ $IOM_TYPE -eq 1 ]; then
    if [ $IOM_SLOTID -eq 1 ]; then
        IOM_LOCAL="side A"
    elif [ $IOM_SLOTID -eq 2 ]; then
        IOM_LOCAL="side B"
    else
        IOM_LOCAL="unknown"
    fi
    logger -s -p user.info -t setup-platform "System: type 5, IOM location: $IOM_LOCAL"
 elif [ $IOM_TYPE -eq 2 ]; then
    logger -s -p user.info -t setup-platform "System: type 7"
 else
    logger -s -p user.info -t setup-platform "System: unknown"
 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:          setup-por
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Determine BMC Power On Reset flag
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

store_clear_por


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

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

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

# read the T2 ROV after the GPIOs are enabled
t2_rov() {
    local val0 val1 val2
    # Note that the values are *not* read in order.
    val0=$(cat /sys/class/gpio/gpio58/value 2>/dev/null)
    val1=$(cat /sys/class/gpio/gpio56/value 2>/dev/null)
    val2=$(cat /sys/class/gpio/gpio57/value 2>/dev/null)
    echo $((val0 | (val1 << 1) | (val2 << 2)))
}

rov=$(t2_rov)

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

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

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

# Only bounce the T2 if we actually need to modify the voltage
if [ $cur_volts -ne $target_volts ]; then
    # Set values before turning out output;  we're using "PCIE, then MCS"
    echo 1 > /sys/class/gpio/gpio42/value
    echo 1 > /sys/class/gpio/gpio43/value
    echo out > /sys/class/gpio/gpio42/direction
    echo out > /sys/class/gpio/gpio43/direction
    echo 0 > /sys/class/gpio/gpio16/value
    echo out > /sys/class/gpio/gpio16/direction
    # T2 is in reset;  note that this may cause NMI messages on the uServer,
    # which shouldn't be up anyway when this is first run.

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

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

    sleep 1

    # Let T2 come out of reset
    echo 1 > /sys/class/gpio/gpio16/value
    echo "T2 ROV value set based on $rov."
    sleep 2
    echo 0 > /sys/class/gpio/gpio42/value
    echo 0 > /sys/class/gpio/gpio43/value
else
    echo "T2 ROV already correctly set."
fi
# Bring back pmbus if necessary
if [ $reload -eq 0 ]; then
    modprobe pmbus
fi


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

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


#!/bin/sh
#This script is for checking if M.2 supports NVMe, by looking for 0x6a address on i2c bus

sleep 30

power_status=`/usr/local/bin/power-util server status | grep ON`

if [ ! -z "$power_status" ]; then
	M2_1_i2c_NVMe=`/usr/sbin/i2cdetect -y 7 | grep 6a`
	M2_2_i2c_NVMe=`/usr/sbin/i2cdetect -y 8 | grep 6a`

	if [ ! -z "$M2_1_i2c_NVMe" ]; then
      kv set M2_1_NVMe 1
	else
      kv set M2_1_NVMe 0
	fi

	if [ ! -z "$M2_2_i2c_NVMe" ]; then
      kv set M2_2_NVMe 1
	else
      kv set M2_2_NVMe 0
	fi
fi


#!/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
#
PWM_DIR=/sys/devices/platform/ast_pwm_tacho.0

set -e

# The PWM frequency is

#   clk_source / ((2 ^ division_h) * (2 * division_l) * (unit + 1))
#
# Our clk_source is 24Mhz.  4-pin fans are generally supposed to be driven with
# a 25Khz PWM control signal.  Therefore we want the divisor to equal 800.
#
# We also want the unit to be as large as possible, since this controls the
# granularity with which we can modulate the PWM signal.  The following
# settings allow us to set the fan from 0 to 100% in increments of 1/100th.
#
# The AST chip supports 3 different PWM clock configurations, but we only use
# type M for now.
echo 0 > $PWM_DIR/pwm_type_m_division_h
echo 4 > $PWM_DIR/pwm_type_m_division_l
echo 99 > $PWM_DIR/pwm_type_m_unit
# Set Type M tacho clock division to 4
echo 0 > $PWM_DIR/tacho_type_m_division

# On FBTTN, there are 4 fans (dual-rotate) connected on DPB.
# Each fan uses same PWM input.
# Tacho output is used for BMC or SCC heartbeat.
# Here is the mapping between the fan and PWN/Tacho,
# staring from the one from the edge
# Fan 0: PWM 0
# Fan 1: PWM 0
# BMC Remote Heartbeat: PWM 0, Type M, Tacho0
# SCC Local Heartbeat:  PWM 0, Type M, Tacho4
# SCC Remote Heartbeat: PWM 0, Type M, Tacho5

# For each fan, setting the type, and 100% initially
# PWM0, PWM1: Type M
for pwm in 0 1; do
    echo 0 > $PWM_DIR/pwm${pwm}_type
    echo 0 > $PWM_DIR/pwm${pwm}_rising
    echo 0 > $PWM_DIR/pwm${pwm}_falling
    echo 1 > $PWM_DIR/pwm${pwm}_en
done

# Enable Tach 0, 4, 5
for tach in 0 4 5; do
    echo 0 > $PWM_DIR/tacho${tach}_source
    echo 1 > $PWM_DIR/tacho${tach}_en
done


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

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

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

echo -n "Setup fscd config... "

default_fsc_config_path="/etc/fsc-config.json"
fsc_type5_config_path="/etc/FSC_BC_Type5_MP_v4_config.json"
fsc_type7_config_path="/etc/FSC_BC_Type7_MP_v4_config.json"
IOM_M2=1      # IOM type: M.2
IOM_IOC=2     # IOM type: IOC

sh /usr/local/bin/check_pal_sku.sh > /dev/NULL
  PAL_SKU=$(($? & 0x3))
  if [ "$PAL_SKU" == "$IOM_M2" ]; then
    cp ${fsc_type5_config_path} ${default_fsc_config_path}
  elif [ "$PAL_SKU" == "$IOM_IOC" ]; then
  	cp ${fsc_type7_config_path} ${default_fsc_config_path}
  else
  	cp ${fsc_type5_config_path} ${default_fsc_config_path}
  	echo "Setup fscd: System type is unknown, using Type5 Config! Please confirm the system type."
  fi

/usr/local/bin/init_pwm.sh
/usr/local/bin/fan-util --set 50
runsv /etc/sv/fscd > /dev/null 2>&1 &
echo "done."


#!/bin/sh
#This script is for config checking and setting of fscd monitored sensor

/etc/check_M2_nvme.sh &


#!/bin/sh
exec /usr/local/bin/gpiod server


#!/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 fbttn... "
  runsv /etc/sv/gpiod > /dev/null 2>&1 &
echo "done."


#!/bin/sh
exec /usr/local/bin/ipmbd 11 11


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

### BEGIN INIT INFO
# Provides:          ipmbd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Provides ipmb message tx/rx service
#
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

echo "Starting IPMB Rx/Tx Daemon"

if [ $(is_server_prsnt 1) == "1" ]; then
  echo "enable Mono Lake IPMB on I2C 3"
  runsv /etc/sv/ipmbd_3 > /dev/null 2>&1 &
fi

echo "enable Expander IPMB on I2C 9"
  runsv /etc/sv/ipmbd_9 > /dev/null 2>&1 &

echo "enable Debug Card IPMB on I2C 11"
  runsv /etc/sv/ipmbd_11 > /dev/null 2>&1 &


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

SERVER_SDR_DONE_FILE="/tmp/sdr_server.done"
timeout_of_waiting_sdr=180  # Maximum wait 3 minutes then go ahead, because get SDR timeout is also 3 minutes.

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

# Check for the slots present and run sensord for those slots only.
SLOTS=
  if [ $(is_server_prsnt 1) == "1" ] && [ $(is_bic_ready 1) == "1" ]; then
    SLOTS="server"
  fi

SLOTS="iom nic $SLOTS"

wait_time=0
logger -p user.info -t sensord "waiting for server SDR being ready..."
until [ $wait_time -eq $timeout_of_waiting_sdr ]
do
  if [ -f $SERVER_SDR_DONE_FILE ]; then
    logger -p user.info -t sensord "server SDR is ready."
    rm -rf $SERVER_SDR_DONE_FILE
    break
  else
    sleep 1
    wait_time=$[$wait_time+1]
  fi
done
if [ $wait_time -eq $timeout_of_waiting_sdr ]; then
  logger -p user.warn -t sensord "server SDR is not ready."
fi

exec /usr/local/bin/sensord $SLOTS


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

### BEGIN INIT INFO
# Provides:          sensor-setup
# Required-Start:    power-on
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO

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

modprobe lm75
modprobe pmbus

# Enable the ADC inputs;  adc0 - adc11 are connected to various voltage sensors

echo 1 > /sys/devices/platform/ast_adc.0/adc0_en
echo 1 > /sys/devices/platform/ast_adc.0/adc1_en
echo 1 > /sys/devices/platform/ast_adc.0/adc2_en
echo 1 > /sys/devices/platform/ast_adc.0/adc3_en
echo 1 > /sys/devices/platform/ast_adc.0/adc4_en
echo 1 > /sys/devices/platform/ast_adc.0/adc5_en
echo 1 > /sys/devices/platform/ast_adc.0/adc6_en
echo 1 > /sys/devices/platform/ast_adc.0/adc7_en
echo 1 > /sys/devices/platform/ast_adc.0/adc8_en
echo 1 > /sys/devices/platform/ast_adc.0/adc9_en
echo 1 > /sys/devices/platform/ast_adc.0/adc10_en
echo 1 > /sys/devices/platform/ast_adc.0/adc11_en


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

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

# setup ADC channels

ADC_PATH="/sys/devices/platform/ast_adc.0"
# For FBTTN tyep V:
# channel 0:  r1:  7.87K; r2:  1.0K; v2: 0mv
# channel 1:  r1:   2.7K; r2:  1.0K; v2: 0mv
# channel 2:  r1:   1.4K; r2:  1.0K; v2: 0mv
# channel 3:  r1: 0.330K; r2:  1.0K; v2: 0mv
# channel 4:  r1: 0.866K; r2:  1.0K; v2: 0mv
# channel 5:  r1:   0.0K; r2:  1.0K; v2: 0mv
# channel 6:  r1:   0.0K; r2:  1.0K; v2: 0mv
# channel 7:  r1:   1.4K; r2:  1.0K; v2: 0mv
# channel 8:  r1: 0.330K; r2:  1.0K; v2: 0mv
# channel 9:  r1:   0.0K; r2:  1.0K; v2: 0mv	# fbttn type VII only
# channel 10: r1:   0.0K; r2:  1.0K; v2: 0mv	# fbttn type VII only
# channel 11: r1:   1.4K; r2:  1.0K; v2: 0mv	# fbttn type V only

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

config_adc 0 7870 1000 0
config_adc 1 2700 1000 0
config_adc 2 1400 1000 0
config_adc 3  330 1000 0
config_adc 4  866 1000 0
config_adc 5    0 1000 0
config_adc 6    0 1000 0
config_adc 7 1400 1000 0
config_adc 8  330 1000 0

# Detect GPIOF7 to recognize IOM type.
IOM_SKU=`cat /sys/class/gpio/gpio47/value`
if [ $IOM_SKU == "0" ]; then  # fbttn type V only
  config_adc 11 1400 1000 0
else                        # fbttn type VII only
  config_adc 9     0 1000 0
  config_adc 10    0 1000 0
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
. /usr/local/fbpackages/utils/ast-functions

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

DEF_PWR_ON=1
TO_PWR_ON=
POWER_ON_SLOT=
RETRY=0

DEBUG_ENABLE=false

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
}

check_por_config()
{
  TO_PWR_ON=-1
  local PWR_ON_RST
  local LAST_STATE

  PWR_ON_RST=$(/usr/bin/kv get slot${1}_por_cfg persistent)

  if [ -z "${PWR_ON_RST}" ]; then       # File not exist
    TO_PWR_ON=$DEF_PWR_ON
  elif [ "$PWR_ON_RST" == "on" ]; then    # Case ON
    TO_PWR_ON=1;
  elif [ "$PWR_ON_RST" == "off" ]; then   # Case OFF
    TO_PWR_ON=0;
  elif [ "$PWR_ON_RST" == "lps" ]; then   # Case LPS
    LAST_STATE=$(/usr/bin/kv get pwr_server${1}_last_state persistent)
    if [ -z "${LAST_STATE}" ]; then
      TO_PWR_ON=$DEF_PWR_ON
    elif [ "$LAST_STATE" == "on" ]; then
      TO_PWR_ON=1;
    elif [ "$LAST_STATE" == "off" ]; then
      TO_PWR_ON=0;
    else
      echo "unexpected value in pwr_server${1}_last_state"
    fi
  else
    echo "unexpected value in slot${1}_por_cfg"
  fi
}

remove_gp_m2_info() {
  for i in {2..13};
  do
    /usr/bin/kv del sys_config/fru2_m2_${i}_info persistent > /dev/null 2>&1
    /usr/bin/kv del sys_config/fru4_m2_${i}_info persistent > /dev/null 2>&1
  done
}

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
  # Remove all GP/GPv2 info
  remove_gp_m2_info

  # Wait for NTP sync
  update_ntp_config_from_kv
  ntp_sync_delay

  # check POR config for each slot
  for i in $(seq 1 4)
  do
    check_por_config ${i}
    if [ $TO_PWR_ON -eq 1 ] && [ $(is_server_prsnt ${i}) == "1" ] && [ $(get_slot_type ${i}) == "0" ] ; then
      POWER_ON_SLOT+=(${i})
      power-util slot${i} on &
    fi
  done

  # Wait for all slots to finish power-up.
  wait

fi


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

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

echo -n "Setup Caching for Bridge IC info.."
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 1) == "1" ]] && [[ $(get_slot_type 1) == "0" || $(get_slot_type 1) == "4" ]]; then
  /usr/local/bin/bic-cached 1 > /dev/null 2>&1 &
fi
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 2) == "1" ]] && [[ $(get_slot_type 2) == "0" || $(get_slot_type 2) == "4" ]]; then
  /usr/local/bin/bic-cached 2 > /dev/null 2>&1 &
fi
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 3) == "1" ]] && [[ $(get_slot_type 3) == "0" || $(get_slot_type 3) == "4" ]]; then
  /usr/local/bin/bic-cached 3 > /dev/null 2>&1 &
fi
#Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
#get_slot_type is to get slot type to check if the slot type is server
if [[ $(is_server_prsnt 4) == "1" ]] && [[ $(get_slot_type 4) == "0" || $(get_slot_type 4) == "4" ]]; then
  /usr/local/bin/bic-cached 4 > /dev/null 2>&1 &
fi

echo "done."


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

### BEGIN INIT INFO
# Provides:          setup_bios_util
# Required-Start:    board-id
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup bios-util config
### END INIT INFO

echo -n "Setup bios-util config... "

bios_util_config_path="/usr/local/fbpackages/bios-util/bios_support.json"
fby2_bios_config_path="/usr/local/fbpackages/bios-util/BIOS_UTIL_FBY2.json"

cp -rf ${fby2_bios_config_path} ${bios_util_config_path}

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
#

ME_UTIL="/usr/local/bin/me-util"
BIC_UTIL="/usr/bin/bic-util"
FRUID_UTIL="/usr/local/bin/fruid-util"
FW_UTIL="/usr/bin/fw-util"
SLOT_NAME=$1
INTERFACE="PECI_INTERFACE"

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

case $SLOT_NAME in
    slot1)
      SLOT_NUM=1
      ;;
    slot2)
      SLOT_NUM=2
      ;;
    slot3)
      SLOT_NUM=3
      ;;
    slot4)
      SLOT_NUM=4
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      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"

# check if auto crashdump is already running
if [ -f $PID_FILE ]; then
  echo "Another auto crashdump for $SLOT_NAME is running"
  exit 1
else
  touch $PID_FILE
fi

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

DUMP_SCRIPT="/usr/local/bin/dump.sh"
CRASHDUMP_FILE="/mnt/data/crashdump_$SLOT_NAME"
CRASHDUMP_LOG_ARCHIVE="/mnt/data/crashdump_$SLOT_NAME.tar.gz"
LOG_MSG_PREFIX=""

DWR=0
SECOND_DUMP=0
DELAY_SEC=30

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

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

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 "${LOG_MSG_PREFIX}Crashdump for FRU: $SLOT_NUM started"

#HEADER LINE for the dump
$DUMP_SCRIPT "time" > $CRASHDUMP_FILE

# Get BMC version & hostname
strings /dev/mtd0 | grep 2016.07 >> $CRASHDUMP_FILE
uname -a >> $CRASHDUMP_FILE
cat /etc/issue >> $CRASHDUMP_FILE

# Get fw info
echo "Get firmware version info: " >> "$CRASHDUMP_FILE"
RES=$("$FW_UTIL" "$SLOT_NAME" "--version")
echo "$RES" >> "$CRASHDUMP_FILE"

# Get FRUID info
echo "Get FRUID Info:" >> $CRASHDUMP_FILE
RES=$($FRUID_UTIL $SLOT_NAME)
echo "$RES" >> $CRASHDUMP_FILE

# Get Device ID
echo "Get Device ID:" >> $CRASHDUMP_FILE
RES=$($ME_UTIL $SLOT_NAME 0x18 0x01)
echo "$RES" >> $CRASHDUMP_FILE

echo "Get CPU Microcode Update Revision:" >> "$CRASHDUMP_FILE"
RES="$($BIC_UTIL $SLOT_NAME 0xe0 0x29 0x15 0xa0 0x00 0x30 0x05 0x05 0xa1 0x00 0x00 0x04 0x00)"
echo "$RES" >> "$CRASHDUMP_FILE"
RET=$?

if [ "$RET" -eq "0" ] && [ "${RES:0:11}" == "15 A0 00 40" ]; then
  echo "Use BIC wired PECI interface"
  INTERFACE="PECI_INTERFACE"
else
  echo "Use PECI through ME interface due to BIC wired PECI abnormal"
  INTERFACE="ME_INTERFACE"
fi

# Major Firmware Revision
REV=$(echo $RES| awk '{print $3;}')
# Check whether the first parameter is numeric or not
# If not, then it is an error message from me-util
is_numeric $(echo $RES| awk '{print $1;}')
if [ "$?" == 1 ] ;then
  Mode=$(echo $(($REV & 0x80)))
  if [ $Mode -ne 0 ] ;then
    echo "Device firmware update or Self-initialization in progress or Firmware in the recovery boot-loader mode" >> $CRASHDUMP_FILE
  fi
  # Get Self-test result
  echo "Get Self-test result:" >> $CRASHDUMP_FILE
  RES=$($ME_UTIL $SLOT_NAME 0x18 0x04)
  echo "$RES" >> $CRASHDUMP_FILE
  CC=$(echo $RES| awk '{print $1;}')
  # Check whether the first parameter is numeric or not
  # If not, then it is an error message from me-util
  is_numeric $CC
  if [ "$?" == 1 ] ;then
    if [ $CC -ne 55 ] ;then
      if [ $CC -eq 56 ] ;then
        echo "Self Test function not implemented in this controller" >> $CRASHDUMP_FILE
      elif [ $CC -eq 57 ] ;then
        echo "Corrupted or inaccessible data or devices" >> $CRASHDUMP_FILE
      elif [ $CC -eq 58 ] ;then
        echo "Fatal hardware error" >> $CRASHDUMP_FILE
      elif [ $CC -eq 80 ] ;then
        echo "PSU Monitoring service error" >> $CRASHDUMP_FILE
      elif [ $CC -eq 81 ] ;then
        echo "Firmware entered Recovery boot-loader mode" >> $CRASHDUMP_FILE
      elif [ $CC -eq 82 ] ;then
        echo "HSC Monitoring service error" >> $CRASHDUMP_FILE
      elif [ $CC -eq 83 ] ;then
        echo "Firmware entered non-UMA restricted mode of operation" >> $CRASHDUMP_FILE
      else
        echo "Unknown error" >> $CRASHDUMP_FILE
      fi
    fi
  fi
fi

# Sensors & sensor thresholds
echo "Sensor history at dump:" >> $CRASHDUMP_FILE 2>&1
$DUMP_SCRIPT $SLOT_NAME "sensors" >> $CRASHDUMP_FILE
echo "Sensor threshold at dump:" >> $CRASHDUMP_FILE 2>&1
$DUMP_SCRIPT $SLOT_NAME "threshold" >> $CRASHDUMP_FILE

echo COREID dump
date
echo "Dumping coreid.."
"$DUMP_SCRIPT" "$SLOT_NAME" "coreid" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping pcu.."
"$DUMP_SCRIPT" "$SLOT_NAME" "pcu" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping ubox.."
"$DUMP_SCRIPT" "$SLOT_NAME" "ubox" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping pcie.."
"$DUMP_SCRIPT" "$SLOT_NAME" "pcie" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping iio.."
"$DUMP_SCRIPT" "$SLOT_NAME" "iio" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping imc.."
"$DUMP_SCRIPT" "$SLOT_NAME" "imc" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping mesh.."
"$DUMP_SCRIPT" "$SLOT_NAME" "mesh" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping upi.."
"$DUMP_SCRIPT" "$SLOT_NAME" "upi" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping uncore.."
"$DUMP_SCRIPT" "$SLOT_NAME" "uncore" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping tor.."
"$DUMP_SCRIPT" "$SLOT_NAME" "tor" $INTERFACE >> "$CRASHDUMP_FILE"
date
echo "Dumping core mca.."
#MSR dump (core MCA)
"$DUMP_SCRIPT" "$SLOT_NAME" "msr" $INTERFACE >> "$CRASHDUMP_FILE"

# only second/dwr autodump need to rename accordingly
if [ "$DWR" == "1" ] || [ "$SECOND_DUMP" == "1" ]; then
  # dwr
  $DUMP_SCRIPT $SLOT_NAME  "dwr" $INTERFACE >> $CRASHDUMP_FILE

  # rename the archieve file based on whether dump in DWR mode or not
  if [ "$?" == "2" ]; then
    CRASHDUMP_LOG_ARCHIVE="/mnt/data/crashdump_dwr_$SLOT_NAME.tar.gz"
    LOG_MSG_PREFIX="DWR "
  else
    CRASHDUMP_LOG_ARCHIVE="/mnt/data/crashdump_second_$SLOT_NAME.tar.gz"
    LOG_MSG_PREFIX="SECOND_DUMP "
  fi
fi

echo -n "Auto Dump End at " >> $CRASHDUMP_FILE
date >> $CRASHDUMP_FILE

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

# Remove current pid file
rm $PID_FILE

echo "${LOG_MSG_PREFIX}Auto Dump Stored in $CRASHDUMP_LOG_ARCHIVE"
exit 0


#!/bin/bash

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

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

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

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

  cat | \
    sed 's/^0x/0xe0 0x29 0x15 0xa0 0x00 0x/g' $1 | \
      "$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
}

function print_find_core_id {
  printf "0x30 0x05 0x09 0xB1 0x00 0x%02x 0x10 0x04\n"
}

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

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

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

function pcie_dump {

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

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

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

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

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

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

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

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

function uncore_mca_dump {
  echo
  echo find uncore MCA dump core id
  echo =============================
  echo
  for (( CORE_ID=0; CORE_ID<=35; CORE_ID++ )); do
    RES=$(echo "$(print_find_core_id $CORE_ID)" | execute_cmd)
    echo CORE ID = $CORE_ID
    echo $RES
    # Completion Code
    CC=$(echo $RES| awk '{print $1;}')
    # Success Get CORED ID
    if [ "$CC" == "40" ]; then
      [ -r $CMD_DIR/crashdump_uncore ] && \
      cat $CMD_DIR/crashdump_uncore | execute_cmd
      return $CORE_ID
    else
      printf "CORE_ID 0x%02x CC 0x%s\n" $CORE_ID $CC
      if [ "$CORE_ID" == "35" ]; then
        # No CORED ID (Processor ID at EDS file) is valid
        echo
        echo  Fail to dump uncore MCA since no Processor ID is found
        echo
      fi
    fi
  done
}

function tor_dump {
  echo
  echo TOR dump:
  echo =================
  echo
  echo "0x30 0x0a 0x01 0xa5 0x00 0x80 0x03 0x00 0x02 0x00 0x03 0x00" | execute_cmd
  echo "0x30 0x0a 0x01 0xa5 0x00 0x80 0x01 0x00 0x00 0x00 0x00 0x80" | execute_cmd
  echo "0x30 0x0a 0x01 0xa5 0x00 0x80 0x01 0x30 0x00 0x00 0x00 0x00" | execute_cmd
  # get number of read value N
  RES=$(echo "0x30 0x05 0x05 0xa1 0x00 0x80 0x02 0x00" | execute_cmd)
  echo $RES
  # Completion Code
  CC=$(echo $RES| awk '{print $1;}')
  MAX_NUM=2015 # for tesing (2020-5= 2015)
  # Success
  if [ "$CC" == "40" ]; then
    echo "start TOR dump"
    for (( NUM=0; NUM<$MAX_NUM; NUM++ )); do
      echo "0x30 0x0a 0x01 0xa5 0x00 0x80 0x01 0x30 0x00 0x00 0x00 0x00" | execute_cmd
    done
  else
    echo "get TOR dump number fail"
  fi
  echo "0x30 0x0a 0x01 0xa5 0x00 0x80 0x04 0x00 0x02 0x00 0x03 0x00" | execute_cmd
}

function dwr_dump {

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

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

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

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

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

function print_dump_help {
  echo "$0 <slot#> coreid <INTERFACE>   ==> for CPU CoreID"
  echo "$0 <slot#> msr    <INTERFACE>   ==> for Core MCA"
  echo "$0 <slot#> pcu    <INTERFACE>   ==> for PCU"
  echo "$0 <slot#> ubox   <INTERFACE>   ==> for UBOX"
  echo "$0 <slot#> iio    <INTERFACE>   ==> for IIO"
  echo "$0 <slot#> imc    <INTERFACE>   ==> for IMC, MC, Mmap"
  echo "$0 <slot#> mesh   <INTERFACE>   ==> for Mesh"
  echo "$0 <slot#> upi    <INTERFACE>   ==> for UPI"
  echo "$0 <slot#> uncore <INTERFACE>   ==> for Uncore MCA"
  echo "$0 <slot#> sensors              ==> for sensor history"
  echo "$0 <slot#> threshold            ==> for sensor threshold"
  echo "$0 <slot#> pcie   <INTERFACE>   ==> for PCIe"
  echo "$0 <slot#> tor    <INTERFACE>   ==> for TOR dump"
  echo "$0 <slot#> dwr    <INTERFACE>   ==> for DWR check"
  exit 1
}

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

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

echo ""

if [ "$2" = "coreid" ]; then
  [ -r $CMD_DIR/crashdump_coreid ] && \
      cat $CMD_DIR/crashdump_coreid | execute_cmd
elif [ "$2" = "msr" ]; then # core MCA
  [ -r $CMD_DIR/crashdump_msr ] && \
      cat $CMD_DIR/crashdump_msr | execute_cmd
elif [ "$2" = "pcu" ]; then
  [ -r $CMD_DIR/crashdump_pcu ] && \
      cat $CMD_DIR/crashdump_pcu | execute_cmd
elif [ "$2" = "ubox" ]; then
  [ -r $CMD_DIR/crashdump_ubox ] && \
      cat $CMD_DIR/crashdump_ubox | execute_cmd
elif [ "$2" = "iio" ]; then
  [ -r $CMD_DIR/crashdump_iio ] && \
      cat $CMD_DIR/crashdump_iio | execute_cmd
elif [ "$2" = "imc" ]; then
  [ -r $CMD_DIR/crashdump_imc ] && \
      cat $CMD_DIR/crashdump_imc | execute_cmd
elif [ "$2" = "mesh" ]; then
  [ -r $CMD_DIR/crashdump_mesh ] && \
      cat $CMD_DIR/crashdump_mesh | execute_cmd
elif [ "$2" = "upi" ]; then
  [ -r $CMD_DIR/crashdump_upi ] && \
      cat $CMD_DIR/crashdump_upi | execute_cmd
elif [ "$2" = "uncore" ]; then
  uncore_mca_dump
elif [ "$2" = "sensors" ]; then
  /usr/local/bin/sensor-util $1 --history $SENSOR_HISTORY \
   && /usr/local/bin/sensor-util spb --history $SENSOR_HISTORY \
   && /usr/local/bin/sensor-util nic --history $SENSOR_HISTORY
elif [ "$2" = "threshold" ]; then
  /usr/local/bin/sensor-util $1 --threshold \
   && /usr/local/bin/sensor-util spb --threshold \
   && /usr/local/bin/sensor-util nic --threshold
elif [ "$2" = "pcie" ]; then
  [ -r $CMD_DIR/crashdump_pcie ] && \
    cat $CMD_DIR/crashdump_pcie | execute_cmd
    pcie_dump
elif [ "$2" = "tor" ]; then
  tor_dump
elif [ "$2" = "dwr" ]; then
  dwr_dump
  exit $?
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/sh

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

fan_check_delay=5
spb_type=$(get_spb_type)
kernel_ver=$(get_kernel_ver)

if [ $kernel_ver == 5 ]; then
    set_fan1='echo 180 > /sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/hwmon*/pwm1'
    set_fan2='echo 180 > /sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/hwmon*/pwm2'
    eval $set_fan1
    eval $set_fan2
    sleep $fan_check_delay
    if [ $spb_type == 1 ] ; then
        # for Yv2.50
        fan0_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan3_input`
        fan1_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan4_input`
    else
        fan0_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan1_input`
        fan1_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan2_input`
    fi
    #restore the initial value 100% (init_pwm.sh)
    reset_fan1='echo 255 > /sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/hwmon*/pwm1'
    reset_fan2='echo 255 > /sys/devices/platform/ahb/ahb:apb/1e786000.pwm-tacho-controller/hwmon/hwmon*/pwm2'
    eval $reset_fan1
    eval $reset_fan2
else
    echo 70 > /sys/devices/platform/ast_pwm_tacho.0/pwm0_falling
    echo 70 > /sys/devices/platform/ast_pwm_tacho.0/pwm1_falling
    sleep $fan_check_delay
    if [ $spb_type == 1 ] ; then
        # for Yv2.50
        fan0_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho2_rpm`
        fan1_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho3_rpm`
    else
        fan0_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho0_rpm`
        fan1_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho1_rpm`
    fi
    #restore the initial value 100% (init_pwm.sh)
    echo 0 > /sys/devices/platform/ast_pwm_tacho.0/pwm0_falling
    echo 0 > /sys/devices/platform/ast_pwm_tacho.0/pwm1_falling
fi

# default 10K_FAN
fan_config=0

if [ $fan0_rpm -ge "8000" ] || [ $fan1_rpm -ge "8000" ]; then
    # 15K_FAN
    fan_config=1
fi

echo "$fan_config" > /tmp/fan_config

# add SEL log for Northdome if 10K fan detected
if [ "$spb_type" -eq "3" ] && [ "$fan_config" -eq "0" ]; then
    logger -p user.crit "10K Fan detected"
fi


#!/bin/sh

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

sv stop fscd

FSCD_END_REASON=$1

SLOT_TYPE_GPV2=4
has_gpv2=0

pwm_value=100

spb_type=$(get_spb_type)
kernel_ver=$(get_kernel_ver)
if [ $kernel_ver == 5 ]; then
    if [ $spb_type == 1 ] ; then
        # for Yv2.50
        fan0_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan3_input`
        fan1_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan4_input`
    else
        fan0_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan1_input`
        fan1_rpm=`cat /sys/bus/platform/drivers/aspeed_pwm_tacho/1e786000.pwm-tacho-controller/hwmon/hwmon*/fan2_input`
    fi
else
    if [ $spb_type == 1 ] ; then
        # for Yv2.50
        fan0_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho2_rpm`
        fan1_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho3_rpm`
    else
        fan0_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho0_rpm`
        fan1_rpm=`cat /sys/devices/platform/ast_pwm_tacho.0/tacho1_rpm`
    fi
fi

if [ $fan0_rpm -le "500" ] || [ $fan1_rpm -le "500" ]; then
    # for fan fail condition
    pwm_value=100
else
    sku_type=0
    server_type=0
    for i in `seq 1 1 4`
    do
        tmp_sku=$(get_slot_type $i)
        if [ "$tmp_sku" == "$SLOT_TYPE_GPV2" ] ; then
            has_gpv2=1
        fi
        sku_type=$(($(($tmp_sku << $(($(($i*4)) - 4))))+$sku_type))
        tmp_server=$(get_server_type $i)
        server_type=$(($(($tmp_server << $(($(($i*4)) - 4))))+$server_type))
    done
    if [ "$has_gpv2" == "1" ] ; then
        # only for GPv2 case
        pwm_value=90
    elif [ "$sku_type" == "0" ] && [ "$server_type" == "17476" ] ; then
        # onle for FBND case
        pwm_value=95
    else
        pwm_value=100
    fi
fi

if [ "$FSCD_END_REASON" == "$FSCD_END_INVALID_CONFIG" ] ; then
    logger -p user.warning "Invalid fan config, fscd stopped, set fan speed to $pwm_value%"
else
    logger -p user.warning "SLED not seated, fscd stopped, set fan speed to $pwm_value%"
fi
/usr/local/bin/fan-util --set $pwm_value


#!/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
PWM_DIR=/sys/devices/platform/ast_pwm_tacho.0

set -e
kernel_ver=$(get_kernel_ver)
if [ $kernel_ver == 4 ]; then
    # The PWM frequency is

    #   clk_source / ((2 ^ division_) * (2 * division_l) * (unit + 1))
    #
    # Our clk_source is 24Mhz.  4-pin fans are generally supposed to be driven with
    # a 25Khz PWM control signal.  Therefore we want the divisor to equal 960.
    #
    # We also want the unit to be as large as possible, since this controls the
    # granularity with which we can modulate the PWM signal.  The following
    # settings allow us to set the fan from 0 to 100% in increments of 1/96th.
    #
    # The AST chip supports 3 different PWM clock configurations, but we only use
    # type M for now.
    echo 0 > $PWM_DIR/pwm_type_m_division_h
    echo 5 > $PWM_DIR/pwm_type_m_division_l
    echo 95 > $PWM_DIR/pwm_type_m_unit

    # On fby2, there are 2 fans connected.
    # Each fan uses same PWM input and provide one tacho output.
    # Here is the mapping between the fan and PWN/Tacho,
    # staring from the one from the edge
    # Fan 0: PWM 0, Tacho0
    # Fan 1: PWM 0, Tacho1
    #
    # On fby2.50, there are 4 fan connected.
    # Fan 0: PWM 0, Tacho0
    # Fan 1: PWM 0, Tacho2
    # Fan 2: PWM 0, Tacho1
    # Fan 3: PWM 0, Tacho3

    fan_num=0
fi

spb_type=$(get_spb_type)
fan_type=$(get_fan_type)

if [[ $spb_type == "1" && $fan_type == "0" ]]; then
    # 0 base
    fan_num=3
    echo "FAN CONFIG : Dual Rotor FAN"
else
    # 0 base
    fan_num=1
    echo "FAN CONFIG : Single Rotor FAN"
fi

if [ $kernel_ver == 4 ]; then
    # For each fan, setting the type, and 100% initially
    for pwm in 0 1; do
        echo 0 > $PWM_DIR/pwm${pwm}_type
        echo 0 > $PWM_DIR/pwm${pwm}_rising
        echo 0 > $PWM_DIR/pwm${pwm}_falling
        echo 1 > $PWM_DIR/pwm${pwm}_en
    done

    # Enable Tach
    if [[ $spb_type == "1" && $fan_type == "1" ]]; then
        echo 0 > $PWM_DIR/tacho2_source
        echo 0 > $PWM_DIR/tacho3_source
        echo 1 > $PWM_DIR/tacho2_en
        echo 1 > $PWM_DIR/tacho3_en
    else
        for tach in 0 $fan_num; do
            echo 0 > $PWM_DIR/tacho${tach}_source
        done
        t=0
        while [ $t -le $fan_num ]; do
            echo 1 > $PWM_DIR/tacho${t}_en
            t=$((t+1))
        done
    fi
fi


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

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

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

default_fsc_config_path="/etc/fsc-config.json"
default_aggregate_config_path="/etc/aggregate-sensor-conf.json"
sku_type=0
server_type=0
full_config=1
dev_type=0

DEV_TYPE_UNKNOWN=0
DEV_TYPE_SSD=1
DEV_TYPE_VSI_ACC=2
DEV_TYPE_BRCM_ACC=3
DEV_TYPE_OTHER_ACC=4
DEV_TYPE_DUAL_M2=5

FAN_CONFIG_10K=0
FAN_CONFIG_15K=1

SLOT_TYPE_GPV2=4
has_gpv2=0
invalid_gpv2_config=0
fan_config=0

function init_fsc_config_default()
{
  for i in `seq 1 1 4`
  do
    tmp_sku=$(get_slot_type $i)
    if [ "$tmp_sku" == "$SLOT_TYPE_GPV2" ] ; then
      has_gpv2=1
    fi
    sku_type=$(($(($tmp_sku << $(($(($i*4)) - 4))))+$sku_type))
    tmp_server=$(get_server_type $i)
    server_type=$(($(($tmp_server << $(($(($i*4)) - 4))))+$server_type))
  done

  case "$sku_type" in
    "0") # 4S
      case "$server_type" in
        "0")
          echo "Run FSC 4 TLs Config"
          cp /etc/FSC_FBY2_MP_4TL_config.json ${default_fsc_config_path}
        ;;
        "17476")
          echo "Run FSC 4 NDs Config"
          cp /etc/FSC_NORTHDOME_4ND_config.json ${default_fsc_config_path}
          if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
              cp /etc/aggregate-sensor-fbnd-conf.json ${default_aggregate_config_path}
          fi
        ;;
        *)
          echo "Unexpected 4 Servers config! Run FSC 4 TLs Config as default config"
          cp /etc/FSC_FBY2_MP_4TL_config.json ${default_fsc_config_path}
        ;;
      esac
    ;;
    "514") # 2S + 2GP
      if [[ $(get_server_type 2) == "0" && $(get_server_type 4) == "0" ]] ; then
        echo "Run FSC 2 GPs and 2 TLs Config"
        cp /etc/FSC_FBY2_PVT_2GP_2TL_config.json ${default_fsc_config_path}
      elif [[ $(get_server_type 2) == "4" && $(get_server_type 4) == "4" ]] ; then
        echo "Run FSC 4 NDs Config"
        cp /etc/FSC_NORTHDOME_4ND_config.json ${default_fsc_config_path}
        if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
          cp /etc/aggregate-sensor-fbnd-conf.json ${default_aggregate_config_path}
        fi
      else
        echo "Unexpected 2 GPs and 2 Servers config! Run FSC 2 GPs and 2 TLs Config as default config"
        cp /etc/FSC_FBY2_PVT_2GP_2TL_config.json ${default_fsc_config_path}
      fi
    ;;
    "257") # 2S + 2CF
      if [[ $(get_server_type 2) == "0" && $(get_server_type 4) == "0" ]] ; then
        echo "Run FSC 2 CFs and 2 TLs Config"
        cp /etc/FSC_FBY2_PVT_2CF_2TL_config.json ${default_fsc_config_path}
      elif [[ $(get_server_type 2) == "4" && $(get_server_type 4) == "4" ]] ; then
        echo "Run FSC 4 NDs Config"
        cp /etc/FSC_NORTHDOME_4ND_config.json ${default_fsc_config_path}
        if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
          cp /etc/aggregate-sensor-fbnd-conf.json ${default_aggregate_config_path}
        fi
      else
        echo "Unexpected 2 CFs and 2 Servers config! Run FSC 2 CFs and 2 TLs Config as default config"
        cp /etc/FSC_FBY2_PVT_2CF_2TL_config.json ${default_fsc_config_path}
      fi
    ;;
    "1028") # 2GPv2 +2TL
      echo "Run FSC 2 GPV2s and 2 TLs Config"
    ;;
    *)
      if [ "$has_gpv2" == "1" ] ; then
        invalid_gpv2_config=1
        echo "Unexpected GPv2 sku type!"
      else
        server_type_tmp="3"
        for i in 1 2 3 4 ; do
          server_type_tmp=$(get_server_type $i)
          if [ "$server_type_tmp" != "3" ] ; then
            break;
          fi
        done

        if [ "$server_type_tmp" == "4" ] ; then
          echo "Unexpected sku type! Use FSC 4 NDs Config as default config"
          cp /etc/FSC_NORTHDOME_4ND_config.json ${default_fsc_config_path}
          if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
            cp /etc/aggregate-sensor-fbnd-conf.json ${default_aggregate_config_path}
          fi
        else
          echo "Unexpected sku type! Use FSC 4 TLs Config as default config"
          cp /etc/FSC_FBY2_MP_4TL_config.json ${default_fsc_config_path}
        fi
      fi
    ;;
  esac

  if [ "$has_gpv2" == "1" ] ; then
    if [ "$dev_type" == "$DEV_TYPE_UNKNOWN" ] ; then
      fw_ver=$(/usr/bin/fw-util bmc --version fscd)
      if [[ $fw_ver =~ "fbgpv2" ]] ; then
        echo "Keep FSC config : $fw_ver"
        logger -p user.info "Keep FSC config : $fw_ver"
      else
        echo "Run default FSC for M.2 devices"
        logger -p user.info "Run default FSC for M.2 devices"
        if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
          cp /etc/FSC_FBGPV2_DVT_config.json ${default_fsc_config_path}
        else
          cp /etc/FSC_FBGPV2_10KFAN_DVT_config.json ${default_fsc_config_path}
        fi
      fi
    elif [ "$dev_type" == "$DEV_TYPE_SSD" ] ; then
      echo "Run FSC for SSD"
      logger -p user.info "Run FSC for SSD"
      if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
        cp /etc/FSC_FBGPV2_DVT_config.json ${default_fsc_config_path}
      else
        cp /etc/FSC_FBGPV2_10KFAN_DVT_config.json ${default_fsc_config_path}
      fi
    elif [ "$dev_type" == "$DEV_TYPE_VSI_ACC" ] ; then
      echo "Run FSC for VSI Accelerator"
      logger -p user.info "Run FSC for VSI Accelerator"
      if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
        cp /etc/FSC_FBGPV2_VSI_DVT_config.json ${default_fsc_config_path}
      else
        cp /etc/FSC_FBGPV2_VSI_10KFAN_DVT_config.json ${default_fsc_config_path}
      fi
    elif [ "$dev_type" == "$DEV_TYPE_BRCM_ACC" ] ; then
      echo "Run FSC for BRCM Accelerator"
      logger -p user.info "Run FSC for BRCM Accelerator"
      if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
        cp /etc/FSC_FBGPV2_BRCM_PVT_config.json ${default_fsc_config_path}
      else
        cp /etc/FSC_FBGPV2_BRCM_10KFAN_PVT_config.json ${default_fsc_config_path}
      fi
    else
      echo "Run default FSC for M.2 devices"
      logger -p user.info "Run default FSC for M.2 devices"
      if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
        cp /etc/FSC_FBGPV2_DVT_config.json ${default_fsc_config_path}
      else
        cp /etc/FSC_FBGPV2_10KFAN_DVT_config.json ${default_fsc_config_path}
      fi
    fi
    if [ $spb_type == 1 ] ; then
      # for Yv2.50
      if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
        cp /etc/aggregate-sensor-yv250-15kfan-conf.json ${default_aggregate_config_path}
        cp /etc/FSC_FBYV250_15KFAN_DVT_config.json ${default_fsc_config_path}
      else
        cp /etc/aggregate-sensor-yv250-10kfan-conf.json ${default_aggregate_config_path}
        cp /etc/FSC_FBYV250_10KFAN_DVT_config.json ${default_fsc_config_path}
      fi
    else
      if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
        cp /etc/aggregate-sensor-gpv2-conf.json ${default_aggregate_config_path}
      else
        cp /etc/aggregate-sensor-gpv2-10kfan-conf.json ${default_aggregate_config_path}
      fi
    fi
  fi
}

function init_fsc_config_yv2nd2()
{
  cp /etc/FSC_NORTHDOME_4ND_config.json ${default_fsc_config_path}
  cp /etc/aggregate-sensor-fbnd-conf.json ${default_aggregate_config_path}
  if [ "$fan_config" == "$FAN_CONFIG_15K" ] ; then
    cp /etc/aggregate-sensor-fbnd-conf.json ${default_aggregate_config_path}
  fi
}

function get_fan_config()
{
  if ! kv get setup_fan_config ; then
    logger -p user.warning "Setup fan config"
    /usr/local/bin/check_fan_config.sh
    kv set setup_fan_config 1
  fi

  fan_config=$(cat /tmp/fan_config)
}

if [ $# -eq 1 ] ; then
  dev_type=$1
fi

echo "Setup fan speed... "

spb_type=$(get_spb_type)

if [ $spb_type == 1 ] ; then
  for i in `seq 1 1 4`
  do
     if [ $(is_server_prsnt $i) == "0" ]; then
       echo "slot$i is empty"
       full_config=0
     fi
  done

  if [ $full_config -eq 0 ]; then
     echo "System is missing a slot - Unexpected sku type (system is not full config)!"
     /usr/local/bin/init_pwm.sh
     /usr/local/bin/fan-util --set 90
     exit 1
  fi
fi

/usr/local/bin/init_pwm.sh
get_fan_config

if [ $spb_type == 3 ]; then
  init_fsc_config_yv2nd2    # YV2ND2
else
  init_fsc_config_default   # original method
fi

/usr/local/bin/fan-util --set 70
runsv /etc/sv/fscd > /dev/null 2>&1 &
logger -p user.info "fscd started"

# Check SLED in/out
if [ $(gpio_get FAN_LATCH_DETECT H5) = 1 ]; then
   /usr/local/bin/fscd_end.sh $FSCD_END_SLED_OUT
elif [ "$invalid_gpv2_config" == "1" ] ; then
   /usr/local/bin/fscd_end.sh $FSCD_END_INVALID_CONFIG
else
   sv restart fscd
fi

echo "done."


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

# Check for the slots present and run sensord for those slots only.
SLOTS=
  #Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
  #get_slot_type is to get slot type to check if the slot type is server
  if [[ $(is_server_prsnt 1) == "1" && $(get_slot_type 1) == "0" ]]; then
    SLOTS="$SLOTS slot1"
  fi
  #Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
  #get_slot_type is to get slot type to check if the slot type is server
  if [[ $(is_server_prsnt 2) == "1" && $(get_slot_type 2) == "0" ]]; then
    SLOTS="$SLOTS slot2"
  fi
  #Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
  #get_slot_type is to get slot type to check if the slot type is server
  if [[ $(is_server_prsnt 3) == "1" && $(get_slot_type 3) == "0" ]]; then
    SLOTS="$SLOTS slot3"
  fi
  #Get slot type (0:TwinLakes, 1:Crace Flat, 2:Glacier Point 3:Empty Slot)
  #get_slot_type is to get slot type to check if the slot type is server
  if [[ $(is_server_prsnt 4) == "1" && $(get_slot_type 4) == "0" ]]; then
    SLOTS="$SLOTS slot4"
  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 fby2... "

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

echo "done."