Set pppd as setuid
[xdandroid:rootfs.git] / init
1 #!/bin/sh
2 fail() {
3         /bin/echo "Failed"
4                 /bin/echo "$1"
5                 exec /bin/sh
6 }
7
8 /bin/mount -o remount,rw / /
9
10 PATH=/sbin:/system/sbin:/system/bin:/bin:/system/xbin:/system/xbin/bb:/data/local/bin
11
12 /bin/echo "Rootfs loaded."
13
14 /bin/mount -t proc proc /proc
15 /bin/mount -t sysfs sys /sys
16
17 /bin/mount | grep mtd
18 notnand=$?
19
20 if [ $notnand = "0" ] ; then
21   echo "running from NAND"
22   /bin/mount -t yaffs2 /dev/block/mtdblock2 /system
23   /bin/mount -t yaffs2 /dev/block/mtdblock3 /data
24 fi
25
26
27 partition=mmcblk0p1
28
29 # Try unpartitioned card
30 if [ ! -d /sys/block/mmcblk0/$partition ] ; then
31         partition=mmcblk0
32 fi
33
34 if [ -f /sys/class/vogue_hw/gsmphone ] ; then
35         echo "GSM phone found"
36 fi
37
38 #mkdir -m 0777 /sdcard
39 #fsck already done in initrd
40 mount -t vfat -o fmask=0000,dmask=0000,rw,flush,noatime,nodiratime /dev/block/$partition /sdcard
41 [ $? -eq 0 ] || fail "Failed to mount the SD card. Cannot continue."
42
43 if [ ! -d /sdcard/cache ] ; then
44         mkdir /sdcard/cache
45 fi
46
47 #mkdir -m 0777 /cache
48 mount /sdcard/cache /cache
49
50 CARD_PATH=`/bin/grep -o "rel_path=.*" /proc/cmdline | /bin/sed -e "s/.*rel_path=//g" -e "s/ .*//g"`
51 if [ "$CARD_PATH" = "" ];then
52         CARD_PATH="android-dist"
53 fi;
54 if [ -d /sdcard/$CARD_PATH ] ; then
55         card=/sdcard/$CARD_PATH
56 else
57         card=/sdcard
58 fi
59
60
61 if [ $notnand = "1" ] ; then
62     
63     if [ -f $card/data.gz ] ; then
64             echo "Please wait... extracting Data Image"
65             if [ -f $card/data.img ] ; then
66                     rm $card/data.img
67             fi
68             gzip -df $card/data.gz
69             [ $? -eq 0 ] || fail "Failed to extract Data Image"
70             mv $card/data $card/data.img
71             echo "done"
72     fi
73     
74     PARTITIONED=0
75     echo "Checking for partition..."
76     if [ -d /sys/block/mmcblk0/mmcblk0p2 ] ; then
77             PARTITIONED=1
78             if [ -d /sys/block/mmcblk0/mmcblk0p3 ] ; then
79                     PARTITIONED=2
80             fi
81     fi
82     
83     if [ -f $card/backupsystem/installedsystem.sqsh -o -f $card/backupsystem/installedsystem.img -o -f $card/backupsystem.ext2 ]; then
84             echo "Partitioned Build Detected"
85     else
86             PARTITIONED=0
87     fi
88     
89     if [ $PARTITIONED -lt 2 ] ; then
90              
91             if [ ! -f $card/data.img ] ; then
92                     echo "Creating a new Data store"
93                     dd if=/dev/zero of=$card/data.img bs=1048576 count=256
94                     [ $? -eq 0 ] || fail "Failed to allocate the storage"
95                     mke2fs -F $card/data.img
96                     [ $? -eq 0 ] || fail "Failed to format the storage"
97             fi
98     fi
99     
100     if [ $PARTITIONED -lt 2 ] ; then
101             losetup /dev/block/loop0 $card/data.img
102             [ $? -eq 0 ] || fail "Failed to find data.img on SD Card"
103             e2fsck -y /dev/block/loop0
104             mount -t ext2 -o noatime,nodiratime,sync /dev/block/loop0 /data
105     else
106             e2fsck -y /dev/block/mmcblk0p3
107             mount -t ext2 -o noatime,nodiratime /dev/block/mmcblk0p3 /data
108     fi
109     [ $? -eq 0 ] || fail "Failed to mount /data"
110     
111     if [ $PARTITIONED -eq 0 ] ; then
112             if      [ -f $card/system.ext2 ] ; then
113                     echo "Using uncompressed system"
114                     losetup /dev/block/loop1 $card/system.ext2
115                     [ $? -eq 0 ] || fail "Failed to reach system.ext2 on SD Card"
116                     e2fsck -y /dev/block/loop1
117                     mount -t ext2 -o noatime,nodiratime /dev/block/loop1 /system
118                     [ $? -eq 0 ] || fail "Failed to mount /system"
119             elif    
120                     [ -f $card/system.sqsh ] ; then
121                     echo "Using SquashFS system"
122                     losetup /dev/block/loop1 $card/system.sqsh
123                     [ $? -eq 0 ] || fail "Failed to find system.sqsh on SD Card"
124                     mount -t squashfs -o ro,noatime,nodiratime /dev/block/loop1 /system
125                     [ $? -eq 0 ] || fail "Failed to mount /system"
126             else
127                     losetup /dev/block/loop1 $card/system.img
128                     [ $? -eq 0 ] || fail "Failed to find system.img on SD Card"
129                     mount -t cramfs -o ro,noatime,nodiratime /dev/block/loop1 /system
130                     [ $? -eq 0 ] || fail "Failed to mount /system"
131             fi
132     else
133             echo "Using partitioned system"
134             e2fsck -y /dev/block/mmcblk0p2
135             mount -t ext2 -o ro,noatime,nodiratime /dev/block/mmcblk0p2 /system
136             [ $? -eq 0 ] || fail "Failed to mount /system"
137     fi
138 fi
139
140 if [ -d $card/AndroidApps ] ; then
141         echo Copying Applications
142         if [ ! -d /data/app ] ; then
143                 mkdir -m 0771 /data/app
144         fi
145         /bin/cp $card/AndroidApps/* /data/app
146         chown -R 1000:1000 /data/app
147 fi
148
149 if [ ! -d $card/media ] ; then
150         echo You have no media folder, please extract the resources to your SD card android folder
151 fi
152
153 SQLITE3="/bin/sqlite3"
154 DB="/data/data/com.android.providers.telephony/databases/telephony.db"
155 if [ -f $DB ] ; then
156         if [ "`$SQLITE3 $DB 'SELECT numeric FROM carriers;' | grep 310995`" != "310995" ] ; then
157                 echo Creating android apn
158                 "$SQLITE3" "$DB" "INSERT INTO carriers VALUES(NULL, 'Android' , '310995' , '310' , '995' , 'internet' , '*' , '*' , '*' , NULL, NULL, NULL, NULL, 'null' , -1, 'default' , 1);"
159
160         fi
161 else
162         echo No telephony database. You must have a new data file. I will check for the android apn on the next reboot
163 fi
164
165 /bin/rm -rf /data/etc
166 /bin/mkdir -m 0755 /data/etc
167 mount /data/etc /etc
168 cp -a /system/etc/* /etc
169 cp -ar /init.etc/* /etc/
170
171 LCDDENSITY=`/bin/grep -o "lcd.density=.*" /proc/cmdline | /bin/sed -e "s/.*lcd.density=//g" -e "s/ .*//g"`
172
173 if [ "$LCDDENSITY" != "" ] ; then
174         echo "ro.sf.lcd_density=$LCDDENSITY" >> /etc/default.prop
175         echo Setting ro.sf.lcd_density=$LCDDENSITY
176 fi
177
178 if [ ! -d /data/shared_prefs ] ; then
179         mkdir -m 0770 /data/shared_prefs
180 fi
181 chmod 0770 /data/shared_prefs
182 chown 1000:1000 /data/shared_prefs
183 mount /data/shared_prefs /shared_prefs
184
185 mount /sdcard/cache /tmp
186
187 cp /system/build.prop /tmp/build.prop
188 KAISER=0
189 DIAMOND=0
190 WIFI_TI=0
191 WIFI_BCM=0
192 ### Is this a Kaiser?
193 if [ "`cat /proc/cpuinfo|grep -o Kaiser`" = "Kaiser" ]; then
194         echo "KAISER detected"
195         KAISER=1
196         WIFI_TI=1
197 elif [ "`cat /proc/cpuinfo|grep -o Polaris`" = "Polaris" ]; then
198         echo "POLARIS detected"
199         KAISER=1
200         WIFI_TI=1
201 elif [ "`cat /proc/cpuinfo|grep -o Diamond`" = "Diamond" ]; then
202         echo "DIAMOND detected"
203         DIAMOND=1
204         WIFI_TI=1
205 elif [ "`cat /proc/cpuinfo|grep -o Raphael`" = "Raphael" ]; then
206         echo "RAPHAEL detected"
207         DIAMOND=1
208         WIFI_TI=1
209 elif [ "`cat /proc/cpuinfo|grep -o blackstone`" = "blackstone" ]; then
210         echo "BLACKSTONE detected"
211         DIAMOND=1
212         WIFI_TI=1
213 elif [ "`cat /proc/cpuinfo|grep -o Topaz`" = "Topaz" ]; then
214         echo "TOPAZ detected"
215         DIAMOND=1
216         WIFI_TI=1
217 elif [ "`cat /proc/cpuinfo|grep -o Rhodium`" = "Rhodium" ]; then
218         echo "RHODIUM detected"
219         DIAMOND=1
220         WIFI_BCM=1
221 else
222         echo "VOGUE detected"
223         sed -i s/^wifi/#wifi/ /tmp/build.prop
224 fi
225
226 mount --bind /tmp/build.prop /system/build.prop
227
228 ### Fix su on some builds...
229 mount --bind /bin/su /system/bin/su 2> /dev/null
230 mount --bind /bin/su /system/xbin/su 2> /dev/null
231
232 RCSCRIPT=""
233 RCCONFIG=""
234
235 echo "Checking for build type..."
236 if [ -f /system/hero.build ] ; then
237         echo "Hero build detected"
238         RCSCRIPT="hero"
239         RCCONFIG="hero"
240         ln /data/app_s /system/app
241
242 elif [ -f /system/eclairhero.build ] ; then
243         echo "HERO 2.1 BUILD DETECTED -- ECLAIR"
244         RCSCRIPT="eclairhero"
245         RCCONFIG="eclairhero"
246
247 elif [ -f /system/eclair.build ] ; then
248         echo "Eclair build detected"
249         RCSCRIPT="eclair"
250         RCCONFIG="eclair"
251
252 elif [ -f /system/tattoo.build ] ; then
253         echo "Tattoo build detected"
254         RCSCRIPT="tattoo"
255         RCCONFIG="tattoo"
256
257 elif [ -f /system/donut.build ] ; then
258         echo "Donut build detected"
259         RCSCRIPT="donut"
260         RCCONFIG="donut"
261
262 elif [ -d /system/lib/donut ] ; then
263         echo "Donut build detected"
264         RCSCRIPT="donut"
265         RCCONFIG="donut"
266
267 elif [ -f /system/xrom.build ] ; then
268         echo "xROM build detected"
269         RCSCRIPT="xrom"
270         RCCONFIG="xrom"
271
272 elif [ -f /system/rogers.build ] ; then
273         echo "Rogers build detected"
274         RCSCRIPT="rogers"
275         RCCONFIG="rogers"
276
277 elif [ -f /system/cyanogen.build ] ; then
278         echo "cyanogen experimental detected.....eating donuts"
279         RCSCRIPT="cyanogen"
280         RCCONFIG="cyanogen"
281
282 elif [ -f /system/custom.build ] ; then
283         echo "Custom init.rc detected"
284         cp /system/sysinit.rc /build.cfg/init.sysinit.rc
285         RCCONFIG="hero"
286         RCSCRIPT="sysinit"
287         
288 else
289         echo "Unknown Android build. Assuming Ion variant"
290         RCSCRIPT="ion"
291         RCCONFIG="ion"
292
293         # for the fake sensors library
294         mount /lib/hw /system/lib/hw -o loop
295         chmod 666 /dev/input/event0
296
297         if [ $DIAMOND -eq 1 ] ; then
298                 RCCONFIG="ion.diamond"
299         fi
300 fi
301
302 if [ $KAISER -eq 1 ] ; then
303         RCSCRIPT="$RCSCRIPT.kaiser"
304 fi
305
306 echo "using /init.$RCSCRIPT.rc as init.rc"
307 echo "using $card/conf/$RCCONFIG.user.conf"
308
309 cp "/init.cfg/init.$RCSCRIPT.rc" /etc/init.rc
310
311 #Assume this rootfs.img will be used only on "good" devices
312 #Meaning raph/diam/blac/
313
314 # echo /dev/block/mmcblk0p2 > /sys/devices/platform/usb_mass_storage/lun0/file
315 ifconfig usb0 192.168.20.1 up
316 busybox telnetd -b 192.168.20.1 -l /bin/sh
317 #chmod 4755 /bin/su
318 #mkdir -m 0777 /smodem
319 if [ -d /data/dropbear/ ] ; then
320         echo "Starting SSH"
321         mknod /dev/random c 1 8
322         mknod /dev/urandom c 1 9
323         /bin/dropbear -r /data/dropbear/dropbear_rsa_host_key -s
324 fi
325
326 if [ "$WIFI_TI" = "1" ] || [ "$WIFI_BCM" = "1" ];then
327         if [ -e "$card/modules-$(uname -r).tar.gz" ] ;then
328                 echo "Installing $card/modules-$(uname -r).tar.gz"
329                 if [ ! -d "/data/modules" ] ; then
330                         mkdir /data/modules
331                 fi
332                 mount --bind /data/modules /lib/modules
333                 tar xzf $card/modules-$(uname -r).tar.gz -C /lib/modules
334                 cd /lib/modules
335                 cp wlan.ko /etc/wifi
336                 cp bcm4329.ko /etc/wifi
337         fi
338         mount --bind /etc/wifi /system/lib/modules
339
340         sed -i s/^#wifi/wifi/ /tmp/build.prop
341 fi
342
343 if [ "$WIFI_TI" = "1" ];then
344         if [ "`grep -c ^wifi /tmp/build.prop`" != "2" ]; then
345                 echo "wifi.interface = tiwlan0" >> /tmp/build.prop
346                 echo "wifi.supplicant_scan_interval = 45" >> /tmp/build.prop
347         fi
348 fi
349
350 if [ "$WIFI_BCM" = "1" ];then
351         if [ "`grep -c ^wifi /tmp/build.prop`" != "2" ]; then
352                 echo "wifi.interface = eth0" >> /tmp/build.prop
353                 echo "wifi.supplicant_scan_interval = 45" >> /tmp/build.prop
354         fi
355
356         # fyi: firmware is loaded from /etc/wifi/bcm432x/bcm4325-rtecdc.bin, nvram from /data/wifi-nvram.txt
357
358         mount --bind /etc/wifi/bcm432x/dhcpcd.conf /system/etc/dhcpcd/dhcpcd.conf
359         mount --bind /etc/wifi/bcm432x/wpa_supplicant.conf /system/etc/wifi/wpa_supplicant.conf
360         mount --bind /etc/wifi/bcm432x/bcm4325-libhardware_legacy.so /system/lib/libhardware_legacy.so
361
362         # init.rc: fix wpa_supplicant service
363         sed -i s/-itiwlan0/-ieth0/ /etc/init.rc
364         sed -i s/-Dtiwlan0/-Dwext/ /etc/init.rc
365
366         # init.rc: fix dhcpcd service, wifi.interface, and wpa_supplicant service socket
367         sed -i s/tiwlan0/eth0/ /etc/init.rc
368
369         # init.rc:
370         sed -i s/user\ wifi/#user\ wifi/ /etc/init.rc
371         sed -i s/group\ wifi/#group\ wifi/ /etc/init.rc
372
373         # extract nvram (todo: clean this up/integrate with TI calibration)
374         if [ ! -e /data/wifi-nvram.txt ]; then
375                 for SKIP in 5744 4736;do
376                         insmod /lib/modules/mtdchar.ko
377                         mknod /dev/mtd0ro c 90 1
378                         mkdir /lib/modules/$(uname -r)
379
380                         dd if=/dev/mtd0ro of=/tmp/tempcal bs=8192 count=1 skip=$SKIP
381                         rm /dev/mtd0ro
382                         rmmod mtdchar
383                         rmdir /lib/modules/$(uname -r)
384                         dd if=/tmp/tempcal of=/data/wifi-nvram.txt bs=1 count=466
385                         rm /tmp/tempcal
386                 
387                         if dd if=/data/wifi-nvram.txt bs=1 count=1 |hexdump |grep bd > /dev/null;then 
388                                 break
389                         fi;
390                 done
391                 if [ "$SKIP" = "5744" ];then
392                         echo "CDMA Rhodium detected"
393                 fi;
394                 if [ "$SKIP" = "4736" ];then
395                         echo "GSM Rhodium detected"
396                 fi;
397
398         fi
399 fi
400
401
402 if [ -f "$card/conf/$RCCONFIG.user.conf" ]; then
403         /bin/userinit.sh -c "$card/conf/$RCCONFIG.user.conf"
404 else
405         echo "No user config files ($RCCONFIG) found on sdcard"
406 fi
407
408 mount -tdebugfs none /dbgfs
409 #Activate baclight control
410 echo 3 > /sys/class/htc_hw/test
411 #Smooth scroll
412 echo 40 >/sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold
413 #Activate full charging
414 echo 2 > /dbgfs/htc_battery/charger_state
415
416 echo 1024 > /dbgfs/micropklt_dbg/sleep_leds
417
418 if /bin/grep -c 'physkeyboard=fuze' /proc/cmdline >/dev/null ; then
419         echo "USING RAPH110|FUZE LAYOUT"
420         cp -f /init.etc/keymaps/fuze_microp-keypad.kcm.bin /etc/keymaps/microp-keypad.kcm.bin
421         cp -f /init.etc/keymaps/fuze_microp-keypad.kl /etc/keymaps/microp-keypad.kl
422         cp -f /init.etc/keymaps/fuze_raph_navi_pad.kl /etc/keymaps/raph_navi_pad.kl
423 elif /bin/grep -c 'physkeyboard=raph' /proc/cmdline >/dev/null ; then
424         echo "USING NEW RAPH LAYOUT"
425         cp -f /init.etc/keymaps/raphfix_microp-keypad.kcm.bin /etc/keymaps/microp-keypad.kcm.bin
426 elif /bin/grep -c 'physkeyboard=tilt2' /proc/cmdline >/dev/null ; then
427         echo "USING EXPERIMENTAL TILT2 LAYOUT"
428         cp -f /init.etc/keymaps/tilt2_microp-keypad.kl /etc/keymaps/microp-keypad.kl
429         cp -f /init.etc/keymaps/tilt2_microp-keypad.kcm.bin /etc/keymaps/microp-keypad.kcm.bin
430         cp -f /init.etc/keymaps/tilt2_navi_pad.kl /etc/keymaps/raph_navi_pad.kl
431 elif /bin/grep -c 'physkeyboard=rhod210' /proc/cmdline >/dev/null ; then
432         echo "USING EXPERIMENTAL RHOD210 LAYOUT"
433         cp -f /init.etc/keymaps/rhod210_microp-keypad.kl /etc/keymaps/microp-keypad.kl
434         cp -f /init.etc/keymaps/rhod210_microp-keypad.kcm.bin /etc/keymaps/microp-keypad.kcm.bin
435         cp -f /init.etc/keymaps/rhod210_navi_pad.kl /etc/keymaps/raph_navi_pad.kl
436 else
437         echo "USING OLD KEYMAP"
438         cp -f /init.etc/keymaps/oldqwerty_microp-keypad.kcm.bin /etc/keymaps/microp-keypad.kcm.bin
439         cp -f /init.etc/keymaps/oldqwerty_microp-keypad.kl /etc/keymaps/microp-keypad.kl
440         cp -f /init.etc/keymaps/oldqwerty_raph_navi_pad.kl /etc/keymaps/raph_navi_pad.kl
441 fi
442
443 for i in /sys/class/input/input* ; do
444         if [ "`cat $i/name`" = "tssc-manager" ] ; then
445                 touchscreendir=$i
446                 echo "Touchscreen device directory is $i"
447         fi
448 done
449
450 if [ -f $card/ts-calibration ] ; then
451         echo "Using Saved Touchscreen Calibration"
452         echo 128,128,1903,128,128,1907,1903,1907,1024,1024 > $touchscreendir/calibration_screen
453         cat $card/ts-calibration > $touchscreendir/calibration_points
454 else
455         mkdir /dev/graphics
456         mknod /dev/graphics/fb0 c 29 0
457         clear
458         echo; echo; echo; echo; echo; echo; echo; echo "Calibrating Touchscreen:"
459         echo "Click the Five Targets in order -- Top Left, Top Right, Middle, Bottom Left, Bottom Right"
460         echo "(Tap lightly. The screen is quite sensitive.)"
461         tssc-calibrate
462         echo 0,0,0,0,0,0,0,0,0,0 | cmp -s $touchscreendir/calibration_points # determine if calibration is still null -- means failed calibration
463         if [ $? -eq 0 ] ; then
464                 echo "Touchscreen Calibration Failed"
465         else
466                 echo "Touchscreen Calibration Successful; Saving..."
467                 cat $touchscreendir/calibration_points > $card/ts-calibration
468         fi
469 fi
470
471 mkdir /dev/msm_camera
472 mknod /dev/msm_camera/control0 c 250 0
473 mknod /dev/msm_camera/config0 c 250 1
474 mknod /dev/msm_camera/frame0 c 250 2
475 chmod a+s /bin/pppd
476
477 exec /init.android