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.2.0.RCZCNXM:user/release-keys");
ui_print("Target: Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.3.0.RCZCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.2.0.RCZCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.3.0.RCZCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.2.0.RCZCNXM:user/release-keys or Redmi/dandelion_cn/dandelion:11/RP1A.200720.011/V12.5.3.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:2a65cf9ce334d69b5bfcd3436099435d3f3e240e"),
                      concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:82426a9e457c52b28550f7387eb2f49369558ff4")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:2a65cf9ce334d69b5bfcd3436099435d3f3e240e"),"\" or \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:82426a9e457c52b28550f7387eb2f49369558ff4"),"\" has unexpected contents."));
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "472,1,26,39,50,32768,32780,40552,40554,40726,40727,40798,40799,40808,40809,40815,40816,40847,40849,42481,42482,42493,42494,42497,42498,42518,45286,48712,48713,48722,48723,48820,48821,54438,54439,54441,54442,54529,54530,54603,54604,57723,57724,57759,57760,58106,58107,59124,59125,61534,61535,62945,62946,65536,65546,69070,69072,73642,73643,73646,73647,74507,74509,74562,74563,74569,74570,74584,74585,75211,75212,75335,75336,82969,82970,85966,85967,87479,87480,87569,87570,87930,87931,88070,88071,95021,95022,96080,96081,98304,98316,110164,110165,114985,114986,116125,116126,116146,116149,118328,118331,122899,122900,123061,123062,123467,123470,124940,124941,125903,125906,127290,127291,131072,131082,131852,131853,133165,133166,133787,133788,137763,137766,139833,139836,142033,142034,142037,142040,146083,146084,146146,146147,146167,146170,147305,147308,147334,147335,147402,147403,147442,147443,147484,147485,147829,147830,148892,148893,148957,148958,149148,149149,149218,149219,149369,149370,163840,163852,163853,163854,166627,166628,166633,166634,171877,171880,171885,171886,172612,172613,172619,172620,177736,177737,182288,182289,185164,185165,185585,185586,196608,196618,207572,207574,220518,220520,220924,220925,220956,220957,220962,220963,221338,221339,221362,221363,224342,224343,224723,224724,225302,225305,225342,225343,225356,225359,226063,226064,227856,227857,228578,228581,229376,229388,232291,232292,246812,246813,247164,247167,249275,249276,249458,249459,252989,252990,253253,253254,254126,254127,256461,256462,258471,258475,258476,258477,258478,258479,258480,258481,258482,258483,258484,258485,258486,258488,260708,260709,262144,262154,265165,265166,267399,267400,274478,274479,283476,283477,288741,288742,294912,294924,309120,309121,316882,316883,322746,322747,327680,327690,335759,335760,339827,339828,352064,352065,353508,353509,356612,356613,360448,360458,367080,367081,380436,380437,385010,385011,392307,392308,393216,393226,412157,412158,417025,417026,422925,422926,424769,424770,425984,425994,428855,428856,440766,440767,458752,458762,485729,485730,491520,491530,494436,494437,496714,496715,498575,498576,503967,503968,504425,504426,504453,504454,504466,504467,504474,504475,504478,504479,504524,504525,504551,504552,504628,504629,504637,504638,504700,504701,504703,504708,504709,504710,504711,504712,504713,504714,505508,505509,505515,505516,506069,506071,506624,506626,506627,506628,506629,506630,506631,506632,506633,506634,506635,506636,506637,506638,506639,506640,506641,506644,508104,508106,508252,508253,508259,508261,508519,508520,508522,508524,509084,509085,509095,509096,509415,509416,509558,509559,509581,509583,510899,510900,524288,524298,524596,524597,532458,532459,534575,534576,534985,534986,539863,539864,553383,553893,553928,554217,554231,554233,557056,557066,569271,569272,572695,572696,574970,574971,575284,575286,575314,575315,575462,575463,580818,580819,588700,588701,589824,589834,594357,594358,598683,598685,614524,614525,622592,622608,626093,626622,626627,626823,655360,655376,688128,688159,694487,694488,720896,720944,731149,731150,753664,753690,771970,771971,786432,786481,794013,794014,795595,795598,803198,917509,917514,926808,929618,944348,944542,944543") == "bac7333f2126e77c07b23f92ff47814a9b3a5264" || 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"), "472,1,26,39,50,32768,32780,40552,40554,40726,40727,40798,40799,40808,40809,40815,40816,40847,40849,42481,42482,42493,42494,42497,42498,42518,45286,48712,48713,48722,48723,48820,48821,54438,54439,54441,54442,54529,54530,54603,54604,57723,57724,57759,57760,58106,58107,59124,59125,61534,61535,62945,62946,65536,65546,69070,69072,73642,73643,73646,73647,74507,74509,74562,74563,74569,74570,74584,74585,75211,75212,75335,75336,82969,82970,85966,85967,87479,87480,87569,87570,87930,87931,88070,88071,95021,95022,96080,96081,98304,98316,110164,110165,114985,114986,116125,116126,116146,116149,118328,118331,122899,122900,123061,123062,123467,123470,124940,124941,125903,125906,127290,127291,131072,131082,131852,131853,133165,133166,133787,133788,137763,137766,139833,139836,142033,142034,142037,142040,146083,146084,146146,146147,146167,146170,147305,147308,147334,147335,147402,147403,147442,147443,147484,147485,147829,147830,148892,148893,148957,148958,149148,149149,149218,149219,149369,149370,163840,163852,163853,163854,166627,166628,166633,166634,171877,171880,171885,171886,172612,172613,172619,172620,177736,177737,182288,182289,185164,185165,185585,185586,196608,196618,207572,207574,220518,220520,220924,220925,220956,220957,220962,220963,221338,221339,221362,221363,224342,224343,224723,224724,225302,225305,225342,225343,225356,225359,226063,226064,227856,227857,228578,228581,229376,229388,232291,232292,246812,246813,247164,247167,249275,249276,249458,249459,252989,252990,253253,253254,254126,254127,256461,256462,258471,258475,258476,258477,258478,258479,258480,258481,258482,258483,258484,258485,258486,258488,260708,260709,262144,262154,265165,265166,267399,267400,274478,274479,283476,283477,288741,288742,294912,294924,309120,309121,316882,316883,322746,322747,327680,327690,335759,335760,339827,339828,352064,352065,353508,353509,356612,356613,360448,360458,367080,367081,380436,380437,385010,385011,392307,392308,393216,393226,412157,412158,417025,417026,422925,422926,424769,424770,425984,425994,428855,428856,440766,440767,458752,458762,485729,485730,491520,491530,494436,494437,496714,496715,498575,498576,503967,503968,504425,504426,504453,504454,504466,504467,504474,504475,504478,504479,504524,504525,504551,504552,504628,504629,504637,504638,504700,504701,504703,504708,504709,504710,504711,504712,504713,504714,505508,505509,505515,505516,506069,506071,506624,506626,506627,506628,506629,506630,506631,506632,506633,506634,506635,506636,506637,506638,506639,506640,506641,506644,508104,508106,508252,508253,508259,508261,508519,508520,508522,508524,509084,509085,509095,509096,509415,509416,509558,509559,509581,509583,510899,510900,524288,524298,524596,524597,532458,532459,534575,534576,534985,534986,539863,539864,553383,553893,553928,554217,554231,554233,557056,557066,569271,569272,572695,572696,574970,574971,575284,575286,575314,575315,575462,575463,580818,580819,588700,588701,589824,589834,594357,594358,598683,598685,614524,614525,622592,622608,626093,626622,626627,626823,655360,655376,688128,688159,694487,694488,720896,720944,731149,731150,753664,753690,771970,771971,786432,786481,794013,794014,795595,795598,803198,917509,917514,926808,929618,944348,944542,944543") && 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"), "206,1,35,37,38,40,41,43,44,46,50,52,53,55,56,58,59,61,62,751,752,1715,1716,2594,2595,3683,3687,32768,32802,40657,40658,42017,42018,52212,52213,55138,55140,55188,55189,55252,55253,56787,56788,57691,57692,58974,58975,58981,58983,59644,59646,60067,60068,63972,63973,64004,64005,64014,64015,64028,64029,64063,64064,65505,65506,65536,65581,68313,68314,68635,68636,68966,68967,69296,69297,69603,69604,69912,69913,70220,70221,70526,70527,70858,70859,71190,71191,72217,72254,73037,73038,73366,73367,73693,73694,74030,74031,74365,74366,74707,74708,75046,75047,75386,75387,75716,75717,76021,76022,76326,76327,76631,76632,76936,76937,77241,77242,77544,77545,77851,77852,78158,78159,78467,78468,86769,86770,98304,98337,101079,101080,104831,104832,105509,105527,112823,112824,116837,116838,117647,117763,118586,118616,118618,118620,120537,120538,131072,131096,134291,134292,134308,134309,135200,135201,135329,136131,136167,137029,137419,137420,137666,137667,137975,137976,138282,138283,138614,138615,138945,138946,139277,139278,139609,139610,139944,139945,140273,140274,140360,140361,141172,141174,142551,142553,142603,142605,142611,142612,142630,142631,145232,145233,145235,145236,145238,145239,145241,148112,150061,150062,150571,150573,152465,154885,154930,154931") == "0357fdd7444400071af23ff8519480ca85f07b0c" || 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"), "206,1,35,37,38,40,41,43,44,46,50,52,53,55,56,58,59,61,62,751,752,1715,1716,2594,2595,3683,3687,32768,32802,40657,40658,42017,42018,52212,52213,55138,55140,55188,55189,55252,55253,56787,56788,57691,57692,58974,58975,58981,58983,59644,59646,60067,60068,63972,63973,64004,64005,64014,64015,64028,64029,64063,64064,65505,65506,65536,65581,68313,68314,68635,68636,68966,68967,69296,69297,69603,69604,69912,69913,70220,70221,70526,70527,70858,70859,71190,71191,72217,72254,73037,73038,73366,73367,73693,73694,74030,74031,74365,74366,74707,74708,75046,75047,75386,75387,75716,75717,76021,76022,76326,76327,76631,76632,76936,76937,77241,77242,77544,77545,77851,77852,78158,78159,78467,78468,86769,86770,98304,98337,101079,101080,104831,104832,105509,105527,112823,112824,116837,116838,117647,117763,118586,118616,118618,118620,120537,120538,131072,131096,134291,134292,134308,134309,135200,135201,135329,136131,136167,137029,137419,137420,137666,137667,137975,137976,138282,138283,138614,138615,138945,138946,139277,139278,139609,139610,139944,139945,140273,140274,140360,140361,141172,141174,142551,142553,142603,142605,142611,142612,142630,142631,145232,145233,145235,145236,145238,145239,145241,148112,150061,150062,150571,150573,152465,154885,154930,154931") && 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"), "116,1,34,39,40,42,43,17487,17488,19863,19864,26162,26163,26171,26173,26176,26177,26178,26179,26181,26182,26183,26184,26186,26189,26638,26639,27010,27011,28496,28497,28587,28589,28591,28592,28594,28595,28597,28598,28600,28601,28603,28604,28606,28607,28609,28610,28612,28613,28615,28616,28618,28619,28621,28622,28624,28625,28629,28630,28650,28651,28657,28658,28678,28679,28695,28696,28700,28701,28718,28719,28724,28725,28744,28745,28759,28760,28764,28765,28784,28785,28791,28792,28812,28813,28828,28829,28833,28834,28836,28837,28839,28840,28842,28843,28845,28846,28848,28849,28857,28858,28860,28861,28863,28864,28866,28867,28869,28870,28872,28874,28878,28879,29607,30079,30101,30102") == "ea977072f659ce2d1a30bbf95a5d7185c4017c93" || 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"), "116,1,34,39,40,42,43,17487,17488,19863,19864,26162,26163,26171,26173,26176,26177,26178,26179,26181,26182,26183,26184,26186,26189,26638,26639,27010,27011,28496,28497,28587,28589,28591,28592,28594,28595,28597,28598,28600,28601,28603,28604,28606,28607,28609,28610,28612,28613,28615,28616,28618,28619,28621,28622,28624,28625,28629,28630,28650,28651,28657,28658,28678,28679,28695,28696,28700,28701,28718,28719,28724,28725,28744,28745,28759,28760,28764,28765,28784,28785,28791,28792,28812,28813,28828,28829,28833,28834,28836,28837,28839,28840,28842,28843,28845,28846,28848,28849,28857,28858,28860,28861,28863,28864,28866,28867,28869,28870,28872,28874,28878,28879,29607,30079,30101,30102") && 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:2a65cf9ce334d69b5bfcd3436099435d3f3e240e"),
                concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:82426a9e457c52b28550f7387eb2f49369558ff4"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:82426a9e457c52b28550f7387eb2f49369558ff4")));
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);
