1#!/bin/bash
2
3# Provide source directive to shellcheck.
4# shellcheck source=meta-fii/meta-kudo/recipes-kudo/kudo-fw-utility/kudo-fw/kudo-lib.sh
5source /usr/libexec/kudo-fw/kudo-lib.sh
6
7# Usage of this utility
8function usage() {
9  echo "usage: power-util mb [on|off|graceful_shutdown|host_reset|host_cycle|shutdown_ack|hotswap]";
10}
11
12hotswap() {
13  kudo.sh rst hotswap
14}
15
16force_off() {
17  echo "Powering down Server"
18
19  set_gpio_ctrl POWER_OUT 1
20  sleep 6
21  set_gpio_ctrl POWER_OUT 0
22}
23
24power_off() {
25  busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
26  busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Off
27}
28
29power_on() {
30  echo "Powering on Server"
31
32  set_gpio_ctrl POWER_OUT 1
33  sleep 1
34  set_gpio_ctrl POWER_OUT 0
35  busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On
36}
37
38power_status() {
39  st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d "." -f6)
40  if [ "${st}" == "On\"" ]; then
41  echo "on"
42  else
43  echo "off"
44  fi
45}
46
47host_status() {
48  BOOT_OK=$(get_gpio_ctrl S0_FW_BOOT_OK)
49  S5_N=$(get_gpio_ctrl S0_SLPS5_N)
50  if [ "$S5_N" == 1 ] || [ "$BOOT_OK" == 1 ]; then
51    echo "on"
52  else
53    echo "off"
54  fi
55}
56
57timestamp() {
58  date +"%s" # current time
59}
60
61graceful_shutdown() {
62  if [ -f "/run/openbmc/host@0-request" ]; then
63    echo "Shutdown host immediately"
64    power_off
65  else
66    echo "Triggering graceful shutdown"
67    mkdir /run/openbmc
68    timestamp > "/run/openbmc/host@0-shutdown-req-time"
69    set_gpio_ctrl S0_SHD_REQ 0
70    sleep 3
71    set_gpio_ctrl S0_SHD_REQ 1
72  fi
73}
74
75host_reset() {
76    echo "Triggering sysreset pin"
77    busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
78    set_gpio_ctrl S0_SYSRESET 0
79    sleep 1
80    set_gpio_ctrl S0_SYSRESET 1
81}
82
83host_cycle() {
84  echo "DC cycling host"
85  force_off
86  sleep 2
87  power_on
88}
89
90shutdown_ack() {
91  echo "Receive shutdown ACK triggered"
92  power_off
93
94  if [ -f "/run/openbmc/host@0-shutdown-req-time" ]; then
95    rm -rf "/run/openbmc/host@0-shutdown-req-time"
96  fi
97}
98
99if [ $# -lt 2 ]; then
100  echo "Total number of parameter=$#"
101  echo "Insufficient parameter"
102  usage;
103  exit 0;
104fi
105
106if [ "$1" != "mb" ]; then
107  echo "Invalid parameter1=$1"
108  usage;
109  exit 0;
110fi
111
112if [ "$2" = "on" ]; then
113  sleep 3
114  if [ "$(power_status)" == "off" ]; then
115    power_on
116  fi
117elif [ "$2" = "off" ]; then
118  if [ "$(power_status)" == "on" ]; then
119    power_off
120    sleep 6
121    if [ "$(host_status)" == "on" ]; then
122      force_off
123    fi
124  fi
125elif [ "$2" == "hotswap" ]; then
126  hotswap
127elif [ "$2" == "graceful_shutdown" ]; then
128  graceful_shutdown
129elif [ "$2" == "host_reset" ]; then
130  host_reset
131elif [ "$2" == "host_cycle" ]; then
132  host_cycle
133elif [ "$2" == "shutdown_ack" ]; then
134  shutdown_ack
135else
136  echo "Invalid parameter2=$2"
137  usage;
138fi
139
140exit 0;
141