Merge "Remove libimgpatch"
diff --git a/install/adb_install.cpp b/install/adb_install.cpp
index 37280a3..ed66442 100644
--- a/install/adb_install.cpp
+++ b/install/adb_install.cpp
@@ -367,11 +367,13 @@
         "\n\nNow send the package you want to apply\n"
         "to the device with \"adb sideload <filename>\"...\n");
   } else {
-    ui->Print("\n\nWaiting for rescue commands...\n");
     command_map.emplace(MinadbdCommand::kWipeData, [&device]() {
       bool result = WipeData(device, false);
       return std::make_pair(result, true);
     });
+    command_map.emplace(MinadbdCommand::kNoOp, []() { return std::make_pair(true, true); });
+
+    ui->Print("\n\nWaiting for rescue commands...\n");
   }
 
   CreateMinadbdServiceAndExecuteCommands(ui, command_map, rescue_mode);
diff --git a/install/fuse_install.cpp b/install/fuse_install.cpp
index ffde4a3..8a7a278 100644
--- a/install/fuse_install.cpp
+++ b/install/fuse_install.cpp
@@ -128,11 +128,12 @@
 
   constexpr auto FUSE_BLOCK_SIZE = 65536;
   bool is_block_map = android::base::ConsumePrefix(&path, "@");
-  auto file_data_reader =
+  auto fuse_data_provider =
       is_block_map ? FuseBlockDataProvider::CreateFromBlockMap(std::string(path), FUSE_BLOCK_SIZE)
                    : FuseFileDataProvider::CreateFromFile(std::string(path), FUSE_BLOCK_SIZE);
 
-  if (!file_data_reader->Valid()) {
+  if (!fuse_data_provider || !fuse_data_provider->Valid()) {
+    LOG(ERROR) << "Failed to create fuse data provider.";
     return false;
   }
 
@@ -142,7 +143,7 @@
     umount2(SDCARD_ROOT, MNT_DETACH);
   }
 
-  return run_fuse_sideload(std::move(file_data_reader)) == 0;
+  return run_fuse_sideload(std::move(fuse_data_provider)) == 0;
 }
 
 InstallResult InstallWithFuseFromPath(std::string_view path, RecoveryUI* ui) {
diff --git a/minadbd/minadbd_services.cpp b/minadbd/minadbd_services.cpp
index 5792c7a..c31afbe 100644
--- a/minadbd/minadbd_services.cpp
+++ b/minadbd/minadbd_services.cpp
@@ -189,6 +189,14 @@
   if (!android::base::WriteFully(sfd, result.data(), result.size())) {
     exit(kMinadbdHostSocketIOError);
   }
+
+  // Send heartbeat signal to keep the rescue service alive.
+  if (!WriteCommandToFd(MinadbdCommand::kNoOp, minadbd_socket)) {
+    exit(kMinadbdSocketIOError);
+  }
+  if (MinadbdCommandStatus status; !WaitForCommandStatus(minadbd_socket, &status)) {
+    exit(kMinadbdMessageFormatError);
+  }
 }
 
 // Reboots into the given target. We don't reboot directly from minadbd, but going through recovery
diff --git a/minadbd/minadbd_types.h b/minadbd/minadbd_types.h
index 99fd45e..002523f 100644
--- a/minadbd/minadbd_types.h
+++ b/minadbd/minadbd_types.h
@@ -53,6 +53,7 @@
   kRebootRescue = 6,
   kWipeCache = 7,
   kWipeData = 8,
+  kNoOp = 9,
 
   // Last but invalid command.
   kError,
diff --git a/otautil/sysutil.cpp b/otautil/sysutil.cpp
index a882985..6cd46c6 100644
--- a/otautil/sysutil.cpp
+++ b/otautil/sysutil.cpp
@@ -38,7 +38,7 @@
 BlockMapData BlockMapData::ParseBlockMapFile(const std::string& block_map_path) {
   std::string content;
   if (!android::base::ReadFileToString(block_map_path, &content)) {
-    LOG(ERROR) << "Failed to read " << block_map_path;
+    PLOG(ERROR) << "Failed to read " << block_map_path;
     return {};
   }
 
diff --git a/recovery.cpp b/recovery.cpp
index b18a8e7..97ca0a5 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -84,6 +84,8 @@
  *
  * The arguments which may be supplied in the recovery.command file:
  *   --update_package=path - verify install an OTA package file
+ *   --install_with_fuse - install the update package with FUSE. This allows installation of large
+ *       packages on LP32 builds. Since the mmap will otherwise fail due to out of memory.
  *   --wipe_data - erase user data (and cache), then reboot
  *   --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user
  *       data (and cache), then reboot
@@ -577,6 +579,7 @@
   static constexpr struct option OPTIONS[] = {
     { "fastboot", no_argument, nullptr, 0 },
     { "fsck_unshare_blocks", no_argument, nullptr, 0 },
+    { "install_with_fuse", no_argument, nullptr, 0 },
     { "just_exit", no_argument, nullptr, 'x' },
     { "locale", required_argument, nullptr, 0 },
     { "prompt_and_wipe_data", no_argument, nullptr, 0 },
@@ -597,6 +600,7 @@
   };
 
   const char* update_package = nullptr;
+  bool install_with_fuse = false;  // memory map the update package by default.
   bool should_wipe_data = false;
   bool should_prompt_and_wipe_data = false;
   bool should_wipe_cache = false;
@@ -632,6 +636,8 @@
         std::string option = OPTIONS[option_index].name;
         if (option == "fsck_unshare_blocks") {
           fsck_unshare_blocks = true;
+        } else if (option == "install_with_fuse") {
+          install_with_fuse = true;
         } else if (option == "locale" || option == "fastboot") {
           // Handled in recovery_main.cpp
         } else if (option == "prompt_and_wipe_data") {
@@ -737,10 +743,24 @@
       } else {
         ensure_path_mounted(update_package);
       }
-      // TODO(xunchang) install package from fuse for large packages on ILP32 builds.
-      auto package = Package::CreateMemoryPackage(
-          update_package, std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
-      status = InstallPackage(package.get(), update_package, should_wipe_cache, retry_count, ui);
+
+      if (install_with_fuse) {
+        LOG(INFO) << "Installing package " << update_package << " with fuse";
+        status = InstallWithFuseFromPath(update_package, ui);
+      } else if (auto memory_package = Package::CreateMemoryPackage(
+                     update_package,
+                     std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
+                 memory_package != nullptr) {
+        status = InstallPackage(memory_package.get(), update_package, should_wipe_cache,
+                                retry_count, ui);
+      } else {
+        // We may fail to memory map the package on 32 bit builds for packages with 2GiB+ size.
+        // In such cases, we will try to install the package with fuse. This is not the default
+        // installation method because it introduces a layer of indirection from the kernel space.
+        LOG(WARNING) << "Failed to memory map package " << update_package
+                     << "; falling back to install with fuse";
+        status = InstallWithFuseFromPath(update_package, ui);
+      }
       if (status != INSTALL_SUCCESS) {
         ui->Print("Installation aborted.\n");
 
diff --git a/recovery_ui/include/recovery_ui/ui.h b/recovery_ui/include/recovery_ui/ui.h
index 797e2f0..a95f935 100644
--- a/recovery_ui/include/recovery_ui/ui.h
+++ b/recovery_ui/include/recovery_ui/ui.h
@@ -230,20 +230,23 @@
 
   bool InitScreensaver();
   void SetScreensaverState(ScreensaverState state);
+
   // Key event input queue
   std::mutex key_queue_mutex;
   std::condition_variable key_queue_cond;
   bool key_interrupted_;
   int key_queue[256], key_queue_len;
-  char key_pressed[KEY_MAX + 1];  // under key_queue_mutex
-  int key_last_down;              // under key_queue_mutex
-  bool key_long_press;            // under key_queue_mutex
-  int key_down_count;             // under key_queue_mutex
-  bool enable_reboot;             // under key_queue_mutex
-  int rel_sum;
 
+  // key press events
+  std::mutex key_press_mutex;
+  char key_pressed[KEY_MAX + 1];
+  int key_last_down;
+  bool key_long_press;
+  int key_down_count;
+  bool enable_reboot;
+
+  int rel_sum;
   int consecutive_power_keys;
-  int last_key;
 
   bool has_power_key;
   bool has_up_key;
diff --git a/recovery_ui/ui.cpp b/recovery_ui/ui.cpp
index cf0d3b5..98c654d 100644
--- a/recovery_ui/ui.cpp
+++ b/recovery_ui/ui.cpp
@@ -70,7 +70,6 @@
       key_down_count(0),
       enable_reboot(true),
       consecutive_power_keys(0),
-      last_key(-1),
       has_power_key(false),
       has_up_key(false),
       has_down_key(false),
@@ -346,7 +345,7 @@
   bool long_press = false;
 
   {
-    std::lock_guard<std::mutex> lg(key_queue_mutex);
+    std::lock_guard<std::mutex> lg(key_press_mutex);
     key_pressed[key_code] = updown;
     if (updown) {
       ++key_down_count;
@@ -393,7 +392,7 @@
   std::this_thread::sleep_for(750ms);  // 750 ms == "long"
   bool long_press = false;
   {
-    std::lock_guard<std::mutex> lg(key_queue_mutex);
+    std::lock_guard<std::mutex> lg(key_press_mutex);
     if (key_last_down == key_code && key_down_count == count) {
       long_press = key_long_press = true;
     }
@@ -518,13 +517,13 @@
 }
 
 bool RecoveryUI::IsKeyPressed(int key) {
-  std::lock_guard<std::mutex> lg(key_queue_mutex);
+  std::lock_guard<std::mutex> lg(key_press_mutex);
   int pressed = key_pressed[key];
   return pressed;
 }
 
 bool RecoveryUI::IsLongPress() {
-  std::lock_guard<std::mutex> lg(key_queue_mutex);
+  std::lock_guard<std::mutex> lg(key_press_mutex);
   bool result = key_long_press;
   return result;
 }
@@ -548,7 +547,7 @@
 
 RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) {
   {
-    std::lock_guard<std::mutex> lg(key_queue_mutex);
+    std::lock_guard<std::mutex> lg(key_press_mutex);
     key_long_press = false;
   }
 
@@ -585,13 +584,12 @@
     consecutive_power_keys = 0;
   }
 
-  last_key = key;
   return (IsTextVisible() || screensaver_state_ == ScreensaverState::OFF) ? ENQUEUE : IGNORE;
 }
 
 void RecoveryUI::KeyLongPress(int) {}
 
 void RecoveryUI::SetEnableReboot(bool enabled) {
-  std::lock_guard<std::mutex> lg(key_queue_mutex);
+  std::lock_guard<std::mutex> lg(key_press_mutex);
   enable_reboot = enabled;
 }
diff --git a/updater/target_files.cpp b/updater/target_files.cpp
index 2789683..919ec4e 100644
--- a/updater/target_files.cpp
+++ b/updater/target_files.cpp
@@ -201,7 +201,7 @@
     "SYSTEM_EXT/build.prop",
     "SYSTEM/vendor/build.prop",
     "SYSTEM/product/build.prop",
-    "SYSTEM/ext/build.prop",
+    "SYSTEM/system_ext/build.prop",
     "ODM/build.prop",  // legacy
     "ODM/etc/build.prop",
     "VENDOR/odm/build.prop",  // legacy