trusty: Support directories as inputs to replace_ramdisk_modules.py

Add support for passing in a directory instead of an image file
to --kernel-ramdisk so the script can be used with the output
of `make modules_install` from a kernel build.

Bug: 364748249
Test: Used with https://r.android.com/3256316
Change-Id: Ica881c48a94bdc0ec990d521f19df8833e75c836
diff --git a/scripts/replace_ramdisk_modules/replace_ramdisk_modules.py b/scripts/replace_ramdisk_modules/replace_ramdisk_modules.py
index d9b2ac4..c799f8f 100644
--- a/scripts/replace_ramdisk_modules/replace_ramdisk_modules.py
+++ b/scripts/replace_ramdisk_modules/replace_ramdisk_modules.py
@@ -46,7 +46,8 @@
         required=True)
     parser.add_argument(
         '--kernel-ramdisk',
-        help='filename of ramdisk to extract kernel modules from',
+        help='filename of ramdisk to extract kernel modules from, '
+             'or the path of an existing directory containing the modules',
         required=True)
     parser.add_argument(
         '--output-ramdisk',
@@ -65,7 +66,19 @@
 # Based on system/tools/mkbootimg/repack_bootimg.py
 class RamdiskImage:
     """A class that supports packing/unpacking a ramdisk."""
-    def __init__(self, ramdisk_img, directory):
+    def __init__(self, ramdisk_img, directory, allow_dir):
+        # The caller gave us a directory instead of an image
+        # Assume it's already been extracted.
+        if os.path.isdir(ramdisk_img):
+            if not allow_dir:
+                raise RuntimeError(
+                    f"Directory not allowed for image {ramdisk_img}")
+
+            self._ramdisk_img = None
+            self._ramdisk_format = None
+            self._ramdisk_dir = ramdisk_img
+            return
+
         self._ramdisk_img = ramdisk_img
         self._ramdisk_format = None
         self._ramdisk_dir = directory
@@ -171,9 +184,11 @@
         kernel_ramdisk = os.path.join(tempdir, _KERNEL_RAMDISK_DIR)
         os.mkdir(kernel_ramdisk)
         android_ramdisk = RamdiskImage(
-            args.android_ramdisk, os.path.join(tempdir, _ANDROID_RAMDISK_DIR))
+            args.android_ramdisk, os.path.join(tempdir, _ANDROID_RAMDISK_DIR),
+            allow_dir=False)
         kernel_ramdisk = RamdiskImage(
-            args.kernel_ramdisk, os.path.join(tempdir, _KERNEL_RAMDISK_DIR))
+            args.kernel_ramdisk, os.path.join(tempdir, _KERNEL_RAMDISK_DIR),
+            allow_dir=True)
         _replace_modules(android_ramdisk, kernel_ramdisk)
         android_ramdisk.repack(args.output_ramdisk)