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/V12.0.9.0.QJOEUXM:user/release-keys");
ui_print("Target: Redmi/merlin_eea/merlin:10/QP1A.190711.020/V12.0.10.0.QJOEUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/merlin_eea/merlin:10/QP1A.190711.020/V12.0.9.0.QJOEUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/merlin_eea/merlin:10/QP1A.190711.020/V12.0.10.0.QJOEUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/merlin_eea/merlin:10/QP1A.190711.020/V12.0.9.0.QJOEUXM:user/release-keys or Redmi/merlin_eea/merlin:10/QP1A.190711.020/V12.0.10.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:28b239c437de61ab7c2e5532591cc3b957ed580d",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:62bc0ec1d1d24ce8fbcbb79aa4fa8ec7065c2750") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:28b239c437de61ab7c2e5532591cc3b957ed580d\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:62bc0ec1d1d24ce8fbcbb79aa4fa8ec7065c2750\" 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:58163104:08d594deea6a9caa2a50417b2f48999d3189abdd",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:1030e526aca9334af155fc691236cfceaa446dfd") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:08d594deea6a9caa2a50417b2f48999d3189abdd\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:1030e526aca9334af155fc691236cfceaa446dfd\" has unexpected contents.");
apply_patch_space(58163104) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "28,1,193,221,228,478,482,498,499,2227,32770,32927,98306,98463,163842,163999,229378,229535,293976,294269,294914,295071,622603,622612,641741,643687,653888,654027,654028") == "5fe5a2b4d1c841a6fec33dfa3e553129689ad8f3" || 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"), "28,1,193,221,228,478,482,498,499,2227,32770,32927,98306,98463,163842,163999,229378,229535,293976,294269,294914,295071,622603,622612,641741,643687,653888,654027,654028") && 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"), "78,1,78,815,821,839,840,845,846,848,849,1363,1364,1366,1367,1528,1529,1540,1544,1548,1549,1765,1766,1845,1846,1916,1917,2102,2103,2181,2182,2261,2262,2341,2342,3228,3229,5628,5629,6704,6708,6709,6711,6712,6714,8967,8969,8976,8978,14442,14443,22481,22482,23712,23714,24706,24707,24775,24776,32768,32770,32808,32859,39985,39986,40307,40308,49199,49200,51920,98306,98344,131101,131102,157588,158122,160630,160678,160679") == "8e7beacb4bb58d2911043eae83d6ff44a893ceac" || 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"), "78,1,78,815,821,839,840,845,846,848,849,1363,1364,1366,1367,1528,1529,1540,1544,1548,1549,1765,1766,1845,1846,1916,1917,2102,2103,2181,2182,2261,2262,2341,2342,3228,3229,5628,5629,6704,6708,6709,6711,6712,6714,8967,8969,8976,8978,14442,14443,22481,22482,23712,23714,24706,24707,24775,24776,32768,32770,32808,32859,39985,39986,40307,40308,49199,49200,51920,98306,98344,131101,131102,157588,158122,160630,160678,160679") && 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"), "200,1,94,5869,5870,11606,11610,14549,14552,17502,17503,18048,18054,18071,18072,24923,24924,32768,32770,32848,32854,33235,33236,36092,36093,36457,36461,36968,36969,36981,36985,36989,36990,38830,38834,40271,40272,44338,44342,48543,48544,56539,56540,56935,56936,65536,65542,65752,65753,74189,74190,83773,83774,94757,94758,98304,98306,98384,98390,103157,103158,110734,110735,110741,110742,110870,110874,110878,110879,126156,126157,131072,131078,142722,142723,145173,145174,151990,151991,163031,163032,163840,163842,163920,163926,166228,166232,166241,166242,166243,166244,166260,166261,166262,166263,166269,166270,166309,166313,166317,166319,166324,166327,166331,166333,166338,166339,166384,166385,166444,166447,166896,166897,167268,167269,168754,168755,168845,168847,168849,168850,168852,168853,168855,168856,168858,168859,168861,168862,168864,168865,168867,168868,168872,168873,168878,168879,169387,169389,173246,173247,176374,176376,193143,193144,196608,196643,202214,202216,202642,202644,210596,210597,215184,215186,215233,215235,215376,215378,216774,216776,217862,217864,217940,217942,227703,227705,229376,229378,229456,229495,234752,234753,250570,250572,253365,253367,255264,255265,255690,255692,262144,262165,277470,277471,294912,294914,294992,294997,305956,305957,305987,305991,305995,305996,309706,309709,320456,325536,325614,325615") == "fc65fec264f02ca353d100cca6d94239edc0b86d" || 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"), "200,1,94,5869,5870,11606,11610,14549,14552,17502,17503,18048,18054,18071,18072,24923,24924,32768,32770,32848,32854,33235,33236,36092,36093,36457,36461,36968,36969,36981,36985,36989,36990,38830,38834,40271,40272,44338,44342,48543,48544,56539,56540,56935,56936,65536,65542,65752,65753,74189,74190,83773,83774,94757,94758,98304,98306,98384,98390,103157,103158,110734,110735,110741,110742,110870,110874,110878,110879,126156,126157,131072,131078,142722,142723,145173,145174,151990,151991,163031,163032,163840,163842,163920,163926,166228,166232,166241,166242,166243,166244,166260,166261,166262,166263,166269,166270,166309,166313,166317,166319,166324,166327,166331,166333,166338,166339,166384,166385,166444,166447,166896,166897,167268,167269,168754,168755,168845,168847,168849,168850,168852,168853,168855,168856,168858,168859,168861,168862,168864,168865,168867,168868,168872,168873,168878,168879,169387,169389,173246,173247,176374,176376,193143,193144,196608,196643,202214,202216,202642,202644,210596,210597,215184,215186,215233,215235,215376,215378,216774,216776,217862,217864,217940,217942,227703,227705,229376,229378,229456,229495,234752,234753,250570,250572,253365,253367,255264,255265,255690,255692,262144,262165,277470,277471,294912,294914,294992,294997,305956,305957,305987,305991,305995,305996,309706,309709,320456,325536,325614,325615") && 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:28b239c437de61ab7c2e5532591cc3b957ed580d",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:62bc0ec1d1d24ce8fbcbb79aa4fa8ec7065c2750",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:62bc0ec1d1d24ce8fbcbb79aa4fa8ec7065c2750");
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("logo.bin", "/dev/block/platform/bootdevice/by-name/logo");
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:58163104:08d594deea6a9caa2a50417b2f48999d3189abdd",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:1030e526aca9334af155fc691236cfceaa446dfd",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:1030e526aca9334af155fc691236cfceaa446dfd");
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("scp.img", "/dev/block/platform/bootdevice/by-name/scp2");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk2");
package_extract_file("tee.img", "/dev/block/platform/bootdevice/by-name/tee2");
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("scp1", "scp2");
switch_active("lk", "lk2");
switch_active("tee1", "tee2");
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("scp.img", "/dev/block/platform/bootdevice/by-name/scp1");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk");
package_extract_file("tee.img", "/dev/block/platform/bootdevice/by-name/tee1");
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("scp2", "scp1");
switch_active("lk2", "lk");
switch_active("tee2", "tee1");
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();
