Move UnpackBootImage to boot_image_unpacker.cc

Bug: 123592422
Test: Ran aosp_cf_x86_phone-userdebug
Change-Id: I980cc712e272a97dcc2b9dce321d662f89a4c244
diff --git a/host/commands/launch/boot_image_unpacker.cc b/host/commands/launch/boot_image_unpacker.cc
index 5558029..e830646 100644
--- a/host/commands/launch/boot_image_unpacker.cc
+++ b/host/commands/launch/boot_image_unpacker.cc
@@ -100,4 +100,26 @@
                      path);
 }
 
+bool BootImageUnpacker::Unpack(const std::string& ramdisk_image_path,
+                               const std::string& kernel_image_path) {
+  if (HasRamdiskImage()) {
+    if (!ExtractRamdiskImage(ramdisk_image_path)) {
+      LOG(ERROR) << "Error extracting ramdisk from boot image";
+      return false;
+    }
+  }
+  if (!kernel_image_path.empty()) {
+    if (HasKernelImage()) {
+      if (!ExtractKernelImage(kernel_image_path)) {
+        LOG(ERROR) << "Error extracting kernel from boot image";
+        return false;
+      }
+    } else {
+      LOG(ERROR) << "No kernel found on boot image";
+      return false;
+    }
+  }
+  return true;
+}
+
 }  // namespace cvd
diff --git a/host/commands/launch/boot_image_unpacker.h b/host/commands/launch/boot_image_unpacker.h
index 69fc7bd..05fe671 100644
--- a/host/commands/launch/boot_image_unpacker.h
+++ b/host/commands/launch/boot_image_unpacker.h
@@ -45,6 +45,9 @@
   // as root.
   bool ExtractRamdiskImage(const std::string& path) const;
 
+  bool Unpack(const std::string& ramdisk_image_path,
+              const std::string& kernel_image_path);
+
  private:
   BootImageUnpacker(SharedFD boot_image, const std::string& cmdline,
                     uint32_t kernel_image_size, uint32_t kernel_image_offset,
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index 41c6294..2a2dcac 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -470,30 +470,6 @@
   return true;
 }
 
-bool UnpackBootImage(const cvd::BootImageUnpacker& boot_image_unpacker,
-                     const vsoc::CuttlefishConfig& config) {
-  if (boot_image_unpacker.HasRamdiskImage()) {
-    if (!boot_image_unpacker.ExtractRamdiskImage(
-            config.ramdisk_image_path())) {
-      LOG(ERROR) << "Error extracting ramdisk from boot image";
-      return false;
-    }
-  }
-  if (!FLAGS_kernel_path.size()) {
-    if (boot_image_unpacker.HasKernelImage()) {
-      if (!boot_image_unpacker.ExtractKernelImage(
-              config.kernel_image_path())) {
-        LOG(ERROR) << "Error extracting kernel from boot image";
-        return false;
-      }
-    } else {
-      LOG(ERROR) << "No kernel found on boot image";
-      return false;
-    }
-  }
-  return true;
-}
-
 template<typename S, typename T>
 static std::string concat(const S& s, const T& t) {
   std::ostringstream os;
@@ -537,9 +513,11 @@
   tmp_config_obj.set_device_title(FLAGS_device_title);
   if (FLAGS_kernel_path.size()) {
     tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
+    tmp_config_obj.set_use_unpacked_kernel(false);
   } else {
     tmp_config_obj.set_kernel_image_path(
         tmp_config_obj.PerInstancePath("kernel"));
+    tmp_config_obj.set_use_unpacked_kernel(true);
   }
 
   auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
@@ -957,7 +935,10 @@
     return LauncherExitCodes::kInvalidHostConfiguration;
   }
 
-  if (!UnpackBootImage(*boot_img_unpacker, *config)) {
+  if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
+                                 config->use_unpacked_kernel()
+                                     ? config->kernel_image_path()
+                                     : "")) {
     LOG(ERROR) << "Failed to unpack boot image";
     return LauncherExitCodes::kBootImageUnpackError;
   }
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index ec6e588..ce3412f 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -78,6 +78,7 @@
 const char* kRefreshRateHz = "refresh_rate_hz";
 
 const char* kKernelImagePath = "kernel_image_path";
+const char* kUseUnpackedKernel = "use_unpacked_kernel";
 const char* kGdbFlag = "gdb_flag";
 const char* kKernelCmdline = "kernel_cmdline";
 const char* kRamdiskImagePath = "ramdisk_image_path";
@@ -203,6 +204,14 @@
   SetPath(kKernelImagePath, kernel_image_path);
 }
 
+bool CuttlefishConfig::use_unpacked_kernel() const {
+  return (*dictionary_)[kUseUnpackedKernel].asBool();
+}
+
+void CuttlefishConfig::set_use_unpacked_kernel(bool use_unpacked_kernel) {
+  (*dictionary_)[kUseUnpackedKernel] = use_unpacked_kernel;
+}
+
 std::string CuttlefishConfig::gdb_flag() const {
   return (*dictionary_)[kGdbFlag].asString();
 }
diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h
index 9a9fa53..0a4cb25 100644
--- a/host/libs/config/cuttlefish_config.h
+++ b/host/libs/config/cuttlefish_config.h
@@ -84,6 +84,9 @@
   std::string kernel_image_path() const;
   void set_kernel_image_path(const std::string& kernel_image_path);
 
+  bool use_unpacked_kernel() const;
+  void set_use_unpacked_kernel(bool use_unpacked_kernel);
+
   std::set<std::string> kernel_cmdline() const;
   void set_kernel_cmdline(const std::set<std::string>& kernel_cmdline);
   void add_kernel_cmdline(const std::string& arg);