releasetools: system_dlkm: add image to dist zip

Bug: 200082547
Test: Manual builds:
      1. Set BOARD_USES_SYSTEM_DLKM_PARTITION
      2. Set BOARD_PREBUILT_SYSTEM_DLKM_IMAGE to external image
      - Check that "m dist" put the system_dlkm image in
      out/dist/aosp_cf_x86_64_phone-img-eng.ramjiyani.zip
Signed-off-by: Ramji Jiyani <ramjiyani@google.com>
Change-Id: I8b7dd40e178db7fe542253bff19040095847fa57
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index 0c39827..f64a7d5 100644
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -761,6 +761,7 @@
   has_boot = OPTIONS.info_dict.get("no_boot") != "true"
   has_init_boot = OPTIONS.info_dict.get("init_boot") == "true"
   has_vendor_boot = OPTIONS.info_dict.get("vendor_boot") == "true"
+  has_system_dlkm = OPTIONS.info_dict.get("system_dlkm") == "true"
 
   # {vendor,odm,product,system_ext,vendor_dlkm,odm_dlkm, system, system_other}.img
   # can be built from source, or  dropped into target_files.zip as a prebuilt blob.
@@ -831,6 +832,19 @@
         if output_zip:
           init_boot_image.AddToZip(output_zip)
 
+  if has_system_dlkm:
+    banner("system_dlkm")
+    system_dlkm_image = common.GetSystemDlkmImage(
+        "IMAGES/system_dlkm.img", "system_dlkm.img", OPTIONS.input_tmp, "SYSTEM_DLKM")
+    if system_dlkm_image:
+      partitions['system_dlkm'] = os.path.join(OPTIONS.input_tmp, "IMAGES", "system_dlkm.img")
+      if not os.path.exists(partitions['system_dlkm']):
+        system_dlkm_image.WriteToDir(OPTIONS.input_tmp)
+        if output_zip:
+          system_dlkm_image.AddToZip(output_zip)
+    else:
+      logger.error("Couldn't locate system_dlkm.img; skipping...")
+
   if has_vendor_boot:
     banner("vendor_boot")
     vendor_boot_image = common.GetVendorBootImage(
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index e5c68bc..22c043a 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -1835,6 +1835,23 @@
     return File(name, data)
   return None
 
+def GetSystemDlkmImage(name, prebuilt_name, unpack_dir, tree_subdir,
+                       info_dict=None):
+  """Return a File object with the desired system dlkm image.
+  Look for it under 'unpack_dir'/IMAGES or 'unpack_dir'/PREBUILT_IMAGES.
+  """
+
+  prebuilt_path = os.path.join(unpack_dir, "IMAGES", prebuilt_name)
+  if os.path.exists(prebuilt_path):
+    logger.info("Using prebuilt %s from IMAGES...", prebuilt_name)
+    return File.FromLocalFile(name, prebuilt_path)
+
+  prebuilt_path = os.path.join(unpack_dir, "PREBUILT_IMAGES", prebuilt_name)
+  if os.path.exists(prebuilt_path):
+    logger.info("Using prebuilt %s from PREBUILT_IMAGES...", prebuilt_name)
+    return File.FromLocalFile(name, prebuilt_path)
+
+  return None
 
 def _BuildVendorBootImage(sourcedir, info_dict=None):
   """Build a vendor boot image from the specified sourcedir.