Chanh Nguyen | bdfb8a9 | 2022-04-28 09:11:28 +0000 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | # shellcheck disable=SC2046 |
| 4 | # shellcheck source=/dev/null |
| 5 | |
| 6 | source /usr/sbin/gpio-lib.sh |
| 7 | |
| 8 | # Usage of this utility |
| 9 | function usage() { |
| 10 | echo "usage: power-util mb [status|shutdown_ack|force_reset|soft_off]"; |
| 11 | } |
| 12 | |
| 13 | power_status() { |
| 14 | st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6) |
| 15 | if [ "$st" == "On\"" ]; then |
| 16 | echo "on" |
| 17 | else |
| 18 | echo "off" |
| 19 | fi |
| 20 | } |
| 21 | |
| 22 | shutdown_ack() { |
| 23 | if [ -f "/run/openbmc/host@0-softpoweroff" ]; then |
| 24 | echo "Receive shutdown ACK triggered after softportoff the host." |
| 25 | touch /run/openbmc/host@0-softpoweroff-shutdown-ack |
| 26 | else |
| 27 | echo "Receive shutdown ACK triggered" |
| 28 | sleep 3 |
| 29 | systemctl start obmc-chassis-poweroff@0.target |
| 30 | fi |
| 31 | } |
| 32 | |
| 33 | soft_off() { |
| 34 | # Trigger shutdown_req |
| 35 | touch /run/openbmc/host@0-softpoweroff |
| 36 | gpio_name_set host0-shd-req-n 0 |
| 37 | sleep 0.05 |
| 38 | gpio_name_set host0-shd-req-n 1 |
| 39 | |
| 40 | # Wait for shutdown_ack from the host in 30 seconds |
| 41 | cnt=30 |
| 42 | while [ $cnt -gt 0 ]; |
| 43 | do |
| 44 | # Wait for SHUTDOWN_ACK and create the host@0-softpoweroff-shutdown-ack |
| 45 | if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then |
| 46 | break |
| 47 | fi |
| 48 | sleep 1 |
| 49 | cnt=$((cnt - 1)) |
| 50 | done |
| 51 | |
| 52 | # Softpoweroff is successed |
| 53 | sleep 2 |
| 54 | rm -rf /run/openbmc/host@0-softpoweroff |
| 55 | if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then |
| 56 | rm -rf /run/openbmc/host@0-softpoweroff-shutdown-ack |
| 57 | fi |
| 58 | echo 0 |
| 59 | } |
| 60 | |
| 61 | force_reset() { |
| 62 | if [ -f "/run/openbmc/host@0-softpoweroff" ]; then |
| 63 | # In graceful host reset, after trigger os shutdown, |
| 64 | # the phosphor-state-manager will call force-warm-reset |
| 65 | # in this case the force_reset should wait for shutdown_ack from host |
| 66 | cnt=30 |
| 67 | while [ $cnt -gt 0 ]; |
| 68 | do |
| 69 | if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then |
| 70 | break |
| 71 | fi |
| 72 | echo "Waiting for shutdown-ack count down $cnt" |
| 73 | sleep 1 |
| 74 | cnt=$((cnt - 1)) |
| 75 | done |
| 76 | # The host OS is failed to shutdown |
| 77 | if [ $cnt == 0 ]; then |
| 78 | echo "Shutdown-ack time out after 30s." |
| 79 | exit 0 |
| 80 | fi |
| 81 | fi |
| 82 | rm -f /run/openbmc/host@0-on |
| 83 | echo "Triggering sysreset pin" |
| 84 | gpio_name_set host0-sysreset-n 0 |
| 85 | sleep 1 |
| 86 | gpio_name_set host0-sysreset-n 1 |
| 87 | } |
| 88 | |
| 89 | |
| 90 | if [ ! -d "/run/openbmc/" ]; then |
| 91 | mkdir -p "/run/openbmc/" |
| 92 | fi |
| 93 | |
| 94 | if [ "$2" == "shutdown_ack" ]; then |
| 95 | shutdown_ack |
| 96 | elif [ "$2" == "status" ]; then |
| 97 | power_status |
| 98 | elif [ "$2" == "force_reset" ]; then |
| 99 | force_reset |
| 100 | elif [ "$2" == "soft_off" ]; then |
| 101 | ret=$(soft_off) |
| 102 | if [ "$ret" == 0 ]; then |
| 103 | echo "The host is already softoff" |
| 104 | else |
| 105 | echo "Failed to softoff the host" |
| 106 | fi |
| 107 | exit "$ret"; |
| 108 | else |
| 109 | echo "Invalid parameter2=$2" |
| 110 | usage; |
| 111 | fi |
| 112 | |
| 113 | exit 0; |