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.2.0.QJQINXM:user/release-keys");
ui_print("Target: Redmi/lime/lime:10/QKQ1.200830.002/V12.0.3.0.QJQINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.3.0.QJQINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQINXM:user/release-keys or Redmi/lime/lime:10/QKQ1.200830.002/V12.0.3.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:071a37bf7296175adda9604fde22d8eb06aa8702",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:071a37bf7296175adda9604fde22d8eb06aa8702\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39\" has unexpected contents.");
apply_patch_space(102981632) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "160,1,195,223,232,252,517,529,530,549,550,1867,1868,2292,26199,26205,26206,27573,27574,32768,32770,32929,32949,32982,32983,33197,33199,33362,33366,33374,33375,33383,33387,33392,33393,33420,33424,33428,33429,38306,38307,38319,38325,38326,38327,39627,39633,39665,39666,39672,39678,39679,39680,39685,39691,39692,39693,39813,39819,39825,39826,41625,41626,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,98306,98465,163842,164001,229378,229537,294914,295073,430208,438910,622607,622612,651264,653280,663632,663773,663774") == "97120cf3f8e6d576690f1c85eb6554e9cd283c5a" || 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"), "160,1,195,223,232,252,517,529,530,549,550,1867,1868,2292,26199,26205,26206,27573,27574,32768,32770,32929,32949,32982,32983,33197,33199,33362,33366,33374,33375,33383,33387,33392,33393,33420,33424,33428,33429,38306,38307,38319,38325,38326,38327,39627,39633,39665,39666,39672,39678,39679,39680,39685,39691,39692,39693,39813,39819,39825,39826,41625,41626,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,98306,98465,163842,164001,229378,229537,294914,295073,430208,438910,622607,622612,651264,653280,663632,663773,663774") && 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"), "18,1,32770,32843,98306,98379,163842,163915,229378,229451,294914,294987,295006,295007,299389,300334,305096,305170,305171") == "288b1eebfc9540b972a96f4514fde535120a848d" || 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"), "18,1,32770,32843,98306,98379,163842,163915,229378,229451,294914,294987,295006,295007,299389,300334,305096,305170,305171") && 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"), "322,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,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,127860,127861,127864,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,128214,128472,129195,129196,131072,131083,133015,133016,134220,134221,139284,139285,143810,143812,143822,163842,163914,229378,229450,294914,294986,294989,294997,295084,295982,300676,300747,300748") == "ec77fce6d8cd32fc845e207cf86194852eb275d5" || 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"), "322,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,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,127860,127861,127864,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,128214,128472,129195,129196,131072,131083,133015,133016,134220,134221,139284,139285,143810,143812,143822,163842,163914,229378,229450,294914,294986,294989,294997,295084,295982,300676,300747,300748") && 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:071a37bf7296175adda9604fde22d8eb06aa8702",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:5409eb227da2452c8641437059a5f485dffb9f39");

# ---- 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/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 ---


# Update dynamic partition metadata

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

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

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