summaryrefslogtreecommitdiff
path: root/packages/altboot/files/init.altboot
blob: 3bffc2bec558fc9058f82e0a9b74e059ede467fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/sh
#
# altboot provides a simple bootmenu before init is beeing run.
# There are two default menu entries: "Normal boot" and "Single user mode"
# New menu entries can be created be putting files into /etc/altboot-menu.
#

test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"

CURRENT_ENV="`set`"
VERSION="DEVELOPER SNAPSHOT"

# Set some defaults in case altboot.cfg is missing
REAL_INIT="/sbin/init.sysvinit"

# Read default runlevel from inittab
INIT_RUNLEVEL="`cat /etc/inittab | sed -n "/^id\:/s/id\:\([0-9]\)\:.*$/\1/p"`"
test -z "$INIT_RUNLEVEL" && INIT_RUNLEVEL=5

OUT_TTY="`tty`" ; test -z "$OUT_TTY" && OUT_TTY="/dev/tty1"

case "`uname -r`" in
2.6*)	ALTBOOT_CFG_FILE="/etc/altboot-2.6.cfg";;
2.4*)	ALTBOOT_CFG_FILE="/etc/altboot-2.4.cfg";;
*)	echo "Warning: Unknown kernel [`uname -r`], using kernel 2.6 configuration!"
	ALTBOOT_CFG_FILE="/etc/altboot-2.6.cfg";;
esac

test -e "$ALTBOOT_CFG_FILE" && . "$ALTBOOT_CFG_FILE" || echo "WARNING: No $ALTBOOT_CFG_FILE found! Check your installation of Altboot!" > "$OUT_TTY"

C_RED="\033[31m"
C_YELLOW="\033[33m"
C_BLUE="\033[34m"
C_WHITE="\033[37m"
C_RESET="\033[0m"


die() {
	echo -e "${C_RED}ERROR: $1${C_RESET}" >"$OUT_TTY"
	exec $SH_SHELL <"$OUT_TTY" >"$OUT_TTY" 2>&1
}

debug_shell() {		
	# VT 2 = Opie, VT 3 = GPE
	test -z "$1" && VT=4 || VT=$1
	
	echo -e "\033c" > /dev/tty$VT

	echo -en "\nPress <ENTER> to activate the debug shell." > /dev/tty$VT
	read junk </dev/tty$VT
		
	echo "" > /dev/tty$VT	
	/bin/sh  </dev/tty$VT >/dev/tty$VT 2>&1
	
	#openvt -lf -c$VT -- /bin/sh </dev/tty$VT >/dev/tty$VT 2>&1
}

# This function prints the boot-menu
# $1: Directory containing the scripts for the menu-items
show_menu() {
	test -z "$1" && die "DEBUG: Parameter 1 is empty in show_menu"
	! test -d "$1" && die "show_menu: [$1] not found or no directory."

	echo "" 
	echo -e "altboot v$VERSION\n" 

	
	m_entry=""
	# Build "m_entry" for scripts in /etc/altboot-menu
	cd $1

	cnt=0 ; reset_pref "menu_filelist"
	for file in `ls -1`
	do	
		if ! test -d "$1/$file"
		then			
			M_TITLE="`$1/$file title`"
			if ! test -z "$M_TITLE"
			then
				let cnt=$cnt+1
				# Keep a list of existing "modules" together with an index number
				# This sure is ugly, but Busybox sh doesn't do arrays....
				#m_entry="`echo -e "$m_entry\n$cnt:$file\n"`"
				
				set_pref "menu_filelist" "$cnt" "$file"
				echo -e "\t\t[$cnt] $M_TITLE"					
			fi
			M_TITLE=""
		fi
	done

	# Display directories below /etc/altboot-menu as menu-item
	# and add all scripts inside the directory to m_entry
	for dir in `ls -1`
	do	
		if test -d "$1/$dir"
		then			
			M_TITLE="`basename "$1/$dir"`"
			if ! test -z "$M_TITLE"
			then
				let cnt=$cnt+1
				# Keep a list of existing "modules" together with an index number
				# This sure is ugly, but Busybox sh doesn't do arrays....
				#m_entry="`echo -e "$m_entry\n$cnt:$dir:DIR\n"`"
				
				set_pref "menu_filelist" "$cnt" "$dir:DIR"
				echo -e "\t\t[$cnt] $M_TITLE"					

				OLD_PWD="$PWD"
				cd "$1/$dir"
				for file in `ls -1`
				do	
					if ! test -d "$1/$dir/$file"
					then			
						M_TITLE="`$1/$dir/$file title`"
						if ! test -z "$M_TITLE"
						then
							let cnt=$cnt+1
							# Keep a list of existing "modules" together with an index number
							# This sure is ugly, but Busybox sh doesn't do arrays....
							#m_entry="`echo -e "$m_entry\n$cnt:$dir/$file\n"`"
							
							set_pref "menu_filelist" "$cnt" "$dir/$file"
							#echo -e "\t\t[$cnt] $M_TITLE"					
						fi
						M_TITLE=""
					fi
				done
				cd "$OLD_PWD"

			fi
			M_TITLE=""
		fi
	done

	#echo_pref "menu_filelist"
	echo ""
	
}

# This function is used to display the content of directories below
# /etc/altboot-menu as menu-items
show_sub_menu() {
	dirname="`basename "$1"`"
		
	d_entries="`dump_pref "menu_filelist" | grep "$dirname/"`"
	
	#echo "[$d_entries]"
	
	echo -e "\naltboot v$VERSION: $dirname menu\n" 
	
	
	for d_entry in $d_entries
	do
		d_entry_number="`echo "$d_entry"| sed -n "s/\(.*\)\#\#\(.*\)\#\#\#/\1/p"`"
		d_entry_file="`echo "$d_entry"| sed -n "s/\(.*\)\#\#\(.*\)\#\#\#/\2/p"`"
		d_entry_title="`$d_entry_file title`"
		
#		echo "number: [$d_entry_number]"
#		echo "file: [$d_entry_file]"
#		echo "title: [$d_entry_title]"
		
		echo -e "\t\t[$d_entry_number] $d_entry_title"
	done
		
	echo ""
	
}

# Shows the timer and sets $launch_altboot to yes if a keypress was detected
run_timer() {
	if test "$TIMEOUT" != 0
	then			
			
		mount -t proc proc /proc >/dev/null 2>&1
		
		case "`uname -r`" in
		2.4*)	key_ints="`cat /proc/interrupts | grep keyboard | awk '{print $2}'`";;
		2.6*)	key_ints="`cat /proc/interrupts | grep Spitzkbd`"
			test -z "$key_ints" && key_ints="`cat /proc/interrupts | grep -i corgikbd`"
			test -z "$key_ints" && key_ints="`cat /proc/interrupts | grep -i locomokbd`"
			test -z "$key_ints" && key_ints="`cat /proc/interrupts | grep -i tosakbd`";;	
		esac		
	
		test -z "$key_ints" && debug_echo "Couldn't read keyboard ints!"
		
		stty -echo  <"$OUT_TTY" >"$OUT_TTY" 2>&1
		echo -en "\n\nPlease press any key to launch altboot." > "$OUT_TTY"
		
		test -z "$TIMEOUT" && TIMEOUT="3"

		cnt=0
		while test "$cnt" != "$TIMEOUT"
		do		
			sleep 1
			case "`uname -r`" in
			2.4*)	key_ints_now="`cat /proc/interrupts | grep keyboard | awk '{print $2}'`";;
			2.6*)	key_ints_now="`cat /proc/interrupts | grep Spitzkbd`"
				test -z "$key_ints_now" && key_ints_now="`cat /proc/interrupts | grep -i corgikbd`"
				test -z "$key_ints_now" && key_ints_now="`cat /proc/interrupts | grep -i locomokbd`"
				test -z "$key_ints_now" && key_ints_now="`cat /proc/interrupts | grep -i tosakbd`";;	
			esac		
			
			if test "$key_ints_now" != "$key_ints" -o -z "$key_ints_now"
			then				
				launch_altboot=yes				
				stty echo <"$OUT_TTY" >"$OUT_TTY" 2>&1
				break
			fi
			echo -n "." >"$OUT_TTY"
			let cnt=$cnt+1
		done
		
		if test "$launch_altboot" != "yes"
		then
			AUTOBOOT=yes
		else
			rm -f /etc/.altboot*.last
		fi
				
	else
		launch_altboot=yes
	fi
} 

# This function launches the selected menu item / script
# $1: Directory containing the scripts for the menu-items
launch_selection() {
	test -z "$1" && die "Parameter 1 of launch_selection is empty!"
	
	case "$junk" in
	*)	#file="`echo "$m_entry"| sed -n "/$junk\:/s/^.*\:\(.*\)/\1/p"`"
		
		get_pref "menu_filelist" "$junk" file_
		type="`echo "$file_" | sed -n "s/\(.*\)\:\(.*\)/\2/p"`"
		file="`echo "$file_" | sed -n "s/\(.*\)\:\(.*\)/\1/p"`"
		test -z "$file" && file="$file_"
		
		#echo "[$file_]: [$type] / [$file] ($junk)"
		
		# The selected menu-item points to a directory
		if test "$type" = DIR
		then
			show_sub_menu /etc/altboot-menu/$file >"$OUT_TTY"
			wait_for_input >"$OUT_TTY"			
			launch_selection /etc/altboot-menu >"$OUT_TTY"						
		fi
		
		if test "$type" = MAIN
		then
			show_sub_menu /etc/altboot-menu >"$OUT_TTY"
			wait_for_input >"$OUT_TTY"			
			launch_selection /etc/altboot-menu >"$OUT_TTY"						
		fi	
		
		
				
		. $1/$file run "$file" >"$OUT_TTY"	
		die "WARNING: Using failsafe shell" >"$OUT_TTY"			
		
		;;	
	esac
}


wait_for_input() {
	while true
	do
		
		
		# Do _not_ change the next few lines!
		# 	
		# This is required to work around an annoying busybox bug.
		# Every key you press while this script runs will be
		# picked up by the next "read $junk".	
		# So the next read would pick up the "any" key the user pressed
		# above to launch the altboot menu.		
				
 		
		# Bash throws an ugly error on kill
		if ! (readlink /bin/sh | grep -q bash)
		then
			# This filters an "<ENTER>" from the user as "any key"
			( while :; do read x< "$OUT_TTY" 2>&1; done; ) > /dev/null 2>&1 &
			sleep 1; kill $! >/dev/null 2>&1
		fi
								
		echo -n "Please choose one of the above [$last_selection]: " <"$OUT_TTY" > "$OUT_TTY" 2>&1
		stty echo <"$OUT_TTY" >"$OUT_TTY" 2>&1
		read junk< "$OUT_TTY" 2>&1
		
		# This filters other chars the user may have used
		
		#echo "junk: [$junk]"
		junk="`echo "$junk" | sed "s/[a-zA-Z]//g"`"
		#echo "junk: [$junk]"
				
		if test "$junk" -lt "$cnt" -o "$junk" -eq "$cnt" 
		then
			if test ! -z "$junk"
			then
				# Don't remount rw if the drive is already mounted rw
				# Only helpful for testing / debugging
				if test "`mount|sed -n "/\/dev\/root/s/.*(\(.*\))/\1/p"`" != "rw"
				then					
					mount -o remount,rw / >/dev/null 2>&1
					echo "$junk" > /etc/altboot.conf
					mount -o remount,ro / >/dev/null 2>&1	
				else
					echo "$junk" > /etc/altboot.conf
				fi
			else
				junk="$last_selection"
				break
			fi
			break
		fi
	done
}

# * * * * * * This is the main function * * * * * *
# Note: this is positively ugly. If someone knows a better way to detect wheter
# we are already booted into a runlevel _without_ reading /var and / or using `runlevel`
# PLEASE let me know.

if ( echo "$VERSION" |  egrep -iq "(snapshot|-rc)" )
then
	ENABLE_DEBUG="yes"
	debug_shell 4 >/dev/null 2>&1 &
fi

if test -f /proc/cmdline -a "`ps ax|wc -l|tr -d " "`" -gt 30 -a "$1" != "-force" -a "$1" != "+force"
then
	echo "altboot: Using real init [$REAL_INIT] [$*] [`ps ax|wc -l|tr -d " "`] *" >"$OUT_TTY"
	exec $REAL_INIT $*
#	exec $SH_SHELL </dev/tty0 >/dev/tty0 2>&1
	exit 0
else	
	# Boot original init if altboot is turned off
	if test "$ENABLE_ALTBOOT" != "yes" 
	then	
		echo "altboot: Using real init [$REAL_INIT] **" >"$OUT_TTY"
		exec $REAL_INIT $INIT_RUNLEVEL
		exit 0
	fi

	# Execute scripts in /etc/altboot.rc before doing anything else.
	# Required in special situations, like booting spitz
	RC_FILES=`ls /etc/altboot.rc | grep \.sh$`

	for file in $RC_FILES
	do
		 . /etc/altboot.rc/$file >"$OUT_TTY" 2>&1 || echo "/etc/altboot.rc/$file failed!"
	done
			
	# Make sure altboots master password is set
	set_password >"$OUT_TTY" 
	
	test "$ASK_PW_ON_BOOT" = "yes" && verify_master_pw >"$OUT_TTY" 

	# When started with -force, always print the menu
	echo "$*" | grep -q -- "-force" && TIMEOUT=0
	
	# This timeout works by reading /proc/interrupts to see if the keyboard interrupt
	# increases while the timer is running. A TIMEOUT of 0 will always launch altboot.	
	run_timer >"$OUT_TTY" 
		
	echo "" >"$OUT_TTY"
	
	
	if test "$launch_altboot" != yes
	then
		# last_selection is the previously selected menu item by the user
		last_selection="`cat /etc/altboot.conf`" >/dev/null 2>&1
		test -z "$last_selection" && last_selection="1"	
		
		echo "Booting last selection: [$last_selection]" >"$OUT_TTY"
		
		# Set up the wanna-be array of available menu entries and their numbers
		show_menu /etc/altboot-menu >/dev/null
		junk="$last_selection"
		launch_selection /etc/altboot-menu >"$OUT_TTY"

	fi

	# Anything after this point will never be reached if $launch_altboot != yes	
	
	# Show the altboot menu
	stty -echo <"$OUT_TTY" >"$OUT_TTY" 2>&1
	show_menu /etc/altboot-menu >"$OUT_TTY"

	# Load last selection for use as default if <ENTER> is pressed at the prompt
	last_selection="`cat /etc/altboot.conf`" >/dev/null 2>&1
	test -z "$last_selection" && last_selection="1"	

	# Ask the user which menu-item to use
	wait_for_input >"$OUT_TTY" 
	
	# This should _never_ happen.
	if test -z "$junk"
	then
		echo "WARNING: Trying failsafe mode" >"$OUT_TTY"
		mount -o remount,rw / >"$OUT_TTY" 2>&1
		echo "Dumping environment to /altboot.env"
		echo "$CURRENT_ENV" > /altboot.env
		mount -o remount,ro / >"$OUT_TTY" 2>&1
		junk=1
	fi

	launch_selection /etc/altboot-menu >"$OUT_TTY"	
	
	# Uhoh, something went terribly wrong if we reach this point!
	die "WARNING: Failsafe fall-through activated. Spawning emergency shell"	

fi