getprop("ro.product.device") == "dandelion" || abort("E3004: This package is for \"dandelion\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/dandelion_in/dandelion:10/QP1A.190711.020/V12.0.22.0.QCDINXM:user/release-keys");
ui_print("Target: Redmi/dandelion_in/dandelion:10/QP1A.190711.020/V12.0.23.0.QCDINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_in/dandelion:10/QP1A.190711.020/V12.0.22.0.QCDINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_in/dandelion:10/QP1A.190711.020/V12.0.23.0.QCDINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_in/dandelion:10/QP1A.190711.020/V12.0.22.0.QCDINXM:user/release-keys or Redmi/dandelion_in/dandelion:10/QP1A.190711.020/V12.0.23.0.QCDINXM: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:b4d51dcce2b65133d9f0b0459f4038945812374d",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:d94a75b291e02cdbed1b73414be5bd3719350d5b") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:b4d51dcce2b65133d9f0b0459f4038945812374d\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:d94a75b291e02cdbed1b73414be5bd3719350d5b\" has unexpected contents.");
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "26,1,168,195,202,387,391,407,408,419,32770,32903,98306,98439,163842,163975,229378,229511,294914,295047,524306,524307,547036,548702,557397,557519,557520") == "1c318d3ca6b8a1cbff196b3944c6a912f45be053" || 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"), "26,1,168,195,202,387,391,407,408,419,32770,32903,98306,98439,163842,163975,229378,229511,294914,295047,524306,524307,547036,548702,557397,557519,557520") && 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"), "10,1,32770,32795,98306,98331,105117,105449,107124,107160,107161") == "48806720bf7dfec9fc14fb28cf572f12246f264e" || 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"), "10,1,32770,32795,98306,98331,105117,105449,107124,107160,107161") && 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"), "20,1,77,5809,5811,8752,32770,32830,98306,98366,163842,163902,229378,229438,229442,229445,245905,246643,250554,250617,250618") == "c08d3bd36e9e75966936f5c8d5c2653e139b2b40" || 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"), "20,1,77,5809,5811,8752,32770,32830,98306,98366,163842,163902,229378,229438,229442,229445,245905,246643,250554,250617,250618") && 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:b4d51dcce2b65133d9f0b0459f4038945812374d",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:d94a75b291e02cdbed1b73414be5bd3719350d5b",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:d94a75b291e02cdbed1b73414be5bd3719350d5b");
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("md1img.img", "/dev/block/platform/bootdevice/by-name/md1img");
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("lk.img", "/dev/block/platform/bootdevice/by-name/lk2");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp2");
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("lk", "lk2");
switch_active("scp1", "scp2");
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("lk.img", "/dev/block/platform/bootdevice/by-name/lk");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp1");
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("lk2", "lk");
switch_active("scp2", "scp1");
switch_active("preloader2", "preloader");
),
ui_print("main loader images are already updated");
);
delete("/cache/recovery/last_mtupdate_stage");
package_extract_file("vbmeta_vendor.img", "/dev/block/platform/bootdevice/by-name/vbmeta_vendor");
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");
post_ota_action();
