#
# Copyright 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import init.bullhead.usb.rc
import init.bullhead.diag.rc
import init.bullhead.fp.rc
import init.bullhead.sensorhub.rc
import init.bullhead.ramdump.rc
import init.bullhead.misc.rc

on early-init
    mount debugfs debugfs /sys/kernel/debug
    chown system system /sys/kernel/debug/kgsl/proc

on charger
    # disable FPS clk
    write /sys/bus/spi/drivers/fpc1020/spi1.2/clk_enable 0

    # low power governing
    write /sys/devices/system/cpu/cpu1/online 0
    write /sys/devices/system/cpu/cpu2/online 0
    write /sys/devices/system/cpu/cpu3/online 0
    write /sys/devices/system/cpu/cpu4/online 0
    write /sys/devices/system/cpu/cpu5/online 0
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor powersave

    # Disable CPU retention
    write /sys/module/lpm_levels/system/a53/cpu0/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a53/cpu1/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a53/cpu2/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a53/cpu3/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a57/cpu4/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a57/cpu5/retention/idle_enabled 0
    # Disable L2 retention
    write /sys/module/lpm_levels/system/a53/a53-l2-retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a57/a57-l2-retention/idle_enabled 0

on property:sys.boot_completed=1
    # allow CPUs to go in deeper idle state than C0
    write /sys/module/lpm_levels/parameters/sleep_disabled 0

on init
    # Set permissions for persist partition
    mkdir /persist 0771 system system
    mkdir /firmware 0771 system system

    symlink /sdcard /storage/sdcard0

    # NXP HAL expects /dev/pn54x
    symlink /dev/pn548 /dev/pn54x

    write /proc/sys/vm/page-cluster 0

on fs
    mount_all ./fstab.bullhead
    swapon_all ./fstab.bullhead

    restorecon_recursive /persist

on early-boot
    # set RLIMIT_MEMLOCK to 64MB
    setrlimit 8 67108864 67108864

    # Allow subsystem (modem etc) debugging
    write /sys/module/subsystem_restart/parameters/enable_debug ${persist.sys.ssr.enable_debug}
    write /sys/module/pil_msa/parameters/pbl_mba_boot_timeout_ms ${persist.sys.mba_boot_timeout}
    write /sys/module/pil_msa/parameters/modem_auth_timeout_ms ${persist.sys.modem_auth_timeout}
    write /sys/module/peripheral_loader/parameters/proxy_timeout_ms ${persist.sys.pil_proxy_timeout}

on boot
    ###################### jolla-kernel Settings ########################
    # take the A57s offline when thermal hotplug is disabled
    write /sys/devices/system/cpu/cpu4/online 0
    write /sys/devices/system/cpu/cpu5/online 0

    # disable msm_thermal to switch governor
    write /sys/module/msm_thermal/core_control/enabled 0

    # some files in /sys/devices/system/cpu are created after the restorecon of
    # /sys/. These files receive the default label "sysfs".
    # Restorecon again to give new files the correct label.
    restorecon_recursive /sys/devices/system/cpu

    # Best effort limiting for first time boot if msm_performance module is absent
    write /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq 960000

    # Limit A57 max freq from msm_perf module in case CPU 4 is offline
    write /sys/module/msm_performance/parameters/cpu_max_freq "4:960000 5:960000"

    # Disable CPU retention
    write /sys/module/lpm_levels/system/a53/cpu0/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a53/cpu1/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a53/cpu2/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a53/cpu3/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a57/cpu4/retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a57/cpu5/retention/idle_enabled 0

    # Disable L2 retention
    write /sys/module/lpm_levels/system/a53/a53-l2-retention/idle_enabled 0
    write /sys/module/lpm_levels/system/a57/a57-l2-retention/idle_enabled 0

    # configure governor settings for little cluster
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor interactive
    restorecon_recursive /sys/devices/system/cpu
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/use_sched_load 1
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/use_migration_notif 1
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/above_hispeed_delay 19000
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/go_hispeed_load 85
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/timer_rate 19000
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/hispeed_freq 960000
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/io_is_busy 1
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/target_loads "50 600000:65 787200:75 960000:80"
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/min_sample_time 40000
    write /sys/devices/system/cpu/cpu0/cpufreq/interactive/max_freq_hysteresis 80000
    write /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq 384000

    # online CPU4
    write /sys/devices/system/cpu/cpu4/online 1

    # configure governor settings for big cluster
    write /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor interactive
    restorecon_recursive /sys/devices/system/cpu
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/use_sched_load 1
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/use_migration_notif 1
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/above_hispeed_delay 19000
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/go_hispeed_load 99
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/timer_rate 19000
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/hispeed_freq 1248000
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/io_is_busy 1
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/target_loads "70 960000:80 1248000:85"
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/min_sample_time 40000
    write /sys/devices/system/cpu/cpu4/cpufreq/interactive/max_freq_hysteresis 80000
    write /sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq 633600

    # restore A57's max
    copy /sys/devices/system/cpu/cpu4/cpufreq/cpuinfo_max_freq /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq

    # plugin remaining A57s
    write /sys/devices/system/cpu/cpu5/online 1

    # Restore CPU 4 max freq from msm_performance
    write /sys/module/msm_performance/parameters/cpu_max_freq "4:4294967295 5:4294967295"

    # enable rps static configuration
    write /sys/class/net/rmnet_ipa0/queues/rx-0/rps_cpus 8

    # re-enable msm_thermal
    write /sys/module/msm_thermal/core_control/enabled 1

    # set GPU default power level to 5 (180MHz) instead of 4 (305MHz)
    write /sys/class/kgsl/kgsl-3d0/default_pwrlevel 5

    write /sys/devices/platform/kcal_ctrl.0/kcal_fix_yellow 1
    write /sys/android_touch/sweep2wake 0
    write /sys/android_touch/sweep2sleep 0
    write /sys/android_touch/doubletap2wake 0
    write /sys/android_touch/doubletap2sleep 0
    write /sys/android_touch/scroff_volctr 1
    write /sys/android_touch/sovc_auto_off_delay 2500
    write /sys/kernel/dyn_fsync/Dyn_fsync_active 1
    #####################################################################

    # update foreground cpuset now that processors are up
    # foreground gets all CPUs except CPU 3
    # CPU 3 is reserved for the top app
    write /dev/cpuset/foreground/cpus 0-2,4-5
    write /dev/cpuset/foreground/boost/cpus 4-5
    write /dev/cpuset/background/cpus 0
    write /dev/cpuset/system-background/cpus 0-2
    write /dev/cpuset/top-app/cpus 0-5

    # Bluetooth
    chown bluetooth net_bt /sys/class/rfkill/rfkill0/type
    chown bluetooth net_bt /sys/class/rfkill/rfkill0/state
    chmod 0660 /sys/class/rfkill/rfkill0/state
    chown bluetooth net_bt /dev/ttyHS0
    chmod 0660 /dev/ttyHS0
    chown bluetooth net_bt /sys/class/rfkill/rfkill0/device/extldo
    chmod 0660 /sys/class/rfkill/rfkill0/device/extldo

    # Fingerprint
    chown system system /sys/bus/spi/drivers/fpc1020/spi1.2/clk_enable
    chown system system /sys/bus/spi/drivers/fpc1020/spi1.2/wakeup_enable
    chown system system /sys/bus/spi/drivers/fpc1020/spi1.2/irq

    # create symlink for fb1 as HDMI
    symlink /dev/graphics/fb1 /dev/graphics/hdmi

    # setup permissions for fb1 related nodes
    chown system graphics /sys/class/graphics/fb1/hpd
    chown system graphics /sys/class/graphics/fb1/vendor_name
    chown system graphics /sys/class/graphics/fb1/product_description
    chmod 0664 /sys/devices/virtual/graphics/fb1/hpd
    chmod 0664 /sys/devices/virtual/graphics/fb1/vendor_name
    chmod 0664 /sys/devices/virtual/graphics/fb1/product_description

    # Grant permission to modify the color_temp setting
    chown system graphics /sys/class/graphics/fb0/color_temp
    chmod 0664 /sys/class/graphics/fb0/color_temp

    # Allow QMUX daemon to assign port open wait time
    chown radio radio /sys/devices/virtual/hsicctl/hsicctl0/modem_wait

    #Create QMUX deamon socket area
    mkdir /dev/socket/qmux_radio 0770 radio radio
    chmod 2770 /dev/socket/qmux_radio
    mkdir /dev/socket/qmux_audio 0770 media audio
    chmod 2770 /dev/socket/qmux_audio
    mkdir /dev/socket/qmux_bluetooth 0770 bluetooth bluetooth
    chmod 2770 /dev/socket/qmux_bluetooth
    mkdir /dev/socket/qmux_gps 0770 gps gps
    chmod 2770 /dev/socket/qmux_gps

    # Subsytem Restart
    write /sys/bus/msm_subsys/devices/subsys0/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys1/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys2/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys3/restart_level "related"

    # Subsytem Ramdump collection
    write /sys/module/subsystem_restart/parameters/enable_ramdumps 0
    write /sys/module/msm_poweroff/parameters/download_mode 0

    # Adjust socket buffer to enlarge TCP receive window for high bandwidth
    write /proc/sys/net/ipv4/tcp_adv_win_scale 1

    # Assign TCP buffer thresholds to be ceiling value of technology maximums
    # Increased technology maximums should be reflected here.
    write /proc/sys/net/core/rmem_max  8388608
    write /proc/sys/net/core/wmem_max  8388608

    # Leds
    chown system system /sys/class/leds/red/on_off_ms
    chown system system /sys/class/leds/green/on_off_ms
    chown system system /sys/class/leds/blue/on_off_ms
    chown system system /sys/class/leds/red/rgb_start

    write /sys/block/mmcblk0/queue/rq_affinity 0
    write /sys/block/mmcblk0/queue/scheduler bfq
    write /sys/block/mmcblk0/queue/read_ahead_kb 2048

on post-fs
    symlink /dev/block/platform/soc.0/f9824900.sdhci /dev/block/bootdevice

    mkdir /persist/data 0700 system system
    mkdir /persist/data/sfs 0700 system system
    mkdir /persist/data/tz 0700 system system
    mkdir /persist/data/app_g 0700 system system
    mkdir /persist/widevine 0770 drm system

    start qseecomd
    exec - root root system -- /system/bin/init.bullhead.qseecomd.sh

on post-fs-data
    # Mark the copy complete flag to not completed
    mkdir /data/misc/radio 0770 radio radio
    write /data/misc/radio/copy_complete 0
    chown radio radio /data/misc/radio/copy_complete
    chmod 0660 /data/misc/radio/copy_complete

    write /sys/kernel/boot_adsp/boot 1

    #Update touch firmware if required
    write /sys/bus/i2c/devices/2-0020/check_fw 1
    write /sys/devices/soc.0/f9924000.i2c/power/control on

    # Create /data/time folder for time-services
    mkdir /data/time/ 0700 system system

    # Create the directories used by CnE subsystem
    mkdir /data/connectivity 0771 system system

    # NFC: create data/nfc for nv storage
    mkdir /data/nfc 0770 nfc nfc
    mkdir /data/nfc/param 0770 nfc nfc

    # use qcril.db to handle Ecc and load MBN
    copy /system/etc/qcril.db /data/misc/radio/qcril.db
    chown radio radio /data/misc/radio/qcril.db
    chmod 0600 /data/misc/radio/qcril.db

    # Create folder for mm-qcamera-daemon
    mkdir /data/misc/camera 0770 camera camera

    # change permission for ldaf
    chown input system /persist/calibration
    chmod 0650 /persist/calibration
    chown input system /persist/calibration/offset
    chmod 0640 /persist/calibration/offset
    chown input system /persist/calibration/xtalk
    chmod 0640 /persist/calibration/xtalk

    #Create directories for Location services
    mkdir /data/misc/location 0770 gps gps
    mkdir /data/misc/location/mq 0770 gps gps
    mkdir /data/misc/location/xtwifi 0770 gps gps
    mkdir /data/misc/location/gpsone_d 0770 system gps

    # Audio
    mkdir /data/misc/audio 0770 audio audio

    # perf daemon
    mkdir /data/misc/perfd 0755 root system
    chmod 2755 /data/misc/perfd
    rm /data/misc/perfd/default_values

    # Subsytem Ramdump collection
    mkdir /data/ramdump 0700 root root

    setprop vold.post_fs_data_done 1

service irsc_util /system/bin/irsc_util "/etc/sec_config"
    class main
    user root
    oneshot

service rmt_storage /system/bin/rmt_storage
    class core
    user root
    group system wakelock

service start_hci_filter /system/bin/wcnss_filter
    class late_start
    user bluetooth
    group bluetooth system
    disabled

on property:wc_transport.start_hci=true
    start start_hci_filter

on property:wc_transport.start_hci=false
    stop start_hci_filter

# QMUX must be in multiple groups to support external process connections
service qmuxd /system/bin/qmuxd
    class main
    user radio
    group radio audio bluetooth wakelock gps

service perfd /system/bin/perfd
    class main
    user root
    group system
    writepid /dev/cpuset/system-background/tasks

service qti /system/vendor/bin/qti
    class main
    user radio
    group radio net_raw diag usb net_admin

service netmgrd /system/bin/netmgrd
    class main
    group radio system wakelock

service per_mgr /system/bin/pm-service
    class core
    user system
    group system net_raw

service per_proxy /system/bin/pm-proxy
    class core
    user system
    group system net_raw
    disabled

on property:init.svc.per_mgr=running
    start per_proxy

on property:sys.shutdown.requested=*
    stop per_proxy

on property:ro.boot.hardware.sku=LGH790
    setprop ro.boot.wificountrycode US

on property:ro.boot.hardware.sku=LGH791
    setprop ro.boot.wificountrycode 00

on property:ro.boot.hardware.sku=LGH791F
    setprop ro.boot.wificountrycode 00

on property:ro.boot.hardware.sku=LGH798
    setprop ro.boot.wificountrycode 00

service wpa_supplicant /system/bin/wpa_supplicant \
    -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \
    -I/system/etc/wifi/wpa_supplicant_overlay.conf \
    -e/data/misc/wifi/entropy.bin -g@android:wpa_wlan0
    #   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
    class main
    socket wpa_wlan0 dgram 660 wifi wifi
    disabled
    oneshot

service p2p_supplicant /system/bin/wpa_supplicant \
    -ip2p0 -Dnl80211 -c/data/misc/wifi/p2p_supplicant.conf \
    -I/system/etc/wifi/p2p_supplicant_overlay.conf -N \
    -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \
    -I/system/etc/wifi/wpa_supplicant_overlay.conf \
    -puse_p2p_group_interface=1 \
    -e/data/misc/wifi/entropy.bin -g@android:wpa_wlan0
#   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
    class main
    socket wpa_wlan0 dgram 660 wifi wifi
    disabled
    oneshot

service loc_launcher /system/bin/loc_launcher
    #loc_launcher will start as root and set its uid to gps
    class late_start
    group gps inet net_raw diag net_admin wifi

service cnss-daemon /system/bin/cnss-daemon -n -l
    class late_start

service qseecomd /vendor/bin/qseecomd
    class core
    user root
    group root

service msm_irqbalance /system/bin/msm_irqbalance -f /vendor/etc/msm_irqbalance.conf
    socket msm_irqbalance seqpacket 660 root system
    class core
    user root
    group root
    writepid /dev/cpuset/system-background/tasks

service time_daemon /system/bin/time_daemon
    class late_start
    user root
    group root

service charger /sbin/healthd -c
    class charger
    critical
    seclabel u:r:healthd:s0

service qcamerasvr /vendor/bin/mm-qcamera-daemon
    class late_start
    user camera
    group camera system inet input graphics
    writepid /dev/cpuset/system-background/tasks

on property:persist.radio.atfwd.start=false
    stop atfwd

service atfwd /system/bin/ATFWD-daemon
    class late_start
    user system
    group system radio

#on property:ro.debuggable=1
#    start ss_ramdump

service ss_ramdump /system/bin/subsystem_ramdump
    class main
    user root
    group root system
    disabled

service cnd /system/bin/cnd
    class late_start
    socket cnd stream 660 root inet
    group root wakelock

service imsqmidaemon /system/bin/imsqmidaemon
    class main
    user system
    socket ims_qmid stream 0660 system radio
    group radio net_raw log diag

service imsdatadaemon /system/bin/imsdatadaemon
    class main
    user system
    socket ims_datad stream 0660 system radio
    group system wifi radio inet net_raw log diag net_admin
    disabled

on property:sys.ims.QMI_DAEMON_STATUS=1
    start imsdatadaemon

service bullhead-sh /system/bin/init.bullhead.sh
    class late_start
    user radio
    group system
    oneshot

# bugreport is triggered by holding down volume down, volume up and power
service bugreport /system/bin/dumpstate -d -p -B -z \
        -o /data/user_de/0/com.android.shell/files/bugreports/bugreport
    class main
    disabled
    oneshot
    keycodes 114 115 116

service fingerprintd /system/bin/fingerprintd
    class late_start
    user system
    group system input

