]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/altboot/files/altboot.func
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / altboot / files / altboot.func
1 #! /bin/sh
2 # This function checks for the presence of a real filesystem and loop-images on the target
3 # $1 = folder of rootfs, $2 = runlevel (defaults to 5)
4 check_target() {
5         # Check if there is a /sbin/init or /sbin/init.sysvinit on the card
6         if test -x $1/sbin/init -o -x $1/$REAL_INIT
7         then
8                 real_fs_found=1
9         else
10                 echo -e "Note: No INIT [$REAL_INIT] found on target"
11         fi
12         
13         # Check for loop-images
14         if (ls $1/$IMAGE_PATH/*rootfs.bin) >/dev/null 2>&1
15         then
16                 image_found=1
17         else
18                 echo "Note: No boot-images found in [$1/$IMAGE_PATH]"
19         fi
20         
21         # Check if we have both, a real fs and boot-images. If so, ask the user what to boot
22         if test "$real_fs_found" = 1 -a "$image_found" = 1
23         then
24                 echo -e "\nI have found a real filesystem and boot-images on the target"
25                 echo -e "What do you want to boot?\n"
26                 
27                 echo -e "\t[1] The real filesystem"
28                 echo -e "\t[2] A loop-image"
29                 echo ""
30                 
31                 while test -z "$ans"
32                 do
33                         echo -n "Your choice: "
34                         read junk < /dev/tty1
35                         
36                         if test "$junk" = 1 -o "$junk" = 2
37                         then
38                                 ans="$junk"
39                         fi
40                 done
41                 
42                 case "$ans" in
43                 1)      pivot_realfs "$1" "$2">/dev/tty0;;
44                 2)      pivot_image "$1" "$2">/dev/tty0;;
45                 esac
46                                 
47                 exit 0
48         fi
49
50         # Boot a real filesystem        
51         test "$real_fs_found" = 1 && pivot_realfs "$1" >/dev/tty0
52         
53         # Boot a loop-image
54         test "$image_found" = 1 && pivot_image "$1" >/dev/tty0
55                 
56         if test "$real_fs_found" != 1 -a "$image_found" != 1
57         then
58                 die "Nothing to do!"
59         fi
60 }
61
62 # This function pivot_root's into a real filesystem calling $newrootfs/sbin/init
63 # $1 = The new rootfs
64 pivot_realfs() {
65         test -z "$2" && RL="5" || RL="$2"
66         mkdir -p $1/media/ROM || die "mkdir -p $1/media/ROM failed"
67
68         mount -o remount,ro / >/dev/null 2>&1
69
70         do_pivot "$1" "$RL"
71 }
72
73 # This function loop-mounts an image-file and pivot_root's into it
74 # $1: The new rootfs
75 pivot_image() {
76         test -z "$2" && RL="5" || RL="$2"
77         cd $1/$IMAGE_PATH
78
79         # Check for rootfs images on the card
80         if test "`ls *rootfs.bin | wc -l | tr -d " "`" -gt 1
81         then
82                 echo -e "\n\nPlease select a rootfs:\n"
83
84                 # Show all available images
85                 x=0
86                 for file in `ls *rootfs.bin`
87                 do
88                         let x=$x+1
89                         echo -e "\t\t[$x] $file"
90                 done
91
92                 echo ""                 
93
94                 IMAGE_NAME=""
95                 while test -z "$IMAGE_NAME"
96                 do
97                         echo -en "Please choose one of the above: "
98                         read junk < /dev/tty1
99
100                         x=0
101                         for file in `ls *rootfs.bin`
102                         do
103                                 let x=$x+1
104                                 if test "$x" = "$junk"
105                                 then
106                                         IMAGE_NAME="$file"                                              
107                                 fi
108                         done    
109                 done
110         else
111                 IMAGE_NAME="`ls *rootfs.bin`"           
112                 test -z "$IMAGE_NAME" && die "No rootfs found (*rootfs.bin) in $1/$IMAGE_PATH"
113         fi
114
115         echo "Using [$IMAGE_NAME]"
116
117         mkdir -p /media/image || die "mkdir -p /media/image failed"
118
119         echo "Setting up loopback (/dev/loop0) for $IMAGE_NAME"
120         losetup /dev/loop0 $1/$IMAGE_PATH/$IMAGE_NAME || die "losetup /dev/loop0 $1/$IMAGE_PATH/$IMAGE_NAME failed!"
121         check_fs /dev/loop0 $IMAGE_TYPE
122         
123         echo -e "\n* * * Booting rootfs image * * *\n"
124
125         # Busybox's "mount" doesn't seem to like "-o loop" for some reason
126         # It works on collie and b0rks on poodle.
127         if [ "$IMAGE_TYPE" = "" ]; then
128                 IMAGE_TYPE="auto"
129         fi
130         # If mount fails it has the tendency to spew out a _lot_ of error messages.
131         # We direct the output to /dev/null so the user can see which step actually failed.     
132         mount /dev/loop0 -t $IMAGE_TYPE /media/image >/dev/null 2>&1 || die "mount -t $IMAGE_TYPE /dev/loop0 /media/image failed!"      
133
134         mkdir -p /media/image/media/ROM || die "mkdir -p /media/image/media/ROM failed"
135
136         do_pivot /media/image "$RL"
137 }
138
139 #$1=mountpoint of the soon-to-be rootfs, $2=Runlevel
140 do_pivot(){
141         echo -n "Pivoting root..."
142         if (/sbin/pivot_root "$1" "$1/media/ROM") 
143         then
144                 echo "Success"
145
146                 # This is important since we are still cd'ed into the old root
147                 cd /
148                 
149                 ! test -d "$1" && mkdir -p "$1"
150                 
151                 # Move mountpoints from the old rootfs into the new one.
152                 # The *real* mount is kinda touchy feely about that
153                 /bin/busybox mount -o move /media/ROM/proc /proc >/dev/null 2>&1
154
155                 for mpt in ` mount | grep "/media/ROM/" | awk '{print $3}'`
156                 do
157                         new_mpt="`echo "$mpt" | sed -n "s/\/media\/ROM//p"`"
158                         
159                         echo "Moving mountpoint [$mpt] -> [$new_mpt]" >/dev/tty0 2>&1
160                         
161                         ! test -d "$new_mpt" && mkdir -p "$new_mpt" 
162                         /bin/busybox mount -o move "$mpt" "$new_mpt"
163                 done
164                 
165                 echo "Calling INIT"
166                 
167                 #read junk
168                 
169                 exec /usr/sbin/chroot . /sbin/init $2 >/dev/tty0 2>&1
170         else
171                 echo "FAILED"
172                 die "* * * pivot_root failed! * * *" 
173         fi
174
175 }
176
177 # This functions configures the master password for altboot if none is set
178 set_password() {
179         mount -o remount,rw /
180         if test -z "$MASTER_PASSWORD"
181         then
182                 echo -e "\nAltboot is a boot-manager which allows to boot from SD,\nCF, USB-Storage and NFS"
183                 echo -e "\nFor security reasons altboot requires a password\nto boot into init=/bin/sh."
184                 echo -e "${C_RED}This is *not* your root password!\nIt is used by altboot alone!${C_RESET}\n"
185
186                 while true
187                 do
188                         echo -en "\nNew password: "
189
190                         stty -echo
191                         read junk1 < /dev/tty0
192                         stty echo
193
194                         if ! test -z "$junk1"
195                         then
196                                 echo -en "\nRepeat: "
197                                 
198                                 stty -echo
199                                 read junk2  < /dev/tty0
200                                 stty echo
201                                 echo ""
202                                 
203                                 if test "$junk1" = "$junk2"
204                                 then
205                                         crypt_pw="`echo "$junk1" | md5sum | awk '{print $1}'`"
206                                         
207                                         if test -e "${ALTBOOT_CFG_FILE}"
208                                         then
209                                                 sed "/^MASTER_PASSWORD/s/\(.*\=\).*/\1\"$crypt_pw\"/" "${ALTBOOT_CFG_FILE}" > ${ALTBOOT_CFG_FILE}_
210                                                 mv ${ALTBOOT_CFG_FILE}_ ${ALTBOOT_CFG_FILE}
211                                                 MASTER_PASSWORD="$crypt_pw"
212                                                 echo "Password changed."
213                                         else
214                                                 echo "${ALTBOOT_CFG_FILE} is missing, no password saved"
215                                         fi
216                                         
217                                         break
218                                 else
219                                         echo -e "Passwords didn't match, try again\n"
220                                 fi
221                         fi      
222                 done
223         fi
224         
225 }
226
227 # This function asks for altboots master password. It only returns if the correct password was supplied
228 verify_master_pw() {
229         if ! test -z "$MASTER_PASSWORD"
230         then
231                 auth_timeout="3"
232
233                 echo "Please enter your altboot master password"
234
235                 cnt=0
236                 while true
237                 do
238                         let cnt=$cnt+$auth_timeout
239                         echo -n "Password: "
240                         read junk < /dev/tty0
241
242                         if test "`echo "$junk" | md5sum | awk '{print $1}'`" = "$MASTER_PASSWORD"
243                         then
244                                 break
245                         else
246                                 echo "[`echo "$junk" | md5sum | awk '{print $1}'`]"
247                                 echo "[$MASTER_PASSWORD]"
248                                 echo "Wrong password, sleeping $cnt seconds..."
249                                 sleep $cnt
250
251 #                               if test "$cnt" -gt 10
252 #                               then
253 #                                       break
254 #                               fi
255                         fi
256                 done
257         fi
258 }
259
260
261 check_fs() {
262         if [ "$FSCK_IMAGES" = "yes" ]
263         then
264                 FSCK=""
265                 if [ "$2" = "" ]; then
266                         FSTYPE="ext2"
267                 else
268                         FSTYPE="$2"
269                 fi
270                 case "$FSTYPE" in
271                 ext2 | ext3)
272                         if [ -e /sbin/fsck.ext3 ]; then
273                                 FSCK="/sbin/fsck.ext3"
274                         elif [ -e /sbin/e3fsck ]; then
275                                 FSCK="/sbin/e3fsck"
276                         elif [ -e /sbin/fsck.ext2 ]; then
277                                 FSCK="/sbin/fsck.ext2"
278                         elif [ -e /sbin/e2fsck ]; then
279                                 FSCK="/sbin/e2fsck"
280                         fi
281                         test -n "$FSCK" && FSCK="$FSCK -p"
282                 ;;
283                 vfat)
284                     if [ -e /sbin/dosfsck ]; then
285                         FSCK="/sbin/dosfsck -a"
286                     fi
287                 ;;
288                 esac
289                 if [ "$FSCK" = "" ]; then
290                         echo "Could not find fsck for $FSTYPE!"
291                 else
292                         echo "Checking file system on $1"
293                         $FSCK $1 || sleep 2
294                 fi
295         fi
296 }
297
298 # Make the initial rootfs a bit more usable
299 init_rootfs(){
300         echo -n "Mounting rootfs rw..." >/dev/tty0
301         mount -o remount,rw / >/dev/tty0 2>&1 && echo ok  >/dev/tty0|| die "mount -o remount,rw / failed"
302
303         mount | grep -q "/proc " >/dev/null 2>&1 && echo "Note: /proc is already mounted" || mount proc -t proc /proc >/dev/tty0 2>&1
304         
305         if ( uname -r | grep -q "2.6." )
306         then
307                 mount | grep -q "/sys " >/dev/null 2>&1 && echo "Note: /sys is already mounted" || mount sys -t sysfs /sys >/dev/tty0 2>&1
308         fi
309         
310         echo -n "Generating device files..." >/dev/tty0
311         /etc/init.d/devices start && echo ok  >/dev/tty0 || die "FAILED"
312 }
313
314 mount_sd(){
315         if mount | grep -q "/media/card"
316         then
317                 echo "Note: /media/card is already mounted"
318         else
319                 # We can't trust that the SD device file is there when running kernel 2.6 w/ udev
320                 # and starting udev at this point may not be the best idea...   
321                 if `uname -r | grep -q "2.6"`
322                 then
323                         #Let's just assume the device file name never changes...
324                         dev_no="`echo "$SD_DEVICE" | sed -n "s/\/dev\/mmcblk\(.*\)p\(.*\)/\1/p"`"
325                         part_no="`echo "$SD_DEVICE" | sed -n "s/\/dev\/mmcblk\(.*\)p\(.*\)/\2/p"`"
326                         ! test -e /dev/mmcblk${dev_no} && mknod /dev/mmcblk${dev_no} b 254 0
327                         ! test -e /dev/mmcblk${dev_no}p${part_no} && mknod /dev/mmcblk${dev_no}p${part_no} b 254 $part_no                               
328                 fi
329
330                 # Kernel 2.6 has the SD driver compiled into the kernel
331                 if test -n "$SD_KERNEL_MODULE"  
332                 then
333                         echo -n "Loading SD kernel module..."
334                         /sbin/insmod $SD_KERNEL_MODULE >/dev/null 2>&1 && echo ok || die "insmod failed"
335                 else
336                         echo "No SD kernel module configured, assuming it's build-in"
337                 fi
338                 
339                 check_fs "$SD_DEVICE"
340
341                 echo -n "Mounting $SD_MOUNTPOINT..."  >/dev/tty0
342                 /bin/mount -t auto -o defaults,noatime $SD_DEVICE $SD_MOUNTPOINT >/dev/null 2>&1 && echo ok  >/dev/tty0|| die "/bin/mount -t auto -o defaults,noatime $SD_DEVICE $SD_MOUNTPOINT failed"
343         fi
344         echo "" 
345         
346           # Give the SD and CF mounting some time. This is a must for SD                        
347           sleep 2       
348 }
349
350 mount_cf(){
351         if mount | grep -q "/media/cf "
352         then
353                 echo "Note: /media/cf is already mounted"
354         else    
355                 /etc/init.d/pcmcia status | grep -q running || /etc/init.d/pcmcia start && echo "Note: cardmgr is already active"
356
357                 echo ""
358
359                 # Give the SD and CF mounting some time. This is a must for SD                  
360                 sleep 2
361                 
362                 mount | grep -q "/media/cf " || mount /media/cf
363         fi
364 }
365
366 mount_home(){
367         if mount | grep -q "/home "
368         then
369                 echo "Note: /home is already mounted"
370         else
371
372                 if ( cat /etc/fstab | grep -v "^#" | grep "/home " )
373                 then
374                         echo "Mounting /home"
375                         home_fstab="`grep "/home " /etc/fstab`"
376                         home_dev="`echo "$home_fstab" | awk '{print $1}'`"
377                         home_fs="`echo "$home_fstab" | awk '{print $3}'`"               
378                         home_options="`echo "$home_fstab" | awk '{print $4}'`"
379
380                         mount -t "$home_fs" -o $home_options "$home_dev" /home
381                 fi      
382         fi
383 }