-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit
More file actions
executable file
·273 lines (217 loc) · 8.71 KB
/
init
File metadata and controls
executable file
·273 lines (217 loc) · 8.71 KB
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
#!/bin/busybox sh
if [ "$DEBUG" = "1" ]; then
LOG=/tmp/log
set -x
else
LOG=/dev/null
test -e "$LOG" || busybox mknod $LOG c 1 3
fi
exec 2>> $LOG
# Prepare environment
if busybox test ! -e /dev/tty; then
export PATH="/sbin:/bin:/system/bin:/system/xbin:$PATH"
# Install busybox synlinks
for link in `busybox --list`; do
if ! busybox which $link > /dev/null; then
busybox ln $(busybox which busybox) /bin/$link
fi
done
# initialise /proc and /sys
mount -t proc proc /proc
mount -t sysfs sys /sys
if [ "$DEBUG" = "1" ]; then
# ensure that we can open a controlling tty
mknod /dev/tty c 5 0
# create device nodes then spawn on them
mknod /dev/tty2 c 4 2 && openvt
mknod /dev/tty3 c 4 3 && openvt
fi
# initialise /dev
echo $(which mdev) > /proc/sys/kernel/hotplug
mdev -s
sleep 1
# re-run this script with a controlling tty
exec env setsid cttyhack /bin/sh "$0" "$@"
# Environment ready, start preparing Android boot
else
source /functions
# Scan sys again to populate late usb devices
mdev -s
sleep 1
# Declare config variables
CHRDST="${RAMDISK:-/chroot}"
SRCDST="${RAMDISK:-/mnt}"
ROOT="${ROOT:-}"
SCRIPT="${SCRIPT:-initrd.sh}"
RAMDISK="${RAMDISK:-ramdisk.img}"
SYSTEM="${SYSTEM:-system.img}"
DATA="${DATA:-}"
CACHE="${CACHE:-}"
FSTAB="${FSTAB:-}"
DEBUG="${DEBUG:-0}"
AUTOMOUNT="${AUTOMOUNT:-0}"
HEADER="${HEADER:-}"
# Create a few dirs if they do not exist
for i in "$CHRDST" "$SRCDST" /dev/block; do
if [ ! -d "$i" ]; then
mkdir "$i"
fi
done
for c in `cat /proc/cmdline`; do
case $c in
iso-scan/filename=*)
eval `echo $c | cut -b1-3,18-`
;;
*)
;;
esac
done
# Locate the source/boot device
printLog "Searching for source device..."
if device="$(getDeviceFile "$ROOT" default root)"; then
if ! mount "$device" "$SRCDST" > /dev/null 2>&1; then
printLog "\tCould not mount source device '$device' at '$SRCDST'"
else
printLog "\tSource device '$device' mounted at '$SRCDST'"
fi
fi
# Locate the ramdisk device/file
printLog "Searching for ramdisk..."
if device="$(getDeviceFile "$RAMDISK" nodev ramdisk)"; then
# Allows us to break out at any time
while :; do
printLog "\tFound ramdisk '$device'"
# Mount/Extract ramdisk
if echo "$device" | grep -qe '^\/dev\/'; then
if ! mount "$device" "$CHRDST"; then
printLog "\tCould not mount the ramdisk"; break
fi
else
mount -t tmpfs tmpfs "$CHRDST"
LASTPWD="$(pwd)"
cd "$CHRDST"
if ! zcat "$device" | cpio -id > /dev/null; then
printLog "\tFailed to extract the ramdisk"; break
fi
cd "$LASTPWD"
fi
# Make sure that some specific dirs exists in the ramdisk
for i in system data cache; do
if [ ! -d "$CHRDST/$i" ]; then
mkdir "$CHRDST/$i"
fi
done
# Prepare fstab file
printLog "Preparing fstab file"
for i in $CHRDST/fstab* "$CHRDST/fstab"; do
if [ "$i" = "$CHRDST/fstab" ] || [ -f "$i" ]; then
printLog "\tFound fstab at '$i'"
if [ -n "$FSTAB" ] && [ -f "$SRCDST/$(echo "$FSTAB" | sed 's/^\///')" ]; then
printLog "\tReplace fstab with '$SRCDST/$(echo "$FSTAB" | sed 's/^\///')'"
cp -f "$SRCDST/$(echo "$FSTAB" | sed 's/^\///')" "$i"
elif [ -f "$FSTAB" ] && [ "$FSTAB" != "$i" ]; then
printLog "\tReplace fstab with '$FSTAB"
cp -f "$FSTAB" "$i"
fi
FSTAB="$i"
if [ ! -f $FSTAB ]; then
touch "$FSTAB"
fi
break
fi
done
# Mount system
printLog "Searching for system image"
if sysdev="$(getDeviceFile "$SYSTEM" default system)"; then
printLog "\tFound system image '$sysdev'"
LASTPWD="$(pwd)"
cd "$CHRDST"
if [ "$AUTOMOUNT" != "1" -a "$DEBUG" != "1" ] || mount -o ro "$sysdev" system; then
for i in cache data; do
device=
key=
if [ "$i" = "data" ] && [ -n "$DATA" ]; then
device="$(getDeviceFile "$DATA" default data)"
key="$(getDeviceFile "$HEADER" default meta)"
elif [ "$i" = "cache" ] && [ -n "$CACHE" ]; then
device="$(getDeviceFile "$CACHE" default cache)"
fi
if [ -n "$device" ]; then
printLog "\tMounting $device to /$i"
if [ "$AUTOMOUNT" = "1" ] && [ -z "$key" ]; then
if ! mount -o nosuid,nodev,noatime "$device" "$i"; then
printLog "\tFailed to mount $i"
fi
else
if [ -z "$key" ]; then
if entry="$(grep -e "\(\s\)\+/$i\(\s\)\+" "$FSTAB")"; then
if [ "$device" = "$(echo "$entry" | sed 's/^\([^ \t]\+\).*/\1/')" ]; then
continue
fi
fi
echo "$device /$i $(getDeviceType "$device") nosuid,nodev,noatime wait,check" > "$FSTAB.bak"
else
echo "$device /$i $(getDeviceType "$device") nosuid,nodev,noatime wait,check,encryptable=userdata,forcefdeorfbe=$key" > "$FSTAB.bak"
echo "ro.crypto.keyfile.userdata=$key" >> "default.prop"
fi
fi
sed "s/.*\s\+\/$i\s\+.*//" "$FSTAB" >> "$FSTAB.bak"
mv -f "$FSTAB.bak" "$FSTAB"
else
printLog "\tMoutning tmpfs to /$i"
mount -t tmpfs tmpfs $i
fi
done
while :; do
if ! mountpoint -q system; then
if entry="$(grep -e '\(\s\)\+/system\(\s\)\+' "$FSTAB")"; then
if [ "$sysdev" = "$(echo "$entry" | sed 's/^\([^ \t]\+\).*/\1/')" ]; then
break
fi
fi
echo "$sysdev /system $(getDeviceType "$sysdev") ro wait" > "$FSTAB.bak"
fi
sed 's/.*\s\+\/system\s\+.*//' "$FSTAB" >> "$FSTAB.bak"
mv -f "$FSTAB.bak" "$FSTAB"
break
done
else
printLog "\tFailed to mount system image"
fi
cd "$LASTPWD"
fi
break
done
fi
# Launch debug console
if [ "$DEBUG" = "1" ]; then
echo -e "\nSwitch between TTY's using F1, F2 and F3\nScroll using Shift+PgUp/PgDn\nEnter 'exit' to continue booting..."
if [ -d "$CHRDST/system" ]; then
ln -s "$CHRDST/system" /
ln -s /system/lib/modules /lib/
ln -s /system/lib/firmware /lib/
fi
if [ ! -x "$CHRDST/system/bin/sh" ] || ! USER="debug" "$CHRDST/system/bin/sh" -l 2>&1; then
sh 2>&1
fi
SWITCH=${SWITCH:-chroot}
fi
# Start Android
if [ -x "$CHRDST/init" ]; then
# Allow executing a custom script from src before running Android
if [ -n "$SCRIPT" ] && [ -x "$SRCDST/$(echo "$SCRIPT" | sed 's/^\///')" ]; then
SCRIPT="$SRCDST/$(echo "$SCRIPT" | sed 's/^\///')";
fi
# Let Android handle this
echo > /proc/sys/kernel/hotplug
# Run Android if the custom script allows it
if [ ! -x "$SCRIPT" ] || ( "$SCRIPT" ); then
exec ${SWITCH:-switch_root} "$CHRDST" /init
fi
fi
fi
# Stop kernel panic by starting an emergency shell
while :; do
echo -e "\nAndroid-x86 emergency console\n"
busybox sh 2>&1
done