| 1 | #!/bin/sh |
| 2 | # Copyright (C) 2006-2010 OpenWrt.org |
| 3 | # Copyright (C) 2010 Vertical Communications |
| 4 | |
| 5 | fs_wait_for_key () { |
| 6 | local timeout=$3 |
| 7 | local timer |
| 8 | local do_failsafe |
| 9 | local keypress_true="$(mktemp)" |
| 10 | local keypress_wait="$(mktemp)" |
| 11 | local keypress_sec="$(mktemp)" |
| 12 | if [ -z "$keypress_wait" ]; then |
| 13 | keypress_wait=/tmp/.keypress_wait |
| 14 | touch $keypress_wait |
| 15 | fi |
| 16 | if [ -z "$keypress_true" ]; then |
| 17 | keypress_true=/tmp/.keypress_true |
| 18 | touch $keypress_true |
| 19 | fi |
| 20 | if [ -z "$keypress_sec" ]; then |
| 21 | keypress_sec=/tmp/.keypress_sec |
| 22 | touch $keypress_sec |
| 23 | fi |
| 24 | |
| 25 | trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT |
| 26 | trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1 |
| 27 | |
| 28 | [ -n "$timeout" ] || timeout=1 |
| 29 | [ $timeout -ge 1 ] || timeout=1 |
| 30 | timer=$timeout |
| 31 | lock $keypress_wait |
| 32 | { |
| 33 | while [ $timer -gt 0 ]; do |
| 34 | echo "$timer" >$keypress_sec |
| 35 | timer=$(($timer - 1)) |
| 36 | sleep 1 |
| 37 | done |
| 38 | lock -u $keypress_wait |
| 39 | rm -f $keypress_wait |
| 40 | } & |
| 41 | |
| 42 | echo "Press the [$1] key and hit [enter] $2" |
| 43 | # if we're on the console we wait for input |
| 44 | { |
| 45 | while [ -r $keypress_wait ]; do |
| 46 | timer="$(cat $keypress_sec)" |
| 47 | |
| 48 | [ -n "$timer" ] || timer=1 |
| 49 | timer="${timer%%\ *}" |
| 50 | [ $timer -ge 1 ] || timer=1 |
| 51 | do_failsafe="" |
| 52 | { |
| 53 | read -t "$timer" do_failsafe |
| 54 | if [ "$do_failsafe" = "$1" ]; then |
| 55 | echo "true" >$keypress_true |
| 56 | lock -u $keypress_wait |
| 57 | rm -f $keypress_wait |
| 58 | fi |
| 59 | } |
| 60 | done |
| 61 | } |
| 62 | lock -w $keypress_wait |
| 63 | |
| 64 | trap - INT |
| 65 | trap - USR1 |
| 66 | |
| 67 | keypressed=1 |
| 68 | [ "$(cat $keypress_true)" = "true" ] && keypressed=0 |
| 69 | rm -f $keypress_true |
| 70 | rm -f $keypress_wait |
| 71 | rm -f $keypress_sec |
| 72 | |
| 73 | return $keypressed |
| 74 | } |
| 75 | |
| 76 | failsafe_wait() { |
| 77 | FAILSAFE= |
| 78 | pi_failsafe_net_message=true |
| 79 | preinit_net_echo "Please press button now to enter failsafe" |
| 80 | pi_failsafe_net_message=false |
| 81 | fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE |
| 82 | } |
| 83 | |
| 84 | boot_hook_add preinit_main failsafe_wait |
| 85 | |
| 86 | |