getprop("ro.product.device") == "lancelot" || abort("E3004: This package is for \"lancelot\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/lancelot_global/lancelot:10/QP1A.190711.020/V12.0.1.0.QJCMIXM:user/release-keys");
ui_print("Target: Redmi/lancelot_global/lancelot:10/QP1A.190711.020/V12.0.2.0.QJCMIXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lancelot_global/lancelot:10/QP1A.190711.020/V12.0.1.0.QJCMIXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lancelot_global/lancelot:10/QP1A.190711.020/V12.0.2.0.QJCMIXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lancelot_global/lancelot:10/QP1A.190711.020/V12.0.1.0.QJCMIXM:user/release-keys or Redmi/lancelot_global/lancelot:10/QP1A.190711.020/V12.0.2.0.QJCMIXM: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:01c89d030209608188dce2cb4131604a99124312",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:cbe7cb0b08c5465991f76e51a95e2e9d247cea81") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:01c89d030209608188dce2cb4131604a99124312\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:cbe7cb0b08c5465991f76e51a95e2e9d247cea81\" 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:59065360:cd5e5649beee161aa5452e1505747145732d10c7",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:59033568:4714135ba5bb1ba341af7b5f8c358a6beb2c3d1f") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59065360:cd5e5649beee161aa5452e1505747145732d10c7\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59033568:4714135ba5bb1ba341af7b5f8c358a6beb2c3d1f\" has unexpected contents.");
apply_patch_space(59065360) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "50,1,187,215,222,472,476,492,493,32768,32770,32920,32941,33975,33977,33992,33996,34000,34001,34161,34165,34173,34174,34187,34191,34195,34196,34208,34212,34216,34217,34249,58334,59021,59023,59047,98306,98456,163842,163992,229378,229528,294914,295064,589837,589845,614575,616438,626206,626341,626342") == "1af1edc10ec4dcd67aaa74a05dd41df50cddb1e5" || 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"), "50,1,187,215,222,472,476,492,493,32768,32770,32920,32941,33975,33977,33992,33996,34000,34001,34161,34165,34173,34174,34187,34191,34195,34196,34208,34212,34216,34217,34249,58334,59021,59023,59047,98306,98456,163842,163992,229378,229528,294914,295064,589837,589845,614575,616438,626206,626341,626342") && 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"), "76,1,76,813,819,837,838,843,844,846,847,1361,1362,1364,1365,1526,1527,1538,1542,1546,1547,1761,1762,1841,1842,1912,1913,2098,2099,2177,2178,2257,2258,2337,2338,3289,3290,4779,4780,7367,7368,7757,7924,8624,8628,8629,8631,8632,8634,10887,10889,10896,10898,15743,15744,22873,22874,23896,23898,23948,23997,24017,24018,29116,29117,30719,32770,32806,98306,98342,131101,131102,149358,149821,152198,152243,152244") == "4278c7adb68056dad219374e570dd14a93e4f7e6" || 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"), "76,1,76,813,819,837,838,843,844,846,847,1361,1362,1364,1365,1526,1527,1538,1542,1546,1547,1761,1762,1841,1842,1912,1913,2098,2099,2177,2178,2257,2258,2337,2338,3289,3290,4779,4780,7367,7368,7757,7924,8624,8628,8629,8631,8632,8634,10887,10889,10896,10898,15743,15744,22873,22874,23896,23898,23948,23997,24017,24018,29116,29117,30719,32770,32806,98306,98342,131101,131102,149358,149821,152198,152243,152244") && 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"), "198,1,85,5375,5376,9712,9716,11857,11860,14012,14013,14548,14554,14571,14572,20811,20812,23219,23220,23584,23588,24095,24096,24108,24112,24116,24117,25957,25961,27398,27399,31465,31469,32768,32770,32838,32845,35747,35748,42641,42642,43043,43044,50209,50210,58260,58261,65536,65543,70152,70153,70812,70813,78291,78292,84399,84400,84406,84407,98304,98306,98374,98381,99049,99050,114731,114732,116143,116144,122527,122528,129769,129770,131072,131079,132887,132891,132900,132901,132902,132903,132918,132919,132920,132921,132923,132925,132927,132928,132967,132971,132975,132977,132982,132985,132989,132991,132996,132997,133042,133043,133102,133105,133554,133555,133926,133927,135412,135413,135503,135505,135507,135508,135510,135511,135513,135514,135516,135517,135519,135520,135522,135523,135525,135526,135530,135531,135536,135537,135541,135542,135558,135559,135578,135579,135584,135585,135604,135605,135620,135621,135625,135626,135642,135643,136053,136055,157830,157831,163840,163842,163910,163941,164510,164512,167271,167275,167321,167322,167702,167706,167716,167717,173013,173014,176859,176860,177031,177035,177042,177043,177083,177087,177092,177093,177229,177233,178489,178491,179569,179571,179580,179648,181144,181146,184275,184277,192966,229378,229446,229451,229453,262145,262151,280350,281197,285656,285726,285727") == "c37f07d61e8ed3300f9fcc992445c563b6afc981" || 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"), "198,1,85,5375,5376,9712,9716,11857,11860,14012,14013,14548,14554,14571,14572,20811,20812,23219,23220,23584,23588,24095,24096,24108,24112,24116,24117,25957,25961,27398,27399,31465,31469,32768,32770,32838,32845,35747,35748,42641,42642,43043,43044,50209,50210,58260,58261,65536,65543,70152,70153,70812,70813,78291,78292,84399,84400,84406,84407,98304,98306,98374,98381,99049,99050,114731,114732,116143,116144,122527,122528,129769,129770,131072,131079,132887,132891,132900,132901,132902,132903,132918,132919,132920,132921,132923,132925,132927,132928,132967,132971,132975,132977,132982,132985,132989,132991,132996,132997,133042,133043,133102,133105,133554,133555,133926,133927,135412,135413,135503,135505,135507,135508,135510,135511,135513,135514,135516,135517,135519,135520,135522,135523,135525,135526,135530,135531,135536,135537,135541,135542,135558,135559,135578,135579,135584,135585,135604,135605,135620,135621,135625,135626,135642,135643,136053,136055,157830,157831,163840,163842,163910,163941,164510,164512,167271,167275,167321,167322,167702,167706,167716,167717,173013,173014,176859,176860,177031,177035,177042,177043,177083,177087,177092,177093,177229,177233,178489,178491,179569,179571,179580,179648,181144,181146,184275,184277,192966,229378,229446,229451,229453,262145,262151,280350,281197,285656,285726,285727") && 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:01c89d030209608188dce2cb4131604a99124312",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:cbe7cb0b08c5465991f76e51a95e2e9d247cea81",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:cbe7cb0b08c5465991f76e51a95e2e9d247cea81");
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:59065360:cd5e5649beee161aa5452e1505747145732d10c7",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:59033568:4714135ba5bb1ba341af7b5f8c358a6beb2c3d1f",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:59033568:4714135ba5bb1ba341af7b5f8c358a6beb2c3d1f");
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();
