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_eea2/dandelion:11/RP1A.200720.011/V12.5.7.0.RCZEUXM:user/release-keys");
ui_print("Target: Redmi/dandelion_eea2/dandelion:11/RP1A.200720.011/V12.5.8.0.RCZEUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_eea2/dandelion:11/RP1A.200720.011/V12.5.7.0.RCZEUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_eea2/dandelion:11/RP1A.200720.011/V12.5.8.0.RCZEUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_eea2/dandelion:11/RP1A.200720.011/V12.5.7.0.RCZEUXM:user/release-keys or Redmi/dandelion_eea2/dandelion:11/RP1A.200720.011/V12.5.8.0.RCZEUXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
patch_partition_check(concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:52b67282016567d56729acf6ce56604e439ce9ba"),
                      concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:fd3cfa359f7ef3b50676ef79ad8994d152ffc14a")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:52b67282016567d56729acf6ce56604e439ce9ba"),"\" or \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:fd3cfa359f7ef3b50676ef79ad8994d152ffc14a"),"\" has unexpected contents."));
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "14,1,32,45,56,102,10323,16492,557060,557072,561031,562715,571633,571755,571756") == "98d56e1e1653c0932b8cac391efc814026d5c668" || 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"), "14,1,32,45,56,102,10323,16492,557060,557072,561031,562715,571633,571755,571756") && 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"), "134,1,41,44,45,48,49,52,53,56,60,63,64,67,68,71,72,75,76,767,768,1731,1732,2585,2586,3700,3706,3712,3714,11527,11528,12646,12648,12696,12747,12760,12761,21632,21633,22953,22954,23004,23005,25802,25803,26124,26125,26455,26456,26785,26786,27092,27093,27401,27402,27709,27710,28015,28016,28347,28348,28679,28680,30527,30528,30856,30857,31183,31184,31520,31521,31855,31856,32197,32198,32536,32537,32768,32827,32916,32917,33272,33273,33605,33606,33936,33937,34241,34242,34546,34547,34851,34852,35156,35157,35461,35462,35764,35765,36071,36072,36378,36379,36687,36688,36756,36770,44989,44990,56339,56643,59293,59294,62308,63173,65536,65580,73529,73530,76724,77033,77839,77840,78649,78775,81385,81386,84914,84999,96199,114323,114670,116489,116529,116530") == "29548eeed399859167e8d2b761db18ae38861b1a" || 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"), "134,1,41,44,45,48,49,52,53,56,60,63,64,67,68,71,72,75,76,767,768,1731,1732,2585,2586,3700,3706,3712,3714,11527,11528,12646,12648,12696,12747,12760,12761,21632,21633,22953,22954,23004,23005,25802,25803,26124,26125,26455,26456,26785,26786,27092,27093,27401,27402,27709,27710,28015,28016,28347,28348,28679,28680,30527,30528,30856,30857,31183,31184,31520,31521,31855,31856,32197,32198,32536,32537,32768,32827,32916,32917,33272,33273,33605,33606,33936,33937,34241,34242,34546,34547,34851,34852,35156,35157,35461,35462,35764,35765,36071,36072,36378,36379,36687,36688,36756,36770,44989,44990,56339,56643,59293,59294,62308,63173,65536,65580,73529,73530,76724,77033,77839,77840,78649,78775,81385,81386,84914,84999,96199,114323,114670,116489,116529,116530") && 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"), "90,1,15,6504,6505,15190,15191,29805,29806,32768,32776,32917,32918,36218,36219,36581,36582,36594,36595,38435,38436,42450,42451,56840,56841,65536,65542,76344,76345,97588,97589,97596,97597,98304,98312,104954,104955,122600,122601,122655,122656,125074,125075,131072,131078,135038,135039,140155,140156,140163,140164,140170,140171,146469,146470,146478,146480,146488,146489,146490,146491,146493,146494,146506,146507,146510,146512,146513,146514,146515,146516,146528,146529,146530,146533,146982,146983,147354,147355,148840,148841,163840,163874,173701,173702,180298,259765,260551,264681,264749,264750") == "b587a621bbec1dd498cda659d0c292cd2675d07e" || 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"), "90,1,15,6504,6505,15190,15191,29805,29806,32768,32776,32917,32918,36218,36219,36581,36582,36594,36595,38435,38436,42450,42451,56840,56841,65536,65542,76344,76345,97588,97589,97596,97597,98304,98312,104954,104955,122600,122601,122655,122656,125074,125075,131072,131078,135038,135039,140155,140156,140163,140164,140170,140171,146469,146470,146478,146480,146488,146489,146490,146491,146493,146494,146506,146507,146510,146512,146513,146514,146515,146516,146528,146529,146530,146533,146982,146983,147354,147355,148840,148841,163840,163874,173701,173702,180298,259765,260551,264681,264749,264750") && 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 ---


# 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.");

# 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 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:52b67282016567d56729acf6ce56604e439ce9ba"),
                concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:fd3cfa359f7ef3b50676ef79ad8994d152ffc14a"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:fd3cfa359f7ef3b50676ef79ad8994d152ffc14a")));
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);
