]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/altboot/files/init.altboot
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / altboot / files / init.altboot
1 #!/bin/sh
2 #
3 # altboot provides a simple bootmenu before init is beeing run.
4 # There are two default menu entries: "Normal boot" and "Single user mode"
5 # New menu entries can be created be putting files into /etc/altboot-menu.
6 #
7
8 test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"
9
10 CURRENT_ENV="`set`"
11 VERSION="0.0.4"
12
13 # Set some defaults in case altboot.cfg is missing
14 REAL_INIT="/sbin/init.sysvinit"
15
16 # Read default runlevel from inittab
17 INIT_RUNLEVEL="`cat /etc/inittab | sed -n "/^id\:/s/id\:\([0-9]\)\:.*$/\1/p"`"
18 test -z "$INIT_RUNLEVEL" && INIT_RUNLEVEL=5
19
20 case "`uname -r`" in
21 2.6*)   ALTBOOT_CFG_FILE="/etc/altboot-2.6.cfg";;
22 2.4*)   ALTBOOT_CFG_FILE="/etc/altboot-2.4.cfg";;
23 *)      echo "Warning: Unknown kernel [`uname -r`], using kernel 2.6 configuration!"
24         ALTBOOT_CFG_FILE="/etc/altboot-2.6.cfg";;
25 esac
26
27 test -e "$ALTBOOT_CFG_FILE" && . "$ALTBOOT_CFG_FILE" || echo "WARNING: No $ALTBOOT_CFG_FILE found! Check your installation of Altboot!" > /dev/tty1
28
29 C_RED="\033[31m"
30 C_BLUE="\033[34m"
31 C_WHITE="\033[37m"
32 C_RESET="\033[0m"
33
34 die() {
35         echo "ERROR: $1" >/dev/tty0
36         #exec $SH_SHELL </dev/tty0 >/dev/tty0 2>&1
37         
38         echo -e "\nPress <ENTER> to launch the altboot menu"
39         read junk
40         exec /sbin/init.altboot -force</dev/tty0 >/dev/tty0 2>&1
41 }
42
43
44 # This function prints the boot-menu
45 # $1: Directory containing the scripts for the menu-items
46 show_menu() {
47         test -z "$1" && die "DEBUG: Parameter 1 is empty in show_menu"
48         echo "" 
49         echo -e "altboot v$VERSION\n" 
50
51         cnt=0
52         m_entry=""
53         if test -d $1
54         then
55                 # Build "m_entry" for scripts in /etc/altboot-menu
56                 cd $1
57                 for file in `ls -1`
58                 do      
59                         if ! test -d "$1/$file"
60                         then                    
61                                 M_TITLE="`$1/$file title`"
62                                 if ! test -z "$M_TITLE"
63                                 then
64                                         let cnt=$cnt+1
65                                         # Keep a list of existing "modules" together with an index number
66                                         # This sure is ugly, but Busybox sh doesn't do arrays....
67                                         m_entry="`echo -e "$m_entry\n$cnt:$file\n"`"
68                                         echo -e "\t\t[$cnt] $M_TITLE"                                   
69                                 fi
70                                 M_TITLE=""
71                         fi
72                 done
73
74                 # Display directories below /etc/altboot-menu as menu-item
75                 # and add all scripts inside the directory to m_entry
76                 for dir in `ls -1`
77                 do      
78                         if test -d "$1/$dir"
79                         then                    
80                                 M_TITLE="`basename "$1/$dir"`"
81                                 if ! test -z "$M_TITLE"
82                                 then
83                                         let cnt=$cnt+1
84                                         # Keep a list of existing "modules" together with an index number
85                                         # This sure is ugly, but Busybox sh doesn't do arrays....
86                                         m_entry="`echo -e "$m_entry\n$cnt:$dir:DIR\n"`"
87                                         echo -e "\t\t[$cnt] $M_TITLE"                                   
88                                         
89                                         OLD_PWD="$PWD"
90                                         cd "$1/$dir"
91                                         for file in `ls -1`
92                                         do      
93                                                 if ! test -d "$1/$dir/$file"
94                                                 then                    
95                                                         M_TITLE="`$1/$dir/$file title`"
96                                                         if ! test -z "$M_TITLE"
97                                                         then
98                                                                 let cnt=$cnt+1
99                                                                 # Keep a list of existing "modules" together with an index number
100                                                                 # This sure is ugly, but Busybox sh doesn't do arrays....
101                                                                 m_entry="`echo -e "$m_entry\n$cnt:$dir/$file\n"`"
102                                                                 #echo -e "\t\t[$cnt] $M_TITLE"                                  
103                                                         fi
104                                                         M_TITLE=""
105                                                 fi
106                                         done
107                                         cd "$OLD_PWD"
108
109                                 fi
110                                 M_TITLE=""
111                         fi
112                 done
113                 
114                 
115                 echo ""
116         else
117                 echo "WARNING: $1 not found"
118         fi
119         
120 }
121
122 # This function is used to display the content of directories below
123 # /etc/altboot-menu as menu-items
124 show_sub_menu() {
125         dirname="`basename "$1"`"
126                 
127         d_entries="`echo "$m_entry"|grep "$dirname/"`"
128         
129 #       echo "[$d_entries]"
130         
131         echo -e "\naltboot v$VERSION: $dirname menu\n" 
132         
133         
134         for d_entry in $d_entries
135         do
136                 d_entry_number="`echo "$d_entry"| sed -n "s/\(.*\)\:\(.*\)/\1/p"`"
137                 d_entry_file="`echo "$d_entry"| sed -n "s/\(.*\)\:\(.*\)/\2/p"`"
138                 d_entry_title="`$d_entry_file title`"
139                 
140 #               echo "number: [$d_entry_number]"
141 #               echo "file: [$d_entry_file]"
142 #               echo "title: [$d_entry_title]"
143                 
144                 echo -e "\t\t[$d_entry_number] $d_entry_title"
145         done
146                 
147         echo ""
148         
149 }
150
151 # Shows the timer and sets $launch_altboot to yes if a keypress was detected
152 run_timer() {
153         if test "$TIMEOUT" != 0
154         then
155                         
156                 mount -t proc proc /proc >/dev/null 2>&1
157                 
158                 case "`uname -r`" in
159                 2.4*)   key_ints="`cat /proc/interrupts | grep keyboard | awk '{print $2}'`";;
160                 2.6*)   key_ints="`cat /proc/interrupts | grep Spitzkbd`";;     
161                 esac            
162         
163                 stty -echo
164                 echo -en "\n\nPlease press any key to launch altboot." > /dev/tty1
165                 
166                 test -z "$TIMEOUT" && TIMEOUT="3"
167
168                 cnt=0
169                 while test "$cnt" != "$TIMEOUT"
170                 do              
171                         sleep 1
172                         case "`uname -r`" in
173                         2.4*)   key_ints_now="`cat /proc/interrupts | grep keyboard | awk '{print $2}'`";;
174                         2.6*)   key_ints_now="`cat /proc/interrupts | grep Spitzkbd`";; 
175                         esac            
176                         
177                         if test "$key_ints_now" != "$key_ints" 
178                         then                            
179                                 launch_altboot=yes                              
180                                 stty echo
181                                 break
182                         fi
183                         echo -n "." >/dev/tty1
184                         let cnt=$cnt+1
185                 done
186         else
187                 launch_altboot=yes
188         fi
189
190
191 # This function launches the selected menu item / script
192 # $1: Directory containing the scripts for the menu-items
193 launch_selection() {
194         test -z "$1" && die "Parameter 1 of launch_selection is empty!"
195         
196         case "$junk" in
197         *)      file="`echo "$m_entry"| sed -n "/$junk\:/s/^.*\:\(.*\)/\1/p"`"
198                 
199                 # The selected menu-item points to a directory
200                 if test "$file" = DIR
201                 then
202                         dir="`echo "$m_entry"| sed -n "/$junk\:/s/^.*\:\(.*\)\:\(.*\)/\1/p"`"
203                         show_sub_menu /etc/altboot-menu/$dir >/dev/tty0
204                         wait_for_input >/dev/tty0                       
205                         launch_selection /etc/altboot-menu >/dev/tty0                                           
206                 fi
207                 
208                 if test "$file" = MAIN
209                 then
210                         show_sub_menu /etc/altboot-menu >/dev/tty0
211                         wait_for_input >/dev/tty0                       
212                         launch_selection /etc/altboot-menu >/dev/tty0                                           
213                 fi      
214                 
215                 #echo "[$file]"
216                                 
217                 . $1/$file run "$file" >/dev/tty0       
218                 die "WARNING: Using failsafe shell" >/dev/tty0                  
219                 
220                 ;;      
221         esac
222 }
223
224
225 wait_for_input() {
226         while true
227         do
228                 
229                 
230                 # Do _not_ change the next few lines!
231                 #       
232                 # This is required to work around an annoying busybox bug.
233                 # Every key you press while this script runs will be
234                 # picked up by the next "read $junk".   
235                 # So the next read would pick up the "any" key the user pressed
236                 # above to launch the altboot menu.             
237                                 
238                 
239                 # This filters an "<ENTER>" from the user as "any key"
240                 ( while :; do read x< /dev/tty0 2>&1; done; ) > /dev/null 2>&1 &
241                 sleep 1; kill $!
242                                 
243                 
244
245                 echo -n "Please choose one of the above [$last_selection]: " </dev/tty0 > /dev/tty0 2>&1
246                 read junk< /dev/tty0 2>&1
247                 
248                 # This filters other chars the user may have used
249                 junk="`echo "$junk" |  sed -n "s/.*\([0-9]\)/\1/p"`"
250                 
251                 if test "$junk" -lt "$cnt" -o "$junk" -eq "$cnt" 
252                 then
253                         if test ! -z "$junk"
254                         then
255                                 # Don't remount rw if the drive is already mounted rw
256                                 # Only helpful for testing / debugging
257                                 if test "`mount|sed -n "/\/dev\/root/s/.*(\(.*\))/\1/p"`" != "rw"
258                                 then                                    
259                                         mount -o remount,rw / >/dev/null 2>&1
260                                         echo "$junk" > /etc/altboot.conf
261                                         mount -o remount,ro / >/dev/null 2>&1   
262                                 else
263                                         echo "$junk" > /etc/altboot.conf
264                                 fi
265                         else
266                                 junk="$last_selection"
267                                 break
268                         fi
269                         break
270                 fi
271         done
272 }
273
274 # * * * * * * This is the main function * * * * * *
275
276 if test "`runlevel`" != "unknown" -a "$1" != "-force"
277 then
278         echo "altboot: Using real init [$REAL_INIT] [$*] *" >/dev/tty1
279         exec $REAL_INIT $*
280 #       exec $SH_SHELL </dev/tty0 >/dev/tty0 2>&1
281         exit 0
282 else    
283         # Boot original init if altboot is turned off
284         if test "$ENABLE_ALTBOOT" != "yes" 
285         then    
286                 echo "altboot: Using real init [$REAL_INIT] **" >/dev/tty1
287                 exec $REAL_INIT $INIT_RUNLEVEL
288                 exit 0
289         fi
290
291         # Execute scripts in /etc/altboot.rc before doing anything else.
292         # Required in special situations, like booting spitz
293         RC_FILES=`ls /etc/altboot.rc | grep \.sh$`
294
295         for file in $RC_FILES
296         do
297                  . /etc/altboot.rc/$file >/dev/tty1 2>&1 || echo "/etc/altboot.rc/$file failed!"
298         done
299                 
300         # Make sure altboots master password is set
301         set_password >/dev/tty0 
302         
303         test "$ASK_PW_ON_BOOT" = "yes" && verify_master_pw >/dev/tty0 
304
305
306         # When started with -force, always print the menu
307         echo "$*" | grep -q -- "-force" && TIMEOUT=0
308
309         
310         # This timeout works by reading /proc/interrupts to see if the keyboard interrupt
311         # increases while the timer is running. A TIMEOUT of 0 will always launch altboot.      
312         run_timer >/dev/tty1 2>&1
313
314                 
315         echo "" >/dev/tty0
316         
317         
318         if test "$launch_altboot" != yes
319         then
320                 # last_selection is the previously selected menu item by the user
321                 last_selection="`cat /etc/altboot.conf`" >/dev/null 2>&1
322                 test -z "$last_selection" && last_selection="1" 
323                 
324                 echo "Booting last selection: [$last_selection]" >/dev/tty0
325                 
326                 # Set up the wanna-be array of available menu entries and their numbers
327                 show_menu /etc/altboot-menu >/dev/null
328                 junk="$last_selection"
329                 launch_selection /etc/altboot-menu >/dev/tty0
330
331         fi
332         # Anything after this point will never be reached if $launch_altboot != yes
333         
334         # Show the altboot menu
335         show_menu /etc/altboot-menu >/dev/tty0
336
337         # Load last selection for use as default if <ENTER> is pressed at the prompt
338         last_selection="`cat /etc/altboot.conf`" >/dev/null 2>&1
339         test -z "$last_selection" && last_selection="1" 
340
341         # Ask the user which menu-item to use
342         wait_for_input >/dev/tty0 
343         
344         # This should _never_ happen.
345         if test -z "$junk"
346         then
347                 echo "WARNING: Trying failsafe mode" >/dev/tty0
348                 mount -o remount,rw / >/dev/tty0 2>&1
349                 echo "Dumping environment to /altboot.env"
350                 echo "$CURRENT_ENV" > /altboot.env
351                 mount -o remount,ro / >/dev/tty0 2>&1
352                 junk=1
353         fi
354
355         launch_selection /etc/altboot-menu >/dev/tty0   
356         
357         # Uhoh, something went terribly wrong if we reach this point!
358         die "WARNING: Failsafe fall-through activated. Spawning emergency shell"        
359
360 fi
361
362