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.5.0.QJQINXM:user/release-keys");
ui_print("Target: Redmi/lime/lime:10/QKQ1.200830.002/V12.0.6.0.QJQINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.5.0.QJQINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.6.0.QJQINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lime/lime:10/QKQ1.200830.002/V12.0.5.0.QJQINXM:user/release-keys or Redmi/lime/lime:10/QKQ1.200830.002/V12.0.6.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:c91bb939a0c1896257b6b03954be1fc58bec19c7",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:daf3b4b4754442f74cd9a21693ca442ad3b43faa") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:c91bb939a0c1896257b6b03954be1fc58bec19c7\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:daf3b4b4754442f74cd9a21693ca442ad3b43faa\" 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"), "28,1,32770,32931,98306,98467,163842,164003,186050,186185,229378,229539,246088,246096,294914,295075,314451,314456,315428,315432,622610,622612,655361,655380,658028,660043,670503,670644,670645") == "849d572ec04f7e51405517d566f5cfa05e136fe4" || 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"), "28,1,32770,32931,98306,98467,163842,164003,186050,186185,229378,229539,246088,246096,294914,295075,314451,314456,315428,315432,622610,622612,655361,655380,658028,660043,670503,670644,670645") && 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"), "150,1,106,116,120,125,126,208,212,217,218,1892,1896,2373,2374,2637,2641,2645,2646,2648,2652,2654,2665,2688,2689,2725,2729,2779,2780,2941,2942,2954,2958,2962,2963,2966,2972,2973,2974,2986,2990,2994,3001,3010,3011,3039,3045,4769,4772,5368,5369,8866,8867,9039,9286,9637,9638,9642,9643,9647,9648,9652,9653,9658,9659,9663,9664,9665,9666,9669,9670,9671,9672,9675,9676,9677,9678,9680,9682,9683,9684,9687,9688,9689,9690,9693,9694,9695,9696,9699,9700,9706,9707,9711,9712,9715,9717,9720,9722,9726,9727,9731,9732,9733,9734,9737,9738,9739,9740,9743,9744,9745,9746,9749,9750,9751,9752,9755,9756,9757,9758,9761,9762,9767,9768,9769,9770,9774,9775,10187,10188,11277,11285,13538,32770,32845,98306,98381,163842,163917,229378,229453,294914,294989,295003,295010,307329,308310,313198,313274,313275") == "b088243653f186eecf3d35f7c80143f9c063d2d4" || 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"), "150,1,106,116,120,125,126,208,212,217,218,1892,1896,2373,2374,2637,2641,2645,2646,2648,2652,2654,2665,2688,2689,2725,2729,2779,2780,2941,2942,2954,2958,2962,2963,2966,2972,2973,2974,2986,2990,2994,3001,3010,3011,3039,3045,4769,4772,5368,5369,8866,8867,9039,9286,9637,9638,9642,9643,9647,9648,9652,9653,9658,9659,9663,9664,9665,9666,9669,9670,9671,9672,9675,9676,9677,9678,9680,9682,9683,9684,9687,9688,9689,9690,9693,9694,9695,9696,9699,9700,9706,9707,9711,9712,9715,9717,9720,9722,9726,9727,9731,9732,9733,9734,9737,9738,9739,9740,9743,9744,9745,9746,9749,9750,9751,9752,9755,9756,9757,9758,9761,9762,9767,9768,9769,9770,9774,9775,10187,10188,11277,11285,13538,32770,32845,98306,98381,163842,163917,229378,229453,294914,294989,295003,295010,307329,308310,313198,313274,313275") && 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"), "18,1,32770,32842,98306,98378,163842,163914,229378,229450,294914,294986,294989,294997,295403,296302,301000,301074,301075") == "f655f5982932790910d2dbd91c7504857e8f583e" || 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"), "18,1,32770,32842,98306,98378,163842,163914,229378,229450,294914,294986,294989,294997,295403,296302,301000,301074,301075") && 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:c91bb939a0c1896257b6b03954be1fc58bec19c7",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:daf3b4b4754442f74cd9a21693ca442ad3b43faa",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:daf3b4b4754442f74cd9a21693ca442ad3b43faa");

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

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

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