#!/bin/bash
#shellcheck disable=SC1091
#shellcheck disable=SC2001
#shellcheck disable=SC2059
. /usr/local/bin/openbmc-utils.sh
gpio_export_by_name "${ASPEED_GPIO}" GPIOG4 TXD8_SD2DAT2_SALT13 2> /dev/null

cached_eeprom_dir=/mnt/data1/mac_idprom
cache_eeprom_file=/mnt/data1/mac_idprom/cached_eeprom_54
eeprom_file=/sys/bus/i2c/devices/8-0054/eeprom
crc_cal_file=/usr/local/bin/crc_16.py
startfile=/tmp/startfile
endfile=/tmp/endfile
erasefile=/tmp/erasefile
inputfile=/tmp/inputfile
eeprom_data=/tmp/eeprom_data

# Start Bytes
printf '\xcf\x06' > $startfile
# End Bytes
printf '\x43\x00\x01' > $endfile
# ff bytes for paddding at the end
printf '\xff' > $erasefile

prog=$0

usage() {
    echo
    echo "Usage: $prog <Mac_Address>"
    echo
    echo "Mac Address:"
    echo "  Format(Hex): xx:xx:xx:xx:xx:xx "
    echo "  Example    : d4:eb:68:b5:65:68"
    echo
}

clean_up() {
    # cleaning up temp files
    rm $startfile 2> /dev/null
    rm $endfile 2> /dev/null
    rm $erasefile 2> /dev/null
    rm $inputfile 2> /dev/null
}

get_idprom_permission () {

    perm=$( gpio_get_value TXD8_SD2DAT2_SALT13 )
    echo "$perm"

}

set_idprom_permission () {

    gpio_set_value TXD8_SD2DAT2_SALT13 "$1"

}

padding_cache_eeprom_mac () {

    ((i = "${1}"))
    while [[ $i -lt 512 ]]
    do
        dd seek=$i count=1 if=$erasefile of=$cache_eeprom_file bs=1 2> /dev/null
        ((i = i +1))
    done

}

write_cache_eeprom_mac () {

    ((i = "${1}"))
    dd seek=$i count=2 if=$startfile of=$cache_eeprom_file bs=1 2> /dev/null
    ((i = i + 2))
    dd seek=$i count=6 if=$inputfile of=$cache_eeprom_file bs=1 2> /dev/null
    end=$((i + 6))
    dd seek=$end count=3 if=$endfile of=$cache_eeprom_file bs=1 2> /dev/null
    end=$((end + 3))
    echo $end
}

read_eeprom_end() {

    hexdump -ve '1/1 "%.2x"' $cache_eeprom_file > $eeprom_data

    ((i = 0))
    while [[ $i -lt 512 ]]
    do
        # Looks for end of the eeprom content
        # So, it checks for ffffffff bytes to mark the end
        var=$( dd skip=$i count=8 if=$eeprom_data bs=1 2> /dev/null )
        if [ "$var" = "ffffffff" ];then
            echo $((i / 2))
            break
        fi
        ((i = i + 1))
    done

    rm $eeprom_data

}

check_crc_mac_address() {

    # Check for CRC of eeprom
    crc=$(python $crc_cal_file $eeprom_file check)
    if [[ $crc -eq 1 ]];then
        echo "CRC is Valid"
    else
        echo "Invalid CRC ... Fatal Error !"
        echo "FAIL"
    fi

    # Check for programmed mac address in eeprom
    mac_address=$(read_x86_mac_from_eeprom)

    if [[ $mac_address = "$input_mac_address" ]]; then
        echo "x86 mac address programmed successfully"
        echo "PASS"
    else
        echo "x86 mac address is not programmed"
        echo "FAIL"
    fi

}

if [ $# -lt 1 ]; then
    usage
    clean_up
    echo "FAIL"
    exit 1
fi

if ! [[ $1 =~ ^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$ ]]; then
    echo "Invalid mac address"
    usage
    clean_up
    echo "FAIL"
    exit 1
fi

input_mac_address="$1"
arg_str=$(echo "$1" | sed 's/:/\\x/g')
input_str="\x$arg_str"
printf "$input_str" > $inputfile

# Check for mac address in eeprom
mac_address=$(read_x86_mac_from_eeprom)
mac_address_present=0

if [[ $mac_address =~ ^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$ ]]; then
    echo "x86 mac address is present"
    mac_address_present=1
else
    echo "No x86 mac address"
fi

# CRC check
crc=$(python $crc_cal_file $eeprom_file check)

if [[ $crc -eq 1 ]];then
    echo "CRC is Valid"
    if [[ $mac_address_present -eq 1 ]];then
        clean_up
        echo "PASS"
        exit 1
    fi
    mkdir -p $cached_eeprom_dir
    # Copying EEPROM to buffer
    dd if=$eeprom_file of=$cache_eeprom_file 2> /dev/null
    sync
else
    echo "Invalid CRC... checking for cached eeprom"
    if [  -f  "$cache_eeprom_file" ]; then
        echo "Cache file is present"
        crc=$(python $crc_cal_file $cache_eeprom_file check)
        if [[ $crc -eq 1 ]];then
            set_idprom_permission 0
            dd if=$cache_eeprom_file of=$eeprom_file 2> /dev/null
            set_idprom_permission 1
            check_crc_mac_address
            clean_up
            exit 1
        else
            echo "Fatal Error! Cached eeprom has bad CRC. Unable to repair IDPROM"
            clean_up
            echo "FAIL"
            exit 1
        fi
    else
        echo "Fatal Error! IDPROM is corrupt and no cached IDPROM is available. Unable to repair IDPROM"
        clean_up
        echo "FAIL"
        exit 1
    fi
fi

# Get start address to write from
start_address=$(read_eeprom_end)

# Writing x86 mac address to eeprom
end_address=$(write_cache_eeprom_mac "$start_address")

# Padding ff to end of the buffer
padding_cache_eeprom_mac "$end_address"

# Updating the CRC for eeprom
# after mac address addition
python $crc_cal_file $cache_eeprom_file

# Setting write permission
# 0 - permission to write
# 1 - permission denial
set_idprom_permission 0

# Copying buffer to EEPROM
dd if=$cache_eeprom_file of=$eeprom_file 2> /dev/null

# Setting back write protection
set_idprom_permission 1

check_crc_mac_address

clean_up


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

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

get_version () {
    length=$( stat -c%s "$1" )
    ((i = length - 4))
    ((major_offset_found = 0))
    while [[ $i -ne 0 ]]
    do
        var=$( dd skip=$i count=4 if="$1" bs=1 2> /dev/null )
        if [ "$var" = "fa7f" ];then
            if [ $major_offset_found -eq 0 ]; then
                ((major_offset_found = 1))
                ((major_offset = i + 4))
            else
                ((minor_offset = i + 4))
                break
            fi
        fi
        ((i = i - 1))
    done
    major=$( dd skip=$major_offset count=2 if="$1" bs=1 2> /dev/null)
    minor=$( dd skip=$minor_offset count=2 if="$1" bs=1 2> /dev/null)
    echo "version is $major.$minor"
}

#Port 0 - I210 (x86 side)
#Port 1 - 1G PHY (front panel) ethernet port
#Port 5 - BMC CPU
monitored_ports=("0" "1" "5")

start_monitoring() {
    reset_count=0
    while : ; do
        sleep 3
        if ! echo "status" > /dev/switch 2> /dev/null; then
            # A device busy error may be seen if more than
            # one instance of this script is run at the same
            # time
            continue
        fi
        ports_status=$( tr -d '\0' < /dev/switch )
        if [ "$ports_status" ]; then
            # Loop over the "ports_status" line by line, each line has
            # the status of one port. If that port status was not read
            # before (i.e first check) then it is printed out, but if
            # the status was read before then it is only printed
            # if the status has changed since the last check.
            # Sample "ports_status" output:
                  #Link status for port 0 is UP
                  #Link status for port 1 is UP
                  #Link status for port 2 is DOWN
                  #Link status for port 3 is DOWN
                  #Link status for port 4 is DOWN
                  #Link status for port 5 is UP
                  #Link status for port 6 is DOWN
            # The "all_ports_down" flag is set to true before the ports
            # status is checked, then if any of the ports is UP the flag
            # is set to false. If "all_ports_down" flag is true for 2
            # polling intervals (i.e. all ports are down for more than
            # three seconds, then the switch will be reset to recover for a
            # maximum three consecutive attempts.
            all_ports_down=1
            while read -r line
            do
                port=$( echo "$line" | cut -d ' ' -f 5)
                status=$( echo "$line" | cut -d ' ' -f 7)
                if [[ -z "$port" ]]; then
                    continue
                fi
                if [[ " ${monitored_ports[*]} " =~ $port ]]; then
                    if [ -z "${port_status[$port]}" ]; then
                        printf '%(%Y-%m-%d %H:%M:%S)T\n' -1
                        printf "port %s link status is %s\n" "$port" "$status"
                        port_status["$port"]=$status
                    else
                        if [[ "${port_status[$port]}" != "$status" ]]; then
                            printf '%(%Y-%m-%d %H:%M:%S)T\n' -1
                            printf "port %s link status changed to %s\n" "$port" "$status"
                        fi
                    fi
                    port_status["$port"]=$status
                    if [ "$status" = "UP" ]; then
                        # For testing only, to trigger a switch reset
                        if [ ! -f /tmp/link_down_test ]; then
                            all_ports_down=0
                        fi
                    fi
                fi
            done < <(printf '%s\n' "$ports_status")

            if [ "$all_ports_down" -eq 1 ]; then
                if [ -n "$reset_pending" ] && [ "$reset_pending" -eq 1 ]; then
                    printf '%(%Y-%m-%d %H:%M:%S)T\n' -1
                    printf "all ports are down, resetting the switch to recover\n"
                    # RESET SWITCH
                    oob_switch_reset
                    ((reset_count+=1))
                    # Testing shows that MDIO access fails for 3 or 4
                    # seconds after switch reset
                    sleep 6
                    reset_pending=0
                else
                    if [ "$reset_count" -eq 3 ]; then
                        printf '%(%Y-%m-%d %H:%M:%S)T\n' -1
                        printf "all ports are down, exhausted all %s recovery attempts\n" "$reset_count"
                        # May issue router power cycle at the point
                        exit 0
                    fi
                    reset_pending=1
                fi
            else
                reset_count=0
                reset_pending=0
            fi
        fi
    done
}

gb_util() {

    if [ ! -f /sys/bus/i2c/devices/8-0050/eeprom ]; then
        echo "/sys/bus/i2c/devices/8-0050/eeprom does not exist"
        return 1
    fi

    if [ "$1" = "upgrade" ];then
        if [ -f "$2" ]; then
            oob_switch_eeprom_select
            cat < "$2" 1<> /sys/bus/i2c/devices/8-0050/eeprom
            oob_switch_eeprom_deselect
        else
            echo "$2 does not exist"
        fi
    fi

    if [ "$1" = "enable" ];then
        oob_switch_eeprom_select
    fi

    if [ "$1" = "disable" ];then
        oob_switch_eeprom_deselect
    fi

    if [ "$1" = "reset" ];then
        oob_switch_reset
    fi

    if [ "$1" = "erase" ];then
        printf '\xff' > /tmp/erasefile
        oob_switch_eeprom_select
        ((i = 0))
        while [[ $i -lt 512 ]]
        do
            dd seek=$i count=1 if=/tmp/erasefile of=/sys/bus/i2c/devices/8-0050/eeprom bs=1 2> /dev/null
            ((i = i +1))
        done
        oob_switch_eeprom_deselect
        rm /tmp/erasefile
    fi

    if [ "$1" = "eeprom_version" ];then
        oob_switch_eeprom_select
        hexdump -ve '1/1 "%.2x"' /sys/bus/i2c/devices/8-0050/eeprom > /tmp/eeprom_data
        oob_switch_eeprom_deselect
        get_version /tmp/eeprom_data
        rm /tmp/eeprom_data
    fi

    if [ "$1" = "packaged_version" ];then
        hexdump -ve '1/1 "%.2x"' "$2" > /tmp/eeprom_data
        get_version /tmp/eeprom_data
        rm /tmp/eeprom_data
    fi

    if [ "$1" = "check" ];then
        i2cset -f -y 12 0x31 0x0c
        i2cset -f -y 12 0x32 0x00
        i2cset -f -y 12 0x33 0x00
        i2cset -f -y 12 0x34 0x00
        i2cset -f -y 12 0x30 0x1
        access=$( i2cget -f -y 12 0x35 )
        if [ $(( access & 0x4 )) -eq $(( 0x0 )) ]; then
            echo "access is disabled"
        else
            echo "access is enabled"
        fi
    fi

    if [ "$1" = "status" ];then
        if [ ! -c "/dev/switch" ]; then
            echo "/dev/switch does not exist, mv88e6320 kernel module needed"
            return 1
        fi
        if echo "status" > /dev/switch 2> /dev/null; then
            cat /dev/switch
        fi
    fi

    if [ "$1" = "statistics" ];then
        if [ ! -c "/dev/switch" ]; then
            echo "/dev/switch does not exist, mv88e6320 kernel module needed"
            return 1
        fi
        if echo "statistics" > /dev/switch 2> /dev/null; then
            cat /dev/switch
        fi
    fi

    if [ "$1" = "monitor" ];then
        if [ ! -c "/dev/switch" ]; then
            echo "/dev/switch does not exist, mv88e6320 kernel module needed"
            return 1
        fi
        start_monitoring
    fi
}

case "$1" in
    "--help" | "?" | "-h")
        program=$(basename "$0")
        echo "Usage: switch firmware upgrade command"
        echo "  <$program upgrade image> enable access to switch EEPROM, upgrade FW with provided image, then disable access"
        echo "  <$program enable> enable access to switch EEPROM for firmware upgrade"
        echo "  <$program disable> disable access to switch EEPROM"
        echo "  <$program reset> reset switch, can be done after FW upgrade to activate new image"
        echo "  <$program erase> erase the FW image from the switch EEPROM, switch will boot with HW configuration after next reset"
        echo "  <$program eeprom_version> get the FW version from the switch EEPROM"
        echo "  <$program packaged_version image> get the FW version from the switch FW image file"
        echo "  <$program check> check access status to switch EEPROM"
        echo "  <$program status> get all ports status (UP or DOWN)"
        echo "  <$program statistics> get statisitcs for ports that are UP"
        echo "  <$program monitor> start monitoring and recovery. Switch will be reset if all used ports are DOWN for more than 3 seconds"
    ;;

    *)
        gb_util "$1" "$2"
    ;;
esac


#!/bin/bash
#
# Copyright (c) 2022 Cisco Systems Inc.
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# 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:    85
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

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

# Power on if not already powered on
wedge_power.sh on


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

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh
. /usr/local/bin/board-utils.sh

#BMC Temp sensor
i2c_device_add 8 0x4A lm75

#BMC IDPROM
i2c_device_add 8 0x54 24c64

#Chassis IDPROM
i2c_device_add 8 0x56 24c64

#enable access to GE switch EEPROM
oob_switch_eeprom_select

#GE switch EEPROM
i2c_device_add 8 0x50 24c04

#disable access to GE switch EEPROM
oob_switch_eeprom_deselect


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

#shellcheck disable=SC1091,SC2009
. /usr/local/bin/openbmc-utils.sh

CONSOLE_SH=/usr/local/bin/us_console.sh
FILE=/etc/us_pseudo_tty
TTY=/dev/ttyS4

mTerm_server_running() {
  pid=$(ps | grep -v grep | grep '/usr/local/bin/mTerm_server' -m 1 |
        awk '{print $1}')
  if [ "$pid" ] ; then
    return 0
  fi
  return 1
}

start_sol_session() {
  if [ -e $FILE ]; then
    read -r TTY<$FILE
  fi
  $CONSOLE_SH connect

  echo "You are in SOL session."
  echo "Use ctrl-x to quit."
  echo "-----------------------"
  echo

  trap '"$CONSOLE_SH" disconnect' INT TERM QUIT EXIT

  /usr/bin/microcom -s 57600 "$TTY"

  echo
  echo
  echo "-----------------------"
  echo "Exit from SOL session."
}

# if mTerm server is running use mTerm_client to connect to userver
# otherwise fallback to the old method
echo 0x3 > "$SMBCPLD_SYSFS_DIR/uart_selection"
if mTerm_server_running; then
  exec /usr/local/bin/mTerm_client wedge
else
  start_sol_session
fi


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

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

prog="$0"

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

do_status() {
    cat /sys/bus/platform/devices/pseq/power_state
}

do_on_com_e() {
    #Commands to turn x86 on
    PDEV=/sys/bus/platform/devices
    if [ -w ${PDEV}/pseq/config ]; then
        echo on > ${PDEV}/pseq/config
    else
        i2cset -f -y 2 0x21 0x8c
        i2cset -f -y 2 0x22 0x40
        i2cset -f -y 2 0x23 0x01
        i2cset -f -y 2 0x24 0x00
        i2cset -f -y 2 0x25 0x02
        i2cset -f -y 2 0x20 0x0
    fi
}

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

    do_on_com_e
    echo " Done"
}

do_off_com_e() {
    PDEV=/sys/bus/platform/devices
    if [ -w ${PDEV}/pseq/config ]; then
        echo off > ${PDEV}/pseq/config
    else
        i2cset -f -y 2 0x21 0x8c
        i2cset -f -y 2 0x22 0x40
        i2cset -f -y 2 0x23 0x01
        i2cset -f -y 2 0x24 0x00
        i2cset -f -y 2 0x25 0x04
        i2cset -f -y 2 0x20 0x0
    fi
}

do_off() {
    echo -n "Power off x86 ..."
    do_off_com_e
    echo " Done"
}

do_reset_com_e() {
    #Commands to reboot x86
    PDEV=/sys/bus/platform/devices
    if [ -w ${PDEV}/pseq/config ]; then
        echo cycle > ${PDEV}/pseq/config
    else
        i2cset -f -y 2 0x21 0x8c
        i2cset -f -y 2 0x22 0x40
        i2cset -f -y 2 0x23 0x01
        i2cset -f -y 2 0x24 0x00
        i2cset -f -y 2 0x25 0x08
        i2cset -f -y 2 0x20 0x0
    fi
}

do_reset() {
    echo -n "Reset x86 ..."
    do_reset_com_e
    echo " Done"
}

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 "$@"
        ;;
    *)
        usage
        exit 1
        ;;
esac

exit $?


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

eeprom_file=/sys/bus/i2c/devices/8-0054/eeprom
crc_cal_file=/usr/local/bin/crc_16.py

crc=$(python $crc_cal_file $eeprom_file check)
if [[ $crc -eq 1 ]];then
    echo "CRC is Valid"
else
    echo "Invalid CRC ... Fatal Error !"
fi

read_x86_mac_from_eeprom


#!/bin/bash
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
logger "run-dhc6.sh: dhclient -6 Started.."
pid="/var/run/dhclient6.usb0.pid"
exec dhclient -1 -6 -d -D LL --address-prefix-len 64 -pf ${pid} usb0 "$@"


#!/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 &
echo "done."


#!/bin/sh
# execute <fscd.py> <min syslog level>
# log_level = <debug, warning, info>
# Default log level in fscd is set to warning, if any other level is needed
# set here
. /usr/local/fbpackages/utils/ast-functions

default_fsc_config="/etc/fsc-config.json"
if [[ -f ${default_fsc_config} ]]; then
  rm ${default_fsc_config}
fi

gp_board_id0=$(gpio_get BOARD_ID0 GPION5)
gp_board_id1=$(gpio_get BOARD_ID1 GPION6)
gp_board_id2=$(gpio_get BOARD_ID2 GPION7)

if [ $gp_board_id0 -eq 1 ] && [ $gp_board_id1 -eq 1 ] && [ $gp_board_id2 -eq 0 ]; then
  ln -s /etc/fsc-config_BRCM.json ${default_fsc_config}
else
  ln -s /etc/fsc-config_MICRON.json ${default_fsc_config}
fi

exec /usr/bin/fscd.py 'warning'


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

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

echo "Setup fan speed... "
/usr/local/bin/fan-util --set 70
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-gpiod
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup sensor monitoring
### END INIT INFO

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

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

exec /usr/local/bin/ipmbd 0 1


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

exec /usr/local/bin/ipmbd 8 1


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

### BEGIN INIT INFO
# Provides:          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/bin/openbmc-utils.sh
echo -n "Starting IPMB Rx/Tx Daemon.."

i2c_mslave_add 0 0x17 #MB
i2c_mslave_add 8 0x10 #USB DBG
i2c_mslave_add 1 0x10  #NIC0
i2c_mslave_add 9 0x10  #NIC1
i2c_mslave_add 2 0x10  #NIC2
i2c_mslave_add 10 0x10 #NIC3
i2c_mslave_add 4 0x10  #NIC4
i2c_mslave_add 11 0x10 #NIC5
i2c_mslave_add 7 0x10  #NIC6
i2c_mslave_add 13 0x10 #NIC7

ulimit -q 1024000
runsv /etc/sv/ipmbd_8 > /dev/null 2>&1 &
runsv /etc/sv/ipmbd_0 > /dev/null 2>&1 &

echo "done."


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

/etc/init.d/sync-rtc.sh &


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

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

# To enable GPIOAC
devmem_clear_bit $(scu_addr ac) 0
devmem_clear_bit $(scu_addr ac) 1
devmem_clear_bit $(scu_addr ac) 2
devmem_clear_bit $(scu_addr ac) 3
devmem_clear_bit $(scu_addr ac) 5
devmem_clear_bit $(scu_addr ac) 6
devmem_clear_bit $(scu_addr ac) 7

#setup BMC SKU ID
gpio_export SKU_ID1_PAX0 GPIOB6

gpio_export SKU_ID0_PAX0 GPIOAC0

gpio_export SKU_ID1_PAX1 GPIOB7

gpio_export SKU_ID0_PAX1 GPIOAC2

gpio_export SKU_ID1_PAX2 GPIOM0

gpio_export SKU_ID0_PAX2 GPIOB0

gpio_export SKU_ID1_PAX3 GPIOM6

gpio_export SKU_ID0_PAX3 GPIOB4

# PDB 12V POWER (output)
gpio_export BMC_IPMI_PWR_ON GPIOE1
gpio_set BMC_IPMI_PWR_ON 1

# BMC ready
gpio_export BMC_READY_N GPIOB2

# System power good
gpio_export SYS_PWR_READY GPIOB3

# BMC power button (input)
gpio_export BMC_PWR_BTN_IN_N GPIOE0

gpio_set BMC_READY_N 0

# CPLD SMB ALERT
gpio_export CPLD_SMB_ALERT_N_R GPIOE5

gpio_export RST_PEX_I2C_SW_R_N GPIOA0

# Reserved
gpio_export BMC_GPIOA1 GPIOA1
gpio_export BMC_GPIOA2 GPIOA2
gpio_export BMC_GPIOA3 GPIOA3

# POST LED
gpio_export LED_POSTCODE_0 GPIOF0
gpio_set LED_POSTCODE_0 0
gpio_export LED_POSTCODE_1 GPIOF1
gpio_set LED_POSTCODE_1 0
gpio_export LED_POSTCODE_2 GPIOF2
gpio_set LED_POSTCODE_2 0
gpio_export LED_POSTCODE_3 GPIOF3
gpio_set LED_POSTCODE_3 0
gpio_export LED_POSTCODE_4 GPIOF4
gpio_set LED_POSTCODE_4 0
gpio_export LED_POSTCODE_5 GPIOF5
gpio_set LED_POSTCODE_5 0
gpio_export LED_POSTCODE_6 GPIOF6
gpio_set LED_POSTCODE_6 0
gpio_export LED_POSTCODE_7 GPIOF7
gpio_set LED_POSTCODE_7 0

gpio_export PEX0_SYS_ERR_BMC GPION4
gpio_export PEX1_SYS_ERR_BMC GPIOG0
gpio_export PEX2_SYS_ERR_BMC GPIOG3
gpio_export PEX3_SYS_ERR_BMC GPIOG7

# Board revision ID
gpio_export REV_ID0 GPIOG4
gpio_export REV_ID1 GPIOG5
gpio_export REV_ID2 GPIOG6

kv set mb_rev $(($(gpio_get REV_ID2)<<2 |
        $(gpio_get REV_ID1)<<1 |
        $(gpio_get REV_ID0)))

# OCP3.0 NIC power good
gpio_export OCP_V3_0_NIC_POWER_GOOD GPIOH0
gpio_export OCP_V3_1_NIC_POWER_GOOD GPIOH1
gpio_export OCP_V3_2_NIC_POWER_GOOD GPIOH2
gpio_export OCP_V3_3_NIC_POWER_GOOD GPIOH3
gpio_export OCP_V3_4_NIC_POWER_GOOD GPIOH4
gpio_export OCP_V3_5_NIC_POWER_GOOD GPIOH5
gpio_export OCP_V3_6_NIC_POWER_GOOD GPIOH6
gpio_export OCP_V3_7_NIC_POWER_GOOD GPIOH7

# SEL FLASH PAX
gpio_export SEL_FLASH_PAX0 GPIOAA0
gpio_export SEL_FLASH_PAX1 GPIOAA1
gpio_export SEL_FLASH_PAX2 GPIOAA5
gpio_export SEL_FLASH_PAX3 GPIOAA6

gpio_export OCP_V3_3_PRSNTB_R_N GPIOAA2
gpio_export OCP_V3_4_PRSNTB_R_N GPIOAA3
gpio_export OCP_V3_5_PRSNTB_R_N GPIOAB3

gpio_export CARRIER_0_ALERT_R_N GPIOAB0
gpio_export CARRIER_1_ALERT_R_N GPIOAB1

gpio_export BMC_BOOT_RCVR_B0_PAX0 GPIOL0
gpio_set BMC_BOOT_RCVR_B0_PAX0 0
gpio_export BMC_BOOT_RCVR_B0_PAX1 GPIOL1
gpio_set BMC_BOOT_RCVR_B0_PAX1 0
gpio_export BMC_BOOT_RCVR_B0_PAX2 GPIOL2
gpio_set BMC_BOOT_RCVR_B0_PAX2 0
gpio_export BMC_BOOT_RCVR_B0_PAX3 GPIOL3
gpio_set BMC_BOOT_RCVR_B0_PAX3 0

# BSM EMMC PRSNT
gpio_export BSM_EMMC_PRSNT_R_N GPIOL4

gpio_export RST_BMC_SMB4_BUF_R_N GPIOL5

# NVME PRSNT
gpio_export NVME_0_1_PRSNTB_R_N GPIOL6
gpio_export NVME_2_3_PRSNTB_R_N GPIOL7

gpio_export SMB_MB_BMC_MUX_OE_R GPIOM3
gpio_set SMB_MB_BMC_MUX_OE_R 0
gpio_export SMB_MB_BMC_MUX_S0_R GPIOM4
gpio_set SMB_MB_BMC_MUX_S0_R 1
gpio_export SMB_MB_BMC_MUX_S1_R GPIOM5
gpio_set SMB_MB_BMC_MUX_S1_R 0

# BOARD ID
gpio_export BOARD_ID0 GPION5
gpio_export BOARD_ID1 GPION6
gpio_export BOARD_ID2 GPION7

# BATTERY_DETECT
gpio_export BATTERY_DETECT GPIOQ6
gpio_set BATTERY_DETECT 0

gpio_export SMB_PMBUS_ISO_HSC_R2_ALERT_0_1 GPIOS0
gpio_export SMB_PMBUS_ISO_HSC_R2_ALERT_2_3 GPIOS1
gpio_export OCP_V3_6_PRSNTB_R_N GPIOS2
gpio_export OCP_V3_7_PRSNTB_R_N GPIOS3
gpio_export PD_GPIOS4 GPIOS4
gpio_set PD_GPIOS4 0
gpio_export PD_GPIOS5 GPIOS5
gpio_export PD_GPIOS6 GPIOS6
gpio_set PD_GPIOS6 0
gpio_export PD_GPIOS7 GPIOS7
gpio_set PD_GPIOS7 0

gpio_export RST_SMB_CARRIER_N GPIOY0
gpio_export OCP_V3_0_PRSNTB_R_N GPIOY3

gpio_export OCP_V3_1_PRSNTB_R_N GPIOZ2
gpio_export OCP_V3_2_PRSNTB_R_N GPIOZ3

gpio_export PU_GPIOZ4 GPIOZ4
gpio_set PU_GPIOZ4 0
gpio_export PU_GPIOZ5 GPIOZ5
gpio_set PU_GPIOZ5 0
gpio_export PU_GPIOZ6 GPIOZ6
gpio_set PU_GPIOZ6 0
gpio_export PU_GPIOZ7 GPIOZ7
gpio_set PU_GPIOZ7 1

gpio_export NCSI_BMC_TX_EN GPIOT1
gpio_set NCSI_BMC_TX_EN 0
gpio_export NCSI_BMC_TXD0 GPIOT2
gpio_set NCSI_BMC_TXD0 0
gpio_export NCSI_BMC_TXD1 GPIOT3
gpio_set NCSI_BMC_TXD1 0
gpio_export PD_GPIOT4 GPIOT4
gpio_set PD_GPIOT4 0
gpio_export PD_GPIOT5 GPIOT5
gpio_set PD_GPIOT5 0

gpio_export NCSI_BMC_2_TX_EN GPIOT7
gpio_set NCSI_BMC_2_TX_EN 0
gpio_export NCSI_BMC_2_TXD0 GPIOU0
gpio_set NCSI_BMC_2_TXD0 0
gpio_export NCSI_BMC_2_TXD1 GPIOU1
gpio_set NCSI_BMC_2_TXD1 0
gpio_export PD_GPIOU2 GPIOU2
gpio_set PD_GPIOU2 0
gpio_export PD_GPIOU3 GPIOU3
gpio_set PD_GPIOU3 0

gpio_export USB2_SEL0 GPIOZ0
gpio_set USB2_SEL0 0
gpio_export USB2_SEL1 GPIOZ1
gpio_set USB2_SEL1 0

# To enable GPIOP
devmem_clear_bit $(scu_addr 88) 16
devmem_clear_bit $(scu_addr 88) 17
devmem_clear_bit $(scu_addr 88) 18
devmem_clear_bit $(scu_addr 88) 19
devmem_clear_bit $(scu_addr 88) 20
devmem_clear_bit $(scu_addr 88) 21
devmem_clear_bit $(scu_addr 88) 22
devmem_clear_bit $(scu_addr 88) 23

gpio_export BMC_FORCE_THROTTLE_N GPIOP6
gpio_set BMC_FORCE_THROTTLE_N 1
gpio_export HSC_TIMER_R_N GPIOP2
gpio_export HSC_OC_R_N GPIOP3
gpio_export HSC_UV_R_N GPIOP4
gpio_export PMBUS_HSC_ALERT_R_N GPIOP5
gpio_export FM_SYS_THROTTLE_R GPIOP7

gpio_export EP0_CBL_PRSNT_R1 GPIOJ0
gpio_export EP1_CBL_PRSNT_R1 GPIOJ1
gpio_export EP2_CBL_PRSNT_R1 GPIOJ2
gpio_export EP3_CBL_PRSNT_R1 GPIOJ3
gpio_export MB0_CBL_PRSNT_P1_R1 GPIOJ4
gpio_export MB0_CBL_PRSNT_P0_R1 GPIOJ5
gpio_export MB1_CBL_PRSNT_P1_R1 GPIOJ6
gpio_export MB1_CBL_PRSNT_P0_R1 GPIOJ7

gpio_export RST_PMBUS_N GPIOY1
gpio_set RST_PMBUS_N 1

gpio_export RST_FIO_IOEXP_N GPIOY2
gpio_set RST_FIO_IOEXP_N 1

gpio_export CPLD_BMC_GPIO_R_01 GPIOAC3

gpio_export CPLD_BMC_GPIO_R_00 GPIOAC1
gpio_set CPLD_BMC_GPIO_R_00 0


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

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

. /usr/local/bin/openbmc-utils.sh
. /usr/local/fbpackages/utils/ast-functions

KVSET_CMD=/usr/bin/kv

echo "Setup TCA9539 input/output pin"
i2cset -y -f 5 0x77 0x06 0x00
i2cset -y -f 5 0x77 0x07 0x0f
i2cset -y -f 5 0x77 0x02 0x0f

#Setup BB MAXIM ADC
gp_board_id0=$(gpio_get BOARD_ID0 GPION5)
gp_board_id1=$(gpio_get BOARD_ID1 GPION6)
if [ $gp_board_id0 -eq 1 ] && [ $gp_board_id1 -eq 0 ]; then
  i2c_device_add 23 0x33 max11615
  i2c_device_add 23 0x35 max11617
fi

#Setup TMP75 for carrier board
gp_carrier1_prsnt=$(gpio_get NVME_0_1_PRSNTB_R_N)
if [ $gp_carrier1_prsnt -eq 0 ]; then
  i2c_device_add 21 0x48 tmp75
  i2c_device_add 21 0x49 tmp75
  i2cset -y -f 21 0x1d 0x0b 0x2
  i2cset -y -f 21 0x1f 0x0b 0x2
  i2c_device_add 21 0x1d adc128d818
  i2c_device_add 21 0x1f adc128d818
fi

gp_carrier2_prsnt=$(gpio_get NVME_2_3_PRSNTB_R_N)
if [ $gp_carrier2_prsnt -eq 0 ]; then
  i2c_device_add 22 0x48 tmp75
  i2c_device_add 22 0x49 tmp75
  i2cset -y -f 22 0x1d 0x0b 0x2
  i2cset -y -f 22 0x1f 0x0b 0x2
  i2c_device_add 22 0x1d adc128d818
  i2c_device_add 22 0x1f adc128d818
fi

#Set PWR_AVR to 128 samples
i2cset -y -f 5 0x11 0xd4 0x3F1E w

#Set FIO LED
i2cset -y -f 0x08 0x77 0x7 0x3F
i2cset -y -f 0x08 0x77 0x3 0x80

#Check carrier#0 type
retry=0
while [ $retry -lt 3 ]
do
carrier0=`i2ctransfer -f -y 23 w2@0x17 0x00 0x05 r1`
if [ $carrier0 = "0xcc" ]; then
    $KVSET_CMD set "carrier_0" "m.2"
    i2cset -y -f 21 0x77 0x2 0xFF
    echo "Carrier#0 Type is m.2"
    #Set M.2 carrier INA219 Calibration register
    i2cset -y -f 21 0x45 5 0xC800 w
    break
elif [ $carrier0 = "0x22" ] || [ $carrier0 = "0x44" ]; then
    $KVSET_CMD set "carrier_0" "e1.s"
    i2cset -y -f 21 0x77 0x02 0x00
    echo "Carrier#0 Type is e1.s"
    break
elif [ $carrier0 = "0x33" ]; then
    $KVSET_CMD set "carrier_0" "e1.s_v2"
    i2cset -y -f 21 0x77 0x02 0x00
    echo "Carrier#0 Type is e1.s_v2"
    break
elif [ $carrier0 = "0xdd" ]; then
    $KVSET_CMD set "carrier_0" "m.2_v2"
    i2cset -y -f 21 0x77 0x02 0xFF
    echo "Carrier#0 Type is m.2_v2"
    #Set M.2 carrier INA219 Calibration register
    i2cset -y -f 21 0x45 5 0xC800 w
    break
else
    retry=$(($retry+1))
fi
done

#Check carrier#1 type
retry=0
while [ $retry -lt 3 ]
do
carrier1=`i2ctransfer -f -y 23 w2@0x17 0x00 0x06 r1`
if [ $carrier1 = "0xcc" ]; then
    $KVSET_CMD set "carrier_1" "m.2"
    i2cset -y -f 22 0x77 0x2 0xFF
    echo "Carrier#1 Type is m.2"
    #Set M.2 carrier INA219 Calibration register
    i2cset -y -f 22 0x45 5 0xC800 w
    break
elif [ $carrier1 = "0x22" ] || [ $carrier1 = "0x44" ]; then
    $KVSET_CMD set "carrier_1" "e1.s"
    i2cset -y -f 22 0x77 0x02 0x00
    echo "Carrier#1 Type is e1.s"
    break
elif [ $carrier1 = "0x33" ]; then
    $KVSET_CMD set "carrier_1" "e1.s_v2"
    i2cset -y -f 22 0x77 0x02 0x00
    echo "Carrier#1 Type is e1.s_v2"
    break
elif [ $carrier1 = "0xdd" ]; then
    $KVSET_CMD set "carrier_1" "m.2_v2"
    i2cset -y -f 22 0x77 0x02 0xFF
    echo "Carrier#1 Type is m.2_v2"
    #Set M.2 carrier INA219 Calibration register
    i2cset -y -f 22 0x45 5 0xC800 w
    break
else
    retry=$(($retry+1))
fi
done

# Mark driver loaded
echo > /tmp/driver_probed


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

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

rev=$(kv get mb_rev)

if [ $rev -eq 0 ]; then
  gp_carrier1_prsnt=$(gpio_get NVME_0_1_PRSNTB_R_N)
  if [ $gp_carrier1_prsnt -eq 0 ]; then
    i2cset -y -f 21 0x77 0x6 0
  fi

  gp_carrier2_prsnt=$(gpio_get NVME_2_3_PRSNTB_R_N)
  if [ $gp_carrier2_prsnt -eq 0 ]; then
    i2cset -y -f 22 0x77 0x6 0
  fi
fi


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

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

. /usr/local/bin/openbmc-utils.sh

if [ "$(gpio_get OCP_V3_0_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 1 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_4_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 9 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_1_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 2 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_5_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 10 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_2_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 4 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_6_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 11 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_3_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 7 0x50 24c32
fi

if [ "$(gpio_get OCP_V3_7_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 13 0x50 24c32
fi

if [ "$(gpio_get NVME_0_1_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 21 0x50 24c64
fi

if [ "$(gpio_get NVME_2_3_PRSNTB_R_N)" == "0" ]; then
  i2c_device_add 22 0x50 24c64
fi


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

gp_sys_pwr_good=$(gpio_get SYS_PWR_READY GPIOB3)
if [ $gp_sys_pwr_good -eq 1 ]; then
  echo "Setup TMP422 driver"
  i2c_device_add 6 0x4d tmp422
  i2c_device_add 6 0x4e tmp422

  echo "Setup VR sensor driver"

  gp_board_id0=$(gpio_get BOARD_ID0 GPION5)
  gp_board_id1=$(gpio_get BOARD_ID1 GPION6)
  gp_board_id2=$(gpio_get BOARD_ID2 GPION7)

  if [ $gp_board_id0 -eq 1 ] && [ $gp_board_id1 -eq 1 ] && [ $gp_board_id2 -eq 0 ]; then
    i2c_device_add 5 0x34 mpq8645p
    i2c_device_add 5 0x36 mpq8645p
    i2c_device_add 5 0x60 isl69260
    i2c_device_add 5 0x61 isl69260
    i2c_device_add 5 0x72 isl69260
    i2c_device_add 5 0x75 isl69260
  else
    i2c_device_add 5 0x30 mpq8645p
    i2c_device_add 5 0x31 mpq8645p
    i2c_device_add 5 0x32 mpq8645p
    i2c_device_add 5 0x33 mpq8645p
    i2c_device_add 5 0x34 mpq8645p
    i2c_device_add 5 0x35 mpq8645p
    i2c_device_add 5 0x36 mpq8645p
    i2c_device_add 5 0x3b mpq8645p
  fi

  sv restart sensord
  kv set pon_driver_loaded on
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: Power on Server
### END INIT INFO
. /usr/local/fbpackages/utils/ast-functions

store_clear_por


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

i2cset -f -y 17 0x2c 0x00 0x24 0x04 0x14 0x25 0x00 0x02 0xBB 0x20 0x02 0x00 0x00 0x00 0x01 0x32 0x01 0x32 0x32 s
i2cset -f -y 17 0x2c 0xff 0x01 s
i2cset -f -y 19 0x2c 0x00 0x24 0x04 0x14 0x25 0x00 0x02 0xBB 0x20 0x02 0x00 0x00 0x00 0x01 0x32 0x01 0x32 0x32 s
i2cset -f -y 19 0x2c 0xff 0x01 s
i2cset -f -y 20 0x2c 0x00 0x24 0x04 0x14 0x25 0x00 0x02 0xBB 0x20 0x02 0x00 0x00 0x00 0x01 0x32 0x01 0x32 0x32 s
i2cset -f -y 20 0x2c 0xff 0x01 s


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

addr=$(/usr/bin/mac-util --get)
if [[ "$addr" == "ff:ff:ff:ff:ff:ff" ]]
then
  addr="02:00:00:00:00:01"
fi
modprobe g_ether host_addr=02:00:00:00:00:02 dev_addr=$addr

ifconfig usb0 up

dhclient -d -pf /var/run/dhclient.usb0.pid usb0 > /dev/null 2>&1 &


#!/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 RTC with NTP
sync_rtc()
{
  while [ "$(ntpq -c rv | grep leap_none)" == "" ]
  do
    sleep 10
  done
  # if leap code returned normal sync state
  echo Syncing up RTC time with NTP...
  test -x /etc/init.d/hwclock.sh && /etc/init.d/hwclock.sh stop
  return
}

sync_rtc


#!/bin/bash

SLOT_NAME=$1

case $SLOT_NAME in
    scm)
      SLOT_NUM=1
      ;;
    *)
      N=${0##*/}
      N=${N#[SK]??}
      echo "Usage: $N {scm}"
      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 runnung"
  exit 1
else
  touch $PID_FILE
fi

# Set crashdump timestamp
sys_runtime=$(awk '{print $1}' /proc/uptime)
sys_runtime=$(printf "%0.f" "$sys_runtime")
echo $((sys_runtime+630)) > /tmp/cache_store/fru${SLOT_NUM}_crashdump

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

echo "Auto Dump for $SLOT_NAME Started"

#HEADER LINE for the dump
$DUMP_SCRIPT "time" > "$CRASHDUMP_FILE"
#COREID dump
$DUMP_SCRIPT "$SLOT_NAME" "coreid" >> "$CRASHDUMP_FILE"
#MSR dump
$DUMP_SCRIPT "$SLOT_NAME" "msr" >> "$CRASHDUMP_FILE"
# 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"

tar zcf "$CRASHDUMP_LOG_ARCHIVE" -C "$(dirname "$CRASHDUMP_FILE")" "$(basename "$CRASHDUMP_FILE")" && \
rm -rf "$CRASHDUMP_FILE" && \
logger -t "ipmid" -p daemon.crit "Crashdump for $SLOT_NAME is generated at $CRASHDUMP_LOG_ARCHIVE"

echo "Auto Dump for $SLOT_NAME Completed"

rm $PID_FILE
exit 0


#!/bin/bash

BIC_UTIL="/usr/bin/bic-util"
SENSOR_HISTORY=180

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

if [ "$#" -ne 2 ]; then
  echo "$0 <slot#> coreid    ==> for CPU CoreID"
  echo "$0 <slot#> msr       ==> for CPU MSR"
  echo "$0 <slot#> sensors   ==> for sensor history"
  echo "$0 <slot#> threshold ==> for sensor threshold"
  exit 1
fi

echo ""

if [ "$2" = "coreid" ]; then
  [ -r /usr/local/fbpackages/crashdump/crashdump_coreid ] && $BIC_UTIL "$1" --me_file /usr/local/fbpackages/crashdump/crashdump_coreid
elif [ "$2" = "msr" ]; then
  [ -r /usr/local/fbpackages/crashdump/crashdump_msr ] && $BIC_UTIL "$1" --me_file /usr/local/fbpackages/crashdump/crashdump_msr
elif [ "$2" = "sensors" ]; then
  /usr/local/bin/sensor-util all --history $SENSOR_HISTORY
elif [ "$2" = "threshold" ]; then
  /usr/local/bin/sensor-util all --threshold
fi

exit 0


#!/bin/sh
#
# Copyright 2020-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
#
exec /usr/local/bin/front-paneld


#!/bin/bash
#
# Copyright 2020-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-front-paneld
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start front panel control daemon
### END INIT INFO

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


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

exec /usr/local/bin/ipmbd 0 1


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

exec /usr/local/bin/ipmbd 4 1


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

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

declare -a ipmbd_list
ipmbd_list=(0 4)
for ipmb in "${ipmbd_list[@]}"
do
    runsv "/etc/sv/ipmbd_$ipmb" &> /dev/null &
done

echo "Done"


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

exec /usr/local/bin/sensord scm smb psu1 psu2


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

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

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

echo "done."


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

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

usage() {
    echo "Usage: $(basename $0) [Fan Unit (1..4)]"
}

# FCB PWM 1 ~ 4 control Fantray 1 2 3 4
show_pwm()
{
    pwm="$FCMCPLD_SYSFS_DIR/fan$1_pwm"
    val=$(head -n 1 "$pwm")

    # Convert the percentage to our 1/64th level (0-63) and round.
    echo "$(( (val * 1000 / 63 + 5) /10 ))%"
}

show_rpm()
{
    front_rpm="$FCMCPLD_SYSFS_DIR/fan$((($1 * 2 - 1)))_input"
    rear_rpm="$FCMCPLD_SYSFS_DIR/fan$((($1 * 2)))_input"
    echo "$(cat "$front_rpm"), $(cat "$rear_rpm")"
}

set -e

# refer to the comments in init_pwn.sh regarding
# the fan unit and tacho mapping
if [ "$#" -eq 0 ]; then
    FANS="1 2 3 4"
elif [ "$#" -eq 1 ]; then
    if [ "$1" -le 0 ] || [ "$1" -ge 5 ]; then
        echo "Fan $1: not a valid Fan Unit"
        usage
        exit 1
    fi
    FANS="$1"
else
    usage
    exit 1
fi

for fan in $FANS; do
    FAN="${fan}"
    UNIT=${FAN}

    echo "Fan $fan RPMs: $(show_rpm "$UNIT"), ($(show_pwm "$UNIT"))"
done


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

echo "Setup fan speed..."

echo "Setting fan speed to 50%..."
/usr/local/bin/set_fan_speed.sh 50

echo "Starting fscd..."
runsv /etc/sv/fscd > /dev/null 2>&1 &
echo "done."


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

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

usage() {
    echo "Usage: $(basename $0) <PERCENT (0..100)> <Fan Unit (1..4)>"
}

set -e

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

# check percent argument be number
check_number=$(echo "$1" | grep -E "^[0-9]+$")
if [ "$check_number" = '' ]; then
    echo "Percent argument can be only integer number"
    usage
    exit 1
fi

# check percent in range 0-100
if [ "$1" -lt 0 ] || [ "$1" -gt 100 ]; then
    echo "Percent argument out of range"
    usage
    exit 1
fi

# FCB PWM 1 ~ 4 control Fantray 1 2 3 4
if [ "$#" -eq 1 ]; then
    FAN_UNIT="1 2 3 4"
else
    if [ "$2" -le 0 ] || [ "$2" -gt 4 ]; then
        echo "Fan $2: not a valid Fan Unit"
        exit 1
    fi

    FAN="$2"
    FAN_UNIT=$((FAN / 1))
fi

# Convert the percentage to our 1/64th level (0-63) and round.
step=$(( ($1 * 630 + 5) / 1000 ))
cnt=1

for unit in ${FAN_UNIT}; do
    pwm="$FCMCPLD_SYSFS_DIR/fan${unit}_pwm"
    echo "$step" > "${pwm}"
    if [ "$#" -eq 1 ]; then
        echo "Successfully set fan $cnt speed to $1%"
        cnt=$((cnt + 1))
    else
        echo "Successfully set fan $2 speed to $1%"
    fi
done


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

#shellcheck disable=SC1091
# Do not change this line to openbmc-utils.sh, or it will generate a source loop.
. /usr/local/bin/i2c-utils.sh

SCMCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 2-003e)
SMBCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 12-003e)
PWRCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 47-003e)
FCMCPLD_SYSFS_DIR=$(i2c_device_sysfs_abspath 48-003e)

PWR_USRV_SYSFS="${SCMCPLD_SYSFS_DIR}/com_exp_pwr_enable"
PWR_USRV_FORCE_OFF="${SCMCPLD_SYSFS_DIR}/com_exp_pwr_force_off"
PWR_TH_RST_SYSFS="${SMBCPLD_SYSFS_DIR}/mac_reset_n"

# CPLD version
SMB_CPLD_VER=$(head -n 1 "$SMBCPLD_SYSFS_DIR/cpld_ver" 2> /dev/null)
SMB_CPLD_SUB_VER=$(head -n 1 "$SMBCPLD_SYSFS_DIR/cpld_sub_ver" 2> /dev/null)
PWR_CPLD_VER=$(head -n 1 "$PWRCPLD_SYSFS_DIR/cpld_ver" 2> /dev/null)
PWR_CPLD_SUB_VER=$(head -n 1 "$PWRCPLD_SYSFS_DIR/cpld_sub_ver" 2> /dev/null)
SCM_CPLD_VER=$(head -n 1 "$SCMCPLD_SYSFS_DIR/cpld_ver" 2> /dev/null)
SCM_CPLD_SUB_VER=$(head -n 1 "$SCMCPLD_SYSFS_DIR/cpld_sub_ver" 2> /dev/null)
FCM_CPLD_VER=$(head -n 1 "$FCMCPLD_SYSFS_DIR/cpld_ver" 2> /dev/null)
FCM_CPLD_SUB_VER=$(head -n 1 "$FCMCPLD_SYSFS_DIR/cpld_sub_ver" 2> /dev/null)

export SCMCPLD_SYSFS_DIR
export SMBCPLD_SYSFS_DIR
export PWRCPLD_SYSFS_DIR
export FCMCPLD_SYSFS_DIR
export PWR_USRV_SYSFS
export PWR_USRV_FORCE_OFF
export PWR_TH_RST_SYSFS
export SMB_CPLD_VER
export SMB_CPLD_SUB_VER
export PWR_CPLD_VER
export PWR_CPLD_SUB_VER
export SCM_CPLD_VER
export SCM_CPLD_SUB_VER
export FCM_CPLD_VER
export FCM_CPLD_SUB_VER

wedge_is_us_on() {
    local val0 val1

    val0=$(head -n 1 < "$PWR_USRV_SYSFS" 2> /dev/null)
    val1=$(head -n 1 < "$PWR_USRV_FORCE_OFF" 2> /dev/null)

    if [ "$val0" == "0x1" ] && [ "$val1" == "0x1" ] ; then
        return 0            # powered on
    else
        return 1
    fi

    return 0
}

wedge_board_type_rev() {
    rev=$(wedge_board_rev)
    case $rev in
        0)
            echo "Cloudripper_EVT"
            ;;
        1)
            echo "Cloudripper_EVT2"
            ;;
        2)
            echo "Cloudripper_EVT3"
            ;;
        3)
            echo "Cloudripper_DVT"
            ;;
        4)
            echo "Cloudripper_PVT"
            ;;
        *)
            echo "Cloudripper (Undefine $rev)"
            ;;
    esac
}

wedge_slot_id() {
    printf "1\n"
}

wedge_board_rev() {
    echo "$(( $(head -n 1 "$SMBCPLD_SYSFS_DIR/board_ver") ))"
}

wedge_power_on_board() {
    # Cloudripper SCM power is controlled by SCM CPLD
    return 0
}

wedge_prepare_cpld_update() {
    echo "Stop fscd service."
    systemctl stop fscd.service

    echo "Disable watchdog."
    /usr/local/bin/wdtcli stop

    echo "Set fan speed 40%."
    set_fan_speed.sh 40
}


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

# shellcheck disable=SC1091
source /usr/local/bin/openbmc-utils.sh

DLL_PATH=/usr/lib/libcpldupdate_dll_ast_jtag.so

chip_ver=$(aspeed_g6_chip_ver)

usage() {
    prog=$(basename "$0")
    echo "Usage: $prog -s <CPLD_TYPE> -f <img_file> <hw|sw>"
    echo
    echo "CPLD_TYPE: ( FCM | SCM | SMB | PWR )"
    echo
    echo "img_file: Image file for lattice CPLD"
    echo "  VME file for software mode"
    echo "  JED file for hardware mode"
    echo "options:"
    echo "  hw: Program the CPLD using JTAG hardware mode"
    echo "  sw: Program the CPLD using JTAG software mode"
    echo
    echo
}

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

CPLD_TYPE="$2"
UPDATE_IMG="$4"

enable_fcm_jtag_chain(){
    gpio_set_value BMC_JTAG_MUX_IN_R      1
    gpio_set_value BMC_FCM_SEL_R          0

    gpio_set_value PWR_CPLD_JTAG_EN_N     1
    gpio_set_value FCM_CPLD_JTAG_EN_N     0
    gpio_set_value SYS_CPLD_JTAG_EN_N     1
    gpio_set_value SCM_CPLD_JTAG_EN_N     1
    gpio_set_value BMC_SCM_CPLD_EN_R      1
    gpio_set_value BMC_FPGA_JTAG_EN       1

    gpio_set_value DOM_FPGA1_JTAG_EN_N_R  1
    gpio_set_value DOM_FPGA2_JTAG_EN_N_R  1
    # set PWR_CPLD_HITLESS to 1 will disable wedge_power.sh reset -s
    gpio_set_value PWR_CPLD_HITLESS_R     1
}

enable_smb_jtag_chain(){
    gpio_set_value BMC_JTAG_MUX_IN_R      1
    gpio_set_value BMC_FCM_SEL_R          1
    gpio_set_value PWR_CPLD_JTAG_EN_N     1
    gpio_set_value FCM_CPLD_JTAG_EN_N     1

    gpio_set_value SYS_CPLD_JTAG_EN_N     0
    gpio_set_value SCM_CPLD_JTAG_EN_N     1
    gpio_set_value BMC_SCM_CPLD_EN_R      1
    gpio_set_value BMC_FPGA_JTAG_EN       1

    gpio_set_value DOM_FPGA1_JTAG_EN_N_R  1
    gpio_set_value DOM_FPGA2_JTAG_EN_N_R  1
    # set PWR_CPLD_HITLESS to 1 will disable wedge_power.sh reset -s
    gpio_set_value PWR_CPLD_HITLESS_R     1
}

enable_scm_jtag_chain(){
    gpio_set_value BMC_JTAG_MUX_IN_R      1
    gpio_set_value BMC_FCM_SEL_R          1
    gpio_set_value PWR_CPLD_JTAG_EN_N     1

    gpio_set_value FCM_CPLD_JTAG_EN_N     1
    gpio_set_value SYS_CPLD_JTAG_EN_N     1
    gpio_set_value SCM_CPLD_JTAG_EN_N     0
    gpio_set_value BMC_SCM_CPLD_EN_R      0
    gpio_set_value BMC_FPGA_JTAG_EN       1

    gpio_set_value DOM_FPGA1_JTAG_EN_N_R  1
    gpio_set_value DOM_FPGA2_JTAG_EN_N_R  1
    # set PWR_CPLD_HITLESS to 1 will disable wedge_power.sh reset -s
    gpio_set_value PWR_CPLD_HITLESS_R     1
}

enable_pdb_jtag_chain(){
    gpio_set_value BMC_JTAG_MUX_IN_R      1
    gpio_set_value BMC_FCM_SEL_R          1
    gpio_set_value PWR_CPLD_JTAG_EN_N     0

    gpio_set_value FCM_CPLD_JTAG_EN_N     1
    gpio_set_value SYS_CPLD_JTAG_EN_N     1
    gpio_set_value SCM_CPLD_JTAG_EN_N     1
    gpio_set_value BMC_SCM_CPLD_EN_R      1
    gpio_set_value BMC_FPGA_JTAG_EN       1

    gpio_set_value DOM_FPGA1_JTAG_EN_N_R  1
    gpio_set_value DOM_FPGA2_JTAG_EN_N_R  1
    # set PWR_CPLD_HITLESS to 1 will disable wedge_power.sh reset -s
    gpio_set_value PWR_CPLD_HITLESS_R     1
}

disable_jtag_chain(){

    gpio_set_value BMC_JTAG_MUX_IN_R      0
    gpio_set_value BMC_FCM_SEL_R          1

    gpio_set_value FCM_CPLD_JTAG_EN_N     1
    gpio_set_value SYS_CPLD_JTAG_EN_N     1
    gpio_set_value SCM_CPLD_JTAG_EN_N     1
    gpio_set_value BMC_SCM_CPLD_EN_R      1
    gpio_set_value BMC_FPGA_JTAG_EN       1
    gpio_set_value PWR_CPLD_JTAG_EN_N     1

    gpio_set_value DOM_FPGA1_JTAG_EN_N_R  1
    gpio_set_value DOM_FPGA2_JTAG_EN_N_R  1
    # set PWR_CPLD_HITLESS to 0 will enable wedge_power.sh reset -s
    gpio_set_value PWR_CPLD_HITLESS_R     0
}

trap 'rm -rf /tmp/cpld_update' INT TERM QUIT EXIT

echo 1 > /tmp/cpld_update

if [ -e "$UPDATE_IMG" ]; then
    if [[  $CPLD_TYPE == "FCM" ]]; then
        enable_fcm_jtag_chain
    elif [[  $CPLD_TYPE == "SMB" ]]; then
        enable_smb_jtag_chain
    elif [[  $CPLD_TYPE == "SCM" ]]; then
        enable_scm_jtag_chain
    elif [[  $CPLD_TYPE == "PWR" ]]; then
        enable_pdb_jtag_chain
   else
        echo 'argument '"$CPLD_TYPE"' is wrong'
        exit 1
    fi
else
    echo 'argument '"$UPDATE_IMG"' not exist'
    exit 1
fi

expect=0
cpld_update_sw_mode(){
        expect=1
        for n in {1..5}
        do
                echo "Program $CPLD_TYPE $n times"
                ispvm -f 1000 dll $DLL_PATH "${UPDATE_IMG}"
                result=$?
                if [ $result -eq $expect ]; then
                        break
                fi
        done
}

cpld_update_hw_mode(){
        expect=0
        if [ "$chip_ver" == "A3" ]; then
                if [[  $CPLD_TYPE == "PWR" ]]; then
                        cpldprog -p "${UPDATE_IMG}" -R
                else
                        cpldprog -p "${UPDATE_IMG}"
                fi
        else
                echo "Only AST262x A3 chip can support HW Mode !!!"
                exit 1
        fi
        result=$?
}

wedge_prepare_cpld_update

case $5 in
    hw)
        cpld_update_hw_mode
        ;;
    sw)
        cpld_update_sw_mode
        ;;
    *)
        # default: hw mode
        cpld_update_hw_mode
        ;;
esac

disable_jtag_chain

# 0 is returned upon upgrade success
if [ $result -eq $expect ]; then
    echo "Upgrade successful."
    exit 0
else
    echo "Upgrade failure. Return code from utility : $result"
    exit 1
fi


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

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

echo "SMB_SYSCPLD: $((SMB_CPLD_VER)).$((SMB_CPLD_SUB_VER))"
echo "SMB_PWRCPLD: $((PWR_CPLD_VER)).$((PWR_CPLD_SUB_VER))"
echo "SCMCPLD: $((SCM_CPLD_VER)).$((SCM_CPLD_SUB_VER))"
echo "FCMCPLD: $((FCM_CPLD_VER)).$((FCM_CPLD_SUB_VER))"


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

#shellcheck disable=SC1091,SC2034
. /usr/local/bin/openbmc-utils.sh

DOMFPGA1_SYSFS_DIR=$(i2c_device_sysfs_abspath 13-0060)
DOMFPGA2_SYSFS_DIR=$(i2c_device_sysfs_abspath 5-0060)

dump_fpga_version() {
    local fpga_dir=$1
    local fpga_name=$2

    fpga_ver=$(head -n 1 "$fpga_dir/fpga_ver" 2> /dev/null)
    ret=$?
    if [ $ret -ne 0 ]; then
        echo "${fpga_name} is not detected"
        return
    fi

    fpga_sub_ver=$(head -n 1 "${fpga_dir}/fpga_sub_ver" 2> /dev/null)
    ret=$?
    if [ $ret -ne 0 ]; then
        echo "${fpga_name} is not detected"
        return
    fi

    echo "${fpga_name}: $((fpga_ver)).$((fpga_sub_ver))"
}

dump_fpga_version "$DOMFPGA1_SYSFS_DIR" DOMFPGA1
dump_fpga_version "$DOMFPGA2_SYSFS_DIR" DOMFPGA2


#!/bin/bash
#
# Copyright 2020-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:    85
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

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

wedge_power_on_board

echo -n "Checking microserver power status ... "
if wedge_is_us_on 10 "."; then
    echo "on"
    on=1
else
    echo "off"
    on=0
fi

if [ $on -eq 0 ]; then
    # Power on now
    wedge_power.sh on -f
fi


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

#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

SMB_DIR=$SMBCPLD_SYSFS_DIR
SCM_CMD=$SMB_DIR"/scm_present_int_status"
FAN_CMD=$FCMCPLD_SYSFS_DIR
DEBUG_CARD_PRSNT_GPIO="/tmp/gpionames/DEBUG_PRESENT_N/value"

function usage
{
  program=$(basename "$0")
  echo "Usage:"
  echo "     $program "
  echo "     $program <TYPE>"
  echo "     <TYPE>: scm/fan/psu/debug_card"
}

# Will return <key> : <val>
# eg - scm : 1
function get_presence
{
  local file=$1 key=$2
  val=$(head -n1 "$file" | awk '{print substr($0,0,3) }' | sed "s/.x//")
  if [ "$val" = "0" ]; then
      val=1
  else
      val=0
  fi
  echo "$key : $val"
}

# SCM
function get_scm_presence
{
  key="scm"
  get_presence "$SCM_CMD" "$key"
}

# FANs
function get_fan_presence
{
  local num=1
  while [ $num -le 4 ]; do
      key="fan${num}"
      file="$FAN_CMD/fan${num}_present"
      get_presence "$file" "$key"
      (( num++ ))
  done
}

# Power supplies
function get_psu_presence
{
  for i in {1..2}; do
      key="psu${i}"
      file="$SMB_DIR/psu_present_${i}_N_int_status"
      get_presence "$file" "$key"
  done
}

# Debug card
function get_debug_card_presence
{
  key="debug_card"
  val=$(head -n1 $DEBUG_CARD_PRSNT_GPIO | awk '{print substr($0,0,3) }' | sed "s/.x//")
  if [ "$val" = "0" ]; then
      val=0
  else
      val=1
  fi
  echo $key ":" $val
}

# get all info
if [[ $1 = "" ]]; then
  echo "CARDS"
  get_scm_presence

  echo "FANS"
  get_fan_presence

  echo "POWER SUPPLIES"
  get_psu_presence

  echo "DEBUG CARD"
  get_debug_card_presence

  exit 0
else
  if [[ $1 = "scm" ]]; then
    get_scm_presence
  elif [[ $1 = "fan" ]]; then
    get_fan_presence
  elif [[ $1 = "psu" ]]; then
    get_psu_presence
  elif [[ $1 = "debug_card" ]]; then
    get_debug_card_presence
  else
    usage
  fi
  exit 0
fi


#!/bin/bash
#
# Copyright 2020-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() {
	program=$(basename "$0")
	echo "Usage:"
	echo "  $program <led>"
	echo "  <led>: sys/fan/psu/scm"
	echo "  $program without option to display all leds"
	echo
	echo "Output:"
	echo "  <led>: color"
	exit 1
}

read_led_color_value() {
	case $1 in
	"sys" | "fan" | "psu" | "scm")
		cat "/sys/class/leds/$1/multi_intensity"
	;;
	*)
		usage
	;;
	esac
}

# color: red-blue-green (value: 0-255)
#   red(255 0 0)
#   blue(0 255 0)
#   green(0 0 255)
#   amber(255 0 45)
print_led_color() {
	color=""
	case $(read_led_color_value "$1") in
	"255 0 0")
		color="red"
		;;
	"0 255 0")
		color="blue"
		;;
	"0 0 255")
		color="green"
		;;
	"255 0 45")
		color="amber"
		;;
	"0 0 0")
		color="off"
		;;
	*)
		color="unknown"
		;;
	esac
	echo "$1: $color"
}

if [ $# -ge 1 ]; then
	led=$1
	case $1 in
		"?" | "help")
			usage
			;;
		"sys" | "fan" | "psu" | "scm")
			print_led_color "$led"
			exit 0
			;;
		*)
			usage
			;;
	esac
fi

leds=("sys" "fan" "psu" "scm")
for led in ${leds[*]}
do
	print_led_color "$led"
done


#!/bin/bash
#
# Copyright 2020-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:     60 S
# Default-Stop:
# Short-Description:  Set up GPIO pins as appropriate
### END INIT INFO

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

# Export GPIO
gpio_export_by_name "${ASPEED_GPIO}" GPIOB1 NP_POWER_STABLE_3V3_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOF3 SMB_CPLD_HITLESS_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOF6 BMC_EMMC_CD_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOF7 BMC_EMMC_WP_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOG2 CPLD_INT_BMC_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOG4 CPLD_RESERVED_2_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOG6 BMC_DOM_FPGA2_RST_L_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOG7 FCM_CARD_PRESENT
gpio_export_by_name "${ASPEED_GPIO}" GPIOH0 GPIO_H0
gpio_export_by_name "${ASPEED_GPIO}" GPIOH1 GPIO_H1
gpio_export_by_name "${ASPEED_GPIO}" GPIOH2 GPIO_H2
gpio_export_by_name "${ASPEED_GPIO}" GPIOH3 GPIO_H3
gpio_export_by_name "${ASPEED_GPIO}" GPIOH4 GPIO_H4
gpio_export_by_name "${ASPEED_GPIO}" GPIOH5 GPIO_H5
gpio_export_by_name "${ASPEED_GPIO}" GPIOM1 BMC_UART_SEL_5
gpio_export_by_name "${ASPEED_GPIO}" GPIOM3 SCM_CPLD_HITLESS_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOO0 PWR_CPLD_JTAG_EN_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOO1 BMC_JTAG_MUX_IN_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOO2 FCM_CPLD_JTAG_EN_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOO3 SYS_CPLD_JTAG_EN_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOO4 DOM_FPGA1_JTAG_EN_N_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOO5 GPIO_H6
gpio_export_by_name "${ASPEED_GPIO}" GPIOO6 SCM_CPLD_JTAG_EN_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOO7 GPIO_H7
gpio_export_by_name "${ASPEED_GPIO}" GPIOP0 BMC_UART_SEL_2
gpio_export_by_name "${ASPEED_GPIO}" GPIOP6 BMC_CPLD_SPARE3_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ0 FCM_CPLD_HITLESS_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ2 PWR_CPLD_HITLESS_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ3 BMC_IN_P1220_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ4 BMC_FCM_SEL_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ6 BMC_CPLD_SPARE4_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOQ7 SYS_CPLD_RST_L_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOR0 BMC_CPLD_SPARE2_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOR1 DOM_FPGA2_JTAG_EN_N_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOR2 BMC_CPLD_SPARE1_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOR3 DEBUG_PRESENT_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOR4 BMC_SCM_CPLD_EN_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOR5 BMC_CPLD_TPM_RST_L_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOR6 BMC_DOM_FPGA1_RST_L_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOV1 CPLD_RESERVED_3_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOV2 CPLD_RESERVED_4_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOV3 BMC_P1220_SYS_OK
gpio_export_by_name "${ASPEED_GPIO}" GPIOV4 BMC_POWER_OK
gpio_export_by_name "${ASPEED_GPIO}" GPIOW0 SCM_LPC_AD_0
gpio_export_by_name "${ASPEED_GPIO}" GPIOW1 SCM_LPC_AD_1
gpio_export_by_name "${ASPEED_GPIO}" GPIOW2 SCM_LPC_AD_2
gpio_export_by_name "${ASPEED_GPIO}" GPIOW3 SCM_LPC_AD_3
gpio_export_by_name "${ASPEED_GPIO}" GPIOW4 SCM_LPC_CLK
gpio_export_by_name "${ASPEED_GPIO}" GPIOW5 SCM_LPC_FRAME_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOW6 SCM_LPC_SERIRQ_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOW7 BMC_LPCRST_N
gpio_export_by_name "${ASPEED_GPIO}" GPIOX6 BMC_FPGA_JTAG_EN
gpio_export_by_name "${ASPEED_GPIO}" GPIOY2 CPU_CATERR_MSMI_R
gpio_export_by_name "${ASPEED_GPIO}" GPIOY3 BMC_EMMC_RST_N

# Set GPIO direction
gpio_set_direction NP_POWER_STABLE_3V3_R    "in"
gpio_set_direction SMB_CPLD_HITLESS_R       "out"
gpio_set_direction BMC_EMMC_CD_N            "in"
gpio_set_direction BMC_EMMC_WP_N            "in"
gpio_set_direction CPLD_INT_BMC_R           "in"
gpio_set_direction CPLD_RESERVED_2_R        "in"
gpio_set_direction BMC_DOM_FPGA2_RST_L_R    "out"
gpio_set_direction FCM_CARD_PRESENT         "in"
gpio_set_direction GPIO_H0                  "in"
gpio_set_direction GPIO_H1                  "in"
gpio_set_direction GPIO_H2                  "in"
gpio_set_direction GPIO_H3                  "in"
gpio_set_direction GPIO_H4                  "in"
gpio_set_direction GPIO_H5                  "in"
gpio_set_direction BMC_UART_SEL_5           "in"
gpio_set_direction SCM_CPLD_HITLESS_R       "out"
gpio_set_direction PWR_CPLD_JTAG_EN_N       "out"
gpio_set_direction BMC_JTAG_MUX_IN_R        "out"
gpio_set_direction FCM_CPLD_JTAG_EN_N       "out"
gpio_set_direction SYS_CPLD_JTAG_EN_N       "out"
gpio_set_direction DOM_FPGA1_JTAG_EN_N_R    "out"
gpio_set_direction GPIO_H6                  "in"
gpio_set_direction SCM_CPLD_JTAG_EN_N       "out"
gpio_set_direction GPIO_H7                  "in"
gpio_set_direction BMC_UART_SEL_2           "in"
gpio_set_direction BMC_CPLD_SPARE3_R        "in"
gpio_set_direction FCM_CPLD_HITLESS_R       "out"
gpio_set_direction PWR_CPLD_HITLESS_R       "out"
gpio_set_direction BMC_IN_P1220_R           "in"
gpio_set_direction BMC_FCM_SEL_R            "out"
gpio_set_direction BMC_CPLD_SPARE4_R        "in"
gpio_set_direction SYS_CPLD_RST_L_R         "out"
gpio_set_direction BMC_CPLD_SPARE2_R        "in"
gpio_set_direction DOM_FPGA2_JTAG_EN_N_R    "out"
gpio_set_direction BMC_CPLD_SPARE1_R        "out"
gpio_set_direction DEBUG_PRESENT_N          "in"
gpio_set_direction BMC_SCM_CPLD_EN_R        "out"
gpio_set_direction BMC_CPLD_TPM_RST_L_R     "out"
gpio_set_direction BMC_DOM_FPGA1_RST_L_R    "out"
gpio_set_direction CPLD_RESERVED_3_R        "in"
gpio_set_direction CPLD_RESERVED_4_R        "in"
gpio_set_direction BMC_P1220_SYS_OK         "in"
gpio_set_direction BMC_POWER_OK             "in"
gpio_set_direction SCM_LPC_AD_0             "in"
gpio_set_direction SCM_LPC_AD_1             "in"
gpio_set_direction SCM_LPC_AD_2             "in"
gpio_set_direction SCM_LPC_AD_3             "in"
gpio_set_direction SCM_LPC_CLK              "in"
gpio_set_direction SCM_LPC_FRAME_N          "in"
gpio_set_direction SCM_LPC_SERIRQ_N         "in"
gpio_set_direction BMC_LPCRST_N             "in"
gpio_set_direction CPU_CATERR_MSMI_R        "in"
gpio_set_direction BMC_EMMC_RST_N           "out"
gpio_set_direction BMC_FPGA_JTAG_EN         "out"

# Set GPIO default value
# Default set to 1. RST signal is active low, set it to high for normal operation
gpio_set_value BMC_DOM_FPGA2_RST_L_R    1
# Default set to 1. Enable signal active low, set it to high for normal operation(No JTAG burn in)
gpio_set_value PWR_CPLD_JTAG_EN_N       1
# Default set to 1. Enable signal active low, set it to high for normal operation(No JTAG burn in)
gpio_set_value BMC_FPGA_JTAG_EN         1
gpio_set_value BMC_JTAG_MUX_IN_R        0
# Default set to 0. Use header for CPLD FW burn in as default setting
gpio_set_value FCM_CPLD_JTAG_EN_N       1
# Default set to 1. Enable signal active low, set it to high for normal operation(No JTAG burn in)
gpio_set_value SYS_CPLD_JTAG_EN_N       1
# Default set to 1. Enable signal active low, set it to high for normal operation(No JTAG burn in)
gpio_set_value DOM_FPGA1_JTAG_EN_N_R    1
# Default set to 1. Enable signal active low, set it to high for normal operation(No JTAG burn in)
gpio_set_value SCM_CPLD_JTAG_EN_N       1
# Default set to 1. Use header for SCM CPLD FW burn in as default setting
gpio_set_value BMC_FCM_SEL_R            0
# Default set to 1. RST signal is active low, set it to high for normal operation
gpio_set_value SYS_CPLD_RST_L_R         1
# Default set to 1. Enable signal active low, set it to high for normal operation(No JTAG burn in)
gpio_set_value DOM_FPGA2_JTAG_EN_N_R    1
# Default set to 1. Use header for SCM CPLD FW burn in as default setting
gpio_set_value BMC_SCM_CPLD_EN_R        1
# Default set to 1. RST signal is active low, set it to high for normal operation
gpio_set_value BMC_DOM_FPGA1_RST_L_R    1
# Default set to 0. 0 to enable wedge_power.sh reset -s, 1 to disable wedge_power.sh reset -s
gpio_set_value PWR_CPLD_HITLESS_R       0
# set BMC_EMMC_RST_N High to prevent EMMC disable
gpio_set_value BMC_EMMC_RST_N 1


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

cmd_bic_ver="bic-util scm 0xe2 0x0b 0x15 0xa0 0x00 0x02"
COUNTER=0
MAX_RETRY=10
rex='^[0-9]+$'

echo "Initializing Bridge-IC I2C Frequency..."
while [ $COUNTER -lt $MAX_RETRY ]; do
    readarray -td ' ' bic_data <<<"$($cmd_bic_ver)"
    if [[ ${bic_data[4]} =~ $rex ]] ; then
        break
    fi
    sleep 3
    ((COUNTER+=1))
done

if [ "$COUNTER" -eq $MAX_RETRY ];then
    echo "Error: Cannot access Bridge-IC." >&2; exit 1
fi

bic_major=$(( 10#${bic_data[3]} ))
bic_minor=$(( 10#${bic_data[4]} ))
bic_ver="$bic_major.${bic_data[4]}"

echo Bridge-IC version is v"$bic_ver".

if [ "$bic_major" -ge 1 ];then
    if [ "$bic_minor" -ge 11 ];then
        ret=$(bic-util scm 0xe0 0x2a 0x15 0xa0 0x00 0x01)
        IANA="15 A0 00"
        if [ -z "${ret##*$IANA*}" ];then
            echo Bridge-IC is initialized SCL to 1Mhz.
        else
            echo "Error: Cannot access Bridge-IC." >&2; exit 1
        fi
    fi
fi

exit 0


#!/bin/sh
#
# Copyright 2020-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_board.sh
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Setup the board
### END INIT INFO

# shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh

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

# Make the backup BIOS flash connect to COMe instead of BMC
# echo 0 > "${SCMCPLD_SYSFS_DIR}/com_spi_oe_n"
# echo 0 > "${SCMCPLD_SYSFS_DIR}/com_spi_sel"

# Setup management port LED
/usr/local/bin/setup_mgmt.sh led &

# Init server_por_cfg
if [ ! -f "/mnt/data/kv_store/server_por_cfg" ]; then
    /usr/bin/kv set server_por_cfg on persistent
fi

# Force COMe's UART connect to BMC UART-5 and FB USB debug
# UART connect to BMC UART-2
echo 0x3 > "$SMBCPLD_SYSFS_DIR/uart_selection"

# export_gpio_pin for PCA9555 4-0027
gpiocli export -c 4-0027 -o 8 --shadow ISO_DEBUG_RST_BTN_N
gpiocli export -c 4-0027 -o 9 --shadow ISO_PWR_BTN_N
gpiocli export -c 4-0027 -o 10 --shadow PWRGD_PCH_PWROK
gpiocli export -c 4-0027 -o 11 --shadow ISO_CB_RESET_N
gpiocli export -c 4-0027 -o 12 --shadow COM_PWROK
gpiocli export -c 4-0027 -o 13 --shadow CPU_CATERR_MSMI_EXT
gpiocli export -c 4-0027 -o 14 --shadow ISO_COM_SUS_S3_N
gpiocli export -c 4-0027 -o 15 --shadow DEBUG_UART_SELECT