#!/bin/bash
#
# Copyright 2018-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/bin/openbmc-utils.sh

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

prog="$0"

usage() {
    echo "Usage: $prog <command> [command options]"
    echo
    echo "Commands:"
    echo "  status: Get the current microserver power status"
    echo
    echo "  on: Power on microserver if not powered on already"
    echo "    options:"
    echo "      -f: Re-do power on sequence no matter if microserver has "
    echo "          been powered on or not."
    echo
    echo "  off: Power off microserver ungracefully"
    echo
    echo "  reset: Power reset microserver ungracefully"
    echo "    options:"
    echo "      -s: Power reset whole yamp system ungracefully"
    echo
    echo "  lcreset: Power-cycle one or all LC(s)"
    echo "    options:"
    echo "      -a  : Reset all LCs or "
    echo "      -1 , -2 , ... , -8 : Reset a single LC (1, 2 ... 8) "
    echo
}

do_status() {
    echo -n "Microserver power is "
    return_code=0

    if wedge_is_us_on; then
        echo "on"
    else
        echo "off"
        return_code=1
    fi

    return $return_code
}

do_on() {
    local force opt ret
    force=0
    while getopts "f" opt; do
        case $opt in
            f)
                force=1
                ;;
            *)
                usage
                exit -1
                ;;
        esac
    done
    echo -n "Power on microserver ..."
    if [ $force -eq 0 ]; then
        # need to check if uS is on or not
        if wedge_is_us_on; then
            echo " Already on. Skip!"
            return 1
        fi
    fi

    wedge_power_on_board
    ret=$?
    if [ $ret -eq 0 ]; then
        echo " Done"
        logger "Successfully power on micro-server"
    else
        echo " Failed"
        logger "Failed to power on micro-server"
    fi
    return $ret
}

do_off() {
    local ret
    echo -n "Power off microserver ..."
    wedge_power_off_board
    ret=$?
    if [ $ret -eq 0 ]; then
        echo " Done"
        logger "Successfully power off micro-server"
    else
        echo " Failed"
        logger "Failed to power off micro-server"
    fi
    return $ret
}

do_reset() {
    local system opt
    system=0
    while getopts "s" opt; do
        case $opt in
            s)
                system=1
                ;;
            *)
                usage
                exit -1
                ;;
        esac
    done
    if [ $system -eq 1 ]; then
        logger "Power reset the whole system ..."
        echo -n "Power reset the whole system ..."
        sleep 1
        echo 0xde > $PWR_SYSTEM_SYSFS
        sleep 8
        # The chassis shall be reset now... if not, we are in trouble
        echo " Failed"
        return 254
    else
        do_off
        sleep 1
        do_on
    fi
}

toggle_pim_reset() {
  pim=$1
  # First, put the PIMs we want into reset
  for slot in 1 2 3 4 5 6 7 8; do
    if [ $pim -eq 0 ] || [ $slot -eq $pim ]; then
      # Unlike Minipack, YAMP pim index is 1 based
      index=$slot
      # Unlike Minipack, YAMP uses GPIO
      echo Power-cycling LC in slot $slot
      echo out > /tmp/gpionames/LC${index}_LC_PWR_CYC/direction
      echo high > /tmp/gpionames/LC${index}_LC_PWR_CYC/direction
    fi
  done
  # Sleep 1 sec
  sleep 1
  # Finally, put the LC_PWR_CYC bit to 0
  for slot in 1 2 3 4 5 6 7 8; do
    if [ $pim -eq 0 ] || [ $slot -eq $pim ]; then
      # Unlike Minipack, YAMP pim index is 1 based
      index=$slot
      # Unlike Minipack, YAMP uses GPIO
      echo Putting LC in $slot into normal operation
      echo low > /tmp/gpionames/LC${index}_LC_PWR_CYC/direction
    fi
  done
}
do_pimreset() {
    local pim opt retval rc
    retval=0
    pim=-1
    while getopts "12345678a" opt; do
        case $opt in
            a)
                pim=0
                ;;
            1)
                pim=1
                ;;
            2)
                pim=2
                ;;
            3)
                pim=3
                ;;
            4)
                pim=4
                ;;
            5)
                pim=5
                ;;
            6)
                pim=6
                ;;
            7)
                pim=7
                ;;
            8)
                pim=8
                ;;
            *)
                usage
                exit -1
                ;;
        esac
    done
    if [ $pim -eq -1 ]; then
      usage
      exit -1
    fi

    toggle_pim_reset $pim

    return $retval
}

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

command="$1"
shift

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

exit $?


#!/bin/bash

# YAMP's SUP MAC address is the first address of the block
# allocated from "Extended MAC Base" field in SUP's EEPROM.
# That is, SUP's MAC address is the same as "Extended MAC Base"
# field in SUP's EEPROM
# Any error in reading SUP's EEPROM will result in empty output,
# which will be caught in the provisioning script.
weutil SUP 2>&1 | grep "MAC Base" | cut -d ' ' -f 4


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

### 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 lm90
modprobe max6697
modprobe pmbus
modprobe ucd9000


#!/bin/sh

### BEGIN INIT INFO
# Provides:		tcsd trousers
# Required-Start:	$local_fs $remote_fs $network
# Required-Stop:	$local_fs $remote_fs $network
# Should-Start:
# Should-Stop:
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	starts tcsd
# Description:		tcsd belongs to the TrouSerS TCG Software Stack
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/tcsd
NAME=tcsd
DESC="Trusted Computing daemon"
USER="root"

test -x "${DAEMON}" || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

case "${1}" in
	start)
		echo "Starting $DESC: "

    # For YAMP platform, it has separate TPM for BMC only - it's probed during
    # BMC start up. So there is no need for additional device probe / addition

    # Generate symlink for tcsd
    rm -f /dev/tpm
		if [ -e /dev/tpm0 ]
    then
      ln -s /dev/tpm0 /dev/tpm
    else
      ln -s /dev/tpm1 /dev/tpm
    fi

		start-stop-daemon --start --quiet --oknodo --pidfile /var/run/${NAME}.pid --user ${USER} --chuid ${USER} --exec ${DAEMON} -- ${DAEMON_OPTS}
		RETVAL="$?"
		echo "$NAME."
		[ "$RETVAL" = 0 ] && pidof $DAEMON > /var/run/${NAME}.pid
		exit $RETVAL
		;;

	stop)
		echo "Stopping $DESC: "

		start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/${NAME}.pid --user ${USER} --exec ${DAEMON}
		RETVAL="$?"
                echo  "$NAME."
		rm -f /var/run/${NAME}.pid
		exit $RETVAL
		;;

	restart|force-reload)
		"${0}" stop
		sleep 1
		"${0}" start
		exit $?
		;;
	*)
		echo "Usage: ${NAME} {start|stop|restart|force-reload|status}" >&2
		exit 3
		;;
esac

exit 0


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

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

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

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

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

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

echo "done."


#!/bin/sh
#
# Copyright 2016-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-confg
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: setup configure files.
# The file format should be src:dest
### END INIT INFO

ACTION="$1"
case "$ACTION" in
  start)
  if [ -e /etc/config ] && [ "$(ls -A /etc/config)" ]; then
    echo "Start to copy configure files...."
    for file in /etc/config/*
    do
       while IFS='' read -r line || [[ -n "$line" ]]; do
         src=$(echo $line | awk -F":" '{print $1}')
         dest=$(echo $line | awk -F":" '{print $2}')
         if [ -e $src ]; then
	         mkdir -p $dest
           cp -r $src $dest
         fi
       done < "$file"
    done
    echo "Copying is done."
  fi
esac

exit 0


#!/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:          fw_env_config
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Replace /etc/fw_env.config by /etc/fw_env.config.full
### END INIT INFO

ACTION="$1"
case "$ACTION" in
  start)
    CFG_FULL=`grep "Partition_000" /proc/mtd`
    if [ "$CFG_FULL" != "" ]; then
      cp /etc/fw_env.config.full /etc/fw_env.config
    fi
    ;;
esac

exit 0


#!/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:          mount_data0
# Required-Start:    mountvirtfs
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Mount data0 partition from flash chip.
# Description:
### END INIT INFO

. /etc/default/rcS

# Find out which device maps to 'data0' on mtd
# Note: /proc/mtd lists partitions using mtdX, where X is a number,
#       but we mount using /dev/mtdblockX. We'll do some magic here
#       to get the mtdX (char device) and mtdblockX (block device)
#       names.
MOUNT_POINT="/mnt/data"
DATA_CHAR_DEV=$(cat /proc/mtd | awk '{ if ($4 == "\"data0\"") print $1 }' |
  cut -d ':' -f 1 | awk '{ print "/dev/" $1 }')
if [ -z "$DATA_CHAR_DEV" ]
then
  echo "No data0 partition found. Not mounting anything to $MOUNT_POINT."
else
  DEVICE_ID=$(echo $DATA_CHAR_DEV | tail -c 2)
  DATA_BLOCK_DEV=${DATA_CHAR_DEV/mtd/mtdblock}

  echo "data0 partition found on $DATA_BLOCK_DEV; mounting to $MOUNT_POINT."
  mount -t jffs2 $DATA_BLOCK_DEV $MOUNT_POINT

  # if the mount failed, format the partition and remount
  if [ $? -ne 0 ]
  then
    echo "Mount failed; formatting $DATA_BLOCK_DEV and remounting."
    flash_eraseall $DATA_CHAR_DEV
    mount -t jffs2 $DATA_BLOCK_DEV $MOUNT_POINT
  fi
fi

: exit 0


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

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

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

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

check_por_config()
{

  TO_PWR_ON=-1

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

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

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

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

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

# Sync BMC's date with one of the four servers
sync_date()
{
  for i in 1 2 3 4
  do
    if [ $(is_server_prsnt $i) == "1" ] ; then
      # Use standard IPMI command 'get-sel-time' to read RTC time
      output=$(/usr/local/bin/me-util slot$i 0x28 0x48)
      # if the command fails, continue to next slot
      [ $(echo $output | wc -c) != 12 ] && continue
      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
      echo Syncing up BMC time with server$i...
      date $ts
      break
    fi
  done
}

# Check whether it is fresh power on reset
if [ $(is_bmc_por) -eq 1 ]; then

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

  sync_date

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

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

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

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


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

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

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

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

set -e

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

# Slot#1: GPIOM1(97),Slot#2: GPIOM0(96),Slot#3: GPIOM3(99),Slot#4: GPIOM2(98)
if [ $1 = "1" ]; then
    gpio=M1
elif [ $1 = "2" ]; then
    gpio=M0
elif [ $1 = "3" ]; then
    gpio=M3
elif [ $1 = "4" ]; then
    gpio=M2
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
# defined in other files.  We should probably move some more of the
# definitions to this file at some point.

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

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

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

# Set up to read the board revision pins, Y0, Y1, Y2
devmem_set_bit $(scu_addr 70) 19
devmem_clear_bit $(scu_addr a4) 8
devmem_clear_bit $(scu_addr a4) 9
devmem_clear_bit $(scu_addr a4) 10

gpio_export Y0
gpio_export Y1
gpio_export Y2

# SLOT1_PRSNT_N, GPIOH5 (61)
# GPIOH5(61): SCU90[6], SCU90[7] shall be 0
devmem_clear_bit $(scu_addr 90) 6
devmem_clear_bit $(scu_addr 90) 7

gpio_export H5

# SLOT2_PRSNT_N, GPIOH4 (60)
# GPIOH4(60): SCU90[6], SCU90[7] shall be 0
gpio_export H4

# SLOT3_PRSNT_N, GPIOH7 (63)
# GPIOH7(63): SCU90[6], SCU90[7] shall be 0
gpio_export H7

# SLOT4_PRSNT_N, GPIOH6 (62)
# GPIOH6(62): SCU90[6], SCU90[7] shall be 0
gpio_export H6

# BMC_PWR_BTN_IN_N, uServer power button in, on GPIO D0(24)
gpio_export D0

# PWR_SLOT1_BTN_N, 1S Server power out, on GPIO D3
# GPIOD3(27): SCU90[1], SCU8C[9], and SCU70[21] shall be 0
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 8c) 9
devmem_clear_bit $(scu_addr 70) 21

gpio_set D3 1

# PWR_SLOT2_BTN_N, 1S Server power out, on GPIO D1
# Make sure the Power Control Pin is Set properly
# GPIOD1(25): SCU90[1], SCU8C[8], and SCU70[21] shall be 0

devmem_clear_bit $(scu_addr 8c) 8

gpio_set D1 1

# PWR_SLOT3_BTN_N, 1S Server power out, on GPIO D7
# GPIOD7(31): SCU90[1], SCU8C[11], and SCU70[21] shall be 0
devmem_clear_bit $(scu_addr 8c) 11

gpio_set D7 1

# PWR_SLOT4_BTN_N, 1S Server power out, on GPIO D5
# GPIOD5(29): SCU90[1], SCU8C[10], and SCU70[21] shall be 0
devmem_clear_bit $(scu_addr 8c) 10

gpio_set D5 1

# SMB_SLOT0_NIC_ALERT_N, alert for 1S Server NIC I2C, GPIO B0
devmem_clear_bit $(scu_addr 80) 8

gpio_export B0

# Enable P3V3: GPIOS1(145)
# To use GPIOS1 (145), SCU8C[1], SCU94[0], and SCU94[1] must be 0
devmem_clear_bit $(scu_addr 8C) 1
devmem_clear_bit $(scu_addr 94) 0
devmem_clear_bit $(scu_addr 94) 1

gpio_set S1 1

# PWRGD_P3V3: GPIOS2(146)
# To use GPIOS2 (146), SCU8C[2], SCU94[0], and SCU94[1] must be 0
devmem_clear_bit $(scu_addr 8C) 2
devmem_clear_bit $(scu_addr 94) 0
devmem_clear_bit $(scu_addr 94) 1

# Setup GPIOs to Mux Enable: GPIOS3(147), Channel Select: GPIOE4(36), GPIOE5(37)

# To use GPIOS3 (147), SCU8C[3], SCU94[0], and SCU94[1] must be 0
devmem_clear_bit $(scu_addr 8C) 3
devmem_clear_bit $(scu_addr 94) 0
devmem_clear_bit $(scu_addr 94) 1

# To use GPIOE4 (36), 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

# To use GPIOE5 (37), 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_set S3 0
gpio_set E4 1
gpio_set E5 0

# BMC_HEARTBEAT_N, heartbeat LED, GPIO Q7(135)
devmem_clear_bit $(scu_addr 90) 28

gpio_set Q7 0

# USB_OC_N, resettable fuse tripped, GPIO Q6
devmem_clear_bit $(scu_addr 90) 28

gpio_export Q6

# System SPI
# Strap 12 must be 0 and Strape 13 must be 1
devmem_clear_bit $(scu_addr 70) 12
devmem_set_bit $(scu_addr 70) 13

# DEBUG_PORT_UART_SEL_BMC_N: GPIOR1(137)
# To use GPIOR1, SCU88[25] must be 0
devmem_clear_bit $(scu_addr 88) 25

gpio_export R1

# DEBUG UART Controls
# 4 signals: DEBUG_UART_SEL_0/1/2 and DEBUG_UART_RX_SEL_N
# GPIOE0 (32), GPIOE1 (33), GPIOE2 (34) and GPIOE3 (35)

# To enable 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_out E0

# To enable GPIOE1, SCU80[17], SCU8C[12], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 17

gpio_export_out E1

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

gpio_export_out E2

# To enable GPIOE3, SCU80[19], SCU8C[13], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 19

gpio_export_out E3

# Enable GPIOY3: BoardId(Yosemite or Test system)
devmem_clear_bit $(scu_addr a4) 11

# Power LED for Slot#2:
# To use GPIOM0 (96), SCU90[4], SCU90[5], and SCU84[24] must be 0
devmem_clear_bit $(scu_addr 90) 4
devmem_clear_bit $(scu_addr 90) 5
devmem_clear_bit $(scu_addr 84) 24

gpio_set M0 1

# Power LED for Slot#1:
# To use GPIOM1 (97), SCU90[4], SCU90[5], and SCU84[25] must be 0
devmem_clear_bit $(scu_addr 84) 25

gpio_set M1 1

# Power LED for Slot#4:
# To use GPIOM2 (98), SCU90[4], SCU90[5], and SCU84[26] must be 0
devmem_clear_bit $(scu_addr 84) 26

gpio_set M2 1

# Power LED for Slot#3:
# To use GPIOM3 (99), SCU90[4], SCU90[5], and SCU84[27] must be 0
devmem_clear_bit $(scu_addr 84) 27

gpio_set M3 1

# Identify LED for Slot#2:
# To use GPIOF0 (40), SCU80[24] must be 0
devmem_clear_bit $(scu_addr 80) 24

gpio_set F0 1

# Identify LED for Slot#1:
# To use GPIOF1 (41), SCU80[25], SCUA4[12], must be 0
devmem_clear_bit $(scu_addr 80) 25
devmem_clear_bit $(scu_addr A4) 12

gpio_set F1 1

# Identify LED for Slot#4:
# To use GPIOF2 (42), SCU80[26], SCUA4[13],  must be 0
devmem_clear_bit $(scu_addr 80) 26
devmem_clear_bit $(scu_addr A4) 13

gpio_set F2 1

# Identify LED for Slot#3:
# To use GPIOF3 (43), SCU80[27], SCUA4[14], must be 0
devmem_clear_bit $(scu_addr 80) 27
devmem_clear_bit $(scu_addr A4) 14

gpio_set F3 1

# Front Panel Hand Switch GPIO setup
# HAND_SW_ID1: GPIOR2(138)
# To use GPIOR2, SCU88[26] must be 0
devmem_clear_bit $(scu_addr 88) 26

gpio_export R2

# HAND_SW_ID2: GPIOR3(139)
# To use GPIOR3, SCU88[27] must be 0
devmem_clear_bit $(scu_addr 88) 27

gpio_export R3

# HAND_SW_ID4: GPIOR4(140)
# To use GPIOR4, SCU88[28] must be 0
devmem_clear_bit $(scu_addr 88) 28

gpio_export R4

# HAND_SW_ID8: GPIOR5(141)
# To use GPIOR5, SCU88[29] must be 0
devmem_clear_bit $(scu_addr 88) 29

gpio_export R5

# LED POST CODES: 8 GPIO signals

# LED_POSTCODE_0: GPIOG0 (48)
# To use GPIOG0, SCU84[0] must be 0
devmem_clear_bit $(scu_addr 84) 0

gpio_set G0 0

# LED_POSTCODE_1: GPIOG1 (49)
# To use GPIOG1, SCU84[1] must be 0
devmem_clear_bit $(scu_addr 84) 1

gpio_set G1 0

# LED_POSTCODE_2: GPIOG2 (50)
# To use GPIOG2, SCU84[2] must be 0
devmem_clear_bit $(scu_addr 84) 2

gpio_set G2 0

# LED_POSTCODE_3: GPIOG3 (51)
# To use GPIOG3, SCU84[3] must be 0
devmem_clear_bit $(scu_addr 84) 3

gpio_set G3 0

# LED_POSTCODE_4: GPIOP4 (124)
gpio_set P4 0

# LED_POSTCODE_5: GPIOP5 (125)
gpio_set P5 0

# LED_POSTCODE_6: GPIOP6 (126)
# To use GPIOP6, SCU88[22] must be 0
devmem_clear_bit $(scu_addr 88) 22

gpio_set P6 0

# LED_POSTCODE_7: GPIOP7 (127)
# To use GPIOP7, SCU88[23] must be 0
devmem_clear_bit $(scu_addr 88) 23

gpio_set P7 0

# BMC_READY_N: GPIOG6 (54)
# To use GPIOG6, SCU84[6] must be 0
devmem_clear_bit $(scu_addr 84) 6

gpio_set G6 0

# BMC_RST_BTN_IN_N: GPIOS0 (144)
# To use GPIOS0, SCU8C[0]
devmem_clear_bit $(scu_addr 8c) 0

gpio_export S0

# RESET for all Slots
# RST_SLOT1_SYS_RESET_N: GPIOH1 (57)
# To use GPIOH1, SCU90[6], SCU90[7] must be 0
devmem_clear_bit $(scu_addr 90) 6
devmem_clear_bit $(scu_addr 90) 7

gpio_set H1 1

# RST_SLOT2_SYS_RESET_N: GPIOH0 (56)
# To use GPIOH0, SCU90[6], SCU90[7] must be 0
gpio_set H0 1

# RST_SLOT3_SYS_RESET_N: GPIOH3 (59)
# To use GPIOH3, SCU90[6], SCU90[7] must be 0
gpio_set H3 1

# RST_SLOT4_SYS_RESET_N: GPIOH2 (58)
# To use GPIOH2, SCU90[6], SCU90[7] must be 0
gpio_set H2 1

# 12V_STBY Enable for Slots

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

gpio_set O5 1

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

gpio_set O4 1

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

gpio_set O7 1

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

gpio_set O6 1

# PWRGD_P12V_STBY_SLOT1: GPIOP1 (121)
# To use GPIOP1, SCU88[17] must be 0
devmem_clear_bit $(scu_addr 88) 17
gpio_export P1

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

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

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

# Enable the the EXTRST functionality of GPIOB7
devmem_set_bit $(scu_addr 80) 15
devmem_clear_bit $(scu_addr 90) 31
devmem_set_bit $(scu_addr 3c) 3

# Enable GPIO pins: I2C_SLOTx_ALERT_N pins for BIC firmware update
devmem_clear_bit $(scu_addr 88) 2
gpio_export N2
devmem_clear_bit $(scu_addr 88) 3
gpio_export N3
devmem_clear_bit $(scu_addr 88) 4
gpio_export N4
devmem_clear_bit $(scu_addr 88) 5
gpio_export N5

# Yosemite OOM remediation
#   enable kernel panic (force reboot)
echo 1 >> /proc/sys/vm/panic_on_oom


#!/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.."
  /usr/local/bin/front-paneld > /dev/null 2>&1 &
echo "done."


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

set -e

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

# 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 0..1
echo 0 > $PWM_DIR/tacho0_source
echo 1 > $PWM_DIR/tacho1_source

t=0
while [ $t -le 1 ]; do
    echo 1 > $PWM_DIR/tacho${t}_en
    t=$((t+1))
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 fan speed... "
/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
#
# 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 yosemite... "

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

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

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

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

/usr/local/bin/gpiod $SLOTS

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-gpiointrd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

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

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

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

echo "done."


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

### BEGIN INIT INFO
# Provides:          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"

runsv /etc/sv/ipmbd_3 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_1 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_7 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_5 > /dev/null 2>&1 &

if [ $(is_server_prsnt 1) == "0" ]; then
  sv stop ipmbd_3
fi

if [ $(is_server_prsnt 2) == "0" ]; then
  sv stop ipmbd_1
fi

if [ $(is_server_prsnt 3) == "0" ]; then
  sv stop ipmbd_7
fi

if [ $(is_server_prsnt 4) == "0" ]; then
  sv stop ipmbd_5
fi


#!/bin/sh

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


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

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

# 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="$SLOTS slot1"
  fi

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

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

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

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


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

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

# 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

# setup ADC channels

ADC_PATH="/sys/devices/platform/ast_adc.0"

# Enable the ADC inputs;  adc0 - adc7 are connected to various voltage sensors
#
#  adc0 P5V_STBY_SCALED           r1: 5.11k,   r2: 3.48k,  v2: 0mv
#  adc1 P12V_STBY_SCALED          r1: 5.11k,   r2: 1.02k,  v2: 0mv
#  adc2 P3V3_STBY_SCALED          r1: 5.11k,   r2: 8.25k,  v2: 0mv
#  adc3 P12V_STBY_SLOT2_SCALED    r1: 5.11k,   r2: 1.02k,  v2: 0mv
#  adc4 P12V_STBY_SLOT1_SCALED    r1: 5.11k,   r2: 1.02k,  v2: 0mv
#  adc5 P12V_STBY_SLOT4_SCALED    r1: 5.11k,   r2: 1.02k,  v2: 0mv
#  adc6 P12V_STBY_SLOT3_SCALED    r1: 5.11k,   r2: 1.02k,  v2: 0mv
#  adc7 P3V3_SCALED               r1: 5.11k,   r2: 8.25k,  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  5110  3480 0
config_adc 1  5110  1020 0
config_adc 2  5110  8250 0
config_adc 3  5110  1020 0
config_adc 4  5110  1020 0
config_adc 5  5110  1020 0
config_adc 6  5110  1020 0
config_adc 7  5110  8250 0

#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

i2cset -y -f 10 0x40 0xd4 0x3f1c w
rmmod adm1275
modprobe adm1275


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

# TODO: check for the if slot/server is present before starting the daemon
echo -n "Setup crond..."
  mkdir /etc/cron
  mkdir /etc/cron/crontabs
  echo "0 * * * * run-parts /etc/cron.hourly" > /etc/cron/crontabs/root
  echo "0 0 * * * run-parts /etc/cron.daily" >> /etc/cron/crontabs/root
  /etc/init.d/busybox-cron start
echo "done."


#!/bin/sh

# Config

EXPIRE_SECONDS=$(( 3600 * 3 ))

# Code
set -e

NOW="$(date +%s)"

is_expired() {
	IMAGE_DIRPATH="$1"; shift
	if [ -f "$IMAGE_DIRPATH"/image ]; then
		UPDATE_TS="$(stat -c %Y "$IMAGE_DIRPATH"/image)"
	else
		UPDATE_TS="$(stat -c %Y "$IMAGE_DIRPATH")"
	fi

	if [ "$(( UPDATE_TS + EXPIRE_SECONDS ))" -lt "$NOW" ]; then
		return 0
	else
		return 1
	fi
}

set -f
IFS="
"
DIRS=($(find /tmp/restapi-bios_images -type d -mindepth 2 -maxdepth 2))
set +f

for DIR in "${DIRS[@]}"; do
	if is_expired "$DIR"; then
		rm -f "$DIR"/*
		rmdir "$DIR"
	fi
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
#

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

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=usbcons
PIDFILE=/run/usbcons.pid
DESC="USB Serial Console"

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

STOPPER=
ACTION="$1"

case "$ACTION" in
  start)
    # Ability to prevent this from starting by editing cmdline in u-boot.
    # Keeping this here until I get gadget switching working properly. (t4906522)
    if grep "nousbcons" /proc/cmdline > /dev/null 2>&1
    then
      echo "USB Console Disabled."
      exit 0
    fi
    echo -n "Starting $DESC: "
    /usr/local/bin/usbmon.sh > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    killall usbmon.sh
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    killall usbmon.sh
    sleep 1
    /usr/local/bin/usbmon.sh > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  status)
    status $DAEMON
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


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

#
# Load USB gadget drivers in case they are compiled as modules.
# Note: "ast-udc" only exists in kernel 4.1. The driver is renamed to
# "aspeed_vhub" in kernel 5.x, and it will be auto-loaded at bootup time.
#
if uname -r | grep "4\.1\.*" > /dev/null 2>&1; then
    modprobe ast-udc
fi
modprobe g_cdc host_addr=02:00:00:00:00:02 dev_addr=02:00:00:00:00:01

# For g-ether interface, if the remote side brings down the interface, BMC side
# still treats it as up. In this case, all packets through usb0 (i.e. NDP) will
# be queued in the kernel for this interface. Ether interface has the default
# TX queue length as 1000, which means there could be up to 1000 package queued
# in kernel for that. In our case, kmalloc-192 is exhausted when 298 packets
# are queued.
# To solve this issue, we change the TX queue length for usb0 to 64 to avoid
# memory exhaust.
ifconfig usb0 txqueuelen 64

#
# Below logic is required to handle "getty" command line differences on
# different platforms:
#
#   - yocto rocko:
#     "getty" is from BusyBox, and tty_name can be a relative path (such
#     as ttyGS0) or absolute path (/dev/ttyGS0).
#     Given we've been using "/dev/ttyGS0" for long time, let's just keep it.
#
#   - yocto warrior (or newer versions):
#     "getty" is from util-linux, and tty_name must be a relative path (under
#     "/dev").
#
if getty 2>&1 | grep "BusyBox v1" > /dev/null 2>&1; then
    tty_name="/dev/ttyGS0"
else
    tty_name="ttyGS0"
fi

while true; do
    getty "$tty_name" 57600
    sleep 1
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-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

# Enable AST SNOOP DMA with default port 80
echo 1 > /sys/devices/platform/ast-snoop-dma.0/enable

# TODO: check for the if slot/server is present before starting the daemon
echo -n "Setup console  buffering..."

/usr/local/bin/consoled mb --buffer

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

set -e

# 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 FBTP, there are 2 fans connected.
# Here is the mapping between the fan and PWN/Tacho,
# Fan 0: PWM 0, Tacho0
# Fan 1: PWM 1, Tacho2

# 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 0, 2
echo 0 > $PWM_DIR/tacho0_source
echo 1 > $PWM_DIR/tacho0_en

echo 1 > $PWM_DIR/tacho2_source
echo 1 > $PWM_DIR/tacho2_en


#!/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-gpiod
# 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 FBTP... "

/usr/local/bin/gpiod

echo "done."


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

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

. /usr/local/fbpackages/utils/ast-functions
echo -n "Starting IPMB Rx/Tx Daemon.."

runsv /etc/sv/ipmbd_1 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_4 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_9 > /dev/null 2>&1 &

echo "done."


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

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

echo -n "Setup Caching for MEinfo.."
/usr/local/bin/me-cached > /dev/null 2>&1 &
echo "done."


#!/bin/sh

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


#!/bin/bash

PID=$$
# File format autodump<fru>.pid (See pal_is_crashdump_ongoing()
# function definition)
PID_FILE='/var/run/autodump1.pid'

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

# Set current pid
echo $PID > $PID_FILE

# kill previous autodump if exist
if [ ! -z "$OLDPID" ] && (grep "autodump" /proc/$OLDPID/cmdline &> /dev/null) ; then

  LOG_FILE='/tmp/autodump.log'
  LOG_ARCHIVE='/mnt/data/autodump_uncompleted.tar.gz'
  echo -n "(uncompleted) Auto Dump End at " >> $LOG_FILE
  date >> $LOG_FILE

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

  echo "kill pid $OLDPID..."
  kill -s 9 $OLDPID
  killall peci-util &> /dev/null
  killall me-util &> /dev/null
fi
unset OLDPID

DUMP_SCRIPT='/usr/local/bin/dump.sh'
LOG_FILE='/tmp/autodump.log'
LOG_ARCHIVE='/mnt/data/autodump.tar.gz'
LOG_MSG_PREFIX=""

CPU0_SKTOCC_N="/sys/class/gpio/gpio51/value"
CPU1_SKTOCC_N="/sys/class/gpio/gpio208/value"
DWR=0
SECOND_DUMP=0
DELAY_SEC=30
SENSOR_HISTORY=180

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

if [ "$DWR" == "1" ]; then
  LOG_ARCHIVE='/mnt/data/autodump_dwr.tar.gz'
  LOG_MSG_PREFIX="DWR "
  echo "Auto Dump after Demoted Warm Reset"
fi

if [ "$SECOND_DUMP" == "1" ]; then
  LOG_ARCHIVE='/mnt/data/autodump_second.tar.gz'
  LOG_MSG_PREFIX="SECOND_DUMP "
  echo "Auto Dump after system reset"
fi

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

  sleep ${DELAY_SEC}
fi

echo "Auto Dump Started"

# Start to dump and clear LOG_FILE
echo -n "Auto Dump Start at " > $LOG_FILE
date >> $LOG_FILE

# CPU0
$DUMP_SCRIPT 48 coreid >> $LOG_FILE 2>&1
$DUMP_SCRIPT 48 msr >> $LOG_FILE 2>&1

# CPU1
if [ "`cat $CPU1_SKTOCC_N`" -eq "0" ]; then
  $DUMP_SCRIPT 49 coreid >> $LOG_FILE 2>&1
  $DUMP_SCRIPT 49 msr >> $LOG_FILE 2>&1
else
  echo "<<---------- CPU1 is not present, skip it. ---------->>" >> $LOG_FILE
fi

# PCIe
$DUMP_SCRIPT pcie >> $LOG_FILE 2>&1

# Sensors
echo "Sensor history of last ${SENSOR_HISTORY}s at dump:" >> $LOG_FILE 2>&1
/usr/local/bin/sensor-util all --history $SENSOR_HISTORY >> $LOG_FILE 2>&1

echo "Sensor threshold at dump: " >> $LOG_FILE 2>&1
/usr/local/bin/sensor-util all --threshold >> $LOG_FILE 2>&1

# dwr
$DUMP_SCRIPT dwr >> $LOG_FILE 2>&1

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

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

tar zcf $LOG_ARCHIVE -C `dirname $LOG_FILE` `basename $LOG_FILE` && \
rm -rf $LOG_FILE && \
logger -t "ipmid" -p daemon.crit "${LOG_MSG_PREFIX}Crashdump for FRU: 1 is generated at $LOG_ARCHIVE"

# Remove current pid file
rm $PID_FILE

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


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

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

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
echo "eth0 mac fixup"
# get the MAC from u-boot environment
ethaddr=$(fw_printenv ethaddr 2>/dev/null | cut -d'=' -f2 2>/dev/null)

if [ -n "$ethaddr" ]; then
    echo "ethaddr=" $ethaddr
    #  use the one from u-boot environment
    mac="$ethaddr"
fi

if [ -n "$mac" ]; then
    echo "use u-boot mac addr"
    ifconfig eth0 hw ether $mac
else
    echo "no MAC in u-boot environment,  use randomly generated one"
    # no MAC in u-boot environment, use randomly generated one
    mac=$(ifconfig eth0 2>/dev/null |grep HWaddr 2>/dev/null |awk '{ print $5 }')

fi

if [ "$ethaddr" != "$mac" ]; then
    # set the MAC from ifconfig back to u-boot environment so that u-boot
    # can use it
    echo "save mac addr" $mac "to u-boot"
    fw_setenv "ethaddr" "$mac"
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

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

check_por_config()
{

  TO_PWR_ON=-1

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

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

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

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

      # Check if the file/key doesn't exist
      if [ ! -f "${KEYDIR}/pwr_server_last_state" ]; then
        TO_PWR_ON=$DEF_PWR_ON
      else
        LS=`cat ${KEYDIR}/pwr_server_last_state`
        if [ $LS == "on" ]; then
          TO_PWR_ON=1;
        elif [ $LS == "off" ]; then
          TO_PWR_ON=0;
        fi
      fi
    fi
  fi
  logger -s "TO_PWR_ON:$TO_PWR_ON, POR:$POR, LS:$LS"
}

# Check whether it is fresh power on reset
#if [ $(is_bmc_por) -eq 1 ]; then
  ## Move por to U-boot
  #check_por_config
  #if [ $TO_PWR_ON -eq 1 ] ; then
  #  power-util mb on
  #fi
#fi


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

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

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

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

set -e

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

# S: GPIOAA2()??
gpio=AA2

if [ $1 = "on" ]; then
    val=0
elif [ $1 = "off" ]; then
    val=1
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
# defined in other files.  We should probably move some more of the
# definitions to this file at some point.

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

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

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

# Set up to read the board revision pins, GPIOD1(25), GPIOD3 (27), GPIOD5 (29)
devmem_clear_bit $(scu_addr 90) 1
devmem_clear_bit $(scu_addr 70) 21

devmem_clear_bit $(scu_addr 8c) 8
devmem_clear_bit $(scu_addr 8c) 9
devmem_clear_bit $(scu_addr 8c) 10

gpio_export D1
gpio_export D3
gpio_export D5

BOARD_REV=$(( $(gpio_get D1) + ($(gpio_get D3)<<1) + ($(gpio_get D5)<<2) ))

# FM_PWR_BTN_N, Server power button in, on GPIO E2(34)
# To enable 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 E2

# FM_BMC_PWRBTN_OUT_N, power button to Server, on GPIO E3(35)
# To enable GPIOE3, SCU80[19], SCU8C[13], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 19

gpio_set E3 1

# PWRGD_SYS_PWROK, power GOOD , on GPIO B6(14)
# To enable GPIOB6, SCU80[14] must be 0
devmem_clear_bit $(scu_addr 80) 14

gpio_export B6

# LED_POST_CODE_0: GPIOH0 (56)
# To use GPIOH0, SCU90[6], SCU90[7], SCU10[8], SCU94[5] must be 0
devmem_clear_bit $(scu_addr 90) 6
devmem_clear_bit $(scu_addr 90) 7
devmem_clear_bit $(scu_addr 10) 8
devmem_clear_bit $(scu_addr 94) 5

gpio_set H0 1

# LED_POST_CODE_1: GPIOH1 (57)
# To use GPIOH1, SCU90[6], SCU90[7], SCU10[8], SCU94[5]  must be 0
gpio_set H1 1

# LED_POST_CODE_2: GPIOH2 (58)
# To use GPIOH2, SCU90[6], SCU90[7], SCU10[8], SCU94[6]  must be 0
devmem_clear_bit $(scu_addr 94) 6
gpio_set H2 1

# LED_POST_CODE_3: GPIOH3 (59)
# To use GPIOH3, SCU90[6], SCU90[7], SCU10[8], SCU94[6] must be 0
gpio_set H3 1

# LED_POST_CODE_4, GPIOH4 (60)
# GPIOH4(60): SCU90[6], SCU90[7], SCU10[8], SCU94[7] shall be 0
devmem_clear_bit $(scu_addr 94) 7
gpio_set H4 1

#LED_POST_CODE_5, GPIOH5 (61)
# GPIOH5(61): SCU90[6], SCU90[7], SCU10[8], SCU94[7] shall be 0
gpio_set H5 1

# LED_POST_CODE_6, GPIOH6 (62)
# GPIOH6(62): SCU90[6], SCU90[7], SCU10[8] shall be 0
gpio_set H6 1

# LED_POST_CODE_7, GPIOH7 (63)
# GPIOH7(63): SCU90[6], SCU90[7], SCU10[8] shall be 0
gpio_set H7 1

# FM_POST_CARD_PRES_BMC_N: GPIOQ6 (134)
devmem_clear_bit $(scu_addr 90) 28

# Set debounce timer #1 value to 0x12E1FC ~= 100ms
$DEVMEM 0x1e780050 32 0x12E1FC

# Select debounce timer #1 for GPIOQ6
devmem_clear_bit 0x1e780130 6
devmem_set_bit 0x1e780134 6

gpio_export Q6

# BMC_HEARTBEAT_N, heartbeat LED, GPIO U5(165)
# GPIOU5(165): SCUA0[13] shall be 1

# U5 is shared with RGMII1RXCTL - do not make it a GPIO
# devmem_set_bit $(scu_addr A0) 13

# U5 is shared with RGMII1RXCTL - do not make it a GPIO
# gpio_set U5 1

#gpio_set Q7 0

# System SPI
# Strap 12 must be 0 and Strape 13 must be 1
#devmem_clear_bit $(scu_addr 70) 12
#devmem_set_bit $(scu_addr 70) 13

# Power LED for Server:
# To use GPIOAA2 (210), SCUA4[26],  must be 0
devmem_clear_bit $(scu_addr A4) 26

gpio_set AA2 1

# Identify LED for Slot#1:
# To use GPIOF1 (41), SCU80[25], SCUA4[12], must be 0
#devmem_clear_bit $(scu_addr 80) 25
#devmem_clear_bit $(scu_addr A4) 12

#gpio_set F1 1
# Front Panel Hand Switch GPIO setup
# HAND_SW_ID1: GPIOR2(138)
# To use GPIOR2, SCU88[26] must be 0
#devmem_clear_bit $(scu_addr 88) 26

#gpio_export R2

# HAND_SW_ID2: GPIOR3(139)
# To use GPIOR3, SCU88[27] must be 0
#devmem_clear_bit $(scu_addr 88) 27

#gpio_export R3

# HAND_SW_ID4: GPIOR4(140)
# To use GPIOR4, SCU88[28] must be 0
#devmem_clear_bit $(scu_addr 88) 28

#gpio_export R4

# HAND_SW_ID8: GPIOR5(141)
# To use GPIOR5, SCU88[29] must be 0
#devmem_clear_bit $(scu_addr 88) 29

#gpio_export R5

# LED POST CODES: 8 GPIO signals

# LED_POSTCODE_0: GPIOG0 (48)
# To use GPIOG0, SCU84[0] must be 0
#devmem_clear_bit $(scu_addr 84) 0

#gpio_set G0 0

# LED_POSTCODE_1: GPIOG1 (49)
# To use GPIOG1, SCU84[1] must be 0
#devmem_clear_bit $(scu_addr 84) 1

#gpio_set G1 0

# LED_POSTCODE_2: GPIOG2 (50)
# To use GPIOG2, SCU84[2] must be 0
#devmem_clear_bit $(scu_addr 84) 2

#gpio_set G2 0

# LED_POSTCODE_4: GPIOP4 (124)
#gpio_set P4 0

# LED_POSTCODE_5: GPIOP5 (125)
#gpio_set P5 0

# LED_POSTCODE_6: GPIOP6 (126)
# To use GPIOP6, SCU88[22] must be 0
#devmem_clear_bit $(scu_addr 88) 22

#gpio_set P6 0

# LED_POSTCODE_7: GPIOP7 (127)
# To use GPIOP7, SCU88[23] must be 0
#devmem_clear_bit $(scu_addr 88) 23

#gpio_set P7 0

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

# Disable GPIO and enable it from
# KCS
gpio_set S1 1

# BMC_RST_BTN_IN_N: GPIOS0 (144)
# To use GPIOS0, SCU8C[0]
#devmem_clear_bit $(scu_addr 8c) 0

#gpio_export S0

# RESET for all Slots
# RST_SLOT1_SYS_RESET_N: GPIOH1 (57)
# To use GPIOH1, SCU90[6], SCU90[7] must be 0
#devmem_clear_bit $(scu_addr 90) 6
#devmem_clear_bit $(scu_addr 90) 7

#gpio_set H1 1

# 12V_STBY Enable for Slots

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

#gpio_set O5 1

# PWRGD_P12V_STBY_SLOT1: GPIOP1 (121)
# To use GPIOP1, SCU88[17] must be 0
#devmem_clear_bit $(scu_addr 88) 17
#gpio_export P1

# Enable the the EXTRST functionality of GPIOB7
#devmem_set_bit $(scu_addr 80) 15
#devmem_clear_bit $(scu_addr 90) 31
#devmem_set_bit $(scu_addr 3c) 3

# Disable PWM reset during external reset
devmem_clear_bit $(scu_addr 9c) 17
# Disable PWM reset during WDT1 reset
devmem_clear_bit 0x1e78501c 17

# FM_BATTERY_SENSE_EN_N: GPIOF6 (46)
# To use GJPIOF6, SCU80[30] must be 0
devmem_clear_bit $(scu_addr 80) 30
gpio_set F6 1

# FM_BIOS_MRC_DEBUG_MSG_DIS_N: GPIOD0 (24)
# To use GJPIOD0, SCU90[1] must be 0
devmem_clear_bit $(scu_addr 90) 1
gpio_set D0 1

# Platform ID check pin
devmem_clear_bit $(scu_addr 88) 16
devmem_clear_bit $(scu_addr 88) 17
devmem_clear_bit $(scu_addr 88) 18
devmem_clear_bit $(scu_addr 88) 19
devmem_clear_bit $(scu_addr 88) 20

# R6 and R7 are used for RGMII1RXD,
#  Do not make them GPIO
#devmem_clear_bit $(scu_addr 88) 30
#devmem_clear_bit $(scu_addr 88) 31

gpio_export P0
gpio_export P1
gpio_export P2
gpio_export P3
gpio_export P4

# R6 and R7 are used for RGMII1RXD,
#  Do not make them GPIO
#gpio_export R6
#gpio_export R7

# HSC_SMBUS_SWITCH_EN: GPIOS3 (147)
# To use GPIOS3, SCU8C[3] must be 0
devmem_clear_bit $(scu_addr 8C) 3

gpio_export S3

# FM_FAST_PROCHOT_EN_N: GPIOV3 (171)
# To use GPIOV3, SCUA0[19] must be 1
devmem_set_bit $(scu_addr A0) 19

gpio_set V3 0

# FM_OC_DETECT_EN_N: GPIOM3 (99)
# To use GPIOM3, SCU84[27] must be 0
devmem_clear_bit $(scu_addr 84) 27

gpio_set M3 0

# UV_HIGH_SET: GPIOAA5(???)
# To use GIOAA5, SCUA4[29] must be 0
devmem_clear_bit $(scu_addr A4) 29

gpio_export AA5

# FM_PCH_BMC_THERMTRIP_N: GPIOG2 (50)
# To use GPIOG2, SCU84[2] must be 0
devmem_clear_bit $(scu_addr 84) 2

gpio_export G2

# FM_BIOS_SMI_ACTIVE_N: GPIOG7 (55)
gpio_export G7

# FM_PMBUS_ALERT_BUF_EN_N: GPIOL5 (93)
# To use GPIOG2, SCU84[21] and SCU90[5] must be 0
devmem_clear_bit $(scu_addr 84) 21
devmem_clear_bit $(scu_addr 90) 5

gpio_set L5 0

# FM_CPU0_PROCHOT_LVT3_ BMC_N, on GPIO E6(38)
# To enable 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 E6

# FM_CPU1_PROCHOT_LVT3_ BMC_N, on GPIO E7(39)
# To enable GPIOE7, SCU80[23], SCU8C[15], and SCU70[22] must be 0
devmem_clear_bit $(scu_addr 80) 23

gpio_export E7

# RST_BMC_SYSRST_BTN_OUT_N, power reset to Server, on GPIO E1(33)
devmem_clear_bit $(scu_addr 80) 17

gpio_set E1 1

# Disable JTAG Engine
devmem_clear_bit $(jtag_addr 08) 30
devmem_clear_bit $(jtag_addr 08) 31

# FM_CPU_MSMI_LVT3_N
devmem_clear_bit $(scu_addr 88) 3
gpio_export N3

# FM_CPU_CATERR_LVT3_N
devmem_clear_bit $(scu_addr 84) 1
gpio_export G1

# FM_GLOBAL_RST_WARN_N
devmem_set_bit $(scu_addr A4) 3
# H_CPU0_MEMABC_MEMHOT_LVT3_BMC_N
devmem_set_bit $(scu_addr A4) 4
# H_CPU0_MEMDEF_MEMHOT_LVT3_BMC_N
devmem_set_bit $(scu_addr A4) 5
# H_CPU1_MEMGHJ_MEMHOT_LVT3_BMC_N
devmem_set_bit $(scu_addr A4) 6
# H_CPU1_MEMKLM_MEMHOT_LVT3_BMC_N
devmem_set_bit $(scu_addr A4) 7

# Intel remote BMC debug function pins
# GPIOP5 BMC_PREQ_N
devmem_clear_bit $(scu_addr 88) 21
gpio_set P5 1
# GPIOP6 BMC_PWR_DEBUG_N
devmem_clear_bit $(scu_addr 88) 22
# gpio_set P6 1

# GPIOP7 RST_RSMRST_N
devmem_clear_bit $(scu_addr 88) 23
gpio_export P7
# GPIOB1 BMC_DEBUG_EN_N
gpio_export B1
# GPIOR2 BMC_TCK_MUX_SEL
devmem_clear_bit $(scu_addr 88) 26
gpio_export R2
# GPIOR3 BMC_PRDY_N
devmem_clear_bit $(scu_addr 88) 27
gpio_export R3
# GPIOR4 BMC_XDP_PRSNT_IN_N
devmem_clear_bit $(scu_addr 88) 28
gpio_export R4
# GPIOR5 RST_BMC_PLTRST_BUF_N
devmem_clear_bit $(scu_addr 88) 29
gpio_export R5
# GPIOY2 BMC_JTAG_SEL
devmem_clear_bit $(scu_addr A4) 10
devmem_clear_bit $(scu_addr 94) 11
gpio_set Y2 1

# FM_CPU0_SKTOCC_LVT3_N
devmem_clear_bit $(scu_addr 84) 3
gpio_export G3

# FM_CPU1_SKTOCC_LVT3_N
devmem_clear_bit $(scu_addr A4) 24
gpio_export AA0

# FM_BIOS_POST_CMPLT_N
devmem_clear_bit $(scu_addr A4) 31
gpio_export AA7

# PECI_MUX_SELECT
if [ $BOARD_REV -le 2 ]; then
  # Power-On, EVT, preDVT
  devmem_clear_bit $(scu_addr A8) 2
  gpio_set AB2 0
else
  # DVT or newer
  devmem_clear_bit $(scu_addr A4) 28
  gpio_set AA4 0
fi

# BMC_CPLD_FPGA_SEL: GPIOA0
devmem_clear_bit $(scu_addr 80) 0
gpio_set A0 1

# FM_SLPS4_N: GPIOY1
devmem_clear_bit $(scu_addr A4) 9
devmem_clear_bit $(scu_addr 94) 10
gpio_export Y1

# FW_UV_ADR_TRIGGER_EN: GPIOI3
gpio_set I3 1

# FW_FORCE_ADR_N: GPIOI2
gpio_set I2 1

# IRQ_DIMM_SAVE_LVT3_N
gpio_export D4

# FM_OCP_MEZZA_PRES: GPIOAB1
devmem_clear_bit $(scu_addr A8) 1
gpio_export AB1

# FM_UARTSW_MSB_N:
devmem_clear_bit $(scu_addr 90) 27

gpio_export Q4
gpio_export Q5


#!/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: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

store_clear_por


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

# Enable AST SNOOP DMA with default port 80
echo 1 > /sys/devices/platform/ast-snoop-dma.0/enable


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

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

# Sync BMC's date with the server
sync_date()
{
  # if no ntp peer has been declared the system peer
  if ! /usr/sbin/ntpq -p | grep '^\*' > /dev/null ; then
    echo Syncing up BMC time with server...
    # Use standard IPMI command 'get-sel-time' to read RTC time
    date -s @$((16#$(/usr/local/bin/me-util 0x28 0x48 | awk '{print $4$3$2$1}')))
  fi
}

sync_date


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

ARGS="--json /etc/platform-svcd-config.json"
exec /usr/local/bin/platform-svcd $ARGS


#!/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:          platform-svcd
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: dbus based platform service
### END INIT INFO

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

echo -n "Setup platform svcd for Portwell Neptune... "

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

echo "done."


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

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

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

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

if [ $(cat /sys/class/gpio/gpio120/value) == "0" ]; then
  ln -s /etc/sensor-correction-sku0-conf.json /etc/sensor-correction-conf.json
else
  ln -s /etc/sensor-correction-sku1-conf.json /etc/sensor-correction-conf.json
fi

# echo -n "Setup sensor monitoring for FBTP... "

# runsv /etc/sv/sensord > /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
#

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


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

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

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=usbcons
PIDFILE=/run/usbcons.pid
DESC="USB Serial Console"

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

STOPPER=
ACTION="$1"

case "$ACTION" in
  start)
    # Ability to prevent this from starting by editing cmdline in u-boot.
    # Keeping this here until I get gadget switching working properly. (t4906522)
    if grep "nousbcons" /proc/cmdline > /dev/null 2>&1
    then
      echo "USB Console Disabled."
      exit 0
    fi
    echo -n "Starting $DESC: "
    /usr/local/bin/usbmon.sh > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    killall usbmon.sh
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    killall usbmon.sh
    sleep 1
    /usr/local/bin/usbmon.sh > /dev/null 2>&1 &
    echo "$NAME."
    ;;
  status)
    status $DAEMON
    exit $?
    ;;
  *)
    N=${0##*/}
    N=${N#[SK]??}
    echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0


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

modprobe g_cdc host_addr=02:00:00:00:00:02 dev_addr=02:00:00:00:00:01
# For g-ether interface, if the remote side brings down the interface, BMC side
# still treats it as up. In this case, all packets through usb0 (i.e. NDP) will
# be queued in the kernel for this interface. Ether interface has the default
# TX queue length as 1000, which means there could be up to 1000 package queued
# in kernel for that. In our case, kmalloc-192 is exhausted when 298 packets
# are queued.
# To solve this issue, we change the TX queue length for usb0 to 64 to avoid
# memory exhaust.
ifconfig usb0 txqueuelen 64
while true; do
    getty /dev/ttyGS0 57600
    sleep 1
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.
#
# 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-crond
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup crond history buffering
### END INIT INFO

# TODO: check for the if slot/server is present before starting the daemon
echo -n "Setup crond..."
  mkdir /etc/cron
  mkdir /etc/cron/crontabs
  echo "0 * * * * run-parts /etc/cron.daily" > /etc/cron/crontabs/root
  /etc/init.d/busybox-cron start
echo "done."