Massive keymaps reworking. Testing needed.
[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:/bin:/system/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 echo 2048 > /sys/devices/virtual/bdi/179:0/read_ahead_kb
18 echo 7 > /proc/sys/vm/laptop_mode
19 echo 90 > /proc/sys/vm/dirty_ratio
20 echo 60000 > /proc/sys/vm/dirty_expire_centisecs
21 echo 60000 > /proc/sys/vm/dirty_writeback_centisecs
22
23 /bin/mount | grep mtd
24 notnand=$?
25
26 if [ $notnand = "0" ] ; then
27   echo "running from NAND"
28   /bin/mount -t yaffs2 /dev/block/mtdblock2 /system
29   /bin/mount -t yaffs2 /dev/block/mtdblock3 /data
30 fi
31
32 partition=mmcblk0p1
33
34 # Try unpartitioned card
35 if [ ! -d /sys/block/mmcblk0/$partition ] ; then
36         partition=mmcblk0
37 fi
38
39 if [ -f /sys/class/vogue_hw/gsmphone ] ; then
40         echo "GSM phone found"
41 fi
42
43 #mkdir -m 0777 /sdcard
44 #fsck already done in initrd
45 mount -t vfat -o fmask=0000,dmask=0000,rw,flush,relatime,utf8 /dev/block/$partition /sdcard
46 [ $? -eq 0 ] || fail "Failed to mount the SD card. Cannot continue."
47
48 if [ ! -d /sdcard/cache ] ; then
49         mkdir /sdcard/cache
50 fi
51
52 #mkdir -m 0777 /cache
53 mount /sdcard/cache /cache
54
55 CARD_PATH=`/bin/grep -o "rel_path=.*" /proc/cmdline | /bin/sed -e "s/.*rel_path=//g" -e "s/ .*//g"`
56 if [ "$CARD_PATH" = "" ];then
57         CARD_PATH="andboot"
58 fi;
59 if [ -d /sdcard/$CARD_PATH ] ; then
60         card=/sdcard/$CARD_PATH
61 else
62         card=/sdcard
63 fi
64
65
66 if [ $notnand = "1" ] ; then
67     
68     if [ -f $card/data.gz ] ; then
69             echo "Please wait... extracting Data Image"
70             if [ -f $card/data.img ] ; then
71                     rm $card/data.img
72             fi
73             gzip -df $card/data.gz
74             [ $? -eq 0 ] || fail "Failed to extract Data Image"
75             mv $card/data $card/data.img
76             echo "done"
77     fi
78     
79     if [ -d /sys/block/mmcblk0/mmcblk0p4 ] && /bin/grep -q -v no_partitions /proc/cmdline ; then
80             echo "Using partitioned system"
81             /bin/e2fsck -y /dev/block/mmcblk0p3
82             mount -t ext2 -o relatime /dev/block/mmcblk0p3 /system
83             /bin/e2fsck -y /dev/block/mmcblk0p4
84             mount -t ext2 -o relatime /dev/block/mmcblk0p4 /data
85     else
86             echo "Using loopback filesystems"
87             if [ ! -f $card/data.img ] ; then
88                     echo "Creating a new Data store"
89                     dd if=/dev/zero of=$card/data.img bs=1048576 count=256
90                     [ $? -eq 0 ] || fail "Failed to allocate the storage"
91                     mke2fs -F $card/data.img
92                     [ $? -eq 0 ] || fail "Failed to format the storage"
93             fi
94
95             losetup /dev/block/loop0 $card/data.img
96             [ $? -eq 0 ] || fail "Failed to find data.img on SD Card"
97             e2fsck -y /dev/block/loop0
98             mount -t ext2 -o relatime /dev/block/loop0 /data
99
100             losetup /dev/block/loop1 $card/system.ext2
101             [ $? -eq 0 ] || fail "Failed to reach system.ext2 on SD Card"
102             e2fsck -y /dev/block/loop1
103             mount -t ext2 -o relatime /dev/block/loop1 /system
104                     [ $? -eq 0 ] || fail "Failed to mount /system"
105     fi
106 fi
107
108 if [ -f "$card/update.zip" ] && [ ! -f "/sdcard/cache/recovery/intent" ] && [ -z "$SQUASH" ]
109 then
110         RECOVERYCARD=$(echo $card | sed -e 's:/sdcard/:SDCARD\::')
111
112         echo "STARTING OTA UPDATE SYSTEM"
113         rm -f /init.rc
114         ln -s /init.cfg/init.recovery.rc /init.rc
115         mkdir /cache/recovery
116         echo "--update_package=${RECOVERYCARD}/update.zip" >/cache/recovery/command
117         echo "--send_intent=complete" >>/cache/recovery/command
118         exec /init.android
119 fi
120
121 # If not updating, ensure old recovery.intent and update.zip files are removed so we can update again later.
122 rm -f /sdcard/cache/recovery/intent
123 rm -f $card/update.zip
124
125 if [ -d $card/AndroidApps ] ; then
126         echo Copying Applications
127         if [ ! -d /data/app ] ; then
128                 mkdir -m 0771 /data/app
129                 chown -R 1000:1000 /data/app
130         fi
131         (cd $card/AndroidApps; for i in *; do if [ ! -f "/data/app/$i" -o "$i" -nt "/data/app/$i" ]; then \
132                 cp "$i" /data/app; fi done)
133 fi
134
135 if [ ! -d $card/media ] ; then
136         echo You have no media folder, please extract the resources to your SD card android folder
137 fi
138
139 cp -ar /init.etc/* /system/etc
140
141 LCDDENSITY=`/bin/grep -o "lcd.density=.*" /proc/cmdline | /bin/sed -e "s/.*lcd.density=//g" -e "s/ .*//g"`
142
143 if [ "$LCDDENSITY" != "" ] ; then
144         DENS2=`grep lcd_density /system/etc/default.prop | sed -e 's/.*=//'`
145         if [ "$LCDDENSITY" != "$DENS2" ]; then
146                 echo Setting ro.sf.lcd_density=$LCDDENSITY
147                 if [ -z "$DENS2" ]; then
148                         echo "ro.sf.lcd_density=$LCDDENSITY" >> /system/etc/default.prop
149                 else
150                         sed -i /lcd_density/s/=.*/=$LCDDENSITY/ /system/etc/default.prop
151                 fi
152         fi
153 fi
154
155 mount /sdcard/cache /tmp
156
157 KAISER=0
158 DIAMOND=0
159 WIFI_TI=0
160 WIFI_BCM=0
161 XPERIA=0
162 WVGA=0
163 HW=`cat /proc/cpuinfo | grep Hardware`
164 case "$HW" in
165     *Kaiser*)
166         echo "KAISER detected"
167         HW=KAISER
168         KAISER=1
169         WIFI_TI=1
170         ;;
171     *Polaris*)
172         echo "POLARIS detected"
173         HW=POLARIS
174         KAISER=1
175         WIFI_TI=1
176         ;;
177     *Diamond*)
178         echo "DIAMOND detected"
179         HW=DIAMOND
180         DIAMOND=1
181         WIFI_TI=1
182         ;;
183     *Raphael*)
184         echo "RAPHAEL detected"
185         HW=RAPHAEL
186         DIAMOND=1
187         WIFI_TI=1
188         ;;
189     *blackstone*)
190         echo "BLACKSTONE detected"
191         HW=BLACKSTONE
192         DIAMOND=1
193         WIFI_TI=1
194         WVGA=1
195         ;;
196     *Topaz*)
197         echo "TOPAZ detected"
198         HW=TOPAZ
199         DIAMOND=1
200         WIFI_TI=1
201         WVGA=1
202         ;;
203     *Kovsky*)
204         echo "XPERIA detected"
205         HW=KOVSKY
206         XPERIA=1
207         WIFI_TI=1
208         WVGA=1
209         ;;
210     *Rhodium*)
211         echo "RHODIUM detected"
212         HW=RHODIUM
213         DIAMOND=1
214         WIFI_BCM=1
215         WVGA=1
216         ;;
217     *)
218         echo "VOGUE detected"
219         HW=VOGUE
220         ;;
221 esac
222
223 ### Fix su on some builds...
224 mount --bind /bin/su /system/bin/su 2> /dev/null
225 mount --bind /bin/su /system/xbin/su 2> /dev/null
226
227 RCSCRIPT=""
228 RCCONFIG=""
229
230 echo "Checking for build type..."
231 if [ -f /system/hero.build ] ; then
232         echo "Hero build detected"
233         RCSCRIPT="hero"
234         RCCONFIG="hero"
235         ln /data/app_s /system/app
236
237 elif [ -f /system/eclairhero.build ] ; then
238         echo "HERO 2.1 BUILD DETECTED -- ECLAIR"
239         RCSCRIPT="eclairhero"
240         RCCONFIG="eclairhero"
241         mount --bind /lib/eclair/hw /system/lib/hw
242
243 elif [ -f /system/eclair.build ] ; then
244         echo "Eclair build detected"
245         RCSCRIPT="eclair"
246         RCCONFIG="eclair"
247         mount --bind /lib/eclair/hw /system/lib/hw
248
249 elif [ -f /system/froyo.build ] ; then
250         echo "Froyo build detected"
251         RCSCRIPT="froyo"
252         RCCONFIG="froyo"
253         mount --bind /lib/froyo/hw /system/lib/hw
254
255 elif [ -f /system/gingerbread.build ] ; then
256         echo "Gingerbread build detected"
257         RCSCRIPT="gingerbread"
258         RCCONFIG="gingerbread"
259
260 elif [ -f /system/tattoo.build ] ; then
261         echo "Tattoo build detected"
262         RCSCRIPT="tattoo"
263         RCCONFIG="tattoo"
264
265 elif [ -f /system/donut.build ] ; then
266         echo "Donut build detected"
267         RCSCRIPT="donut"
268         RCCONFIG="donut"
269         mount --bind /lib/donut/hw /system/lib/hw
270
271 elif [ -d /system/lib/donut ] ; then
272         echo "Donut build detected"
273         RCSCRIPT="donut"
274         RCCONFIG="donut"
275
276 elif [ -f /system/xrom.build ] ; then
277         echo "xROM build detected"
278         RCSCRIPT="xrom"
279         RCCONFIG="xrom"
280
281 elif [ -f /system/rogers.build ] ; then
282         echo "Rogers build detected"
283         RCSCRIPT="rogers"
284         RCCONFIG="rogers"
285
286 elif [ -f /system/cyanogen.build ] ; then
287         echo "cyanogen experimental detected.....eating donuts"
288         RCSCRIPT="cyanogen"
289         RCCONFIG="cyanogen"
290
291 elif [ -f /system/custom.build ] ; then
292         echo "Custom init.rc detected"
293         cp /system/sysinit.rc /init.cfg/init.sysinit.rc
294         RCCONFIG="hero"
295         RCSCRIPT="sysinit"
296         
297 else
298         echo "Unknown Android build. Assuming Ion variant"
299         RCSCRIPT="ion"
300         RCCONFIG="ion"
301
302         # for the fake sensors library
303         mount /lib/hw /system/lib/hw -o loop
304         chmod 666 /dev/input/event0
305
306         if [ $DIAMOND -eq 1 ] ; then
307                 RCCONFIG="ion.diamond"
308         fi
309 fi
310
311 if [ $KAISER -eq 1 ] ; then
312         RCSCRIPT="$RCSCRIPT.kaiser"
313 fi
314
315 if [ "$LCDDENSITY" != "" ] ; then
316         DENS2=`grep lcd_density /system/etc/default.prop | sed -e 's/.*=//'`
317         if [ "$LCDDENSITY" != "$DENS2" ]; then
318                 echo Setting ro.sf.lcd_density=$LCDDENSITY
319                 if [ -z "$DENS2" ]; then
320                         echo "ro.sf.lcd_density=$LCDDENSITY" >> /system/etc/default.prop
321                 else
322                         sed -i /lcd_density/s/=.*/=$LCDDENSITY/ /system/etc/default.prop
323                 fi
324         fi
325 fi
326 if [ $XPERIA -eq 1 ] ; then
327         ROT=`grep hwrotation /system/etc/default.prop | sed -e 's/.*=//'`
328         if [ "$ROT" != 180 ]; then
329                 echo Setting ro.sf.hwrotation=180
330                 if [ -z "$ROT" ]; then
331                         echo "ro.sf.hwrotation=180" >> /system/etc/default.prop
332                 else
333                         sed -i /hwrotation/s/=.*/=180/ /system/etc/default.prop
334                 fi
335         fi
336 fi
337
338 echo "using $card/conf/$RCCONFIG.user.conf"
339
340 INITRC=`ls -l /init.rc | grep "$RCSCRIPT"`
341 if [ -z "$INITRC" ]; then
342         echo "using /init.$RCSCRIPT.rc as init.rc"
343         rm -f /init.rc
344         ln -s "/init.cfg/init.$RCSCRIPT.rc" /init.rc
345 fi
346
347 #Assume this rootfs.img will be used only on "good" devices
348 #Meaning raph/diam/blac/
349
350 # echo /dev/block/mmcblk0p2 > /sys/devices/platform/usb_mass_storage/lun0/file
351 ifconfig usb0 192.168.20.1 up
352 busybox telnetd -b 192.168.20.1 -l /bin/sh
353 #chmod 4755 /bin/su
354 #mkdir -m 0777 /smodem
355 mknod /dev/urandom c 1 9
356 if [ -d /data/dropbear/ ] ; then
357         echo "Starting SSH"
358         mknod /dev/random c 1 8
359         /bin/dropbear -r /data/dropbear/dropbear_rsa_host_key -s
360 fi
361
362 if [ "$WIFI_TI" = "1" ] || [ "$WIFI_BCM" = "1" ];then
363         if [ -e "$card/modules-$(uname -r).tar.gz" ] && ! `strings /lib/modules/mac80211.ko 2>/dev/null | grep -q "vermagic=$(uname -r)"`; then
364                 echo "Installing $card/modules-$(uname -r).tar.gz"
365                 tar xzf $card/modules-$(uname -r).tar.gz -C /lib/modules
366                 ln -s /lib/modules /lib/modules/`uname -r`
367         else
368                 echo "Modules already unpacked for this kernel version -- skipping installation of $card/modules-$(uname -r).tar.gz"
369         fi
370
371         if [ -f /system/etc/wifi/wlan.ko ]; then
372         cp -R /system/etc/wifi/* /lib/modules/
373         rm /system/etc/wifi/wlan.ko
374         depmod
375         fi
376
377         if [ "`grep -c ^wifi /system/etc/default.prop`" != "2" ]; then
378                 echo "wifi.interface = wlan0" >> /system/etc/default.prop
379                 echo "wifi.supplicant_scan_interval = 45" >> /system/etc/default.prop
380         fi
381 fi
382
383 if [ -f /system/eclair.build ]
384 then
385         mount --bind /lib/eclair/libhardware_legacy.so /system/lib/libhardware_legacy.so
386 fi
387
388 if [ "$WIFI_BCM" = "1" ] ;then
389         # fyi: firmware is loaded from /data/etc/wifi/bcm432x/bcm4325-rtecdc.bin, nvram from /data/wifi-nvram.txt
390
391         # extract nvram (todo: clean this up/integrate with TI calibration)
392         if [ ! -e /data/wifi-nvram.txt ]; then
393                 insmod /lib/modules/mtdchar.ko
394                 mknod /dev/mtd0ro c 90 1
395                 for SKIP in 5744 4736;do
396                         mkdir /lib/modules/$(uname -r)
397
398                         dd if=/dev/mtd0ro of=/tmp/tempcal bs=8192 count=1 skip=$SKIP
399                         rmdir /lib/modules/$(uname -r)
400                         dd if=/tmp/tempcal of=/data/wifi-nvram.txt bs=1 count=466
401                         rm /tmp/tempcal
402                 
403                         if grep nocrc= /data/wifi-nvram.txt 2>&1 > /dev/null;then 
404                                 break
405                         fi;
406                 done
407                 rm /dev/mtd0ro
408                 rmmod mtdchar
409                 if [ "$SKIP" = "5744" ];then
410                         echo "CDMA Rhodium detected"
411                 fi;
412                 if [ "$SKIP" = "4736" ];then
413                         echo "GSM Rhodium detected"
414                 fi;
415
416         fi
417 fi
418
419 for i in local.prop eri.xml serialno; do
420         if [ -f "$card/$i" ]; then
421                 if [ ! -f /data/$i -o "$card/$i" -nt /data/$i ]; then
422                         cp "$card/$i" /data/$i
423                 fi
424         fi
425 done
426
427 if [ -f "$card/conf/$RCCONFIG.user.conf" ]; then
428         /bin/userinit.sh -c "$card/conf/$RCCONFIG.user.conf"
429 else
430         echo "No user config files ($RCCONFIG) found on sdcard"
431 fi
432
433 mount -tdebugfs none /dbgfs
434 #Activate baclight control
435 echo 3 > /sys/class/htc_hw/test
436 #Smooth scroll
437 echo 40 >/sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold
438 #Activate full charging
439 echo 2 > /dbgfs/htc_battery/charger_state
440
441 echo 1024 > /dbgfs/micropklt_dbg/sleep_leds
442
443 KBD=`sed 's/.*physkeyboard=\([0-9a-z_]*\).*/\1/' /proc/cmdline`
444 cp /init.etc/keymaps/default/*.kl /system/usr/keylayout/
445 cp /init.etc/keymaps/default/*.kcm* /system/usr/keychars/
446 if [ -d "/init.etc/keymaps/$KBD" ]
447 then
448         cp /init.etc/keymaps/"$KBD"/*.kl /system/usr/keylayout/
449         cp /init.etc/keymaps/"$KBD"/*.kcm* /system/usr/keychars/
450 fi
451
452 mount -o remount,ro /system
453
454 for i in /sys/class/input/input* ; do
455         if [ "`cat $i/name`" = "tssc-manager" ] ; then
456                 touchscreendir=$i
457                 echo "Touchscreen device directory is $i"
458         fi
459 done
460
461 if [ -f $card/ts-calibration ] ; then
462         echo "Using Saved Touchscreen Calibration"
463         echo 128,128,1903,128,128,1907,1903,1907,1024,1024 > $touchscreendir/calibration_screen
464         cat $card/ts-calibration > $touchscreendir/calibration_points
465 else
466         mkdir /dev/graphics
467         mknod /dev/graphics/fb0 c 29 0
468         clear
469         echo; echo; echo; echo; echo; echo; echo; echo "Calibrating Touchscreen:"
470         echo "Click the Five Targets in order -- Top Left, Top Right, Middle, Bottom Left, Bottom Right"
471         echo "(Tap lightly. The screen is quite sensitive.)"
472         tssc-calibrate
473         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
474         if [ $? -eq 0 ] ; then
475                 echo "Touchscreen Calibration Failed"
476         else
477                 echo "Touchscreen Calibration Successful; Saving..."
478                 cat $touchscreendir/calibration_points > $card/ts-calibration
479         fi
480 fi
481
482 mkdir /dev/msm_camera
483 mknod /dev/msm_camera/control0 c 250 0
484 mknod /dev/msm_camera/config0 c 250 1
485 mknod /dev/msm_camera/frame0 c 250 2
486
487 # Generate a "serial number" for the Android system to identify the device.
488 # Put it in /data so it survives reboots. Won't survive a factory reset, though.
489 [ -f "/data/serialno" ] || echo -e `cat /dev/urandom | tr -dc 'A-Z0-9' | fold -w 12 | head -n 1` >/data/serialno
490
491 SERIALNO=$(cat /data/serialno)
492 cp /proc/cmdline /tmp/cmdline
493 sed -i -e "s:$: androidboot.serialno=${SERIALNO}:" /tmp/cmdline
494 mount --bind /tmp/cmdline /proc/cmdline
495
496 # Transition sequence for FRX06->FRX07 (tiwlan0 renamed)
497 if [ -f "/data/misc/wifi/wpa_supplicant.conf" ]
498 then
499         sed -i -e 's:=tiwlan0:=wlan0:' /data/misc/wifi/wpa_supplicant.conf
500         chown 1000:1010 /data/misc/wifi/wpa_supplicant.conf
501 fi
502
503 # Bind-mount /sdcard/.android_secure to /mnt/secure/asec for native Apps2SD
504 mount --bind /sdcard/.android_secure /mnt/secure/asec
505
506 exec /init.android