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.QJQTWXM:user/release-keys");
ui_print("Target: Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQTWXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.1.0.QJQTWXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQTWXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lime/lime:10/QKQ1.200830.002/V12.0.1.0.QJQTWXM:user/release-keys or Redmi/lime/lime:10/QKQ1.200830.002/V12.0.2.0.QJQTWXM: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:0dd0c4220f21a9ac78b5eec17b56abe93bd01d04",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:26d7e9433a74c7aa21b92cc63d546cdbeac5bb2a") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:0dd0c4220f21a9ac78b5eec17b56abe93bd01d04\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:26d7e9433a74c7aa21b92cc63d546cdbeac5bb2a\" 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"), "138,1,202,230,239,259,260,520,524,536,537,556,557,1874,1875,2305,2306,26212,26213,27580,27581,32768,32770,32936,32956,32996,32997,33211,33213,33376,33380,33388,33389,33397,33401,33406,33407,33434,33438,33442,38321,38333,38339,38340,38341,39641,39647,39679,39680,39686,39692,39693,39694,39699,39705,39706,39707,39827,39833,39839,39840,41639,41640,41691,41692,41778,41779,41879,41880,41947,41948,42332,42336,42375,42376,43340,43341,43991,43993,45924,45926,45934,45935,45957,45958,45962,45963,46396,46397,46428,46434,46435,46436,46535,46541,46542,46543,49335,49339,49367,49368,49382,49388,49389,49390,49392,49393,52641,52645,52677,52691,53061,53065,53073,53074,53161,53167,53172,67202,67889,67891,67915,98306,98472,163842,164008,229378,229544,294914,295080,622610,622612,655361,655380,679512,681591,692392,692539,692540") == "5875e7c9e9e56c5c98751d0416a15784271ec5fb" || 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"), "138,1,202,230,239,259,260,520,524,536,537,556,557,1874,1875,2305,2306,26212,26213,27580,27581,32768,32770,32936,32956,32996,32997,33211,33213,33376,33380,33388,33389,33397,33401,33406,33407,33434,33438,33442,38321,38333,38339,38340,38341,39641,39647,39679,39680,39686,39692,39693,39694,39699,39705,39706,39707,39827,39833,39839,39840,41639,41640,41691,41692,41778,41779,41879,41880,41947,41948,42332,42336,42375,42376,43340,43341,43991,43993,45924,45926,45934,45935,45957,45958,45962,45963,46396,46397,46428,46434,46435,46436,46535,46541,46542,46543,49335,49339,49367,49368,49382,49388,49389,49390,49392,49393,52641,52645,52677,52691,53061,53065,53073,53074,53161,53167,53172,67202,67889,67891,67915,98306,98472,163842,164008,229378,229544,294914,295080,622610,622612,655361,655380,679512,681591,692392,692539,692540") && 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"), "20,1,32770,32844,46958,46996,98306,98380,163842,163916,229378,229452,294914,294988,295000,295009,305237,306200,311054,311128,311129") == "8ebde72cc6b9b1ebb0554e4367da01ffc4ab1aab" || 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"), "20,1,32770,32844,46958,46996,98306,98380,163842,163916,229378,229452,294914,294988,295000,295009,305237,306200,311054,311128,311129") && 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"), "346,1,85,5375,5376,5392,5396,5400,5401,9691,9692,9737,9741,11216,11217,11881,11884,11888,11889,11930,11931,11938,11944,11945,11946,12663,12667,12674,12675,12681,12687,12688,12689,15444,15445,18917,18918,19281,19285,19792,19793,19805,19811,19812,19813,21653,21657,23094,23095,29988,29989,30390,30391,32768,32770,32834,32845,37625,37626,45636,45637,47100,47101,58170,58171,58177,58178,58184,58190,58191,58192,58320,58326,58327,58328,58340,58346,58347,58348,58351,58352,58358,58364,58365,58366,58389,58395,58396,58397,59241,59242,65536,65547,65548,65549,72971,72972,88641,88642,88763,88764,88940,88941,89308,89309,90060,90061,96444,96445,98304,98306,98370,98381,101274,101275,103526,103527,103714,103715,103726,103732,103733,103734,103744,103745,103750,103754,103759,103760,103764,103770,103774,103775,103781,103787,103788,103789,103801,103807,103808,103809,103819,103825,103826,103827,103829,103836,103842,103843,105600,105601,105607,105608,105742,105743,106951,106958,107027,107034,107039,107046,107076,107083,107102,107109,107127,107134,107150,107151,107162,107164,107333,107334,107335,107336,107337,107338,107339,107340,107377,107379,107387,107388,107393,107394,107754,107758,107762,107764,107769,107773,107774,107778,107779,107783,107784,107788,107789,107793,107794,107798,107799,107803,107804,107808,107809,107811,107818,107822,107823,107827,107828,107832,107833,107837,107838,107842,107843,107847,107848,107852,107853,107855,107856,107860,107861,107865,107866,107870,107871,107875,107876,107880,107881,107885,107886,107890,107891,107895,107896,107898,107902,107906,107907,107911,107912,107916,107917,107921,107922,107924,107925,107929,107930,107932,107933,107937,107938,107942,107943,107945,107946,107950,107951,107955,107956,107958,107959,107963,107964,107970,107973,108125,109143,109144,112952,112953,123745,123747,123757,123803,126618,126620,131072,131085,131982,131984,135916,135917,139966,139968,141498,141500,141726,141727,143417,143427,146937,146938,151648,151649,151652,151653,152255,152257,152683,152685,152864,152866,152911,152913,153053,153055,154311,154313,155390,155392,155852,155853,157481,157482,158126,158127,161759,161760,163840,163842,163906,163921,164474,164475,169238,169239,169264,169266,169284,229378,229442,261947,262144,262154,262155,262189,262798,266965,267032,267033") == "e7147786e04651ad8c19c8a1cf1780df62be3e70" || 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"), "346,1,85,5375,5376,5392,5396,5400,5401,9691,9692,9737,9741,11216,11217,11881,11884,11888,11889,11930,11931,11938,11944,11945,11946,12663,12667,12674,12675,12681,12687,12688,12689,15444,15445,18917,18918,19281,19285,19792,19793,19805,19811,19812,19813,21653,21657,23094,23095,29988,29989,30390,30391,32768,32770,32834,32845,37625,37626,45636,45637,47100,47101,58170,58171,58177,58178,58184,58190,58191,58192,58320,58326,58327,58328,58340,58346,58347,58348,58351,58352,58358,58364,58365,58366,58389,58395,58396,58397,59241,59242,65536,65547,65548,65549,72971,72972,88641,88642,88763,88764,88940,88941,89308,89309,90060,90061,96444,96445,98304,98306,98370,98381,101274,101275,103526,103527,103714,103715,103726,103732,103733,103734,103744,103745,103750,103754,103759,103760,103764,103770,103774,103775,103781,103787,103788,103789,103801,103807,103808,103809,103819,103825,103826,103827,103829,103836,103842,103843,105600,105601,105607,105608,105742,105743,106951,106958,107027,107034,107039,107046,107076,107083,107102,107109,107127,107134,107150,107151,107162,107164,107333,107334,107335,107336,107337,107338,107339,107340,107377,107379,107387,107388,107393,107394,107754,107758,107762,107764,107769,107773,107774,107778,107779,107783,107784,107788,107789,107793,107794,107798,107799,107803,107804,107808,107809,107811,107818,107822,107823,107827,107828,107832,107833,107837,107838,107842,107843,107847,107848,107852,107853,107855,107856,107860,107861,107865,107866,107870,107871,107875,107876,107880,107881,107885,107886,107890,107891,107895,107896,107898,107902,107906,107907,107911,107912,107916,107917,107921,107922,107924,107925,107929,107930,107932,107933,107937,107938,107942,107943,107945,107946,107950,107951,107955,107956,107958,107959,107963,107964,107970,107973,108125,109143,109144,112952,112953,123745,123747,123757,123803,126618,126620,131072,131085,131982,131984,135916,135917,139966,139968,141498,141500,141726,141727,143417,143427,146937,146938,151648,151649,151652,151653,152255,152257,152683,152685,152864,152866,152911,152913,153053,153055,154311,154313,155390,155392,155852,155853,157481,157482,158126,158127,161759,161760,163840,163842,163906,163921,164474,164475,169238,169239,169264,169266,169284,229378,229442,261947,262144,262154,262155,262189,262798,266965,267032,267033") && 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:0dd0c4220f21a9ac78b5eec17b56abe93bd01d04",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:26d7e9433a74c7aa21b92cc63d546cdbeac5bb2a",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:26d7e9433a74c7aa21b92cc63d546cdbeac5bb2a");

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


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