1#!/vendor/bin/sh
2
3###################################################################
4### init.insmod.cfg format:                                     ###
5### ----------------------------------------------------------- ###
6### [insmod|setprop|enable|moprobe|rmmod|wait] [path|prop name] ###
7### ...                                                         ###
8###################################################################
9
10modules_dir=
11system_modules_dir=
12vendor_modules_dir=
13
14for dir in system vendor; do
15  for f in /${dir}/lib/modules/*/modules.dep /${dir}/lib/modules/modules.dep; do
16    if [[ -f "$f" ]]; then
17      if [[ "${dir}" == "system" ]]; then
18        system_modules_dir="$(dirname "$f")"
19      else
20        vendor_modules_dir="$(dirname "$f")"
21        modules_dir=${vendor_modules_dir}
22      fi
23      break
24    fi
25  done
26done
27
28if [[ -z "${system_modules_dir}" ]]; then
29  echo "Unable to locate system kernel modules directory" 2>&1
30fi
31
32if [[ -z "${vendor_modules_dir}" ]]; then
33  echo "Unable to locate vendor kernel modules directory" 2>&1
34  exit 1
35fi
36
37# imitates wait_for_file() in init
38wait_for_file()
39{
40    filename="${1}"
41    timeout="${2:-5}"
42
43    expiry=$(($(date "+%s")+timeout))
44    while [[ ! -e "${filename}" ]] && [[ "$(date "+%s")" -le "${expiry}" ]]
45    do
46        sleep 0.01
47    done
48}
49
50if [ $# -eq 1 ]; then
51  cfg_file=$1
52else
53  # Set property even if there is no insmod config
54  # to unblock early-boot trigger
55  setprop vendor.common.modules.ready
56  setprop vendor.device.modules.ready
57  setprop vendor.all.modules.ready
58  setprop vendor.all.devices.ready
59  exit 1
60fi
61
62if [ -f $cfg_file ]; then
63  while IFS="|" read -r action arg
64  do
65    case $action in
66      "insmod") insmod $arg ;;
67      "setprop") setprop $arg 1 ;;
68      "enable") echo 1 > $arg ;;
69      "condinsmod")
70        prop=$(echo $arg | cut -d '|' -f 1)
71        module1=$(echo $arg | cut -d '|' -f 2)
72        module2=$(echo $arg | cut -d '|' -f 3)
73        value=$(getprop $prop)
74        if [[ ${value} == "true" ]]; then
75          insmod ${vendor_modules_dir}/${module1}
76        else
77          insmod ${vendor_modules_dir}/${module2}
78        fi
79        ;;
80      "modprobe")
81        case ${arg} in
82          "system -b *" | "system -b")
83            modules_dir=${system_modules_dir}
84            arg="-b --all=${system_modules_dir}/modules.load" ;;
85          "system *" | "system")
86            modules_dir=${system_modules_dir}
87            arg="--all=${system_modules_dir}/modules.load" ;;
88          "-b *" | "-b" | "vendor -b *" | "vendor -b")
89            modules_dir=${vendor_modules_dir}
90            arg="-b --all=${vendor_modules_dir}/modules.load" ;;
91          "*" | "" | "vendor *" | "vendor")
92            modules_dir=${vendor_modules_dir}
93            arg="--all=${vendor_modules_dir}/modules.load" ;;
94        esac
95        if [[ -d "${modules_dir}" ]]; then
96          modprobe -a -d "${modules_dir}" $arg
97        fi
98        ;;
99      "rmmod") rmmod $arg ;;
100      "wait") wait_for_file $arg ;;
101    esac
102  done < $cfg_file
103fi
104