Merge tag 'android-15.0.0_r5' of https://android.googlesource.com/platform/system/update_engine into HEAD

Android 15.0.0 release 5

Change-Id: Ibdd1b557854579636db54fa1679b3f11dcc76cd6

# -----BEGIN PGP SIGNATURE-----
#
# iF0EABECAB0WIQRDQNE1cO+UXoOBCWTorT+BmrEOeAUCZyvepAAKCRDorT+BmrEO
# eHKEAJ4lCCoIBOWeCe3R6S0HmLqoOo+TyQCdEYUSvtd0TwskFpy/W4BZMG8Rucc=
# =g2ei
# -----END PGP SIGNATURE-----
# gpg: Signature faite le mer 06 nov 2024 16:24:52 EST
# gpg:                avec la clef DSA 4340D13570EF945E83810964E8AD3F819AB10E78
# gpg: Impossible de vérifier la signature : Pas de clef publique
diff --git a/Android.bp b/Android.bp
index 0cbf286..92c993b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -208,6 +208,7 @@
         "libfec",
         "liblz4",
         "libziparchive",
+        "libselinux",
     ],
 }
 
@@ -290,6 +291,7 @@
         "ue_defaults",
         "libpayload_consumer_exports",
         "libpayload_consumer_defaults",
+        "omni_weekly_build_defaults",
     ],
 }
 
@@ -299,6 +301,7 @@
         "ue_defaults",
         "libpayload_consumer_exports_proto-full",
         "libpayload_consumer_defaults",
+        "omni_weekly_build_defaults",
     ],
 }
 
diff --git a/payload_consumer/postinstall_runner_action.cc b/payload_consumer/postinstall_runner_action.cc
index bfdd39e..e7804ea 100644
--- a/payload_consumer/postinstall_runner_action.cc
+++ b/payload_consumer/postinstall_runner_action.cc
@@ -19,6 +19,7 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <selinux/selinux.h>
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -180,6 +181,75 @@
   EnsureUnmounted();
 
 #ifdef __ANDROID__
+#ifdef USE_WEEKLY_BUILD
+  // Check the currently installed /system partition to see if it's ever
+  // been mounted R/W. If it has, we'll run backuptool scripts for it
+  // since we can safely assume something on the partition has been
+  // changed and we won't be breaking verity (since it's already been
+  // broken). If it hasn't ever been mounted R/W, we can assume that
+  // the rom that the user is upgrading to will have everything they
+  // need and no addon.d scripts will need to be run to retain stuff
+  // after the upgrade.
+  //
+  // Use the following disk layout info to make the determination
+  // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
+  // Super block starts from block 0, offset 0x400
+  //   0x2C: len32 Mount time
+  //   0x30: len32 Write time
+  //   0x34: len16 Number of mounts since the last fsck
+  //   0x38: len16 Magic signature 0xEF53
+
+  string source_path;
+
+  if (install_plan_.source_slot != BootControlInterface::kInvalidSlot) {
+    boot_control_->GetPartitionDevice(partition.name, install_plan_.source_slot, &source_path);
+  }
+
+  uint16_t mount_count = 0;
+
+  if (!source_path.empty()) {
+    brillo::Blob chunk;
+
+    utils::ReadFileChunk(source_path, 0x400 + 0x34, sizeof(uint16_t), &chunk);
+    mount_count = *reinterpret_cast<uint16_t*>(chunk.data());
+  }
+
+  LOG(INFO) << source_path << " has been mounted R/W " << mount_count << " times.";
+
+  if (mount_count > 0) {
+    if (!utils::SetBlockDeviceReadOnly(mountable_device, false)) {
+      LOG(ERROR) << "Error marking the device " << mountable_device << " writeable.";
+      return false;
+    }
+    // Mount the target partition R/W
+    LOG(INFO) << "Running backuptool scripts";
+    utils::MountFilesystem(mountable_device, fs_mount_dir_, MS_NOATIME | MS_NODEV | MS_NODIRATIME,
+                           partition.filesystem_type, "seclabel");
+
+    // Switch to a permissive domain
+    if (setexeccon("u:r:backuptool:s0")) {
+      LOG(ERROR) << "Failed to set backuptool context";
+      return false;
+    }
+
+    // Run backuptool script
+    int ret = system("/postinstall/system/bin/backuptool_postinstall.sh");
+    if (ret == -1 || WEXITSTATUS(ret) != 0) {
+      LOG(ERROR) << "Backuptool postinstall step failed. ret=" << ret;
+    }
+
+    // Switch back to update_engine domain
+    if (setexeccon(nullptr)) {
+      LOG(ERROR) << "Failed to set update_engine context";
+      return false;
+    }
+  } else {
+    LOG(INFO) << "Skipping backuptool scripts";
+  }
+
+  utils::UnmountFilesystem(fs_mount_dir_);
+#endif  // USE_WEEKLY_BUILD
+
   // In Chromium OS, the postinstall step is allowed to write to the block
   // device on the target image, so we don't mark it as read-only and should
   // be read-write since we just wrote to it during the update.
diff --git a/update_engine.rc b/update_engine.rc
index 45f05af..4f497a7 100644
--- a/update_engine.rc
+++ b/update_engine.rc
@@ -3,7 +3,7 @@
     class late_start
     user root
     group root system wakelock inet cache media_rw
-    task_profiles OtaProfiles
+    task_profiles ProcessCapacityMax HighIoPriority MaxPerformance
     disabled
 
 on property:ro.boot.slot_suffix=*