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_id2/dandelion:11/RP1A.200720.011/V12.5.6.0.RCZIDXM:user/release-keys");
ui_print("Target: Redmi/dandelion_id2/dandelion:11/RP1A.200720.011/V12.5.7.0.RCZIDXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_id2/dandelion:11/RP1A.200720.011/V12.5.6.0.RCZIDXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_id2/dandelion:11/RP1A.200720.011/V12.5.7.0.RCZIDXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_id2/dandelion:11/RP1A.200720.011/V12.5.6.0.RCZIDXM:user/release-keys or Redmi/dandelion_id2/dandelion:11/RP1A.200720.011/V12.5.7.0.RCZIDXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
patch_partition_check(concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9142a2760ffe73ab698b9c973170633b81130cc8"),
                      concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9a754d9bad1cdeffe1e2378d146da627ce6a1007")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9142a2760ffe73ab698b9c973170633b81130cc8"),"\" or \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9a754d9bad1cdeffe1e2378d146da627ce6a1007"),"\" has unexpected contents."));
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "76,1,31,44,55,17122,30067,32768,32785,48307,48309,48382,48383,48396,48397,48404,48405,48543,48544,48576,48577,48699,51583,55891,55892,55907,55908,56007,56008,59180,59181,60270,60271,60359,60360,60435,60436,63704,63705,63741,63742,63746,63747,63778,63779,64999,65000,65536,65551,67641,67642,69054,69055,69085,69086,69091,69092,69402,69404,69457,69458,69493,69494,69944,69945,70633,70634,70758,70759,75680,589834,589839,595422,597228,606693,606823,606824") == "8ef8bce230ae69aa70278058c9685929ff285706" || 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"), "76,1,31,44,55,17122,30067,32768,32785,48307,48309,48382,48383,48396,48397,48404,48405,48543,48544,48576,48577,48699,51583,55891,55892,55907,55908,56007,56008,59180,59181,60270,60271,60359,60360,60435,60436,63704,63705,63741,63742,63746,63747,63778,63779,64999,65000,65536,65551,67641,67642,69054,69055,69085,69086,69091,69092,69402,69404,69457,69458,69493,69494,69944,69945,70633,70634,70758,70759,75680,589834,589839,595422,597228,606693,606823,606824") && 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"), "28,1,41,44,45,48,49,52,53,56,60,63,64,67,68,71,72,75,76,385,391,544,98337,98338,114316,114663,116482,116522,116523") == "45d8732872c8b13e7ee2fe0d9d230ad4219ad068" || 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"), "28,1,41,44,45,48,49,52,53,56,60,63,64,67,68,71,72,75,76,385,391,544,98337,98338,114316,114663,116482,116522,116523") && 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"), "100,1,16,8701,8702,23316,23317,26420,26421,27096,27097,27459,27460,27472,27473,29313,29314,32768,32777,33337,33338,47727,47728,65536,65543,67232,67233,67240,67241,74590,74591,92236,92237,92291,92292,94710,94711,98304,98313,104677,104678,109794,109795,109802,109803,109809,109810,116108,116109,116117,116119,116122,116123,116124,116125,116127,116128,116139,116140,116143,116145,116146,116147,116148,116149,116160,116163,116612,116613,116984,116985,118470,118471,118561,118563,118566,118567,131072,131112,141400,141401,163840,163863,164767,164768,165430,165431,166924,166925,196083,196084,196608,196611,200969,200970,204807,204810,215236,218650,218707,218708") == "643193cb0d21a2eab5f817577005450748251bae" || 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"), "100,1,16,8701,8702,23316,23317,26420,26421,27096,27097,27459,27460,27472,27473,29313,29314,32768,32777,33337,33338,47727,47728,65536,65543,67232,67233,67240,67241,74590,74591,92236,92237,92291,92292,94710,94711,98304,98313,104677,104678,109794,109795,109802,109803,109809,109810,116108,116109,116117,116119,116122,116123,116124,116125,116127,116128,116139,116140,116143,116145,116146,116147,116148,116149,116160,116163,116612,116613,116984,116985,118470,118471,118561,118563,118566,118567,131072,131112,141400,141401,163840,163863,164767,164768,165430,165431,166924,166925,196083,196084,196608,196611,200969,200970,204807,204810,215236,218650,218707,218708") && 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:9142a2760ffe73ab698b9c973170633b81130cc8"),
                concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9a754d9bad1cdeffe1e2378d146da627ce6a1007"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9a754d9bad1cdeffe1e2378d146da627ce6a1007")));
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);
