realtek: use generic earlycon setup on 5.15
[openwrt/openwrt.git] / package / base-files / files / lib / preinit / 30_failsafe_wait
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="$(sed -e 's/ /\n/g' /proc/cmdline | grep '^console=' | sed -e 's/^console=//' -e 's/,.*//')"
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 do_keypress=""
57 {
58 read -t "$timer" do_keypress < "/dev/$console"
59 case "$do_keypress" in
60 $1)
61 echo "true" >$keypress_true
62 ;;
63 1 | 2 | 3 | 4)
64 echo "$do_keypress" >/tmp/debug_level
65 ;;
66 *)
67 continue;
68 ;;
69 esac
70 lock -u $keypress_wait
71 rm -f $keypress_wait
72 }
73 done
74 } &
75 done
76 lock -w $keypress_wait
77
78 keypressed=1
79 [ "$(cat $keypress_true)" = "true" ] && keypressed=0
80
81 rm -f $keypress_true
82 rm -f $keypress_wait
83 rm -f $keypress_sec
84
85 return $keypressed
86 }
87
88 failsafe_wait() {
89 FAILSAFE=
90 [ "$pi_preinit_no_failsafe" = "y" ] && {
91 fs_wait_for_key "" "" $fs_failsafe_wait_timeout
92 return
93 }
94 grep -q 'failsafe=' /proc/cmdline && FAILSAFE=true && export FAILSAFE
95 if [ "$FAILSAFE" != "true" ]; then
96 fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true
97 [ -f "/tmp/failsafe_button" ] && FAILSAFE=true && echo "- failsafe button "$(cat /tmp/failsafe_button)" was pressed -"
98 [ "$FAILSAFE" = "true" ] && export FAILSAFE && touch /tmp/failsafe
99 fi
100 }
101
102 boot_hook_add preinit_main failsafe_wait