releasetools: squash backuptool support

This is a squash of the following commits:

Author: Chris Soyars <ctso@ctso.me>
Date:   Thu Dec 23 00:44:33 2010 +0100
    Add otapackage support for backuptool

    Change-Id: I512554c579d444067cd3ccbb0e6946a5eb6bc964a

    Modular backuptool.sh.  Executes backup and restore methods defined in arbitrary /system/addon.d/*.sh scripts.

    * Copy backuptool.functions alongside backuptool.sh.
    * Delete both from /system/bin as they are not useful there.

    Patch Series
    ============
    http://review.cyanogenmod.com/#change,13265
    CyanogenMod/android_build
      * edify generator
    http://review.cyanogenmod.com/#change,13266
    CyanogenMod/android_system_core
      * permissions on /system/addon.d
    http://review.cyanogenmod.com/#change,13267
    CyanogenMod/android_vendor_cm
      * 50-cm.sh reference backup script
      * modular backuptool.sh
      * support backuptool.functions used by /system/addon.d/*.sh scripts

    Change-Id: I26b4907d28f49c69627699d2accd2f0fa2d1b112

    update ota_from_target_files to handle mounting/unmounting for backupscript

    backupscript should not be mounting/unmounting itself as it makes other
    scripts have unexpected results (such as modelid_cfg, which expects /system
    to be mounted)

    instead have the ota script handle the mounting/unmounting

    Change-Id: I94511f4147c624d975cb3ecbeaa8b0e98f63437c

    build: Don't run backuptool on GMS builds

    Change-Id: I5dde27f9d16b88049171db9805221d92e67f3e5d

Author: Tom Marshall <tdm@cyngn.com>
Date:   Wed Apr 9 16:14:07 2014 -0700
    build: edify: use set_metadata for backuptool

    Also use saner permissions on backuptool.sh

    Change-Id: I50742b51867aa358f5924b8dc208833092a35bd9

Author: Ricardo Cerqueira <ricardo@cyngn.com>
Date:   Wed Nov 5 18:16:43 2014 +0000
    ota: Include copies of the recovery scripts even if shipping in block mode

Author: Ricardo Cerqueira <ricardo@cyngn.com>
Date:   Wed Nov 5 19:58:59 2014 +0000
    ota: Fix recovery-script cleanups in block mode

    Change-Id: Iadb480f8062cfb7a0e6c9024e32ac0d71a7481d7

Author: Arian <arian.kulmer@web.de>
Date:   Wed Jun 17 12:35:16 2020 +0200
    build: Disable backuptool on user instead of GMS builds

    * Backuptool is not exclusively used for GApps but
      also used by other things (i.e. Magisk), hence
      it shouldn't be disabled on all GMS builds.

    Change-Id: Ia95c6fed21d7bed5e2e0610aa94264edc1d02c80

Author: Davide Garberi <dade.garberi@gmail.com>
Date:   Tue Sep 24 18:45:17 2019 +0200
    releasetools: Move system mount handling to backuptool

    * This allows the ROM to be flashed on any recovery, whether it mounts
      system to /system, /system_root or /mnt/system like Q recovery

    * Remove any type of system mount with static paths from updater-script
      to figure out the path dinamically through backuptool

    Change-Id: I7b33726305cb6f3eec146d26135f0081002a25dc

Author: Jesse Chan <jc@lineageos.org>
Date:   Fri Jun 12 21:53:18 2020 +0800
    releasetools: support dynamic partitions for backuptool

    Change-Id: I4a04e52f64e307a9852d786aabf17975a020b4b8
    Signed-off-by: Jesse Chan <jc@lineageos.org>

Author: Jesse Chan <jc@lineageos.org>
Date:   Tue Jul 7 23:50:10 2020 +0800

    backuptool: do not unmap dynamic partition after finished

    Keep partitions mapped so users can install addons.

    Change-Id: Ic70621986bf136775e686dc8cc690ce076485907
    Signed-off-by: Jesse Chan <jc@lineageos.org>

Author: Alessandro Astone <ales.astone@gmail.com>
Date:   Mon Apr 12 18:40:10 2021 +0200

    backuptool: Map extra dynamic partitions for Addon.d V3

    Change-Id: Ia8fa1963c722c5aa2987b947e4e919e19909a9a8

Change-Id: I2401d334d8e16d18d3770b339ec2c787dd8e477c
diff --git a/core/Makefile b/core/Makefile
index e7164c6..eb652ae 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -6998,6 +6998,7 @@
     $(OTA_FROM_TARGET_FILES) \
         --verbose \
         --path $(HOST_OUT) \
+        --backup=$(backuptool) \
         $(if $(OEM_OTA_CONFIG), --oem_settings $(OEM_OTA_CONFIG)) \
         $(if $(BOOT_VAR_OTA_CONFIG), --boot_variable_file $(BOOT_VAR_OTA_CONFIG)) \
         $(2) \
@@ -7015,6 +7016,16 @@
 
 $(call declare-0p-target,$(INTERNAL_OTA_METADATA))
 
+ifeq ($(TARGET_BUILD_VARIANT),user)
+    $(INTERNAL_OTA_PACKAGE_TARGET): backuptool := false
+else
+ifneq ($(LINEAGE_BUILD),)
+    $(INTERNAL_OTA_PACKAGE_TARGET): backuptool := true
+else
+    $(INTERNAL_OTA_PACKAGE_TARGET): backuptool := false
+endif
+endif
+
 $(INTERNAL_OTA_PACKAGE_TARGET): KEY_CERT_PAIR := $(DEFAULT_KEY_CERT_PAIR)
 $(INTERNAL_OTA_PACKAGE_TARGET): .KATI_IMPLICIT_OUTPUTS := $(INTERNAL_OTA_METADATA)
 $(INTERNAL_OTA_PACKAGE_TARGET): $(BUILT_TARGET_FILES_DIR) $(OTA_FROM_TARGET_FILES) $(INTERNAL_OTATOOLS_FILES)
diff --git a/tools/releasetools/edify_generator.py b/tools/releasetools/edify_generator.py
index e02b2ae..8063c48 100644
--- a/tools/releasetools/edify_generator.py
+++ b/tools/releasetools/edify_generator.py
@@ -156,6 +156,20 @@
            ");")
     self.script.append(self.WordWrap(cmd))
 
+  def RunBackup(self, command, dynamic=False):
+    systemEntry = self.fstab["/system"]
+    if dynamic:
+      for p in ["vendor", "product", "system_ext"]:
+        fstabEntry = self.fstab.get("/"+p, None)
+        if fstabEntry:
+          self.script.append('map_partition("%s");' % (fstabEntry.device,))
+
+      self.script.append(('run_program("/tmp/install/bin/backuptool.sh", "%s", map_partition("%s"), "%s");' % (
+          command, systemEntry.device, systemEntry.fs_type)))
+    else:
+      self.script.append(('run_program("/tmp/install/bin/backuptool.sh", "%s", "%s", "%s");' % (
+          command, systemEntry.device, systemEntry.fs_type)))
+
   def ShowProgress(self, frac, dur):
     """Update the progress bar, advancing it over 'frac' over the next
     'dur' seconds.  'dur' may be zero to advance it via SetProgress
@@ -248,6 +262,12 @@
           p.mount_point, mount_flags))
       self.mounts.add(p.mount_point)
 
+  def Unmount(self, mount_point):
+    """Unmount the partition with the given mount_point."""
+    if mount_point in self.mounts:
+      self.mounts.remove(mount_point)
+      self.script.append('unmount("%s");' % (mount_point,))
+
   def UnpackPackageDir(self, src, dst):
     """Unpack a given directory from the OTA package into the given
     destination directory."""
diff --git a/tools/releasetools/non_ab_ota.py b/tools/releasetools/non_ab_ota.py
index 4b8c20f..debc88d 100644
--- a/tools/releasetools/non_ab_ota.py
+++ b/tools/releasetools/non_ab_ota.py
@@ -221,6 +221,9 @@
   script.SetPermissionsRecursive("/tmp/install", 0, 0, 0o755, 0o644, None, None)
   script.SetPermissionsRecursive("/tmp/install/bin", 0, 0, 0o755, 0o755, None, None)
 
+  if OPTIONS.backuptool:
+    script.RunBackup("backup", target_info.get('use_dynamic_partitions') == "true")
+
   # All other partitions as well as the data wipe use 10% of the progress, and
   # the update of the system partition takes the remaining progress.
   system_progress = 0.9 - (len(block_diff_dict) - 1) * 0.1
@@ -254,6 +257,10 @@
 
   device_specific.FullOTA_PostValidate()
 
+  if OPTIONS.backuptool:
+    script.ShowProgress(0.02, 10)
+    script.RunBackup("restore", target_info.get('use_dynamic_partitions') == "true")
+
   script.WriteRawImage("/boot", "boot.img")
 
   script.ShowProgress(0.1, 10)
diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py
index 985cd56..415b562 100755
--- a/tools/releasetools/ota_from_target_files.py
+++ b/tools/releasetools/ota_from_target_files.py
@@ -144,6 +144,10 @@
       that any changes made to the system partition are done using the new
       recovery (new kernel, etc.).
 
+  --backup <boolean>
+      Enable or disable the execution of backuptool.sh.
+      Disabled by default.
+
 A/B OTA specific options
 
   --disable_fec_computation
@@ -304,6 +308,7 @@
 if OPTIONS.worker_threads == 0:
   OPTIONS.worker_threads = 1
 OPTIONS.two_step = False
+OPTIONS.backuptool = False
 OPTIONS.include_secondary = False
 OPTIONS.block_based = True
 OPTIONS.updater_binary = None
@@ -1221,6 +1226,8 @@
                          "integers are allowed." % (a, o))
     elif o in ("-2", "--two_step"):
       OPTIONS.two_step = True
+    elif o == "--backup":
+      OPTIONS.backuptool = True
     elif o == "--include_secondary":
       OPTIONS.include_secondary = True
     elif o == "--no_signing":
@@ -1336,6 +1343,7 @@
                                  "extra_script=",
                                  "worker_threads=",
                                  "two_step",
+                                 "backup=",
                                  "include_secondary",
                                  "no_signing",
                                  "block",