lpmake: Add a --sparse option for creating super partition images.

This change allows creating a sparse image of the super partition, which
can then be flashed using fastboot.

Bug: 79173901
Test: N/A
Change-Id: Ic49f843a47d5c222ce35ce1bda90fc8c0be578c8
diff --git a/partition_tools/lpmake.cc b/partition_tools/lpmake.cc
index c056cfb..d43a16c 100644
--- a/partition_tools/lpmake.cc
+++ b/partition_tools/lpmake.cc
@@ -45,6 +45,7 @@
             "  -o,--output=FILE              Output file.\n"
             "  --alignment-offset=N          Alignment offset in bytes to device parent.\n"
             "  --alignment=N                 Optimal partition alignment in bytes.\n"
+            "  --sparse                      Output a sparse image for fastboot.\n"
             "\n"
             "Partition format:\n"
             "  <name>:<guid>:<attributes>:<size>\n"
@@ -63,6 +64,7 @@
         { "help", no_argument, nullptr, 'h' },
         { "alignment-offset", required_argument, nullptr, 'O' },
         { "alignment", required_argument, nullptr, 'a' },
+        { "sparse", no_argument, nullptr, 'S' },
         { nullptr, 0, nullptr, 0 },
     };
 
@@ -73,6 +75,7 @@
     uint32_t alignment = kDefaultPartitionAlignment;
     std::string output_path;
     std::vector<std::string> partitions;
+    bool output_sparse = false;
 
     int rv;
     int index;
@@ -116,6 +119,9 @@
                     return EX_USAGE;
                 }
                 break;
+            case 'S':
+                output_sparse = true;
+                break;
             default:
                 break;
         }
@@ -190,9 +196,12 @@
     }
 
     std::unique_ptr<LpMetadata> metadata = builder->Export();
-    if (!WriteToImageFile(output_path.c_str(), *metadata.get())) {
+    if (output_sparse) {
+        if (!WriteToSparseFile(output_path.c_str(), *metadata.get())) {
+            return EX_CANTCREAT;
+        }
+    } else if (!WriteToImageFile(output_path.c_str(), *metadata.get())) {
         return EX_CANTCREAT;
     }
-
     return EX_OK;
 }