blob: 2e6eb0217a2cbd485d36aa877c8170a34eefb774 [file] [log] [blame]
Tom Tung48ea66b2021-08-06 14:19:14 +08001#!/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
16source /usr/share/gpio-host-pwr/lib.sh || exit
17
18gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit
19gpio_init "$HOST_GPIO_PGOOD" || exit
20
21# Set the power status LED
22if [ -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
25fi
26
27# Ensure the watchdog is available before the host starts
28host_pwr_start_watchdog || true
29
30# We don't want to do anything if the machine is already on
31pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || exit
32if (( pgood == 1 )); then
33 echo 'Host is already running, doing nothing' >&2
34 exit 0
35fi
36
37# Do a quick push of the button if PGOOD
38echo "Starting host power" >&2
39rc=0
40gpio_set_value "$HOST_GPIO_PWR_BTN" 1 || rc=$?
41sleep 0.1
42gpio_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
47s=$SECONDS
48while 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
63done
64
65exit $rc