Merge "Selectively load kernel modules based on current kernel number"
diff --git a/shared/config/init.common.rc b/shared/config/init.common.rc
index f5cbb2b..ca6aa33 100644
--- a/shared/config/init.common.rc
+++ b/shared/config/init.common.rc
@@ -14,13 +14,15 @@
     setprop ro.hardware.gralloc ${ro.boot.hardware.gralloc}
     setprop ro.hardware.hwcomposer ${ro.boot.hardware.hwcomposer}
 
+    # start module load in the background
+    start vendor.insmod_sh
+
 on early-init && property:ro.boot.fstab_name="fstab"
     mount /dev/block/vdd /metadata ext4 nodev,noatime,nosuid,errors=panic wait,formattable
 
 on early-init && property:ro.boot.fstab_name="composite-fstab"
     mount /dev/block/by-name/metadata /metadata ext4 nodev,noatime,nosuid,errors=panic wait,formattable
 
-
 on init
     # ZRAM setup
     write /sys/block/zram0/comp_algorithm lz4
@@ -89,6 +91,12 @@
 on sys-boot-completed-set && property:persist.sys.zram_enabled=1
     swapon_all /vendor/etc/${ro.boot.fstab_name}.${ro.hardware}
 
+service vendor.insmod_sh /vendor/bin/init.insmod.sh
+    class main
+    user root
+    group root system
+    disabled
+    oneshot
 
 service socket_vsock_proxy /vendor/bin/socket_vsock_proxy -tcp_port=5555 -vsock_port=6520
 
diff --git a/shared/config/init.insmod.sh b/shared/config/init.insmod.sh
new file mode 100755
index 0000000..ccbf716
--- /dev/null
+++ b/shared/config/init.insmod.sh
@@ -0,0 +1,49 @@
+#!/vendor/bin/sh
+
+# Copyright (C) 2019 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.
+#
+
+KERNEL_VERSION_NUMBER=`uname -r`
+MAINLINE_STR='mainline'
+if [[ $KERNEL_VERSION_NUMBER == *$MAINLINE_STR* ]]; then
+    IS_MAINLINE=1
+else
+    IS_MAINLINE=0
+fi
+
+KERNEL_VERSION_NUMBER=`echo $KERNEL_VERSION_NUMBER | grep -o -E '^[0-9]+\.[0-9]+'`
+# This folder on cuttlefish contains modules for multiple kernel versions.
+# Hence the need to filter them instead of relying on module.order
+VENDOR_MODULES='/vendor/lib/modules/*.ko'
+
+for f in $VENDOR_MODULES
+do
+    MOD_VERSION=`modinfo $f`
+    MOD_VERSION=`echo $MOD_VERSION | grep -o -E 'vermagic: [0-9a-zA-Z\.-]+'`
+    MOD_VERSION_NUMBER=`echo $MOD_VERSION | grep -o -E '[0-9]+\.[0-9]+'`
+    if [[ $MOD_VERSION == *$MAINLINE_STR* ]]; then
+        IS_MOD_MAINLINE=1
+    else
+        IS_MOD_MAINLINE=0
+    fi
+
+    # TODO (137683279) When we have a few more kernel modules, we'll have to do the module
+    # insertion of least dependencies.
+    if [ $IS_MOD_MAINLINE -eq $IS_MAINLINE ] && [ $MOD_VERSION_NUMBER == $KERNEL_VERSION_NUMBER ]
+    then
+        `insmod $f`
+        echo "Insmod " $f
+    fi
+done
diff --git a/shared/device.mk b/shared/device.mk
index 00fdb89..f914d35 100644
--- a/shared/device.mk
+++ b/shared/device.mk
@@ -352,5 +352,11 @@
 
 endif
 
+#
+# Shell script Vendor Module Loading
+#
+PRODUCT_COPY_FILES += \
+   $(LOCAL_PATH)/config/init.insmod.sh:$(TARGET_COPY_OUT_VENDOR)/bin/init.insmod.sh \
+
 # Host packages to install
 PRODUCT_HOST_PACKAGES += socket_forward_proxy socket_vsock_proxy
diff --git a/shared/sepolicy/vendor/file_contexts b/shared/sepolicy/vendor/file_contexts
index 6077828..6c78a40 100644
--- a/shared/sepolicy/vendor/file_contexts
+++ b/shared/sepolicy/vendor/file_contexts
@@ -59,6 +59,7 @@
 /vendor/bin/ip_link_add  u:object_r:ip_link_add_exec:s0
 /vendor/bin/setup_wifi  u:object_r:setup_wifi_exec:s0
 /vendor/bin/hw/android\.hardware\.authsecret@1\.0-service  u:object_r:hal_authsecret_default_exec:s0
+/vendor/bin/init\.insmod\.sh  u:object_r:init_insmod_sh_exec:s0
 
 /vendor/lib(64)?/cuttlefish_auto_resources.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/android\.hardware\.graphics\.mapper@2\.0-impl-2\.1\.so u:object_r:same_process_hal_file:s0
diff --git a/shared/sepolicy/vendor/init_insmod_sh.te b/shared/sepolicy/vendor/init_insmod_sh.te
new file mode 100644
index 0000000..5400a37
--- /dev/null
+++ b/shared/sepolicy/vendor/init_insmod_sh.te
@@ -0,0 +1,11 @@
+type init_insmod_sh, domain;
+type init_insmod_sh_exec, exec_type, vendor_file_type, file_type;
+
+init_daemon_domain(init_insmod_sh)
+
+allow init_insmod_sh vendor_shell_exec:file rx_file_perms;
+allow init_insmod_sh vendor_toolbox_exec:file rx_file_perms;
+
+# Allow insmod
+allow init_insmod_sh self:capability sys_module;
+allow init_insmod_sh vendor_file:system module_load;
diff --git a/vsoc_arm64/BoardConfig.mk b/vsoc_arm64/BoardConfig.mk
index ffd17b6..101eb5f 100644
--- a/vsoc_arm64/BoardConfig.mk
+++ b/vsoc_arm64/BoardConfig.mk
@@ -31,3 +31,6 @@
 TARGET_2ND_CPU_ABI2 := armeabi
 TARGET_2ND_CPU_VARIANT := cortex-a53
 TARGET_TRANSLATE_2ND_ARCH := false
+
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/4.19-arm64/*.ko)
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/mainline-arm64/*.ko)
diff --git a/vsoc_x86/BoardConfig.mk b/vsoc_x86/BoardConfig.mk
index 9c7001a..7e6a7da 100644
--- a/vsoc_x86/BoardConfig.mk
+++ b/vsoc_x86/BoardConfig.mk
@@ -31,3 +31,5 @@
 TARGET_NATIVE_BRIDGE_ABI := armeabi-v7a armeabi
 
 BUILD_BROKEN_DUP_RULES := true
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/4.19-x86_64/*.ko)
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/mainline-x86_64/*.ko)
diff --git a/vsoc_x86_64/BoardConfig.mk b/vsoc_x86_64/BoardConfig.mk
index e978502..5cc5cc9 100644
--- a/vsoc_x86_64/BoardConfig.mk
+++ b/vsoc_x86_64/BoardConfig.mk
@@ -42,3 +42,5 @@
 TARGET_NATIVE_BRIDGE_2ND_ABI := armeabi-v7a armeabi
 
 BUILD_BROKEN_DUP_RULES := true
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/4.19-x86_64/*.ko)
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/mainline-x86_64/*.ko)
diff --git a/vsoc_x86_noapex/BoardConfig.mk b/vsoc_x86_noapex/BoardConfig.mk
index 934b3e9..c29877c 100644
--- a/vsoc_x86_noapex/BoardConfig.mk
+++ b/vsoc_x86_noapex/BoardConfig.mk
@@ -21,3 +21,5 @@
 include device/google/cuttlefish/vsoc_x86/BoardConfig.mk
 
 TARGET_FLATTEN_APEX := true
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/4.19-x86_64/*.ko)
+BOARD_VENDOR_KERNEL_MODULES += $(wildcard device/google/cuttlefish_kernel/mainline-x86_64/*.ko)