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_id/lancelot:10/QP1A.190711.020/V12.0.1.0.QJCIDXM:user/release-keys");
ui_print("Target: Redmi/lancelot_id/lancelot:10/QP1A.190711.020/V12.0.2.0.QJCIDXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lancelot_id/lancelot:10/QP1A.190711.020/V12.0.1.0.QJCIDXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lancelot_id/lancelot:10/QP1A.190711.020/V12.0.2.0.QJCIDXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lancelot_id/lancelot:10/QP1A.190711.020/V12.0.1.0.QJCIDXM:user/release-keys or Redmi/lancelot_id/lancelot:10/QP1A.190711.020/V12.0.2.0.QJCIDXM: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:fdda59342f0d98fa3b98b1dfd83ca8a51110e10c",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:ff61ab1d492f8773cfb40163b145a20bc37ed178") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:fdda59342f0d98fa3b98b1dfd83ca8a51110e10c\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:ff61ab1d492f8773cfb40163b145a20bc37ed178\" has unexpected contents.");
apply_patch_space(70172672) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:59066208:2c5230c300cee0ddde60addcb5df3131f4996676",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:59070448:92d0b56c43904fcbb8875ef8ee88753538d4b1cc") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59066208:2c5230c300cee0ddde60addcb5df3131f4996676\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59070448:92d0b56c43904fcbb8875ef8ee88753538d4b1cc\" has unexpected contents.");
apply_patch_space(59066208) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "18,1,32770,32935,98306,98471,163842,164007,229378,229543,294914,295079,655378,655379,674321,676366,687085,687229,687230") == "e98a486fab2ddff5ea56ffe49fc9fad9b367c2e0" || 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"), "18,1,32770,32935,98306,98471,163842,164007,229378,229543,294914,295079,655378,655379,674321,676366,687085,687229,687230") && 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"), "62,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,5830,6451,7367,7368,8624,8628,8629,8631,8632,8634,10887,10889,10896,32770,32806,98306,98342,131101,131102,149410,149873,152252,152295,152296") == "ac71d70c4e6218a264e65b662973085a039f71f7" || 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"), "62,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,5830,6451,7367,7368,8624,8628,8629,8631,8632,8634,10887,10889,10896,32770,32806,98306,98342,131101,131102,149410,149873,152252,152295,152296") && 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"), "126,1,77,4413,4417,6558,6561,8713,8714,9249,9255,9272,9273,15512,15513,16152,16156,16926,16927,17291,17295,17802,17803,17815,17819,17823,17824,19664,19668,21105,21106,25172,25176,29377,29378,32768,32770,32830,32837,36336,36337,36742,36743,43908,43909,51959,51960,63847,63848,64504,64505,64511,64512,65536,65543,79084,79085,94766,94767,96178,96179,98304,98306,98366,98373,102630,102631,109873,109874,112984,112988,112997,112998,112999,113000,113014,113015,113016,113017,113023,113024,113062,113066,113071,113074,113079,113080,113125,113126,113185,113188,113637,113638,114009,114010,116116,116118,131072,131102,133861,133862,143204,143206,143632,143634,143815,143817,143864,143866,144007,144009,145265,145267,146345,146347,146356,163842,163902,229378,229438,229443,229445,247251,247998,251930,251995,251996") == "7282e11e1155080e2bb016bb04008ce623a6d1b2" || 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"), "126,1,77,4413,4417,6558,6561,8713,8714,9249,9255,9272,9273,15512,15513,16152,16156,16926,16927,17291,17295,17802,17803,17815,17819,17823,17824,19664,19668,21105,21106,25172,25176,29377,29378,32768,32770,32830,32837,36336,36337,36742,36743,43908,43909,51959,51960,63847,63848,64504,64505,64511,64512,65536,65543,79084,79085,94766,94767,96178,96179,98304,98306,98366,98373,102630,102631,109873,109874,112984,112988,112997,112998,112999,113000,113014,113015,113016,113017,113023,113024,113062,113066,113071,113074,113079,113080,113125,113126,113185,113188,113637,113638,114009,114010,116116,116118,131072,131102,133861,133862,143204,143206,143632,143634,143815,143817,143864,143866,144007,144009,145265,145267,146345,146347,146356,163842,163902,229378,229438,229443,229445,247251,247998,251930,251995,251996") && 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:fdda59342f0d98fa3b98b1dfd83ca8a51110e10c",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:ff61ab1d492f8773cfb40163b145a20bc37ed178",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:ff61ab1d492f8773cfb40163b145a20bc37ed178");
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:59066208:2c5230c300cee0ddde60addcb5df3131f4996676",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:59070448:92d0b56c43904fcbb8875ef8ee88753538d4b1cc",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:59070448:92d0b56c43904fcbb8875ef8ee88753538d4b1cc");
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();
