extract_utils: Support images as a source

* Handles both sparse images as well as filesystem images
* Uses debugfs to extract, no root needed!
* Usecase: Factory images

Change-Id: I3e1bc3834dea108e63ab707a1a89dc2d0743913e
diff --git a/extract_utils.sh b/extract_utils.sh
index eaf8f1c..f06d3c6 100644
--- a/extract_utils.sh
+++ b/extract_utils.sh
@@ -1550,6 +1550,29 @@
         SRC="$DUMPDIR"
     fi
 
+    if [ -d "$SRC" ] && [ -f "$SRC"/system.img ]; then
+        DUMPDIR="$TMPDIR"/system_dump
+        mkdir -p "$DUMPDIR"
+
+        for PARTITION in "system" "odm" "product" "system_ext" "vendor"
+        do
+            echo "Extracting "$PARTITION""
+            local IMAGE="$SRC"/"$PARTITION".img
+            if [ -f "$IMAGE" ]; then
+                if [[ $(file -b "$IMAGE") == Linux* ]]; then
+                    extract_img_data "$IMAGE" "$DUMPDIR"/"$PARTITION"
+                elif [[ $(file -b "$IMAGE") == Android* ]]; then
+                    simg2img "$IMAGE" "$DUMPDIR"/"$PARTITION".raw
+                    extract_img_data "$DUMPDIR"/"$PARTITION".raw "$DUMPDIR"/"$PARTITION"/
+                else
+                    echo "Unsupported "$IMAGE""
+                fi
+            fi
+        done
+
+        SRC="$DUMPDIR"
+    fi
+
     if [ "$VENDOR_STATE" -eq "0" ]; then
         echo "Cleaning output directory ($OUTPUT_ROOT).."
         rm -rf "${OUTPUT_TMP:?}"