on init

# Backward compatibility
    symlink /dev /dev/netmux
# prepare generic keypad name
    mount yaffs2 mtd@system /system rw remount
    exec /system/bin/init_prep_keypad.sh
    mount yaffs2 mtd@system /system ro remount

    mount rootfs rootfs / rw remount

    mkdir /cdrom 0770 system system
    mount yaffs2 mtd@cdrom /cdrom
	
    symlink /dev/ttyGS0 /dev/acm0

# Create mountpoints and mount tmpfs
    mkdir /pds 0775 system system
    mkdir /tmp 0777 system system
    mount tmpfs tmpfs /tmp size=2m

# Configure kernel options
    # Enable automatic power control for EHCI USB IPC
    write /sys/devices/platform/ehci-omap.0/usb1/power/level auto
    write /sys/devices/platform/ehci-omap.0/usb1/power/autosuspend 0
    write /sys/devices/platform/ehci-omap.0/usb1/1-3/power/level auto
    write /sys/devices/platform/ehci-omap.0/usb1/1-3/power/autosuspend 0
    # Reset system time
    write /sys/class/rtc/rtc0/unsecured_rtc on

# Power Management
    write /sys/power/sleep_while_idle 1
    write /sys/power/enable_off_mode 1


# Mount mtd partitions
    # mount /pds as r/w
    # We chown/chmod /pds again so because mount is run as root + defaults
    symlink /pds /config
    mount yaffs2 mtd@pds /pds nosuid nodev
    chown system system /pds
    chmod 0775 /pds
    mkdir /pds/comm_drv
    chown radio radio /pds/comm_drv
    chmod 0770 /pds/comm_drv
    mkdir /pds/public
    chown system shell /pds/public
    chmod 0775 /pds/public

# Create basic filesystem structure
    # create moto specific dirs for mystuff
    mkdir /data/download 0775 system system
    mkdir /data/download/mystuff 0775 system system

on early-boot

# Adjust the out of memory thresholds to account for the larget
# space on sholes.  These numbers are in pages (4k).  Must be kept
# in sync with init.rc.  This must be done before the regular init.rc
# since these properies can't change once set.
    setprop ro.FOREGROUND_APP_MEM 2048
    setprop ro.VISIBLE_APP_MEM 3072
    setprop ro.SECONDARY_SERVER_MEM 6144
    setprop ro.BACKUP_APP_MEM 6144
    setprop ro.HOME_APP_MEM 6144
    setprop ro.HIDDEN_APP_MEM 7168
    setprop ro.CONTENT_PROVIDER_MEM 8192
    setprop ro.EMPTY_APP_MEM 8192
    
    chmod 0666 /dev/DspBridge
    class_start media_early_init

on boot

# Overwrite what init.rc selected for the memory killer params to match above.
    write /sys/module/lowmemorykiller/parameters/minfree 2048,3072,6144,7168,8192,8192
    
    chown compass compass /dev/lis331dlh
    chmod 660 /dev/lis331dlh
    chown compass compass /dev/akm8973
    chmod 660 /dev/akm8973
    chown compass compass /dev/sfh7743
    chmod 660 /dev/sfh7743
    chown compass compass /dev/akm8973_dev
    chmod 660 /dev/akm8973_dev
    chown compass compass /dev/akm8973_aot
    chmod 660 /dev/akm8973_aot

    chown radio radio /dev/ttyUSB0
    chmod 640 /dev/ttyUSB0
    chown radio radio /dev/ttyS0
    chmod 640 /dev/ttyS0
    chown radio radio /dev/ts0710mux0
    chmod 640 /dev/ts0710mux0
    chown radio radio /dev/ts0710mux1
    chmod 640 /dev/ts0710mux1
    chown radio radio /dev/ts0710mux2
    chmod 640 /dev/ts0710mux2
    chown radio radio /dev/ts0710mux3
    chmod 640 /dev/ts0710mux3
    chown radio radio /dev/ts0710mux4
    chmod 640 /dev/ts0710mux4
    chown radio radio /dev/ts0710mux5
    chmod 640 /dev/ts0710mux5
    chown radio radio /dev/ts0710mux6
    chmod 640 /dev/ts0710mux6
    chown radio radio /dev/ts0710mux7
    chmod 640 /dev/ts0710mux7
    chown radio radio /dev/ts0710mux8
    chmod 640 /dev/ts0710mux8
    chown radio radio /dev/ts0710mux9
    chmod 640 /dev/ts0710mux9

    # create moto specific dirs for wifi
    mkdir /data/system 0771 system system
    # wifi dirs
    mkdir /data/misc/wifi 0770 wifi wifi
    mkdir /data/misc/wifi/sockets 0770 wifi wifi
    mkdir /data/misc/dhcp 0770 dhcp dhcp
    chown dhcp dhcp /data/misc/dhcp
    mkdir /pds/wifi 0771 wifi wifi
    mkdir /data/comm_drv 0770 radio radio
    mkdir /data/gki 0770 radio radio
    mkdir /tmp/commdrv 0770 radio radio


    # bluetooth power up/down interface
    chown bluetooth bluetooth /sys/class/rfkill/rfkill0/type
    chown bluetooth bluetooth /sys/class/rfkill/rfkill0/state
    chmod 0660                /sys/class/rfkill/rfkill0/state

    # bluetooth uart
    chown bluetooth bluetooth /dev/ttyS1
    chmod 0660 /dev/ttyS1

    # following two dirs are for storing BP Flex and PDS files. NVM Daemon is using these folders
    mkdir /data/bp_nvm 0770 radio radio
    mkdir /pds/bp_nvm 0770 radio radio
    
    # create logger folder for bplogd
    mkdir /data/logger 0770 radio log
    chown radio log /data/logger
    chmod 0770 /data/logger

    # create the lost+found directories, so as to enforce our permissions
    mkdir /pds/lost+found 0770

    # double check the perms, in case lost+found already exists, and set owner
    chown root root /pds/lost+found
    chmod 0770 /pds/lost+found

    #Create location directory
    mkdir /data/location 0771 radio radio

    #Create opprof directory and backup pds directory
    mkdir /data/opprof 0770 radio shell 
    mkdir /pds/opprof 0770 radio shell

    # Create anr directory and change permission to 777 to save trace.txt
    mkdir /data/anr 
    chmod 0777 /data/anr

    # create the necessary dirs for tpapi 
    mkdir /pds/security 0770 system system
    mkdir /data/tpapi 0770 system system
    mkdir /data/tpapi/etc 0770 system system
    mkdir /data/tpapi/etc/tpa 0770 system system
    mkdir /data/tpapi/etc/tpa/persistent 0770 system system    

# Temp workaround for HP library cleanup
    symlink /dev/hp3a-omap /dev/hp3A-omap

# Permissions for video
    chmod 0770 /dev/hp3a-omap
    chmod 0770 /dev/hplens-omap
    chmod 0770 /dev/video0
    chmod 0770 /dev/video1
    chmod 0770 /dev/video2
    chown media camera /dev/hp3a-omap
    chown media camera /dev/hplens-omap
    chown media camera /dev/video0
    chown media graphics /dev/video1
    chown media graphics /dev/video2

# enable usb adb mode
#    write /dev/usb_device_mode msc_adb

# usb permissions
chmod 0660 /dev/mtp
chmod 0660 /dev/ttyGS0
chmod 0660 /dev/usb_device_mode
chmod 0660 /proc/mtpctl
chown mot_usb mot_usb /dev/mtp
chown mot_usb mot_usb /dev/ttyGS0
chown mot_usb mot_usb /dev/usb_device_mode
chown mot_usb mot_usb /proc/mtpctl

mount rootfs rootfs / ro remount

# Change the ownership of led files.
   chown system system /sys/class/leds/lcd-backlight/als
   chown system system /sys/class/leds/lcd-backlight/pwm_mode
   chown system system /sys/class/leds/lcd-backlight/brightness
   chmod 0660 /sys/class/leds/lcd-backlight/brightness
   chown system system /sys/class/leds/keyboard-backlight/brightness
   chmod 0660 /sys/class/leds/keyboard-backlight/brightness
   chown system system /sys/class/leds/button-backlight/brightness
   chmod 0660 /sys/class/leds/button-backlight/brightness
   chown system system /sys/class/leds/red/blink
   chown system system /sys/class/leds/red/brightness
   chmod 0660 /sys/class/leds/red/brightness
   chown system system /sys/class/leds/green/brightness
   chmod 0660 /sys/class/leds/green/brightness
   chmod 0660 /sys/class/leds/torch-flash/camera_strobe
   chmod 0660 /sys/class/leds/torch-flash/flash_light
   chown system system /sys/class/leds/torch-flash/camera_strobe
   chown system system /sys/class/leds/torch-flash/flash_light

# flashlight
   chown system camera /sys/class/leds/spotlight/brightness
   chmod 0660 /sys/class/leds/spotlight/brightness

# Change ownership of vibrator
   chmod 0660 /sys/class/timed_output/vibrator/enable

# Permissions for 3D
    chmod 0666 /dev/pvrsrvkm
    chown system graphics /dev/pvrsrvkm

# Power Management Settings
    write /sys/devices/platform/ohci.0/usb2/2-3/power/level auto
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq 250000
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq 550000
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ondemand
    write /sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold 86
    write /sys/devices/system/cpu/cpu0/cpufreq/ondemand/sampling_rate 46875
    # Wakelock debug
    write /sys/module/wakelock/parameters/debug_mask 7

    write /sys/power/sr_vdd1_autocomp 1
    write /sys/power/sr_vdd2_autocomp 1
    write /sys/power/enable_abb_mode 1 

# Enable panic on softlockup
    write /proc/sys/kernel/softlockup_panic 1

# Install kernel modules

# Install BP related kernel modules
    #Start the engine modules for netmux first for other daemons depends on it
    insmod /system/lib/modules/netmux.ko
    insmod /system/lib/modules/netmux_linkdriver.ko
    insmod /system/lib/modules/sim.ko
    insmod /system/lib/modules/modem_pm_driver.ko
    insmod /system/lib/modules/sec.ko

    insmod /system/lib/modules/mbcache.ko
    insmod /system/lib/modules/jbd2.ko
    insmod /system/lib/modules/ext4.ko

# Start services
    exec /system/bin/mot_boot_mode

# Set core max size (2G)
    setrlimit 4 2147483648 2147483648

# The properties introduced by moto

on property:sys.chargeonly.mode=1
    start battd
    start console
    exec /sbin/charge_only_mode

# Power up to normal
on property:tcmd.suspend=0
    class_start pre-zygote_services
    class_start zygote_services
    class_start post-zygote_services

# Power up to suspend 
on property:tcmd.suspend=1
    class_start pre-zygote_services
    class_start post-zygote_services

# Normal to suspend 
on property:tcmd.suspend=2
    class_stop zygote_services

# aplogd on at boot in emulator
on property:ro.kernel.qemu=1
    start aplogd

on property:persist.service.aplogd.enable=1
    start aplogd

on property:persist.service.aplogd.enable=0
    stop aplogd

on property:ro.sys.atvc_allow_gki_log=1
    start gkilogd

on property:ro.sys.atvc_allow_gki_log=0
    stop gkilogd

# Moto safe power down
on property:sys.reboot=1
    stop ssmgrd
    exec /system/bin/sleep 10
    exec /system/bin/reboot

on property:sys.shutdown=1
    stop ssmgrd
    exec /system/bin/sleep 10
    exec /system/bin/reboot -p

on property:init.svc.wpa_supplicant=stopped
    stop dhcpcd

# The following services are added by moto

# Set property strings for ATVC logging
# This conditionally sets :
# ro.sys.atvc_allow_bp_log
# ro.sys.atvc_allow_gki_log
# ro.sys.atvc_allow_ap_mot_log
# ro.sys.atvc_allow_all_core
# ro.sys.atvc_allow_res_core
# ro.sys.atvc_allow_res_panic
# ro.sys.atvc_allow_efem
# ro.sys.atvc_allow_netmon_usb
# ro.sys.atvc_allow_netmon_ih
# ro.sys.atvc_allow_all_adb
service atvc /system/bin/dbvc_atvc_property_set
    class pre-zygote_services
    user system
    group system
    oneshot

service usbd /system/bin/usbd
    class pre-zygote_services
    user root
    group mot_usb system
    socket usbd stream 0666 mot_usb mot_usb

service nvm-daemon /system/bin/nvm_daemon
    class pre-zygote_services
    user radio
    group radio

# start start_modem.sh to for development purpose 
service start_modem /system/bin/sh /system/bin/start_modem.sh
    class pre-zygote_services
    oneshot

# start commdrv pds script
service start_commdrv /system/bin/sh /system/etc/motorola/comm_drv/commdrv_fs.sh
    class pre-zygote_services
    oneshot
    user radio
    group radio

service gkisystem /system/bin/gkisystem
    class pre-zygote_services
    socket location_shim stream 660 radio system
    socket gpshal_socket stream 660 radio system
    socket opprofdaemon  stream 660 radio system
    socket gkilog_sock   stream 660 radio radio
    group radio system mot_accy
    onrestart exec /system/bin/gki_pd_notifier

service rild_tcmd /system/bin/rild_tcmd
    class pre-zygote_services
    socket rild_tcmd stream 660 radio radio
    user radio
    group radio

service battd /system/bin/battd
    class pre-zygote_services
    socket battdsock stream 0660 mot_accy mot_accy
    user root
    group mot_accy mot_pwric
    oneshot

service catcommands /system/bin/catcommands
    class pre-zygote_services
    group radio 

service bplogd_daemon /system/bin/bplogd_daemon
    class pre-zygote_services
    socket dlog_cmds stream 660 radio log
    socket bp_pd_status stream 660 radio log
    socket bp_aol_cmd stream 666 radio log
    user radio
    group radio inet log sdcard_rw
    oneshot

service clean_dex /system/bin/sh /system/bin/clean_dex.sh
    class pre-zygote_services
    oneshot
    user system
    group system 

service ssmgrd /system/xbin/ssmgrd
    class post-zygote_services
    group radio input mot_pwric
    oneshot

service tcmd /system/bin/tcmd
    class post-zygote_services
    group mot_tcmd radio mot_pwric input system log camera graphics mot_accy sdcard_rw audio inet bluetooth wifi
    socket local_tcmd stream 0660 mot_tcmd mot_tcmd
    socket batch_socket stream 0600 mot_tcmd mot_tcmd
    oneshot

# turn on wifi for tcmd step one
on property:tcmd.load_wlan=1
    insmod /system/lib/modules/tiwlan_drv.ko

# turn on wifi for tcmd step two
on property:tcmd.load_wlan=2
    start wlan_loader

# turn on wifi for tcmd step three
on property:tcmd.load_wlan=3
    exec /system/bin/ifconfig tiwlan0 up

# turn off wifi for tcmd step one
on property:tcmd.unload_wlan=1
    exec /system/bin/ifconfig tiwlan0 down

# turn off wifi for tcmd step two
on property:tcmd.unload_wlan=2
    exec /system/bin/rmmod tiwlan_drv

# turn on bluetooth for tcmd
on property:tcmd.bt_mod=1
    insmod /system/lib/modules/wl127x-test.ko

#turn off bluetooth for tcmd
on property:tcmd.bt_mod=0
    exec /system/bin/rmmod wl127x_test

service panic_daemon /system/bin/panic_daemon
    class post-zygote_services
    user radio
    group radio system graphics
    socket panic_daemon stream 660 radio radio
    onrestart exec /system/bin/reboot

# Ecompass daemon
service akmd2 /system/bin/akmd2
    class post-zygote_services
    user compass
    group compass misc input

service wlan_loader /system/bin/wlan_loader \
    -f /system/etc/wifi/fw_wlan1271.bin -i /system/etc/wifi/tiwlan.ini \
    -e /config/wifi/nvs_map.bin
    class post-zygote_services
    disabled
    oneshot

service wpa_supplicant /system/bin/wpa_supplicant \
    -Dtiwlan0 -itiwlan0 -c/data/misc/wifi/wpa_supplicant.conf
    class post-zygote_services
#   we will start as root and wpa_supplicant will switch to user wifi
#   after setting up the capabilities required for WEXT
#   user wifi
#   group wifi inet keystore
    socket wpa_tiwlan0 dgram 660 wifi wifi
    disabled
    oneshot

service dhcpcd /system/bin/dhcpcd -ABKL tiwlan0
    class post-zygote_services
    disabled
    oneshot

service hciattach /system/bin/hciattach \
    -n -t 10 /dev/ttyS1 texasalt 3000000 flow
    class post-zygote_services
    user bluetooth
    group bluetooth net_bt_admin
    disabled

service baseimage /system/bin/dspexec -v -T /system/lib/dsp/baseimage.dof
    class media_early_init
    oneshot

service pvrsrv /system/bin/pvrsrvinit
    class post-zygote_services
    user root
    oneshot

service pppd /system/xbin/pppd silent ipparam dun plugin /system/lib/libpppd_plugin.so
    class post-zygote_services
    user root
    disabled
    oneshot

service locDrv /system/bin/brcm_guci_drv -config /system/etc/gpsconfig.xml
    class post-zygote_services
    user radio
    group radio inet sdcard_rw

service pm_init /system/bin/sh /system/bin/pm_init.sh
    class post-zygote_services
    oneshot

# aplogd is controlled by the persist.service.aplogd.enable system property
service aplogd /system/bin/aplogd
    class post-zygote_services
    socket ap_aol_cmd  stream 666 system system
    user log
    group log system sdcard_rw
    oneshot

service gkilogd /system/bin/gkilogd
    class post-zygote_services
    socket gki_aol_cmd stream 660 radio log
    user radio
    group radio log sdcard_rw
   
service secclkd /system/bin/secclkd
    class post-zygote_services
    user system
    group system radio
    oneshot 

# bugreport is triggered by the KEY_LEFTSHIFT and KEY_MENU keycodes
service bugreport /system/bin/dumpstate -d -v -o /sdcard/bugreports/bugreport
    disabled
    oneshot
    keycodes 42 139
