b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | # Copyright (C) 2006-2010 OpenWrt.org |
| 2 | # Copyright (C) 2010 Vertical Communications |
| 3 | |
| 4 | fs_wait_for_key () { |
| 5 | local timeout=$3 |
| 6 | local timer |
| 7 | local do_keypress |
| 8 | local keypress_true="$(mktemp)" |
| 9 | local keypress_wait="$(mktemp)" |
| 10 | local keypress_sec="$(mktemp)" |
| 11 | if [ -z "$keypress_wait" ]; then |
| 12 | keypress_wait=/tmp/.keypress_wait |
| 13 | touch $keypress_wait |
| 14 | fi |
| 15 | if [ -z "$keypress_true" ]; then |
| 16 | keypress_true=/tmp/.keypress_true |
| 17 | touch $keypress_true |
| 18 | fi |
| 19 | if [ -z "$keypress_sec" ]; then |
| 20 | keypress_sec=/tmp/.keypress_sec |
| 21 | touch $keypress_sec |
| 22 | fi |
| 23 | |
| 24 | trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT |
| 25 | trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1 |
| 26 | |
| 27 | [ -n "$timeout" ] || timeout=1 |
| 28 | [ $timeout -ge 1 ] || timeout=1 |
| 29 | timer=$timeout |
| 30 | lock $keypress_wait |
| 31 | { |
| 32 | while [ $timer -gt 0 ]; do |
| 33 | pi_failsafe_net_message=true \ |
| 34 | preinit_net_echo "Please press button now to enter failsafe" |
| 35 | echo "$timer" >$keypress_sec |
| 36 | timer=$(($timer - 1)) |
| 37 | sleep 1 |
| 38 | done |
| 39 | lock -u $keypress_wait |
| 40 | rm -f $keypress_wait |
| 41 | } & |
| 42 | |
| 43 | local consoles="$(cat /sys/class/tty/console/active)" |
| 44 | [ -n "$consoles" ] || consoles=console |
| 45 | for console in $consoles; do |
| 46 | [ -c "/dev/$console" ] || continue |
| 47 | [ "$pi_preinit_no_failsafe" != "y" ] && echo "Press the [$1] key and hit [enter] $2" > "/dev/$console" |
| 48 | echo "Press the [1], [2], [3] or [4] key and hit [enter] to select the debug level" > "/dev/$console" |
| 49 | { |
| 50 | while [ -r $keypress_wait ]; do |
| 51 | timer="$(cat $keypress_sec)" |
| 52 | |
| 53 | [ -n "$timer" ] || timer=1 |
| 54 | timer="${timer%%\ *}" |
| 55 | # [ $timer -ge 1 ] || timer=1 |
| 56 | timer=0 |
| 57 | do_keypress="" |
| 58 | { |
| 59 | read -t "$timer" do_keypress < "/dev/$console" |
| 60 | case "$do_keypress" in |
| 61 | $1) |
| 62 | echo "true" >$keypress_true |
| 63 | ;; |
| 64 | 1 | 2 | 3 | 4) |
| 65 | echo "$do_keypress" >/tmp/debug_level |
| 66 | ;; |
| 67 | *) |
| 68 | continue; |
| 69 | ;; |
| 70 | esac |
| 71 | lock -u $keypress_wait |
| 72 | rm -f $keypress_wait |
| 73 | } |
| 74 | done |
| 75 | } & |
| 76 | done |
| 77 | lock -w $keypress_wait |
| 78 | |
| 79 | keypressed=1 |
| 80 | [ "$(cat $keypress_true)" = "true" ] && keypressed=0 |
| 81 | |
| 82 | trap - INT |
| 83 | trap - USR1 |
| 84 | |
| 85 | rm -f $keypress_true |
| 86 | rm -f $keypress_wait |
| 87 | rm -f $keypress_sec |
| 88 | |
| 89 | return $keypressed |
| 90 | } |
| 91 | |
| 92 | failsafe_wait() { |
| 93 | FAILSAFE= |
| 94 | [ "$pi_preinit_no_failsafe" = "y" ] && { |
| 95 | fs_wait_for_key "" "" $fs_failsafe_wait_timeout |
| 96 | return |
| 97 | } |
| 98 | grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE |
| 99 | if [ "$FAILSAFE" != "true" ]; then |
| 100 | fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true |
| 101 | [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "$(cat /tmp/failsafe_button)" was pressed -" |
| 102 | [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe |
| 103 | fi |
| 104 | } |
| 105 | |
| 106 | boot_hook_add preinit_main failsafe_wait |