blob: aa742104f06f4c347d9dd8742c019ee858569e37 [file] [log] [blame]
Tung Nguyen8f221bf2020-12-16 05:52:18 +00001#!/bin/bash
2# Usage of this utility
3function usage() {
ThuBaNguyen98f43be2021-07-01 22:29:49 +07004 echo "usage: power-util mb [on|status|cycle|reset|graceful_reset|force_reset|soft_off]";
Tung Nguyen8f221bf2020-12-16 05:52:18 +00005}
6
Tung Nguyen8f221bf2020-12-16 05:52:18 +00007power_status() {
8 st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6)
9 if [ "$st" == "On\"" ]; then
10 echo "on"
11 else
12 echo "off"
13 fi
14}
15
ThuBaNguyen98f43be2021-07-01 22:29:49 +070016shutdown_ack() {
17 if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
18 echo "Receive shutdown ACK triggered after softportoff the host."
19 touch /run/openbmc/host@0-softpoweroff-shutdown-ack
20 else
21 echo "Receive shutdown ACK triggered"
ThuBaNguyen2f952142021-07-05 13:32:26 +070022<<<<<<< HEAD
ThuBaNguyen98f43be2021-07-01 22:29:49 +070023 fi
24}
25
Tung Nguyen8f221bf2020-12-16 05:52:18 +000026graceful_shutdown() {
27 if [ -f "/run/openbmc/host@0-request" ]; then
28 echo "shutdown host immediately"
29 power_off
30 else
31 echo "Triggering graceful shutdown"
32 gpioset -l 0 49=1
33 sleep 1
34 gpioset -l 0 49=0
35 sleep 30s
ThuBaNguyen2f952142021-07-05 13:32:26 +070036=======
37 sleep 3
38 systemctl start obmc-chassis-poweroff@0.target
39>>>>>>> 397e033ef... meta-ampere: power control: refactor host power control
Tung Nguyen8f221bf2020-12-16 05:52:18 +000040 fi
41}
42
ThuBaNguyen98f43be2021-07-01 22:29:49 +070043soft_off() {
44 # Trigger shutdown_req
45 touch /run/openbmc/host@0-softpoweroff
46 gpioset -l 0 49=1
47 sleep 1s
48 gpioset -l 0 49=0
49
50 # Wait for shutdown_ack from the host in 30 seconds
51 cnt=30
52 while [ $cnt -gt 0 ];
53 do
54 # Wait for SHUTDOWN_ACK and create the host@0-softpoweroff-shutdown-ack
55 if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
56 break
57 fi
58 sleep 1
59 cnt=$((cnt - 1))
60 done
61 # Softpoweroff is successed
62 sleep 2
63 rm -rf /run/openbmc/host@0-softpoweroff
64 if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
65 rm -rf /run/openbmc/host@0-softpoweroff-shutdown-ack
66 fi
67 echo 0
68}
69
Tung Nguyen8f221bf2020-12-16 05:52:18 +000070force_reset() {
ThuBaNguyen2f952142021-07-05 13:32:26 +070071 if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
72 # In graceful host reset, after trigger os shutdown,
73 # the phosphor-state-manager will call force-warm-reset
74 # in this case the force_reset should wait for shutdown_ack from host
75 cnt=30
76 while [ $cnt -gt 0 ];
77 do
78 if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
79 break
80 fi
81 echo "Waiting for shutdown-ack count down $cnt"
82 sleep 1
83 cnt=$((cnt - 1))
84 done
85 # The host OS is failed to shutdown
86 if [ $cnt == 0 ]; then
87 echo "Shutdown-ack time out after 30s."
88 exit 0
89 fi
90 fi
Tung Nguyen8f221bf2020-12-16 05:52:18 +000091 echo "Triggering sysreset pin"
92 gpioset -l 0 91=1
93 sleep 1
94 gpioset -l 0 91=0
95}
96
97if [ $# -lt 2 ]; then
98 echo "Total number of parameter=$#"
99 echo "Insufficient parameter"
100 usage;
101 exit 0;
102fi
103
104if [ $1 != "mb" ]; then
105 echo "Invalid parameter1=$1"
106 usage;
107 exit 0;
108fi
109
ThuBaNguyen2f952142021-07-05 13:32:26 +0700110# check if power guard enabled
111dir="/run/systemd/system/"
112file="reboot-guard.conf"
113units=("reboot" "poweroff" "halt")
114for unit in "${units[@]}"; do
115 if [ -f ${dir}${unit}.target.d/${file} ]; then
116 echo "PowerGuard enabled, cannot do power control, exit!!!"
117 exit -1
Tung Nguyen8f221bf2020-12-16 05:52:18 +0000118 fi
ThuBaNguyen2f952142021-07-05 13:32:26 +0700119done
120
121if [ ! -d "/run/openbmc/" ]; then
Tung Nguyen8f221bf2020-12-16 05:52:18 +0000122 mkdir -p "/run/openbmc/"
ThuBaNguyen2f952142021-07-05 13:32:26 +0700123fi
124
125if [ $2 == "shutdown_ack" ]; then
126 shutdown_ack
Tung Nguyen8f221bf2020-12-16 05:52:18 +0000127elif [ $2 == "status" ]; then
128 power_status
129elif [ $2 == "force_reset" ]; then
130 force_reset
ThuBaNguyen98f43be2021-07-01 22:29:49 +0700131elif [ $2 == "soft_off" ]; then
132 ret=$(soft_off)
133 if [ $ret == 0 ]; then
134 echo "The host is already softoff"
135 else
136 echo "Failed to softoff the host"
137 fi
138 exit $ret;
Tung Nguyen8f221bf2020-12-16 05:52:18 +0000139else
140 echo "Invalid parameter2=$2"
141 usage;
142fi
143
144exit 0;