getprop("ro.product.device") == "merlin" || abort("E3004: This package is for \"merlin\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/merlin_eea/merlin:10/QP1A.190711.020/V11.0.3.0.QJOEUXM:user/release-keys");
ui_print("Target: Redmi/merlin_eea/merlin:10/QP1A.190711.020/V11.0.4.0.QJOEUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/merlin_eea/merlin:10/QP1A.190711.020/V11.0.3.0.QJOEUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/merlin_eea/merlin:10/QP1A.190711.020/V11.0.4.0.QJOEUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/merlin_eea/merlin:10/QP1A.190711.020/V11.0.3.0.QJOEUXM:user/release-keys or Redmi/merlin_eea/merlin:10/QP1A.190711.020/V11.0.4.0.QJOEUXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
show_progress(0.100000, 10);
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:4e09cb3f5be302580e077d054fb19ea9454c2691",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:aa44f084c8b3f8b20d98553e7a807d64a1d89010") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:4e09cb3f5be302580e077d054fb19ea9454c2691\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:aa44f084c8b3f8b20d98553e7a807d64a1d89010\" has unexpected contents.");
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:57506544:4dbd6f67851cb2557415c8796a52c253b9b454d5",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:57512912:1de2949a7ff83466a9790d85b008b48a3970aab6") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:57506544:4dbd6f67851cb2557415c8796a52c253b9b454d5\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:57512912:1de2949a7ff83466a9790d85b008b48a3970aab6\" has unexpected contents.");
apply_patch_space(57506544) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "20,1,32770,32916,98306,98452,163842,163988,229378,229524,294914,295060,571921,571929,589836,589845,598863,600686,610204,610337,610338") == "134bdf064150195e76112d33672051f9ad1f1e83" || block_image_verify(map_partition("system"), package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat")) then
ui_print("Verified system image...");
else
check_first_block(map_partition("system"));
ifelse (block_image_recover(map_partition("system"), "20,1,32770,32916,98306,98452,163842,163988,229378,229524,294914,295060,571921,571929,589836,589845,598863,600686,610204,610337,610338") && block_image_verify(map_partition("system"), package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat"), ui_print("system recovered successfully."), abort("E1004: system partition fails to recover"));
endif;
if (range_sha1(map_partition("vendor"), "52,1,78,815,821,839,840,845,846,1360,1361,1363,1364,1524,1525,1536,1540,1544,1545,1761,1762,1841,1842,1912,1913,2098,2099,2177,2178,2257,2258,2337,2338,3224,3225,4714,4715,7465,7466,7887,32770,32812,98306,98348,163842,163884,163906,163910,171938,172473,175210,175258,175259") == "e38bd2067c206a48fb7904ea0c298fa3bf3a933e" || block_image_verify(map_partition("vendor"), package_extract_file("vendor.transfer.list"), "vendor.new.dat", "vendor.patch.dat")) then
ui_print("Verified vendor image...");
else
check_first_block(map_partition("vendor"));
ifelse (block_image_recover(map_partition("vendor"), "52,1,78,815,821,839,840,845,846,1360,1361,1363,1364,1524,1525,1536,1540,1544,1545,1761,1762,1841,1842,1912,1913,2098,2099,2177,2178,2257,2258,2337,2338,3224,3225,4714,4715,7465,7466,7887,32770,32812,98306,98348,163842,163884,163906,163910,171938,172473,175210,175258,175259") && block_image_verify(map_partition("vendor"), package_extract_file("vendor.transfer.list"), "vendor.new.dat", "vendor.patch.dat"), ui_print("vendor recovered successfully."), abort("E2004: vendor partition fails to recover"));
endif;
if (range_sha1(map_partition("product"), "28,1,81,5371,5372,9457,9461,11456,11459,13464,14028,14045,32770,32834,98306,98370,163842,163906,229378,229442,229448,229449,261449,262144,262145,262241,266399,266466,266467") == "d93871f8f4b1741f89f119bdc01176ecba65fc26" || block_image_verify(map_partition("product"), package_extract_file("product.transfer.list"), "product.new.dat", "product.patch.dat")) then
ui_print("Verified product image...");
else
ifelse (block_image_recover(map_partition("product"), "28,1,81,5371,5372,9457,9461,11456,11459,13464,14028,14045,32770,32834,98306,98370,163842,163906,229378,229442,229448,229449,261449,262144,262145,262241,266399,266466,266467") && block_image_verify(map_partition("product"), package_extract_file("product.transfer.list"), "product.new.dat", "product.patch.dat"), ui_print("product recovered successfully."), abort("E2004: product partition fails to recover"));
endif;

# ---- start making changes here ----

ui_print("Patching boot image...");
show_progress(0.100000, 10);
patch_partition("EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:4e09cb3f5be302580e077d054fb19ea9454c2691",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:aa44f084c8b3f8b20d98553e7a807d64a1d89010",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:aa44f084c8b3f8b20d98553e7a807d64a1d89010");
show_progress(0.600000, 600);

# --- Start patching dynamic partitions ---


# Update dynamic partition metadata

assert(update_dynamic_partitions(package_extract_file("dynamic_partitions_op_list")));

# Patch partition product

ui_print("Patching product image after verification.");
block_image_update(map_partition("product"), package_extract_file("product.transfer.list"), "product.new.dat", "product.patch.dat") ||
  abort("E2001: Failed to update product image.");

# Patch partition vendor

ui_print("Patching vendor image after verification.");
block_image_update(map_partition("vendor"), package_extract_file("vendor.transfer.list"), "vendor.new.dat", "vendor.patch.dat") ||
  abort("E2001: Failed to update vendor image.");

# Patch partition system

ui_print("Patching system image after verification.");
block_image_update(map_partition("system"), package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat") ||
  abort("E1001: Failed to update system image.");

# --- End patching dynamic partitions ---

show_progress(0.100000, 2);
show_mtupdate_stage("/cache/recovery/last_mtupdate_stage");
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "1") ,
(
ui_print("start to update general image");
package_extract_file("dtbo.img", "/dev/block/platform/bootdevice/by-name/dtbo");
package_extract_file("exaid.img", "/dev/block/platform/bootdevice/by-name/exaid");
ui_print("Patching md1img image...");
show_progress(0.100000, 10);
patch_partition("EMMC:/dev/block/platform/bootdevice/by-name/md1img:57506544:4dbd6f67851cb2557415c8796a52c253b9b454d5",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:57512912:1de2949a7ff83466a9790d85b008b48a3970aab6",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:57512912:1de2949a7ff83466a9790d85b008b48a3970aab6");
package_extract_file("spmfw.img", "/dev/block/platform/bootdevice/by-name/spmfw");
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "1");
),
ui_print("general images are already updated");
);
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "3") ,
(
if less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "2") then

ui_print("start to update alt loader image");
package_extract_file("sspm.img", "/dev/block/platform/bootdevice/by-name/sspm_2");
package_extract_file("tee.img", "/dev/block/platform/bootdevice/by-name/tee2");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp2");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk2");
if get_storage_type() then 
 assert(package_extract_file("preloader_ufs.img", "/dev/block/mmcblk0boot1"));
else 
 assert(set_emmc_writable("/sys/block/mmcblk0boot1/force_ro"),
        package_extract_file("preloader_emmc.img", "/dev/block/mmcblk0boot1"));
endif;
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "2");
endif;

switch_active("sspm_1", "sspm_2");
switch_active("tee1", "tee2");
switch_active("scp1", "scp2");
switch_active("lk", "lk2");
switch_active("preloader", "preloader2");
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "3");
),
ui_print("alt loder images are already updated");
);
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "5") ,
(
if less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "4") then

ui_print("start to update main loader image");
package_extract_file("sspm.img", "/dev/block/platform/bootdevice/by-name/sspm_1");
package_extract_file("tee.img", "/dev/block/platform/bootdevice/by-name/tee1");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp1");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk");
if get_storage_type() then 
 assert(package_extract_file("preloader_ufs.img", "/dev/block/mmcblk0boot0"));
else 
 assert(set_emmc_writable("/sys/block/mmcblk0boot0/force_ro"),
        package_extract_file("preloader_emmc.img", "/dev/block/mmcblk0boot0"));
endif;
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "4");
endif;

switch_active("sspm_2", "sspm_1");
switch_active("tee2", "tee1");
switch_active("scp2", "scp1");
switch_active("lk2", "lk");
switch_active("preloader2", "preloader");
),
ui_print("main loader images are already updated");
);
delete("/cache/recovery/last_mtupdate_stage");
package_extract_file("vbmeta_system.img", "/dev/block/platform/bootdevice/by-name/vbmeta_system");
package_extract_file("vbmeta.img", "/dev/block/platform/bootdevice/by-name/vbmeta");
package_extract_file("vbmeta_vendor.img", "/dev/block/platform/bootdevice/by-name/vbmeta_vendor");
post_ota_action();
