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_global2/dandelion:11/RP1A.200720.011/V12.5.15.0.RCZMIXM:user/release-keys");
ui_print("Target: Redmi/dandelion_global2/dandelion:11/RP1A.200720.011/V12.5.16.0.RCZMIXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/dandelion_global2/dandelion:11/RP1A.200720.011/V12.5.15.0.RCZMIXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/dandelion_global2/dandelion:11/RP1A.200720.011/V12.5.16.0.RCZMIXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/dandelion_global2/dandelion:11/RP1A.200720.011/V12.5.15.0.RCZMIXM:user/release-keys or Redmi/dandelion_global2/dandelion:11/RP1A.200720.011/V12.5.16.0.RCZMIXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
patch_partition_check(concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:c36d93bf2f2ff8226675c1bddbfad0c1d479d576"),
                      concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9b2ae04f4b11dcb7055179cfac6323720aff73e6")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:c36d93bf2f2ff8226675c1bddbfad0c1d479d576"),"\" or \"",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9b2ae04f4b11dcb7055179cfac6323720aff73e6"),"\" has unexpected contents."));
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "748,1,33,46,57,17124,28772,32768,32787,52938,52940,53013,53014,53027,53028,53035,53036,53181,53182,53214,53215,53337,56221,60529,60530,60545,60546,60645,60646,63818,63819,64908,64909,64997,64998,65073,65074,65536,65553,68359,68360,68396,68397,68401,68402,68433,68434,69654,69655,72281,72282,73694,73695,73725,73726,73731,73732,74042,74044,74097,74098,74133,74134,74584,74585,75273,75274,75398,75399,80320,80321,81262,81263,86230,86231,93276,93277,95927,95928,98304,98323,104753,104754,106344,106345,131072,131089,134146,134147,136800,136801,139796,139797,140948,140949,141091,141092,147232,147233,148292,148293,155849,155850,157158,157159,157181,157184,160780,160783,163840,163859,165078,165079,165484,165487,166956,166957,168245,168248,170998,170999,173581,173582,177916,177919,180023,180024,180029,180032,183348,183349,183352,183353,183368,183371,184862,184865,184934,184935,184974,184976,186608,186609,186651,186652,186997,186998,188734,188735,188926,188927,189010,189011,189018,189019,189025,189026,190689,190690,193839,193840,194261,194262,194269,194270,194276,194277,194301,194302,196608,196625,198133,198134,198447,198448,200592,200593,200641,200642,201368,201369,202893,202896,205267,205268,205585,205586,208382,208383,208646,208647,209519,209520,211854,211855,213863,213867,213868,213869,213870,213871,213872,213873,213874,213875,213876,213877,213878,213879,213880,213881,213882,213883,213884,213885,213886,213887,213888,213889,213890,213891,213892,213893,213894,213895,213896,213897,213898,213899,213900,213901,213902,213903,213904,213905,213906,213907,213908,213909,213910,213911,213912,213913,213914,213915,213916,213917,213918,213919,213920,213921,213922,213923,213924,213925,213926,213927,213928,213929,213930,213931,213932,213933,213934,213935,213936,213937,213938,213939,213940,213941,213942,213943,213944,213945,213946,213947,213948,213949,213950,213951,213952,213953,213954,213955,213956,213957,213958,213959,213960,213961,213962,213963,213964,213965,213966,213967,213968,213969,213970,213971,213972,213973,213974,213975,213976,213977,213978,213979,213980,213981,213982,213983,213984,213985,213986,213987,213988,213989,213990,213991,213992,213993,213994,213995,213996,213997,213998,213999,214000,214001,214002,214003,214004,214005,214006,214007,214008,214009,214010,214011,214012,214013,214014,214015,214016,214017,214018,214019,214020,214021,214022,214023,214024,214025,214026,214027,214028,214029,214030,214031,214032,214033,214034,214035,214036,214037,214038,214039,214040,214041,214042,214043,214044,214045,214046,214047,214048,214049,214050,214051,214052,214053,214054,214055,214056,214057,214058,214059,214060,214061,214062,214063,214064,214065,214066,214067,214068,214069,214070,214071,214072,214073,214074,214075,214076,214077,214078,214079,214080,214081,214082,214083,214084,214085,214086,214087,214088,214089,214090,214091,214092,214093,214094,214095,214096,214097,214098,214099,214100,214101,214102,214103,214104,214105,214106,214107,214108,214109,214110,214111,214112,214113,214114,214115,214116,214117,214118,214119,214120,214121,214122,214123,214124,214125,214126,214127,214128,214129,214130,214131,214132,214133,214134,214135,214136,214137,214138,214139,214140,214141,214142,214143,214144,214145,214146,214147,214148,214149,214150,214151,214152,214153,214154,214155,214156,214157,214158,214159,214160,214161,214162,214163,214164,214165,214166,214167,214168,214169,214170,214171,214172,214173,214174,214175,214176,214177,214178,214179,214180,214181,214182,214183,214184,214185,214186,214187,214188,214189,214190,214191,214192,214193,214194,214195,214196,214197,214198,214199,214200,214201,214202,214203,214204,214205,214206,214207,214208,214209,214210,214211,214212,214213,214214,214215,214216,214217,214218,214219,214220,214221,214222,214223,214224,214225,214226,214227,214228,214229,214230,214231,214232,214233,214234,214235,214236,214237,214238,214239,214240,214241,214242,214243,214244,214245,214246,214247,214248,214249,214250,214251,214252,214253,214254,214255,214256,214257,214258,214259,214260,214261,214262,214263,214264,214265,214266,214267,214268,214269,214270,214271,214272,214273,214274,214275,214276,214277,214278,214279,214280,214281,214282,214283,214284,214285,214286,214287,214288,214289,214290,214291,214292,214293,214294,214295,214296,214297,214298,214299,214300,214301,214302,214303,214304,214305,214306,214307,214308,214309,214310,214311,214312,214314,217956,217957,218056,218301,218326,218328,218333,218356,218368,218369,218376,218377,218380,218381,218420,218421,218428,218429,218455,218456,218532,218535,218539,218540,218541,218542,218613,218614,218616,218621,218622,218623,218624,218625,218626,218627,219550,219551,219558,219559,220112,220113,220114,220116,220669,220671,220672,220673,220674,220675,220676,220677,220678,220679,220680,220681,220682,220683,220684,220685,220687,220688,220689,220692,222017,222019,222152,222153,222165,222166,222172,222174,222424,222425,222427,222429,222989,222990,223004,223005,223147,223148,223171,223173,223175,223176,229376,229395,232235,232236,240106,240107,242171,242172,242582,242634,242639,246051,246056,524289,524305,542621,544249,552875,552994,552995") == "df52f12455572be241b26145134e8494ca5c53ba" || 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"), "748,1,33,46,57,17124,28772,32768,32787,52938,52940,53013,53014,53027,53028,53035,53036,53181,53182,53214,53215,53337,56221,60529,60530,60545,60546,60645,60646,63818,63819,64908,64909,64997,64998,65073,65074,65536,65553,68359,68360,68396,68397,68401,68402,68433,68434,69654,69655,72281,72282,73694,73695,73725,73726,73731,73732,74042,74044,74097,74098,74133,74134,74584,74585,75273,75274,75398,75399,80320,80321,81262,81263,86230,86231,93276,93277,95927,95928,98304,98323,104753,104754,106344,106345,131072,131089,134146,134147,136800,136801,139796,139797,140948,140949,141091,141092,147232,147233,148292,148293,155849,155850,157158,157159,157181,157184,160780,160783,163840,163859,165078,165079,165484,165487,166956,166957,168245,168248,170998,170999,173581,173582,177916,177919,180023,180024,180029,180032,183348,183349,183352,183353,183368,183371,184862,184865,184934,184935,184974,184976,186608,186609,186651,186652,186997,186998,188734,188735,188926,188927,189010,189011,189018,189019,189025,189026,190689,190690,193839,193840,194261,194262,194269,194270,194276,194277,194301,194302,196608,196625,198133,198134,198447,198448,200592,200593,200641,200642,201368,201369,202893,202896,205267,205268,205585,205586,208382,208383,208646,208647,209519,209520,211854,211855,213863,213867,213868,213869,213870,213871,213872,213873,213874,213875,213876,213877,213878,213879,213880,213881,213882,213883,213884,213885,213886,213887,213888,213889,213890,213891,213892,213893,213894,213895,213896,213897,213898,213899,213900,213901,213902,213903,213904,213905,213906,213907,213908,213909,213910,213911,213912,213913,213914,213915,213916,213917,213918,213919,213920,213921,213922,213923,213924,213925,213926,213927,213928,213929,213930,213931,213932,213933,213934,213935,213936,213937,213938,213939,213940,213941,213942,213943,213944,213945,213946,213947,213948,213949,213950,213951,213952,213953,213954,213955,213956,213957,213958,213959,213960,213961,213962,213963,213964,213965,213966,213967,213968,213969,213970,213971,213972,213973,213974,213975,213976,213977,213978,213979,213980,213981,213982,213983,213984,213985,213986,213987,213988,213989,213990,213991,213992,213993,213994,213995,213996,213997,213998,213999,214000,214001,214002,214003,214004,214005,214006,214007,214008,214009,214010,214011,214012,214013,214014,214015,214016,214017,214018,214019,214020,214021,214022,214023,214024,214025,214026,214027,214028,214029,214030,214031,214032,214033,214034,214035,214036,214037,214038,214039,214040,214041,214042,214043,214044,214045,214046,214047,214048,214049,214050,214051,214052,214053,214054,214055,214056,214057,214058,214059,214060,214061,214062,214063,214064,214065,214066,214067,214068,214069,214070,214071,214072,214073,214074,214075,214076,214077,214078,214079,214080,214081,214082,214083,214084,214085,214086,214087,214088,214089,214090,214091,214092,214093,214094,214095,214096,214097,214098,214099,214100,214101,214102,214103,214104,214105,214106,214107,214108,214109,214110,214111,214112,214113,214114,214115,214116,214117,214118,214119,214120,214121,214122,214123,214124,214125,214126,214127,214128,214129,214130,214131,214132,214133,214134,214135,214136,214137,214138,214139,214140,214141,214142,214143,214144,214145,214146,214147,214148,214149,214150,214151,214152,214153,214154,214155,214156,214157,214158,214159,214160,214161,214162,214163,214164,214165,214166,214167,214168,214169,214170,214171,214172,214173,214174,214175,214176,214177,214178,214179,214180,214181,214182,214183,214184,214185,214186,214187,214188,214189,214190,214191,214192,214193,214194,214195,214196,214197,214198,214199,214200,214201,214202,214203,214204,214205,214206,214207,214208,214209,214210,214211,214212,214213,214214,214215,214216,214217,214218,214219,214220,214221,214222,214223,214224,214225,214226,214227,214228,214229,214230,214231,214232,214233,214234,214235,214236,214237,214238,214239,214240,214241,214242,214243,214244,214245,214246,214247,214248,214249,214250,214251,214252,214253,214254,214255,214256,214257,214258,214259,214260,214261,214262,214263,214264,214265,214266,214267,214268,214269,214270,214271,214272,214273,214274,214275,214276,214277,214278,214279,214280,214281,214282,214283,214284,214285,214286,214287,214288,214289,214290,214291,214292,214293,214294,214295,214296,214297,214298,214299,214300,214301,214302,214303,214304,214305,214306,214307,214308,214309,214310,214311,214312,214314,217956,217957,218056,218301,218326,218328,218333,218356,218368,218369,218376,218377,218380,218381,218420,218421,218428,218429,218455,218456,218532,218535,218539,218540,218541,218542,218613,218614,218616,218621,218622,218623,218624,218625,218626,218627,219550,219551,219558,219559,220112,220113,220114,220116,220669,220671,220672,220673,220674,220675,220676,220677,220678,220679,220680,220681,220682,220683,220684,220685,220687,220688,220689,220692,222017,222019,222152,222153,222165,222166,222172,222174,222424,222425,222427,222429,222989,222990,223004,223005,223147,223148,223171,223173,223175,223176,229376,229395,232235,232236,240106,240107,242171,242172,242582,242634,242639,246051,246056,524289,524305,542621,544249,552875,552994,552995") && 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"), "34,1,41,44,45,48,49,52,53,56,60,63,64,67,68,71,72,75,76,767,768,833,837,1731,1732,2586,2587,2837,98337,98338,114364,114712,116533,116571,116572") == "a98498cd417b678b580084451d74bafc9b619dff" || 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"), "34,1,41,44,45,48,49,52,53,56,60,63,64,67,68,71,72,75,76,767,768,833,837,1731,1732,2586,2587,2837,98337,98338,114364,114712,116533,116571,116572") && 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"), "108,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,140179,140180,140202,140203,146501,146502,146510,146512,146515,146516,146517,146518,146520,146521,146533,146534,146537,146539,146540,146541,146542,146543,146554,146555,146556,146559,147008,147009,147380,147381,148866,148867,163840,163874,173727,173728,196608,196640,210429,210430,211092,211093,212586,212587,229376,229384,241146,241147,246632,246633,250470,250475,252693,252695,262144,262147,263271,267445,267512,267513") == "0a021d984d131e7a79cf91272f7356b1bf24c088" || 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"), "108,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,140179,140180,140202,140203,146501,146502,146510,146512,146515,146516,146517,146518,146520,146521,146533,146534,146537,146539,146540,146541,146542,146543,146554,146555,146556,146559,147008,147009,147380,147381,148866,148867,163840,163874,173727,173728,196608,196640,210429,210430,211092,211093,212586,212587,229376,229384,241146,241147,246632,246633,250470,250475,252693,252695,262144,262147,263271,267445,267512,267513") && 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:c36d93bf2f2ff8226675c1bddbfad0c1d479d576"),
                concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9b2ae04f4b11dcb7055179cfac6323720aff73e6"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/platform/bootdevice/by-name/boot",":67108864:9b2ae04f4b11dcb7055179cfac6323720aff73e6")));
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);
