summaryrefslogtreecommitdiff
path: root/package/base-files/files/lib/preinit/30_failsafe_wait
blob: bcf24f8bb7c5d72519f0fed92747a4037a96f5a4 (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
#!/bin/sh
# Copyright (C) 2006-2010 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

fs_wait_for_key () {
    local timeout=$3
    local timer
    local do_failsafe
    local keypress_true="$(mktemp)"
    local keypress_wait="$(mktemp)"
    local keypress_sec="$(mktemp)"
    if [ -z "$keypress_wait" ]; then
	keypress_wait=/tmp/.keypress_wait
	touch $keypress_wait
    fi
    if [ -z "$keypress_true" ]; then
	keypress_true=/tmp/.keypress_true
	touch $keypress_true
    fi
    if [ -z "$keypress_sec" ]; then
	keypress_sec=/tmp/.keypress_sec
	touch $keypress_sec
    fi

    trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT
    trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1

    [ -n "$timeout" ] || timeout=1
    [ $timeout -ge 1 ] || timeout=1
    timer=$timeout
    lock $keypress_wait
    {
	while [ $timer -gt 0 ]; do
            echo "$timer" >$keypress_sec
	    timer=$(($timer - 1))
	    sleep 1
	done
	lock -u $keypress_wait
	rm -f $keypress_wait
    } &

    echo "Press the [$1] key and hit [enter] $2"
    # if we're on the console we wait for input
    { 
	while [ -r $keypress_wait ]; do
	    timer="$(cat $keypress_sec)"

	    [ -n "$timer" ] || timer=1
	    timer="${timer%%\ *}"
	    [ $timer -ge 1 ] || timer=1
	    do_failsafe=""
	    {
		read -t "$timer" do_failsafe
		if [ "$do_failsafe" = "$1" ]; then
		    echo "true" >$keypress_true
		    lock -u $keypress_wait
		    rm -f $keypress_wait
 		fi
	    }
	done
    }
    lock -w $keypress_wait

    keypressed=1
    [ "$(cat $keypress_true)" = "true" ] && keypressed=0
    [ -f "/tmp/failsafe_button" ] && keypressed=1
    
    rm -f $keypress_true
    rm -f $keypress_wait
    rm -f $keypress_sec
    
    return $keypressed
}

failsafe_wait() {
    FAILSAFE=
    grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE
    if [ "$FAILSAFE" != "true" ]; then
      pi_failsafe_net_message=true
      preinit_net_echo "Please press button now to enter failsafe"
      pi_failsafe_net_message=false
      fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE
    fi
}

boot_hook_add preinit_main failsafe_wait