Tom Tung | 48ea66b | 2021-08-06 14:19:14 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2021 Google LLC |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | source /usr/share/gpio-host-pwr/lib.sh || exit |
| 17 | |
| 18 | gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit |
| 19 | gpio_init "$HOST_GPIO_PGOOD" || exit |
| 20 | |
| 21 | # Set the power status LED |
| 22 | if [ -n "$HOST_LED_PWR" ]; then |
| 23 | echo 'none' > /sys/class/leds/"$HOST_LED_PWR"/trigger || true |
| 24 | echo '255' > /sys/class/leds/"$HOST_LED_PWR"/brightness || true |
| 25 | fi |
| 26 | |
| 27 | # Ensure the watchdog is available before the host starts |
| 28 | host_pwr_start_watchdog || true |
| 29 | |
| 30 | # We don't want to do anything if the machine is already on |
| 31 | pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || exit |
| 32 | if (( pgood == 1 )); then |
| 33 | echo 'Host is already running, doing nothing' >&2 |
| 34 | exit 0 |
| 35 | fi |
| 36 | |
| 37 | # Do a quick push of the button if PGOOD |
| 38 | echo "Starting host power" >&2 |
| 39 | rc=0 |
| 40 | gpio_set_value "$HOST_GPIO_PWR_BTN" 1 || rc=$? |
| 41 | sleep 0.1 |
| 42 | gpio_set_value "$HOST_GPIO_PWR_BTN" 0 || rc=$? |
| 43 | |
| 44 | # Loop until we realize that host power is on |
| 45 | # Limit the loop count to 10 seconds as we should never |
| 46 | # have to wait this long for poweroff |
| 47 | s=$SECONDS |
| 48 | while true; do |
| 49 | if ! pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")"; then |
| 50 | rc=1 |
| 51 | break |
| 52 | fi |
| 53 | if (( pgood == 1 )); then |
| 54 | echo 'Host is now on' >&2 |
| 55 | break |
| 56 | fi |
| 57 | if (( SECONDS - s > 10 )); then |
| 58 | echo 'Poweron timed out, terminating' >&2 |
| 59 | rc=2 |
| 60 | break |
| 61 | fi |
| 62 | sleep 0.1 |
| 63 | done |
| 64 | |
| 65 | exit $rc |