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_cn/dandelion:11/RP1A.200720.011/V12.5.12.0.RCZCNXM:user/release-keys");
ui_print("Target: Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.13.0.RCZCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.12.0.RCZCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.13.0.RCZCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.12.0.RCZCNXM:user/release-keys or Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.13.0.RCZCNXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
patch_partition_check(concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:608fac773be320a45b309293aa29e464e3f9a869"),
                      concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:3f567c200aed6a1a4646a9337b1c9523737d5ce4")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:608fac773be320a45b309293aa29e464e3f9a869"),"\" or \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:3f567c200aed6a1a4646a9337b1c9523737d5ce4"),"\" has unexpected contents."));
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "20,1,29,42,53,1123,11344,11928,12683,26638,27269,27745,655366,655373,688129,688141,709669,711799,723078,723231,723232") == "3450aa90602ae5d8ab76049531f0469a965bf2e5" || 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"), "20,1,29,42,53,1123,11344,11928,12683,26638,27269,27745,655366,655373,688129,688141,709669,711799,723078,723231,723232") && 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"), "196,1,35,38,39,42,43,46,47,50,54,57,58,61,62,65,66,69,70,761,762,1725,1726,2579,2580,3694,3698,32768,32802,40669,40670,42029,42030,55242,55243,58759,58761,58809,58860,58873,58874,60408,60409,61312,61313,62595,62596,62603,62605,63266,63268,63689,63690,65536,65562,67620,67621,67652,67653,67662,67663,67676,67677,67711,67712,67784,67785,67845,67846,67847,67849,67867,67868,67929,67930,67977,67979,67980,67981,67982,67983,67984,67985,67988,67990,68248,68249,68256,68257,68284,68285,69090,69091,69092,69093,69126,69127,69130,69131,69177,69178,71975,71976,72297,72298,72628,72629,72958,72959,73265,73266,73574,73575,73882,73883,74188,74189,74520,74521,74852,74853,76700,76701,77029,77030,77356,77357,77693,77694,78028,78029,78370,78371,78709,78710,79049,79050,79386,79387,79719,79720,80050,80051,80355,80356,80660,80661,80965,80966,81270,81271,81575,81576,81878,81879,82185,82186,82492,82493,82801,82802,82870,82884,91103,91104,98304,98336,99759,99760,102486,102790,105440,105441,108455,109320,119632,119633,122827,123136,123942,123943,124752,124878,127488,127489,131017,131128,141418,141419,141435,141436,142330,160427,160919,163473,163519,163520") == "ded5443f526f87a474b03a690eca779a3fa0873d" || 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"), "196,1,35,38,39,42,43,46,47,50,54,57,58,61,62,65,66,69,70,761,762,1725,1726,2579,2580,3694,3698,32768,32802,40669,40670,42029,42030,55242,55243,58759,58761,58809,58860,58873,58874,60408,60409,61312,61313,62595,62596,62603,62605,63266,63268,63689,63690,65536,65562,67620,67621,67652,67653,67662,67663,67676,67677,67711,67712,67784,67785,67845,67846,67847,67849,67867,67868,67929,67930,67977,67979,67980,67981,67982,67983,67984,67985,67988,67990,68248,68249,68256,68257,68284,68285,69090,69091,69092,69093,69126,69127,69130,69131,69177,69178,71975,71976,72297,72298,72628,72629,72958,72959,73265,73266,73574,73575,73882,73883,74188,74189,74520,74521,74852,74853,76700,76701,77029,77030,77356,77357,77693,77694,78028,78029,78370,78371,78709,78710,79049,79050,79386,79387,79719,79720,80050,80051,80355,80356,80660,80661,80965,80966,81270,81271,81575,81576,81878,81879,82185,82186,82492,82493,82801,82802,82870,82884,91103,91104,98304,98336,99759,99760,102486,102790,105440,105441,108455,109320,119632,119633,122827,123136,123942,123943,124752,124878,127488,127489,131017,131128,141418,141419,141435,141436,142330,160427,160919,163473,163519,163520") && 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"), "118,1,34,41,42,45,46,17691,17692,17732,17733,20151,20152,26450,26451,26459,26461,26464,26465,26467,26468,26470,26471,26472,26473,26475,26478,26927,26928,27299,27300,28785,28786,28876,28878,28881,28882,28885,28886,28889,28890,28893,28894,28897,28898,28901,28902,28905,28906,28910,28911,28915,28916,28920,28921,28925,28926,28930,28931,28937,28938,28959,28960,28967,28968,28989,28990,29007,29008,29014,29015,29033,29034,29041,29042,29061,29062,29077,29078,29083,29084,29104,29105,29112,29113,29134,29135,29152,29153,29159,29160,29163,29164,29167,29168,29171,29172,29175,29176,29179,29180,29192,29193,29196,29197,29201,29202,29206,29207,29211,29212,29216,29218,29223,29224,29955,30434,30455,30456") == "f68e91d62b166b43646f2dfaa99a0e3969882614" || 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"), "118,1,34,41,42,45,46,17691,17692,17732,17733,20151,20152,26450,26451,26459,26461,26464,26465,26467,26468,26470,26471,26472,26473,26475,26478,26927,26928,27299,27300,28785,28786,28876,28878,28881,28882,28885,28886,28889,28890,28893,28894,28897,28898,28901,28902,28905,28906,28910,28911,28915,28916,28920,28921,28925,28926,28930,28931,28937,28938,28959,28960,28967,28968,28989,28990,29007,29008,29014,29015,29033,29034,29041,29042,29061,29062,29077,29078,29083,29084,29104,29105,29112,29113,29134,29135,29152,29153,29159,29160,29163,29164,29167,29168,29171,29172,29175,29176,29179,29180,29192,29193,29196,29197,29201,29202,29206,29207,29211,29212,29216,29218,29223,29224,29955,30434,30455,30456") && 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 ----


# --- Start patching dynamic partitions ---


# Update dynamic partition metadata

assert(update_dynamic_partitions(package_extract_file("dynamic_partitions_op_list")));

# Patch partition system

ui_print("Patching system image after verification.");
show_progress(0.700000, 0);
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.");

# Patch partition vendor

ui_print("Patching vendor image after verification.");
show_progress(0.100000, 0);
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 product

ui_print("Patching product image after verification.");
show_progress(0.100000, 0);
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.");

# --- End patching dynamic partitions ---

ui_print("Patching boot image...");
show_progress(0.100000, 10);
patch_partition(concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:608fac773be320a45b309293aa29e464e3f9a869"),
                concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:3f567c200aed6a1a4646a9337b1c9523737d5ce4"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:3f567c200aed6a1a4646a9337b1c9523737d5ce4")));
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("logo.bin", "/dev/block/platform/bootdevice/by-name/logo");
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();
set_progress(1.000000);
