blob: dfc4fdd57c7edd47ff4d3f357113d38ab5c0f626 [file] [log] [blame]
Adam Shihf15086f2022-09-05 11:05:32 +08001#!/vendor/bin/sh
2
Brian Norris6b137ff2024-10-18 15:06:46 -07003###################################################################
4### init.insmod.cfg format: ###
5### ----------------------------------------------------------- ###
6### [insmod|setprop|enable|moprobe|rmmod|wait] [path|prop name] ###
7### ... ###
8###################################################################
Adam Shihf15086f2022-09-05 11:05:32 +08009
10modules_dir=
Robin Pengdbf355e2023-03-15 01:22:07 +000011system_modules_dir=
12vendor_modules_dir=
Adam Shihf15086f2022-09-05 11:05:32 +080013
Robin Pengdbf355e2023-03-15 01:22:07 +000014for 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
Adam Shihf15086f2022-09-05 11:05:32 +080026done
27
Robin Pengdbf355e2023-03-15 01:22:07 +000028if [[ -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
Adam Shihf15086f2022-09-05 11:05:32 +080034 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
Kelvin Zhang03c030c2024-04-25 00:49:19 +000055 setprop vendor.common.modules.ready
56 setprop vendor.device.modules.ready
57 setprop vendor.all.modules.ready
58 setprop vendor.all.devices.ready
Adam Shihf15086f2022-09-05 11:05:32 +080059 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 ;;
Zheng Panb2a04f22024-03-12 18:21:05 -070069 "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 ;;
Adam Shihf15086f2022-09-05 11:05:32 +080080 "modprobe")
81 case ${arg} in
Robin Pengdbf355e2023-03-15 01:22:07 +000082 "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" ;;
Adam Shihf15086f2022-09-05 11:05:32 +080094 esac
Robin Pengdbf355e2023-03-15 01:22:07 +000095 if [[ -d "${modules_dir}" ]]; then
96 modprobe -a -d "${modules_dir}" $arg
97 fi
98 ;;
Brian Norris6b137ff2024-10-18 15:06:46 -070099 "rmmod") rmmod $arg ;;
Adam Shihf15086f2022-09-05 11:05:32 +0800100 "wait") wait_for_file $arg ;;
101 esac
102 done < $cfg_file
103fi