Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Usage of this utility |
| 3 | function usage() { |
ThuBaNguyen | 98f43be | 2021-07-01 22:29:49 +0700 | [diff] [blame] | 4 | echo "usage: power-util mb [on|status|cycle|reset|graceful_reset|force_reset|soft_off]"; |
Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 5 | } |
| 6 | |
Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 7 | power_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 | |
ThuBaNguyen | 98f43be | 2021-07-01 22:29:49 +0700 | [diff] [blame] | 16 | shutdown_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" |
ThuBaNguyen | 2f95214 | 2021-07-05 13:32:26 +0700 | [diff] [blame^] | 22 | <<<<<<< HEAD |
ThuBaNguyen | 98f43be | 2021-07-01 22:29:49 +0700 | [diff] [blame] | 23 | fi |
| 24 | } |
| 25 | |
Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 26 | graceful_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 |
ThuBaNguyen | 2f95214 | 2021-07-05 13:32:26 +0700 | [diff] [blame^] | 36 | ======= |
| 37 | sleep 3 |
| 38 | systemctl start obmc-chassis-poweroff@0.target |
| 39 | >>>>>>> 397e033ef... meta-ampere: power control: refactor host power control |
Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 40 | fi |
| 41 | } |
| 42 | |
ThuBaNguyen | 98f43be | 2021-07-01 22:29:49 +0700 | [diff] [blame] | 43 | soft_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 Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 70 | force_reset() { |
ThuBaNguyen | 2f95214 | 2021-07-05 13:32:26 +0700 | [diff] [blame^] | 71 | 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 Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 91 | echo "Triggering sysreset pin" |
| 92 | gpioset -l 0 91=1 |
| 93 | sleep 1 |
| 94 | gpioset -l 0 91=0 |
| 95 | } |
| 96 | |
| 97 | if [ $# -lt 2 ]; then |
| 98 | echo "Total number of parameter=$#" |
| 99 | echo "Insufficient parameter" |
| 100 | usage; |
| 101 | exit 0; |
| 102 | fi |
| 103 | |
| 104 | if [ $1 != "mb" ]; then |
| 105 | echo "Invalid parameter1=$1" |
| 106 | usage; |
| 107 | exit 0; |
| 108 | fi |
| 109 | |
ThuBaNguyen | 2f95214 | 2021-07-05 13:32:26 +0700 | [diff] [blame^] | 110 | # check if power guard enabled |
| 111 | dir="/run/systemd/system/" |
| 112 | file="reboot-guard.conf" |
| 113 | units=("reboot" "poweroff" "halt") |
| 114 | for 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 Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 118 | fi |
ThuBaNguyen | 2f95214 | 2021-07-05 13:32:26 +0700 | [diff] [blame^] | 119 | done |
| 120 | |
| 121 | if [ ! -d "/run/openbmc/" ]; then |
Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 122 | mkdir -p "/run/openbmc/" |
ThuBaNguyen | 2f95214 | 2021-07-05 13:32:26 +0700 | [diff] [blame^] | 123 | fi |
| 124 | |
| 125 | if [ $2 == "shutdown_ack" ]; then |
| 126 | shutdown_ack |
Tung Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 127 | elif [ $2 == "status" ]; then |
| 128 | power_status |
| 129 | elif [ $2 == "force_reset" ]; then |
| 130 | force_reset |
ThuBaNguyen | 98f43be | 2021-07-01 22:29:49 +0700 | [diff] [blame] | 131 | elif [ $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 Nguyen | 8f221bf | 2020-12-16 05:52:18 +0000 | [diff] [blame] | 139 | else |
| 140 | echo "Invalid parameter2=$2" |
| 141 | usage; |
| 142 | fi |
| 143 | |
| 144 | exit 0; |