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/lancelot:10/QP1A.190711.020/V11.0.7.0.QJCCNXM:user/release-keys");
ui_print("Target: Redmi/lancelot/lancelot:10/QP1A.190711.020/V12.0.4.0.QJCCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lancelot/lancelot:10/QP1A.190711.020/V11.0.7.0.QJCCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lancelot/lancelot:10/QP1A.190711.020/V12.0.4.0.QJCCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lancelot/lancelot:10/QP1A.190711.020/V11.0.7.0.QJCCNXM:user/release-keys or Redmi/lancelot/lancelot:10/QP1A.190711.020/V12.0.4.0.QJCCNXM: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:ee6da9cb1e25e77757b7816767c796efae383cbc",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:f5bf14e96fa101f50ff0a80b89daad33afa1f02e") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:ee6da9cb1e25e77757b7816767c796efae383cbc\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:f5bf14e96fa101f50ff0a80b89daad33afa1f02e\" has unexpected contents.");
apply_patch_space(209747968) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:59032880:f3ff73201933d5184739fe6e417b2a7bfba3686e",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58972848:b20e9bac9b3f99f69ca452f47d10a49b0dea3d59") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59032880:f3ff73201933d5184739fe6e417b2a7bfba3686e\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58972848:b20e9bac9b3f99f69ca452f47d10a49b0dea3d59\" has unexpected contents.");
apply_patch_space(59032880) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "178,1,216,217,32770,32941,34669,34685,34687,34693,34694,34707,34711,34715,34716,34725,34729,34733,34734,34766,98306,98477,155134,155407,155409,155423,155494,155544,155546,155555,163842,164013,229378,229549,291032,291033,294914,295085,416166,416170,417277,417281,473262,473526,477625,477628,477656,477722,479490,479494,482945,482948,483082,483084,486210,486219,505831,505864,533918,537903,538508,538639,538772,538905,539749,539879,540012,540145,545152,546193,609150,609157,609652,609657,609662,609673,614314,614320,614323,614343,614379,614395,614396,614399,614409,614410,614412,614413,614415,614417,614419,614420,614422,614423,614431,614436,614439,614440,614465,614466,614473,614475,614481,614484,614486,614487,614496,614498,614513,614515,614516,614517,614532,614534,614594,614596,614612,614618,614620,614631,614667,614682,614684,614686,614696,614697,614699,614700,614702,614704,614706,614707,614710,614711,614718,614723,614733,614734,614762,614764,614770,614773,614786,614787,614803,614805,614832,614834,614838,614839,614863,614864,614881,614883,614899,614900,614909,614910,614935,614937,614983,614984,615005,615007,615019,615021,615422,615424,655373,655378,688129,688146,701622,701633,701639,703750,714902,715053,715054") == "9e821901e5ca21ced34b9d4506b504d2d8042306" || 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"), "178,1,216,217,32770,32941,34669,34685,34687,34693,34694,34707,34711,34715,34716,34725,34729,34733,34734,34766,98306,98477,155134,155407,155409,155423,155494,155544,155546,155555,163842,164013,229378,229549,291032,291033,294914,295085,416166,416170,417277,417281,473262,473526,477625,477628,477656,477722,479490,479494,482945,482948,483082,483084,486210,486219,505831,505864,533918,537903,538508,538639,538772,538905,539749,539879,540012,540145,545152,546193,609150,609157,609652,609657,609662,609673,614314,614320,614323,614343,614379,614395,614396,614399,614409,614410,614412,614413,614415,614417,614419,614420,614422,614423,614431,614436,614439,614440,614465,614466,614473,614475,614481,614484,614486,614487,614496,614498,614513,614515,614516,614517,614532,614534,614594,614596,614612,614618,614620,614631,614667,614682,614684,614686,614696,614697,614699,614700,614702,614704,614706,614707,614710,614711,614718,614723,614733,614734,614762,614764,614770,614773,614786,614787,614803,614805,614832,614834,614838,614839,614863,614864,614881,614883,614899,614900,614909,614910,614935,614937,614983,614984,615005,615007,615019,615021,615422,615424,655373,655378,688129,688146,701622,701633,701639,703750,714902,715053,715054") && 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"), "22,1,80,785,791,809,810,815,32770,32814,42786,45283,98306,98350,163842,163886,163907,163912,183465,184028,186948,186999,187000") == "835cdcda4aef8a84c57b211126ba2d9cd74b64cc" || 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"), "22,1,80,785,791,809,810,815,32770,32814,42786,45283,98306,98350,163842,163886,163907,163912,183465,184028,186948,186999,187000") && 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"), "10,1,32770,32782,32796,32797,50125,50278,51079,51104,51105") == "46b77221f66c550882f7a626202e47300a99ad9e" || 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"), "10,1,32770,32782,32796,32797,50125,50278,51079,51104,51105") && 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:ee6da9cb1e25e77757b7816767c796efae383cbc",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:f5bf14e96fa101f50ff0a80b89daad33afa1f02e",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:f5bf14e96fa101f50ff0a80b89daad33afa1f02e");
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:59032880:f3ff73201933d5184739fe6e417b2a7bfba3686e",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58972848:b20e9bac9b3f99f69ca452f47d10a49b0dea3d59",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:58972848:b20e9bac9b3f99f69ca452f47d10a49b0dea3d59");
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();
