summaryrefslogtreecommitdiff
path: root/packages/altboot/files/altboot.func
blob: 9a0b139bfb15c479cfdd79afdfd508d8c1d6d3ab (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#! /bin/sh

C_RED="\033[31m"
C_YELLOW="\033[35m"
C_BLUE="\033[34m"
C_WHITE="\033[38m"
C_RESET="\033[0m"


# This function checks for the presence of a real filesystem and loop-images on the target
# $1 = folder of rootfs, $2 = runlevel (defaults to 5)
# $2 = name of calling module
check_target() {
	# Check if there is a /sbin/init or /sbin/init.sysvinit on the card
	if test -x $1/sbin/init -o -x $1/$REAL_INIT
	then
		real_fs_found=1
	else
		echo -e "Note: No INIT [$REAL_INIT] found on target"
	fi
	
	# Check for loop-images
	if (ls $1/$IMAGE_PATH/*rootfs.bin) >/dev/null 2>&1
	then
		image_found=1
	else
		echo "Note: No boot-images found in [$1/$IMAGE_PATH]"
	fi
	
	# Check if we have both, a real fs and boot-images. If so, ask the user what to boot
	if test "$real_fs_found" = 1 -a "$image_found" = 1
	then
		echo -e "\nI have found a real filesystem and boot-images on the target"
		echo -e "What do you want to boot?\n"
		
		echo -e "\t[1] The real filesystem"
		echo -e "\t[2] A loop-image"
		echo ""
		
		while test -z "$ans"
		do
			echo -n "Your choice: "
			
			if test "$AUTOBOOT" != "yes"
			then
				read junk < "$OUT_TTY"
			else
				if test -e /etc/.altboot-real-or-loop.last
				then
					junk="`cat /etc/.altboot-real-or-loop.last`"
					test -z "$junk" && read junk < "$OUT_TTY" || echo "$junk (autoboot)"
				else
					read junk < "$OUT_TTY"
				fi
			fi	
			
			if test "$junk" = 1 -o "$junk" = 2
			then
				ans="$junk"
				echo "$junk" > /etc/.altboot-real-or-loop.last
			fi
		done
		
		case "$ans" in
		1)	pivot_realfs "$1" "$2">"$OUT_TTY";;
		2)	pivot_image "$1" "$2">"$OUT_TTY";;
		esac
				
		exit 0
	fi

	# Boot a real filesystem	
	test "$real_fs_found" = 1 && pivot_realfs "$1" >"$OUT_TTY"
	
	# Boot a loop-image
	test "$image_found" = 1 && pivot_image "$1" >"$OUT_TTY"
		
	if test "$real_fs_found" != 1 -a "$image_found" != 1
	then
		die "Nothing to do!"
	fi
}

boot_new_rootfs_splash() {
C_RED="\033[37;44m"
C_RESET="\033[0m"

	echo -e "${C_RED}+----------------------------------------------------------+${C_RESET}"
	echo -e "${C_RED}|                                                          |${C_RESET}"
	echo -e "${C_RED}|               Booting the selected rootfs...             |${C_RESET}"
	echo -e "${C_RED}|                                                          |${C_RESET}"
	echo -e "${C_RED}+----------------------------------------------------------+${C_RESET}"	

}

# This function pivot_root's into a real filesystem calling $newrootfs/sbin/init
# $1 = The new rootfs
pivot_realfs() {
	#test -z "$2" && RL="5" || RL="$2"
	mkdir -p $1/media/ROM || die "mkdir -p $1/media/ROM failed"

	mount -o remount,ro / >/dev/null 2>&1

	test "$ENABLE_IMAGECONF" = yes && image_conf $1

	do_pivot "$1" "$RL"
}

# This function loop-mounts an image-file and pivot_root's into it
# $1: The new rootfs
pivot_image() {
	#test -z "$2" && RL="5" || RL="$2"
	cd $1/$IMAGE_PATH

	# Check for rootfs images on the card
	if test "`ls *rootfs.bin | wc -l | tr -d " "`" -gt 1
	then
		echo -e "\n\nPlease select a rootfs:\n"

		# Show all available images
		x=0
		for file in `ls *rootfs.bin`
		do
			let x=$x+1
			echo -e "\t\t[$x] $file"
		done

		echo ""			

		IMAGE_NAME=""
		while test -z "$IMAGE_NAME"
		do
			echo -en "Please choose one of the above: "
			if test "$AUTOBOOT" != "yes"
			then
				read junk < "$OUT_TTY"
			else
				if test -e /etc/.altboot-loopimage.last
				then
					junk="`cat /etc/.altboot-loopimage.last`"
					test -z "$junk" && read junk < "$OUT_TTY" || echo "$junk (autoboot)"
				else
					read junk < "$OUT_TTY"
				fi
			fi	

			x=0
			for file in `ls *rootfs.bin`
			do
				let x=$x+1
				if test "$x" = "$junk"
				then
					IMAGE_NAME="$file"
					echo "$junk" > /etc/.altboot-loopimage.last						
				fi
			done	
		done
	else
		IMAGE_NAME="`ls *rootfs.bin`"		
		test -z "$IMAGE_NAME" && die "No rootfs found (*rootfs.bin) in $1/$IMAGE_PATH"
	fi
	
	
	echo ""

	mkdir -p /media/image || die "mkdir -p /media/image failed"

	losetup /dev/loop0 $1/$IMAGE_PATH/$IMAGE_NAME || die "losetup /dev/loop0 $1/$IMAGE_PATH/$IMAGE_NAME failed!"
	check_fs /dev/loop0 $IMAGE_TYPE
	
	echo -e "\n* * * Mounting rootfs image * * *\n"

	# Busybox's "mount" doesn't seem to like "-o loop" for some reason
	# It works on collie and b0rks on poodle.
	if [ "$IMAGE_TYPE" = "" ]; then
		IMAGE_TYPE="auto"
	fi
	
	# If mount fails it has the tendency to spew out a _lot_ of error messages.
	# We direct the output to /dev/null so the user can see which step actually failed.	
	mount /dev/loop0 -t $IMAGE_TYPE /media/image >/dev/null 2>&1 || die "mount -t $IMAGE_TYPE /dev/loop0 /media/image failed!"	

	mkdir -p /media/image/media/ROM || die "mkdir -p /media/image/media/ROM failed"

	test "$ENABLE_IMAGECONF" = yes && image_conf /media/image
	
	do_pivot /media/image "$RL"
}

#$1=mountpoint of the soon-to-be rootfs, $2=Runlevel
do_pivot(){	

	if test "$USE_KEXEC_ON_NEXT_BOOT" = yes
	then
		if test -e "$KEXEC_SELECTED_KERNEL.kexec.cfg"
		then
			CMDLINE="--append=\"`cat $KEXEC_SELECTED_KERNEL.kexec.cfg`\""
		else
			CMDLINE=""
			echo "WARNING: This kernel has not been configured!"
			echo "Trying to boot anyway..."
		fi
		
		echo "$KEXEC_BIN -l $KEXEC_SELECTED_KERNEL $CMDLINE"
		$KEXEC_BIN -l $KEXEC_SELECTED_KERNEL $CMDLINE
		sync
		
		read junk
		$KEXEC_BIN -e
		exit 0
	fi	


	echo -n "Pivoting root..."
	if (/sbin/pivot_root "$1" "$1/media/ROM") 
	then
		echo "Success"

		# This is important since we are still cd'ed into the old root
		cd /
		
		! test -d "$1" && mkdir -p "$1"
		
		# Move mountpoints from the old rootfs into the new one.
		# The *real* mount is kinda touchy feely about that
 		/bin/busybox mount -o move /media/ROM/proc /proc >/dev/null 2>&1

		for mpt in ` mount | grep "/media/ROM/" | awk '{print $3}'`
		do
			new_mpt="`echo "$mpt" | sed -n "s/\/media\/ROM//p"`"
			
			echo "Moving mountpoint [$mpt] -> [$new_mpt]" >"$OUT_TTY" 2>&1
			
			! test -d "$new_mpt" && mkdir -p "$new_mpt" 
			/bin/busybox mount -o move "$mpt" "$new_mpt"
		done

		clear		
		boot_new_rootfs_splash		
		echo "Calling INIT"
			
		exec /usr/sbin/chroot . $REAL_INIT $2 >"$OUT_TTY" 2>&1
		#exec /usr/sbin/chroot . /sbin/init $2 >/dev/tty0 2>&1
	else
		echo "FAILED"
		die "* * * pivot_root failed! * * *" 
	fi

}

# $1: Path to mounted rootfs
image_conf(){
	! test -d "$1" && die "image_conf: [$1] not found / no directory"	
	
	test -e "$1/etc/.image_conf.done" && return
	
	echo -e "\n\n* * * rootfs configuration * * *\n"
	echo -e "This setup lets you reconfigure your new rootfs."
	echo "Most probably the rootfs is configured with"
	echo "defaults based on a flash installation."
	echo "If unsure, go with the defaults by pressing <ENTER>."
	echo ""
	
	if ( cat $1/etc/fstab | grep -v "^#" | grep -q "/home " )		
	then						
		while true
		do
			echo "Usually your /home directory is located on another flash partition."
			echo -n "Do you want me to move /home inside the loop-image? [N|y] "
			read junk
			
			if test "$junk" = "y" -o "$junk" = "Y"
			then
			 	cat $1/etc/fstab | sed "/.*\/home.*/s/\/home/\/home.flash/" > $1/etc/fstab_
				mv $1/etc/fstab_ $1/etc/fstab

				mkdir -p $1/home.flash
				break
			fi
			
			test "$junk" = "" -o "$junk" = n -o "$junk" = N && break
		done
		
	fi		
				
	echo ""

	if ( cat $1/etc/ipkg.conf | grep -q ^lists_dir )		
	then						
		while true
		do
			echo -e "Wasting RAM is never a good idea.\nOnly say Y if your rootfs is very small in size"
			echo -en "Do you want to store ipkg package data\nin RAM? [N|y] "
			read junk
			
			if test "$junk" = "" -o "$junk" = n -o "$junk" = N
			then
				cat $1/etc/ipkg.conf | sed "/^lists_dir.*/s/\(.*\)/#\ \1/"> $1/etc/ipkg.conf_			 	
				mv $1/etc/ipkg.conf_ $1/etc/ipkg.conf
				break
			fi
			
			test "$junk" = "y" -o "$junk" = "Y" && break
		done
	fi		

	echo ""

	if ( cat $1/etc/ipkg.conf | grep -q "^dest sd" )		
	then						
		while true
		do
			echo -en "Do you want to keep the SD, CF and /home\nipkg install targets? [N|y] "
			read junk
			
			if test "$junk" = "" -o "$junk" = n -o "$junk" = N
			then
				cat $1/etc/ipkg.conf | sed "/^dest\ \(sd\|cf\|home\).*/s/\(.*\)/#\ \1/"	> $1/etc/ipkg.conf_		 	
				mv $1/etc/ipkg.conf_ $1/etc/ipkg.conf
				break
			fi
			
			test "$junk" = "y" -o "$junk" = "Y" && break
		done
	fi	
		
	
	touch "$1/etc/.image_conf.done"	
}

# This functions configures the master password for altboot if none is set
set_password() {
	mount -o remount,rw /
	if test -z "$MASTER_PASSWORD" -a "$ENABLE_DEBUG" != yes
	then
		echo -e "\nAltboot is a boot-manager which allows to boot from SD,\nCF, USB-Storage and NFS"
		echo -e "\nFor security reasons altboot requires a password\nto boot into init=/bin/sh."
		echo -e "${C_RED}This is *not* your root password!\nIt is used by altboot alone!${C_RESET}\n"

		while true
		do
			echo -en "\nNew password: "

			stty -echo <"$OUT_TTY" >"$OUT_TTY" 2>&1
			read junk1 < "$OUT_TTY"
			stty echo <"$OUT_TTY" >"$OUT_TTY" 2>&1

			if ! test -z "$junk1"
			then
				echo -en "\nRepeat: "
				
				stty -echo <"$OUT_TTY" >"$OUT_TTY" 2>&1
				read junk2  < "$OUT_TTY"
				stty echo <"$OUT_TTY" >"$OUT_TTY" 2>&1
				echo ""
				
				if test "$junk1" = "$junk2"
				then
					crypt_pw="`echo "$junk1" | md5sum | awk '{print $1}'`"
					
					if test -e "${ALTBOOT_CFG_FILE}"
					then
						sed "/^MASTER_PASSWORD/s/\(.*\=\).*/\1\"$crypt_pw\"/" "${ALTBOOT_CFG_FILE}" > ${ALTBOOT_CFG_FILE}_
						mv ${ALTBOOT_CFG_FILE}_ ${ALTBOOT_CFG_FILE}
						MASTER_PASSWORD="$crypt_pw"
						echo "Password changed."
					else
						echo "${ALTBOOT_CFG_FILE} is missing, no password saved"
					fi
					
					break
				else
					echo -e "Passwords didn't match, try again\n"
				fi
			fi	
		done
	fi
	
}

# This function asks for altboots master password. It only returns if the correct password was supplied
verify_master_pw() {
	if ! test -z "$MASTER_PASSWORD"
	then
		auth_timeout="3"

		echo -e "\nPlease enter your altboot master password"

		cnt=0
		while true
		do
			let cnt=$cnt+$auth_timeout
			echo -n "Password: "
			stty -echo  <"$OUT_TTY" >"$OUT_TTY" 2>&1
			read junk < "$OUT_TTY"
			stty echo  <"$OUT_TTY" >"$OUT_TTY" 2>&1

			if test "`echo "$junk" | md5sum | awk '{print $1}'`" = "$MASTER_PASSWORD"
			then
				break
			else
				echo "[`echo "$junk" | md5sum | awk '{print $1}'`]"
				echo "[$MASTER_PASSWORD]"
				echo "Wrong password, sleeping $cnt seconds..."
				sleep $cnt

#				if test "$cnt" -gt 10
#				then
#					break
#				fi
			fi
		done
		
		echo ""
	fi
}


check_fs() {
	if [ "$FSCK_IMAGES" = "yes" ]
	then
                FSCK=""
		if [ "$2" = "" ]; then
			FSTYPE="ext2"
		else
			FSTYPE="$2"
		fi
		case "$FSTYPE" in
		ext2 | ext3)
	        	if [ -e /sbin/fsck.ext3 ]; then
		       		FSCK="/sbin/fsck.ext3"
			elif [ -e /sbin/e3fsck ]; then
		       		FSCK="/sbin/e3fsck"
			elif [ -e /sbin/fsck.ext2 ]; then
		       		FSCK="/sbin/fsck.ext2"
			elif [ -e /sbin/e2fsck ]; then
		       		FSCK="/sbin/e2fsck"
			fi
			test -n "$FSCK" && FSCK="$FSCK -p"
		;;
		vfat)
		    if [ -e /sbin/dosfsck ]; then
			FSCK="/sbin/dosfsck -a"
		    fi
		;;
		esac
		if [ "$FSCK" = "" ]; then
		        echo "Could not find fsck for $FSTYPE!"
		else
			echo "Checking file system on $1"
			$FSCK $1 || sleep 2
		fi
	fi
}

# Make the initial rootfs a bit more usable
init_rootfs(){
#	echo -n "Mounting rootfs rw..." 
	mount -o remount,rw / || die "mount -o remount,rw / failed"

	mount | grep -q "/proc " >/dev/null 2>&1 || mount proc -t proc /proc >"$OUT_TTY" 2>&1
	
	if ( uname -r | grep -q "2.6." )
	then
		mount | grep -q "/sys " >/dev/null 2>&1 || mount sys -t sysfs /sys >"$OUT_TTY" 2>&1
	fi
		
	/etc/init.d/devices start || die "FAILED"
}

mount_sd(){
	if mount | grep -q "/media/card"
	then
		echo "Note: /media/card is already mounted"
	else
		# We can't trust that the SD device file is there when running kernel 2.6 w/ udev
		# and starting udev at this point may not be the best idea...	
		if `uname -r | grep -q "2.6"`
		then
			#Let's just assume the device file name never changes...
			dev_no="`echo "$SD_DEVICE" | sed -n "s/\/dev\/mmcblk\(.*\)p\(.*\)/\1/p"`"
			part_no="`echo "$SD_DEVICE" | sed -n "s/\/dev\/mmcblk\(.*\)p\(.*\)/\2/p"`"
			! test -e /dev/mmcblk${dev_no} && mknod /dev/mmcblk${dev_no} b 254 0
			! test -e /dev/mmcblk${dev_no}p${part_no} && mknod /dev/mmcblk${dev_no}p${part_no} b 254 $part_no				
		fi

		# Kernel 2.6 has the SD driver compiled into the kernel
		if test -n "$SD_KERNEL_MODULE"	
		then
			echo -n "Loading SD kernel module..."
			/sbin/insmod $SD_KERNEL_MODULE >/dev/null 2>&1 && echo ok || die "insmod failed"
		fi
		
		check_fs "$SD_DEVICE"

		echo -n "Mounting $SD_MOUNTPOINT..."  >"$OUT_TTY"
		
		if test "$1" = ingore_errors
		then
			/bin/mount -t auto -o defaults,noatime $SD_DEVICE $SD_MOUNTPOINT >/dev/null 2>&1 && echo ok  >"$OUT_TTY" || echo "Could not mount SD card"
		else
			/bin/mount -t auto -o defaults,noatime $SD_DEVICE $SD_MOUNTPOINT >/dev/null 2>&1 && echo ok  >"$OUT_TTY"|| die "/bin/mount -t auto -o defaults,noatime $SD_DEVICE $SD_MOUNTPOINT failed"
		fi		
	fi
	echo ""	
	
	  # Give the SD and CF mounting some time. This is a must for SD			
	  sleep 2	
}

mount_cf(){
	if mount | grep -q "/media/cf "
	then
		echo "Note: /media/cf is already mounted"
	else	
		if ( cat /etc/fstab | grep -v "^#" | grep -q "/media/cf" )
		then
			# As of kernel 2.6.16, /e/i/pcmcia is replaced by udev
			if test -x /etc/init.d/pcmcia
			then
				/etc/init.d/pcmcia status | grep -q running || /etc/init.d/pcmcia start && echo "Note: cardmgr is already active"
			else
				for n in 1 2 3
				do
					! test -e "/dev/hda$n" && mknod /dev/hda$n b 3 $n
				done
			fi			

			# Give the SD and CF mounting some time. This is a must for SD			
			sleep 2

			mount /media/cf -o async
		else
			echo "Note: Your system's fstab does not include an entry for /media/cf"
		fi
	fi
}

mount_home(){
	if mount | grep -q "/home "
	then
		echo "Note: /home is already mounted"
	else

		if ( cat /etc/fstab | grep -v "^#" | grep -q "/home " )
		then
			echo "Mounting /home"
			home_fstab="`grep "/home " /etc/fstab`"
			home_dev="`echo "$home_fstab" | awk '{print $1}'`"
			home_fs="`echo "$home_fstab" | awk '{print $3}'`"		
			home_options="`echo "$home_fstab" | awk '{print $4}'`"

			mount -t "$home_fs" -o $home_options "$home_dev" /home
		else
			echo "Note: Your system's fstab does not include an entry for /home"
		fi
	fi
}

show_menu() {	
	
	echo -e "\nPress <ENTER> to return to the menu"
	read junk
	
	test "$junk" = x && exec /bin/sh || exec /sbin/init.altboot -force<"$OUT_TTY" >"$OUT_TTY" 2>&1
}

mdie() {
	echo -e "${C_RED}ERROR:${C_RESET}${C_WHITE} $1${C_RESET}" >"$OUT_TTY"
	
	echo -e "\nPress <ENTER> to return to the menu"
	read junk
	
	test "$junk" = x && exec /bin/sh || exec /sbin/init.altboot -force<"$OUT_TTY" >"$OUT_TTY" 2>&1
}

# $1: uniq name, $2 identifier, $3 value
set_pref() {
	data_name="$1"
	data_id="$2"
	data_value="$3"
	
	#echo "[$1] [$2] [$3]"
	#export "${data_name}"="`eval echo -e \\$${data_name} | sed "s/\#\#\#/\#\#\#\\n/g"|sed s/^\ // | sed s/^$data_id.*//`"

	export "${data_name}"="`eval echo -e \\$${data_name} `$data_id##$data_value###"
	
}

# $1: uniq name
reset_pref() {
	data_name="$1"
	export "${data_name}"=""	
}

echo_pref() {
	data_name="$1"
	echo "****** $1 ******"
	echo "`eval echo -e \\$${data_name} | sed "s/\#\#\#/\#\#\#\\n/g"|sed s/^\ // `"
	echo "******"
}

dump_pref() {
	data_name="$1"	
	echo "`eval echo -e \\$${data_name} | sed "s/\#\#\#/\#\#\#\\n/g"|sed s/^\ // `"
}

# $1: uniq name, $2 identifier, $3 out var
get_pref() {
	data_name="$1"
	data_id="$2"
	data_out="$3"
	data_list="`eval echo -e \\$${data_name}`"
	
	#data_value="`echo "$data_list"| sed "s/\#\#\#/\\n/g"|sed s/^\ // | grep "^$data_id##" | sed -n "s/.*\#\(.*\)$/\1/p"`"
	#data_value="`echo "$data_list"| sed "s/\#\#\#/\\n/g"|sed s/^\ // |  sed -n "/^$data_id/s/.*\#\(.*\)$/\1/p"`"
	data_value="`echo "$data_list"| sed "s/\#\#\#/\\n/g" | sed -n "s/^\ //;/^$data_id\#/s/.*\#\(.*\)$/\1/p"`"
	# echo "WERT: [$data_value]"
	
	export "${data_out}"="$data_value"
	test -n "$data_value" && return 0
}

debug_echo() {
	test "$ENABLE_DEBUG" = "yes" && echo -e "${C_YELLOW}DEBUG:${C_RESET}${C_WHITE} $1 ${C_RESET}" >"$OUT_TTY" 2>&1
}

start_networking() {

	test -z "$1" && mdie "No remote host configured, check /etc/fstab for NFS hosts"
	
	if test "$USB_NETWORKING_AVAILABLE" = "yes"
	then
		echo ""
		echo "Select the type of your network connection:"
		echo ""
		echo -e "\t[1] LAN or WLAN NIC"
		echo -e "\t[2] USB Connection"
		echo ""
		
		if test "$AUTOBOOT" != "yes" -o ! -e /etc/.altboot-lanselect.last
		then
			while true
			do
				echo -n "Connection Type: "
				read junk
				
				test "$junk" = 1 -o "$junk" = 2 && break
			done
		else
			junk="`cat /etc/.altboot-lanselect.last`"
			test -z "$junk" && junk=1
			
			echo "Connection Type: $junk (autoboot)"
		fi
			
		case "$junk" in
		1)	NW_TYPE="NIC"
			;;
		2)	NW_TYPE="USB"
			;;
		esac
		
		echo "$junk" > /etc/.altboot-lanselect.last
	else
		NW_TYPE=NIC
	fi


	
	# Needed for NFS
	/etc/init.d/portmap start >/dev/null 2>&1 || die "/etc/init.d/portmap start failed!"
	
	# For some reason NFS mounts hang if /e/i/networking is not run.
	# For the time beeing I'm too lazy to investigate ;)
	/etc/init.d/networking start >/dev/null 2>&1 || die "/etc/init.d/networking start failed!"
	
	sleep 2

	if test "$NW_TYPE" = "NIC"
	then	
		# After the PCMCIA service is started, an inserted WLAN card should automatically
		# activate itself.

		if test -x /etc/init.d/pcmcia
		then
			echo -e "\nRunning cardctl to setup networking..."
			/etc/init.d/pcmcia start >/dev/null 2>&1 || die "/etc/init.d/pcmcia start failed!"
			sleep 3
		else	
			# With kernel 2.6.16+ udev is used 
			echo -e "\nRunning udevd to setup networking..."
			ps ax | grep -v grep | grep -q udevd || /etc/init.d/udev start >/dev/null 2>&1 || die "/etc/init.d/udev start failed!"

			# Stop udev to work around some very ugly (yet harmless) error messages on boot
			/etc/init.d/udev stop >/dev/null 2>&1
		fi
	fi
	
	if test "$NW_TYPE" = "USB"
	then
		echo ""
		for module in $USB_NW_MODULES
		do
			echo "modprobing [$module]"
			modprobe $module || die "modprobe $module FAILED"
		done
	
		ifdown "$USB_NW_DEVICE" >/dev/null 2>&1
	
		
		echo -e "\nPlease make sure that usb0 is up on your PC and hit <ENTER>."
		read junk
			
		ifup "$USB_NW_DEVICE"
	fi

	WLAN_NIC="`iwconfig 2>/dev/null | grep ESSID | grep -v wifi | awk '{print $1}'`"

	if test -z "$WLAN_NIC"
	then
		# cardctl needs some time....
		echo -n "Waiting for WLAN card.."
		timeout=0
		while test "$timeout" -lt 10
		do
			WLAN_NIC="`iwconfig 2>/dev/null | grep ESSID | grep -v wifi | awk '{print $1}'`"
			test -n "$WLAN_NIC" && break			
			echo -n "."
			let timeout=$timeout+1
			sleep 1
		done	
		
		if test -z  "$WLAN_NIC"
		then
			debug_echo "WARNING: WLAN_NIC is empty!\n" 
			debug_echo "Filter result: [$WLAN_NIC]"
			debug_echo "iwconfig: [`iwconfig`]"
			mdie "No network interface found"
		fi			
	fi
	
	# WLAN with DHCP needs some time to get a lease, set up the routing, etc.	
	echo -n "Waiting for host [$1] on [$WLAN_NIC]."
	cnt=0
	while true
	do
		if (ping -c 1 $1) >/dev/null 2>&1
		then
			echo " found"
			break
		else
			if test "$cnt" = 30 -o "$cnt" = 60
			then
				echo ""
				echo "WARNING: $NW_TYPE didn't activate in $cnt seconds!"
				
				if test "$cnt" = 30
				then
					let cnt=$cnt+1
			
					if test "$NW_TYPE" = "NIC"
					then
						echo "Restarting udhcpc for [$WLAN_NIC]"
						killall udhcpc
					
						udhcpc -i "$WLAN_NIC" -H `cat /etc/hostname` >"$OUT_TTY" 2>&1
					fi
					
					if test "$NW_TYPE" = "USB"
					then
						echo "ifdown/up $USB_NW_DEVICE..."
						ifdown "$USB_NW_DEVICE" 
						sleep 1
						ifup "$USB_NW_DEVICE"
					fi			
					
				else
					mdie "Failed to activate $NW_TYPE!"
					break			
				fi
			else
				echo -n "."
				let cnt=$cnt+1
			fi
		fi
		sleep 1
	done				
	
	
}