getprop("ro.product.device") == "lime" || abort("E3004: This package is for \"lime\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/lime/lime:10/QKQ1.200830.002/V12.0.1.0.QJQINXM:user/release-keys");
ui_print("Target: Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.1.0.QJQINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lime/lime:10/QKQ1.200830.002/V12.0.1.0.QJQINXM:user/release-keys or Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQINXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
show_progress(0.100000, 10);
patch_partition_check("EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:f764a4d71993b68ce7af60cecb82feb3df31a5f0") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:f764a4d71993b68ce7af60cecb82feb3df31a5f0\" has unexpected contents.");
apply_patch_space(100663296) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "256,1,195,223,232,252,253,513,517,529,530,549,550,1861,2292,2298,2299,26205,26206,27573,27574,32768,32770,32929,32949,32982,32983,33197,33199,33362,33366,33374,33375,33383,33387,33392,33393,33420,33424,33428,38307,38319,38325,38326,39641,39643,39644,39645,39646,39656,39666,39672,39678,39679,39680,39685,39691,39692,39693,39813,39819,39825,39826,41677,41678,41764,41765,41865,41866,41933,41934,42318,42322,42361,42362,43326,43327,43977,43979,45910,45912,45920,45921,45943,45944,45948,45949,46382,46383,46414,46420,46421,46422,46521,46527,46528,49354,49368,49374,49375,49376,49378,49379,52627,52631,52676,52677,53047,53051,53059,53060,53147,53153,53158,53159,53805,53806,53908,53923,53928,53931,53932,53968,54254,54255,54271,54272,54403,54404,54406,54407,54409,54410,61701,61705,61730,61731,61860,61864,61870,61871,62349,62353,62369,62370,65536,65556,67030,67034,67128,67129,67134,67140,67141,67142,67180,67186,67187,67188,67875,67879,67901,67902,68150,68151,68184,68190,68191,68689,81570,81571,82264,82268,82359,82837,82838,82839,84601,84605,84648,84649,84653,84659,84660,84661,84683,84687,84711,84712,86404,86408,86432,86433,87279,87283,87306,87307,87390,87394,87399,87400,90707,90709,94495,94496,96065,96069,98231,98232,98304,98306,98465,98485,98745,98747,98748,98749,98781,98785,98801,98802,98807,98808,99258,99262,99665,99666,100349,100353,100368,100369,102754,102758,102805,102806,103217,103221,103234,103235,105301,105302,110837,110841,110924,163842,164001,229378,229537,294914,295073,622607,622612,651114,653130,663479,663622,663623") == "e698aa686970bfb6d8fc499cf65acb4f037c9d26" || 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"), "256,1,195,223,232,252,253,513,517,529,530,549,550,1861,2292,2298,2299,26205,26206,27573,27574,32768,32770,32929,32949,32982,32983,33197,33199,33362,33366,33374,33375,33383,33387,33392,33393,33420,33424,33428,38307,38319,38325,38326,39641,39643,39644,39645,39646,39656,39666,39672,39678,39679,39680,39685,39691,39692,39693,39813,39819,39825,39826,41677,41678,41764,41765,41865,41866,41933,41934,42318,42322,42361,42362,43326,43327,43977,43979,45910,45912,45920,45921,45943,45944,45948,45949,46382,46383,46414,46420,46421,46422,46521,46527,46528,49354,49368,49374,49375,49376,49378,49379,52627,52631,52676,52677,53047,53051,53059,53060,53147,53153,53158,53159,53805,53806,53908,53923,53928,53931,53932,53968,54254,54255,54271,54272,54403,54404,54406,54407,54409,54410,61701,61705,61730,61731,61860,61864,61870,61871,62349,62353,62369,62370,65536,65556,67030,67034,67128,67129,67134,67140,67141,67142,67180,67186,67187,67188,67875,67879,67901,67902,68150,68151,68184,68190,68191,68689,81570,81571,82264,82268,82359,82837,82838,82839,84601,84605,84648,84649,84653,84659,84660,84661,84683,84687,84711,84712,86404,86408,86432,86433,87279,87283,87306,87307,87390,87394,87399,87400,90707,90709,94495,94496,96065,96069,98231,98232,98304,98306,98465,98485,98745,98747,98748,98749,98781,98785,98801,98802,98807,98808,99258,99262,99665,99666,100349,100353,100368,100369,102754,102758,102805,102806,103217,103221,103234,103235,105301,105302,110837,110841,110924,163842,164001,229378,229537,294914,295073,622607,622612,651114,653130,663479,663622,663623") && 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"), "68,1,103,113,117,122,123,205,209,214,215,1889,1893,2370,2371,2634,2638,2642,2643,2645,2649,2651,2662,2685,2686,2722,2726,2776,2777,2938,2939,2951,2955,2959,2960,2963,2969,2970,2971,2983,2987,2991,2998,3007,3008,3036,3042,3179,4105,4298,4622,4795,32770,32843,98306,98379,163842,163915,229378,229451,294914,294987,295006,295007,298990,299934,304689,304762,304763") == "8a1b4570b3734980f9f19ea0d9bd39b229dd82c2" || 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"), "68,1,103,113,117,122,123,205,209,214,215,1889,1893,2370,2371,2634,2638,2642,2643,2645,2649,2651,2662,2685,2686,2722,2726,2776,2777,2938,2939,2951,2955,2959,2960,2963,2969,2970,2971,2983,2987,2991,2998,3007,3008,3036,3042,3179,4105,4298,4622,4795,32770,32843,98306,98379,163842,163915,229378,229451,294914,294987,295006,295007,298990,299934,304689,304762,304763") && 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"), "378,1,93,109,113,117,118,4454,4458,5933,5934,6598,6601,6605,6606,6647,6648,6655,6661,6662,6663,7380,7384,7391,7392,7398,7404,7405,7406,8154,8155,11114,11118,15331,15332,18087,18088,21560,21561,22199,22203,22973,22974,25381,25382,25745,25749,26256,26257,26269,26275,26276,26277,28117,28121,29558,29559,32768,32770,32842,32853,36533,36534,36939,36940,44105,44106,52108,52109,53572,53573,64642,64643,65536,65547,66896,66897,67289,67290,67368,67370,70738,70740,78220,78221,78227,78228,78234,78240,78241,78242,78370,78376,78377,78378,78390,78396,78397,78398,78401,78402,78408,78414,78415,78416,78439,78445,78446,78447,79291,79292,92990,92991,93010,93011,94977,94978,98304,98306,98378,98389,108881,108882,109058,109059,109426,109427,110178,110179,116562,116563,121315,121316,123567,123568,123755,123756,123767,123773,123774,123775,123785,123786,123791,123795,123800,123801,123805,123811,123815,123816,123822,123828,123829,123830,123842,123848,123849,123850,123860,123866,123867,123868,123870,123877,123883,123884,125641,125642,125648,125649,125783,125784,126992,126999,127068,127075,127080,127087,127117,127124,127143,127150,127168,127175,127191,127192,127203,127205,127374,127375,127376,127377,127378,127379,127380,127381,127419,127421,127429,127430,127432,127434,127435,127436,127797,127801,127805,127809,127810,127812,127817,127821,127822,127826,127827,127831,127832,127836,127837,127841,127842,127846,127847,127851,127852,127856,127857,127859,127866,127870,127871,127875,127876,127880,127881,127885,127886,127890,127891,127895,127896,127900,127901,127903,127904,127908,127909,127913,127914,127918,127919,127923,127924,127928,127929,127933,127934,127938,127939,127943,127944,127946,127950,127954,127955,127959,127960,127964,127965,127969,127970,127972,127973,127977,127978,127980,127981,127985,127986,127990,127991,127993,127994,127998,127999,128003,128004,128006,128007,128011,128012,128022,128025,128177,129195,129196,131072,131083,133015,133016,134220,134221,139284,139285,143810,143812,143822,143867,150739,150741,156090,156092,160024,160025,163840,163842,163914,163929,164163,164165,165695,165697,165923,165924,167614,167624,171134,171135,175845,175846,175849,175850,176452,176454,176880,176882,179104,179107,182156,182157,186193,186195,186240,186242,186382,186384,187640,187642,188719,188721,189181,189182,190809,190810,191454,191455,195087,195088,196608,196621,197734,197735,202498,202499,202524,202526,202544,229378,229450,294914,294986,294989,294997,295118,296016,300710,300781,300782") == "a2128e9062fe185936f629c59b0bcaeaec18b60d" || 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"), "378,1,93,109,113,117,118,4454,4458,5933,5934,6598,6601,6605,6606,6647,6648,6655,6661,6662,6663,7380,7384,7391,7392,7398,7404,7405,7406,8154,8155,11114,11118,15331,15332,18087,18088,21560,21561,22199,22203,22973,22974,25381,25382,25745,25749,26256,26257,26269,26275,26276,26277,28117,28121,29558,29559,32768,32770,32842,32853,36533,36534,36939,36940,44105,44106,52108,52109,53572,53573,64642,64643,65536,65547,66896,66897,67289,67290,67368,67370,70738,70740,78220,78221,78227,78228,78234,78240,78241,78242,78370,78376,78377,78378,78390,78396,78397,78398,78401,78402,78408,78414,78415,78416,78439,78445,78446,78447,79291,79292,92990,92991,93010,93011,94977,94978,98304,98306,98378,98389,108881,108882,109058,109059,109426,109427,110178,110179,116562,116563,121315,121316,123567,123568,123755,123756,123767,123773,123774,123775,123785,123786,123791,123795,123800,123801,123805,123811,123815,123816,123822,123828,123829,123830,123842,123848,123849,123850,123860,123866,123867,123868,123870,123877,123883,123884,125641,125642,125648,125649,125783,125784,126992,126999,127068,127075,127080,127087,127117,127124,127143,127150,127168,127175,127191,127192,127203,127205,127374,127375,127376,127377,127378,127379,127380,127381,127419,127421,127429,127430,127432,127434,127435,127436,127797,127801,127805,127809,127810,127812,127817,127821,127822,127826,127827,127831,127832,127836,127837,127841,127842,127846,127847,127851,127852,127856,127857,127859,127866,127870,127871,127875,127876,127880,127881,127885,127886,127890,127891,127895,127896,127900,127901,127903,127904,127908,127909,127913,127914,127918,127919,127923,127924,127928,127929,127933,127934,127938,127939,127943,127944,127946,127950,127954,127955,127959,127960,127964,127965,127969,127970,127972,127973,127977,127978,127980,127981,127985,127986,127990,127991,127993,127994,127998,127999,128003,128004,128006,128007,128011,128012,128022,128025,128177,129195,129196,131072,131083,133015,133016,134220,134221,139284,139285,143810,143812,143822,143867,150739,150741,156090,156092,160024,160025,163840,163842,163914,163929,164163,164165,165695,165697,165923,165924,167614,167624,171134,171135,175845,175846,175849,175850,176452,176454,176880,176882,179104,179107,182156,182157,186193,186195,186240,186242,186382,186384,187640,187642,188719,188721,189181,189182,190809,190810,191454,191455,195087,195088,196608,196621,197734,197735,202498,202499,202524,202526,202544,229378,229450,294914,294986,294989,294997,295118,296016,300710,300781,300782") && 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 ----

ui_print("Patching boot image...");
show_progress(0.100000, 10);
patch_partition("EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:f764a4d71993b68ce7af60cecb82feb3df31a5f0",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:f764a4d71993b68ce7af60cecb82feb3df31a5f0");

# ---- radio update tasks ----

ui_print("Patching firmware images...");
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64");
package_extract_file("firmware-update/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlib");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hyp");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymaster");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tz");
package_extract_file("firmware-update/featenabler.mbn", "/dev/block/bootdevice/by-name/featenabler");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_config");
package_extract_file("firmware-update/BTFM.bin", "/dev/block/bootdevice/by-name/bluetooth");
package_extract_file("firmware-update/uefi_sec.mbn", "/dev/block/bootdevice/by-name/uefisecapp");
package_extract_file("firmware-update/imagefv.elf", "/dev/block/bootdevice/by-name/imagefv");
package_extract_file("firmware-update/qupv3fw.elf", "/dev/block/bootdevice/by-name/qupfw");
package_extract_file("firmware-update/abl.elf", "/dev/block/bootdevice/by-name/abl");
package_extract_file("firmware-update/dspso.bin", "/dev/block/bootdevice/by-name/dsp");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfg");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsec");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xbl");
package_extract_file("firmware-update/rpm.mbn", "/dev/block/bootdevice/by-name/rpm");
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64bak");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlibbak");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hypbak");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymasterbak");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tzbak");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_configbak");
package_extract_file("firmware-update/uefi_sec.mbn", "/dev/block/bootdevice/by-name/uefisecappbak");
package_extract_file("firmware-update/imagefv.elf", "/dev/block/bootdevice/by-name/imagefvbak");
package_extract_file("firmware-update/qupv3fw.elf", "/dev/block/bootdevice/by-name/qupfwbak");
package_extract_file("firmware-update/abl.elf", "/dev/block/bootdevice/by-name/ablbak");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfgbak");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsecbak");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
package_extract_file("firmware-update/rpm.mbn", "/dev/block/bootdevice/by-name/rpmbak");
show_progress(0.600000, 600);

# --- Start patching dynamic partitions ---


# Patch partition product

ui_print("Patching product image after verification.");
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.");

# Update dynamic partition metadata

assert(update_dynamic_partitions(package_extract_file("dynamic_partitions_op_list")));

# Patch partition vendor

ui_print("Patching vendor image after verification.");
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 system

ui_print("Patching system image after verification.");
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.");

# --- End patching dynamic partitions ---

show_progress(0.100000, 2);

# ---- radio update tasks 2 ----

ui_print("Patching vbmeta dtbo logo binimages...");
package_extract_file("firmware-update/dtbo.img", "/dev/block/bootdevice/by-name/dtbo");
package_extract_file("firmware-update/vbmeta.img", "/dev/block/bootdevice/by-name/vbmeta");
package_extract_file("firmware-update/vbmeta_system.img", "/dev/block/bootdevice/by-name/vbmeta_system");
