30_failsafe_wait 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/sh
  2. # Copyright (C) 2006-2010 OpenWrt.org
  3. # Copyright (C) 2010 Vertical Communications
  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. trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT
  24. trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1
  25. [ -n "$timeout" ] || timeout=1
  26. [ $timeout -ge 1 ] || timeout=1
  27. timer=$timeout
  28. lock $keypress_wait
  29. {
  30. while [ $timer -gt 0 ]; do
  31. echo "$timer" >$keypress_sec
  32. timer=$(($timer - 1))
  33. sleep 1
  34. done
  35. lock -u $keypress_wait
  36. rm -f $keypress_wait
  37. } &
  38. [ "$pi_preinit_no_failsafe" != "y" ] && echo "Press the [$1] key and hit [enter] $2"
  39. echo "Press the [1], [2], [3] or [4] key and hit [enter] to select the debug level"
  40. # if we're on the console we wait for input
  41. {
  42. while [ -r $keypress_wait ]; do
  43. timer="$(cat $keypress_sec)"
  44. [ -n "$timer" ] || timer=1
  45. timer="${timer%%\ *}"
  46. [ $timer -ge 1 ] || timer=1
  47. do_keypress=""
  48. {
  49. read -t "$timer" do_keypress
  50. case "$do_keypress" in
  51. $1)
  52. echo "true" >$keypress_true
  53. ;;
  54. 1 | 2 | 3 | 4)
  55. echo "$do_keypress" >/tmp/debug_level
  56. ;;
  57. *)
  58. continue;
  59. ;;
  60. esac
  61. lock -u $keypress_wait
  62. rm -f $keypress_wait
  63. }
  64. done
  65. }
  66. lock -w $keypress_wait
  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. return $keypressed
  73. }
  74. failsafe_wait() {
  75. FAILSAFE=
  76. [ "$pi_preinit_no_failsafe" == "y" ] && {
  77. fs_wait_for_key "" "" $fs_failsafe_wait_timeout
  78. return
  79. }
  80. grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE
  81. if [ "$FAILSAFE" != "true" ]; then
  82. pi_failsafe_net_message=true
  83. preinit_net_echo "Please press button now to enter failsafe"
  84. pi_failsafe_net_message=false
  85. fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true
  86. [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "`cat /tmp/failsafe_button`" was pressed -"
  87. [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe
  88. fi
  89. }
  90. boot_hook_add preinit_main failsafe_wait