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# shellcheck source=meta-google/recipes-google/gpio/gpio-ctrl/lib.sh 17source /usr/share/gpio-host-pwr/lib.sh || exit 18 19gpio_build_cache 10 "$HOST_GPIO_PGOOD" "$HOST_GPIO_PWR_BTN" || exit 20gpio_init "$HOST_GPIO_PGOOD" || exit 21 22# Set the power status LED 23if [ -n "$HOST_LED_PWR" ]; then 24 echo 'none' > /sys/class/leds/"$HOST_LED_PWR"/trigger || true 25 echo '255' > /sys/class/leds/"$HOST_LED_PWR"/brightness || true 26fi 27 28# Ensure the watchdog is available before the host starts 29host_pwr_start_watchdog || true 30 31# We don't want to do anything if the machine is already on 32pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")" || exit 33if (( pgood == 1 )); then 34 echo 'Host is already running, doing nothing' >&2 35 exit 0 36fi 37 38# Do a quick push of the button if PGOOD 39echo "Starting host power" >&2 40rc=0 41gpio_set_value "$HOST_GPIO_PWR_BTN" 1 || rc=$? 42sleep 0.1 43gpio_set_value "$HOST_GPIO_PWR_BTN" 0 || rc=$? 44 45# Loop until we realize that host power is on 46# Limit the loop count to 10 seconds as we should never 47# have to wait this long for poweroff 48s=$SECONDS 49while true; do 50 if ! pgood="$(gpio_get_value "$HOST_GPIO_PGOOD")"; then 51 rc=1 52 break 53 fi 54 if (( pgood == 1 )); then 55 echo 'Host is now on' >&2 56 break 57 fi 58 if (( SECONDS - s > 10 )); then 59 echo 'Poweron timed out, terminating' >&2 60 rc=2 61 break 62 fi 63 sleep 0.1 64done 65 66exit $rc 67