summaryrefslogtreecommitdiff
path: root/meta-ampere/meta-jade/recipes-ampere/platform/mtjade-utils/ampere_power_util.sh
blob: 2c8ba3d91f728b70ded1913574d3c7236c2d2f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
#ampere_platform_config.sh is platform configuration file
source /usr/sbin/gpio-defs.sh

# Usage of this utility
function usage() {
  echo "usage: power-util mb [status|shutdown_ack|force_reset|soft_off]";
}

power_status() {
  st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6)
  if [ "$st" == "On\"" ]; then
  echo "on"
  else
  echo "off"
  fi
}

shutdown_ack() {
  if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
    echo "Receive shutdown ACK triggered after softportoff the host."
    touch /run/openbmc/host@0-softpoweroff-shutdown-ack
  else
    echo "Receive shutdown ACK triggered"
    sleep 3
    systemctl start obmc-chassis-poweroff@0.target
  fi
}

soft_off() {
  # Trigger shutdown_req
  touch /run/openbmc/host@0-softpoweroff
  gpioset -l $GPIO_CHIP0_IDX $S0_SHD_REQ_L=1
  sleep 1s
  gpioset -l $GPIO_CHIP0_IDX $S0_SHD_REQ_L=0

  # Wait for shutdown_ack from the host in 30 seconds
  cnt=30
  while [ $cnt -gt 0 ];
  do
    # Wait for SHUTDOWN_ACK and create the host@0-softpoweroff-shutdown-ack
    if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
      break
    fi
    sleep 1
    cnt=$((cnt - 1))
  done
  # Softpoweroff is successed
  sleep 2
  rm -rf /run/openbmc/host@0-softpoweroff
  if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
    rm -rf /run/openbmc/host@0-softpoweroff-shutdown-ack
  fi
  echo 0
}

force_reset() {
  if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
    # In graceful host reset, after trigger os shutdown,
    # the phosphor-state-manager will call force-warm-reset
    # in this case the force_reset should wait for shutdown_ack from host
    cnt=30
    while [ $cnt -gt 0 ];
    do
      if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
        break
      fi
      echo "Waiting for shutdown-ack count down $cnt"
      sleep 1
      cnt=$((cnt - 1))
    done
    # The host OS is failed to shutdown
    if [ $cnt == 0 ]; then
      echo "Shutdown-ack time out after 30s."
      exit 0
    fi
  fi
  echo "Triggering sysreset pin"
  gpioset -l $GPIO_CHIP0_IDX $S0_SYSRESET_L=1
  sleep 1
  gpioset -l $GPIO_CHIP0_IDX $S0_SYSRESET_L=0
}

if [ $# -lt 2 ]; then
  echo "Total number of parameter=$#"
  echo "Insufficient parameter"
  usage;
  exit 0;
fi

if [ $1 != "mb" ]; then
  echo "Invalid parameter1=$1"
  usage;
  exit 0;
fi

# check if power guard enabled
dir="/run/systemd/system/"
file="reboot-guard.conf"
units=("reboot" "poweroff" "halt")
for unit in "${units[@]}"; do
  if [ -f ${dir}${unit}.target.d/${file} ]; then
    echo "PowerGuard enabled, cannot do power control, exit!!!"
    exit -1
  fi
done

if [ ! -d "/run/openbmc/" ]; then
  mkdir -p "/run/openbmc/"
fi

if [ $2 == "shutdown_ack" ]; then
  shutdown_ack
elif [ $2 == "status" ]; then
  power_status
elif [ $2 == "force_reset" ]; then
  force_reset
elif [ $2 == "soft_off" ]; then
  ret=$(soft_off)
  if [ $ret == 0 ]; then
    echo "The host is already softoff"
  else
    echo "Failed to softoff the host"
  fi
  exit $ret;
else
  echo "Invalid parameter2=$2"
  usage;
fi

exit 0;