build.sh: Separate module.load for vendor_boot and vendor_dlkm

Modules can be loaded from two different partitions - vendor_boot and
vendor_dlkm. Provide separate outputs in the DIST_DIR for which modules
are to be loaded in each: vendor_boot.modules.load and
vendor_dlkm.modules.load.

Additionally, because it is redundant to load modules from both the
vendor_boot and vendor_dlkm partitions, remove any modules loaded in
vendor_boot from the vendor_dlkm.modules.load.

Signed-off-by: J. Avila <elavila@google.com>
Change-Id: I0e4e67914b7df7bddbb0bdb7dc1514f505e2c526
diff --git a/build.sh b/build.sh
index 0352154..80a5113 100755
--- a/build.sh
+++ b/build.sh
@@ -204,8 +204,8 @@
 #     If BOOT_IMAGE_HEADER_VERSION >= 3, a vendor_boot image will be built
 #     unless SKIP_VENDOR_BOOT is defined.
 #     - MODULES_LIST=<file to list of modules> list of modules to use for
-#       modules.load. If this property is not set, then the default modules.load
-#       is used.
+#       vendor_boot.modules.load. If this property is not set, then the default
+#       modules.load is used.
 #     - TRIM_UNUSED_MODULES. If set, then modules not mentioned in
 #       modules.load are removed from initramfs. If MODULES_LIST is unset, then
 #       having this variable set effectively becomes a no-op.
@@ -268,7 +268,9 @@
 #   VENDOR_DLKM_MODULES_LIST
 #     location (relative to the repo root directory) of an optional file
 #     containing the list of kernel modules which shall be copied into a
-#     vendor_dlkm partition image.
+#     vendor_dlkm partition image. Any modules passed into MODULES_LIST which
+#     become part of the vendor_boot.modules.load will be trimmed from the
+#     vendor_dlkm.modules.load.
 #
 #   VENDOR_DLKM_MODULES_BLOCKLIST
 #     location (relative to the repo root directory) of an optional file
@@ -484,6 +486,18 @@
   create_modules_staging "${VENDOR_DLKM_MODULES_LIST}" "${MODULES_STAGING_DIR}" \
     "${VENDOR_DLKM_STAGING_DIR}" "${VENDOR_DLKM_MODULES_BLOCKLIST}"
 
+  local vendor_dlkm_modules_root_dir=$(echo ${VENDOR_DLKM_STAGING_DIR}/lib/modules/*)
+  local vendor_dlkm_modules_load=${vendor_dlkm_modules_root_dir}/modules.load
+
+  # Modules loaded in vendor_boot should not be loaded in vendor_dlkm.
+  if [ -f ${DIST_DIR}/vendor_boot.modules.load ]; then
+    local stripped_modules_load="$(mktemp)"
+    ! grep -x -v -F -f ${DIST_DIR}/vendor_boot.modules.load \
+      ${vendor_dlkm_modules_load} > ${stripped_modules_load}
+    mv -f ${stripped_modules_load} ${vendor_dlkm_modules_load}
+  fi
+
+  cp ${vendor_dlkm_modules_load} ${DIST_DIR}/vendor_dlkm.modules.load
   local vendor_dlkm_props_file
 
   if [ -z "${VENDOR_DLKM_PROPS}" ]; then
@@ -795,7 +809,7 @@
     done
   fi
   if [ "${TRIM_NONLISTED_KMI}" = "1" ]; then
-      # Create the raw symbol list 
+      # Create the raw symbol list
       cat ${ABI_SL} | \
               ${ROOT_DIR}/build/abi/flatten_symbol_list > \
               ${OUT_DIR}/abi_symbollist.raw
@@ -1024,6 +1038,7 @@
 
     MODULES_ROOT_DIR=$(echo ${INITRAMFS_STAGING_DIR}/lib/modules/*)
     cp ${MODULES_ROOT_DIR}/modules.load ${DIST_DIR}/modules.load
+    cp ${MODULES_ROOT_DIR}/modules.load ${DIST_DIR}/vendor_boot.modules.load
     echo "${MODULES_OPTIONS}" > ${MODULES_ROOT_DIR}/modules.options
 
     mkbootfs "${INITRAMFS_STAGING_DIR}" >"${MODULES_STAGING_DIR}/initramfs.cpio"