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_in2/dandelion:11/RP1A.200720.011/V12.5.9.0.RCZINXM:user/release-keys");
ui_print("Target: Redmi/dandelion_in2/dandelion:11/RP1A.200720.011/V12.5.12.0.RCZINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_in2/dandelion:11/RP1A.200720.011/V12.5.9.0.RCZINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_in2/dandelion:11/RP1A.200720.011/V12.5.12.0.RCZINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_in2/dandelion:11/RP1A.200720.011/V12.5.9.0.RCZINXM:user/release-keys or Redmi/dandelion_in2/dandelion:11/RP1A.200720.011/V12.5.12.0.RCZINXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
patch_partition_check(concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:4dd95300a0e4f6cbf1bbb1b26b707cfb442dac83"),
                      concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:b25ade7ba38d45039174beb1e6739242f6f12da4")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:4dd95300a0e4f6cbf1bbb1b26b707cfb442dac83"),"\" or \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:b25ade7ba38d45039174beb1e6739242f6f12da4"),"\" has unexpected contents."));
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "80,1,33,46,57,28770,30069,32768,32787,48311,48313,48386,48387,48400,48401,48408,48409,48547,48548,48580,48581,48703,51587,55895,55896,55911,55912,56011,56012,59184,59185,60274,60275,60363,60364,60439,60440,63708,63709,63745,63746,63750,63751,63782,63783,65003,65004,65536,65553,67647,67648,69060,69061,69091,69092,69097,69098,71124,71125,71435,71437,71490,71491,71526,71527,71977,71978,72666,72667,72791,72792,77713,77714,93618,524291,524305,543192,544822,553457,553577,553578") == "849fd37245c9788ac33245d6b991f5d279781642" || 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"), "80,1,33,46,57,28770,30069,32768,32787,48311,48313,48386,48387,48400,48401,48408,48409,48547,48548,48580,48581,48703,51587,55895,55896,55911,55912,56011,56012,59184,59185,60274,60275,60363,60364,60439,60440,63708,63709,63745,63746,63750,63751,63782,63783,65003,65004,65536,65553,67647,67648,69060,69061,69091,69092,69097,69098,71124,71125,71435,71437,71490,71491,71526,71527,71977,71978,72666,72667,72791,72792,77713,77714,93618,524291,524305,543192,544822,553457,553577,553578") && 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"), "130,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,23003,23004,25801,25802,26123,26124,26454,26455,26784,26785,27091,27092,27400,27401,27708,27709,28014,28015,28346,28347,28678,28679,30526,30527,30855,30856,31182,31183,31519,31520,31854,31855,32196,32197,32535,32536,32768,32827,32915,32916,33271,33272,33604,33605,33935,33936,34240,34241,34545,34546,34850,34851,35155,35156,35460,35461,35763,35764,36070,36071,36377,36378,36686,36687,36755,36769,44988,44989,56338,56642,59292,59293,62307,63172,65536,65580,73528,73529,76723,77032,77838,77839,78648,78774,81384,81385,96198,114323,114670,116489,116529,116530") == "4ad0a6beae7b950175cf6fc8cb36be65d25506a9" || 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"), "130,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,23003,23004,25801,25802,26123,26124,26454,26455,26784,26785,27091,27092,27400,27401,27708,27709,28014,28015,28346,28347,28678,28679,30526,30527,30855,30856,31182,31183,31519,31520,31854,31855,32196,32197,32535,32536,32768,32827,32915,32916,33271,33272,33604,33605,33935,33936,34240,34241,34545,34546,34850,34851,35155,35156,35460,35461,35763,35764,36070,36071,36377,36378,36686,36687,36755,36769,44988,44989,56338,56642,59292,59293,62307,63172,65536,65580,73528,73529,76723,77032,77838,77839,78648,78774,81384,81385,96198,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"), "104,1,15,8700,8701,12599,12600,27214,27215,30318,30319,30994,30995,32768,32776,34303,34304,34666,34667,34679,34680,36520,36521,50910,50911,65536,65542,70414,70415,91658,91659,91666,91667,98304,98312,99024,99025,116670,116671,116725,116726,119144,119145,131072,131078,131079,131080,134225,134226,134233,134234,134240,134241,140539,140540,140548,140550,140553,140554,140555,140556,140558,140559,140571,140572,140575,140577,140578,140579,140580,140581,140592,140593,140594,140597,141046,141047,141418,141419,142904,142905,163840,163874,167765,167766,196608,196640,203466,203467,204129,204130,205623,205624,229376,229384,234183,234184,239669,239670,243507,243510,254060,258089,258153,258154") == "b7c9b2a2186e089090659705bde89146075c7d64" || 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"), "104,1,15,8700,8701,12599,12600,27214,27215,30318,30319,30994,30995,32768,32776,34303,34304,34666,34667,34679,34680,36520,36521,50910,50911,65536,65542,70414,70415,91658,91659,91666,91667,98304,98312,99024,99025,116670,116671,116725,116726,119144,119145,131072,131078,131079,131080,134225,134226,134233,134234,134240,134241,140539,140540,140548,140550,140553,140554,140555,140556,140558,140559,140571,140572,140575,140577,140578,140579,140580,140581,140592,140593,140594,140597,141046,141047,141418,141419,142904,142905,163840,163874,167765,167766,196608,196640,203466,203467,204129,204130,205623,205624,229376,229384,234183,234184,239669,239670,243507,243510,254060,258089,258153,258154") && 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:4dd95300a0e4f6cbf1bbb1b26b707cfb442dac83"),
                concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:b25ade7ba38d45039174beb1e6739242f6f12da4"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:b25ade7ba38d45039174beb1e6739242f6f12da4")));
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);
