OSDN Git Service

Merge remote-tracking branch 'x86/nougat-x86' into oreo-x86
[android-x86/bootable-newinstaller.git] / initrd / init
1 #!/bin/busybox sh
2 #
3 # By Chih-Wei Huang <cwhuang@linux.org.tw>
4 # and Thorsten Glaser <tg@mirbsd.org>
5 #
6 # License: GNU Public License
7 # We explicitely grant the right to use the scripts
8 # with Android-x86 project.
9 #
10
11 PATH=/sbin:/bin:/system/bin:/system/xbin; export PATH
12
13 # auto installation
14 [ -n "$AUTO_INSTALL" ] && INSTALL=1
15
16 # configure debugging output
17 if [ -n "$DEBUG" -o -n "$INSTALL" ]; then
18         LOG=/tmp/log
19         set -x
20 else
21         LOG=/dev/null
22         test -e "$LOG" || busybox mknod $LOG c 1 3
23 fi
24 exec 2>> $LOG
25
26 # early boot
27 if test x"$HAS_CTTY" != x"Yes"; then
28         # initialise /proc and /sys
29         busybox mount -t proc proc /proc
30         busybox mount -t sysfs sys /sys
31         # let busybox install all applets as symlinks
32         busybox --install -s
33         # spawn shells on tty 2 and 3 if debug or installer
34         if test -n "$DEBUG" || test -n "$INSTALL"; then
35                 # ensure they can open a controlling tty
36                 mknod /dev/tty c 5 0
37                 # create device nodes then spawn on them
38                 mknod /dev/tty2 c 4 2 && openvt
39                 mknod /dev/tty3 c 4 3 && openvt
40         fi
41         if test -z "$DEBUG" || test -n "$INSTALL"; then
42                 echo 0 0 0 0 > /proc/sys/kernel/printk
43         fi
44         # initialise /dev (first time)
45         mkdir -p /dev/block
46         echo /sbin/mdev > /proc/sys/kernel/hotplug
47         mdev -s
48         # re-run this script with a controlling tty
49         exec env HAS_CTTY=Yes setsid cttyhack /bin/sh "$0" "$@"
50 fi
51
52 # now running under a controlling tty; debug output from stderr into log file
53 # boot up Android
54
55 error()
56 {
57         echo $*
58         return 1
59 }
60
61 try_mount()
62 {
63         RW=$1; shift
64         if [ "${ROOT#*:/}" != "$ROOT" ]; then
65                 # for NFS roots, use nolock to avoid dependency to portmapper
66                 mount -o $RW,noatime,nolock $@
67                 return $?
68         fi
69         case $(blkid $1) in
70                 *TYPE=*ntfs*)
71                         mount.ntfs-3g -o rw,force $@
72                         ;;
73                 *TYPE=*)
74                         mount -o $RW,noatime $@
75                         ;;
76                 *)
77                         return 1
78                         ;;
79         esac
80 }
81
82 check_root()
83 {
84         if [ "`dirname $1`" = "/dev" ]; then
85                 [ -e $1 ] || return 1
86                 blk=`basename $1`
87                 [ ! -e /dev/block/$blk ] && ln $1 /dev/block
88                 dev=/dev/block/$blk
89         else
90                 dev=$1
91         fi
92         try_mount ro $dev /mnt || return 1
93         if [ -n "$iso" -a -e /mnt/$iso ]; then
94                 mount --move /mnt /iso
95                 mkdir /mnt/iso
96                 mount -o loop /iso/$iso /mnt/iso
97         fi
98         if [ -e /mnt/$SRC/$RAMDISK ]; then
99                 zcat /mnt/$SRC/$RAMDISK | cpio -id > /dev/null
100         elif [ -b /dev/$RAMDISK ]; then
101                 zcat /dev/$RAMDISK | cpio -id > /dev/null
102         else
103                 return 1
104         fi
105         if [ -e /mnt/$SRC/system.sfs ]; then
106                 mount -o loop,noatime /mnt/$SRC/system.sfs system
107                 if [ -e system/system.img ]; then
108                         mount --move system /sfs
109                         mount -o loop,noatime /sfs/system.img system
110                 fi
111         elif [ -e /mnt/$SRC/system.img ]; then
112                 remount_rw
113                 mount -o loop,noatime /mnt/$SRC/system.img system
114         elif [ -s /mnt/$SRC/system/build.prop ]; then
115                 remount_rw
116                 mount --bind /mnt/$SRC/system system
117         elif [ -z "$SRC" -a -s /mnt/build.prop ]; then
118                 mount --bind /mnt system
119         else
120                 rm -rf *
121                 return 1
122         fi
123         mkdir -p mnt
124         echo " found at $1"
125         rm /sbin/mke2fs
126         hash -r
127 }
128
129 remount_rw()
130 {
131         # "foo" as mount source is given to workaround a Busybox bug with NFS
132         # - as it's ignored anyways it shouldn't harm for other filesystems.
133         mount -o remount,rw foo /mnt
134 }
135
136 debug_shell()
137 {
138         if [ -x system/bin/sh ]; then
139                 echo Running MirBSD Korn Shell...
140                 USER="($1)" system/bin/sh -l 2>&1
141                 [ $? -ne 0 ] && /bin/sh 2>&1
142         else
143                 echo Running busybox ash...
144                 sh 2>&1
145         fi
146 }
147
148 echo -n Detecting Android-x86...
149
150 [ -z "$SRC" -a -n "$BOOT_IMAGE" ] && SRC=`dirname $BOOT_IMAGE`
151 [ -z "$RAMDISK" ] && RAMDISK=ramdisk.img || RAMDISK=${RAMDISK##/dev/}
152
153 for c in `cat /proc/cmdline`; do
154         case $c in
155                 iso-scan/filename=*)
156                         SRC=iso
157                         eval `echo $c | cut -b1-3,18-`
158                         ;;
159                 *)
160                         ;;
161         esac
162 done
163
164 mount -t tmpfs tmpfs /android
165 cd /android
166 while :; do
167         for device in ${ROOT:-/dev/[hmnsv][dmrv][0-9a-z]*}; do
168                 check_root $device && break 2
169                 mountpoint -q /mnt && umount /mnt
170         done
171         sleep 1
172         echo -n .
173 done
174
175 ln -s mnt/$SRC /src
176 ln -s android/system /
177 ln -s ../system/lib/firmware ../system/lib/modules /lib
178
179 if [ -n "$INSTALL" ]; then
180         zcat /src/install.img | ( cd /; cpio -iud > /dev/null )
181 fi
182
183 if [ -x system/bin/ln -a -n "$BUSYBOX" ]; then
184         mv -f /bin /lib .
185         sed -i 's|\( PATH.*\)|\1:/bin|' init.environ.rc
186         rm /sbin/modprobe
187         busybox mv /sbin/* sbin
188         rmdir /sbin
189         ln -s android/bin android/lib android/sbin /
190         hash -r
191 fi
192
193 # load scripts
194 for s in `ls /scripts/* /src/scripts/*`; do
195         test -e "$s" && source $s
196 done
197
198 # ensure keyboard driver is loaded
199 if [ -n "$INSTALL" -o -n "$DEBUG" ]; then
200         busybox modprobe -a atkbd hid-apple
201         auto_detect &
202 fi
203
204 if [ 0$DEBUG -gt 0 ]; then
205         echo -e "\nType 'exit' to continue booting...\n"
206         debug_shell debug-found
207 fi
208
209 # A target should provide its detect_hardware function.
210 # On success, return 0 with the following values set.
211 # return 1 if it wants to use auto_detect
212 [ "$AUTO" != "1" ] && detect_hardware && FOUND=1
213
214 [ -n "$INSTALL" ] && do_install
215
216 load_modules
217 mount_data
218 mount_sdcard
219 setup_tslib
220 setup_dpi
221 post_detect
222
223 if [ 0$DEBUG -gt 1 ]; then
224         echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
225         echo -e "Type 'exit' to enter Android...\n"
226
227         debug_shell debug-late
228         SETUPWIZARD=${SETUPWIZARD:-0}
229 fi
230
231 [ "$SETUPWIZARD" = "0" ] && echo "ro.setupwizard.mode=DISABLED" >> default.prop
232
233 [ -n "$DEBUG" ] && SWITCH=${SWITCH:-chroot}
234
235 # We must disable mdev before switching to Android
236 # since it conflicts with Android's init
237 echo > /proc/sys/kernel/hotplug
238
239 export ANDROID_ROOT=/system
240
241 exec ${SWITCH:-switch_root} /android /init
242
243 # avoid kernel panic
244 while :; do
245         echo
246         echo '  Android-x86 console shell. Use only in emergencies.'
247         echo
248         debug_shell fatal-err
249 done