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.QJQCNXM:user/release-keys");
ui_print("Target: Redmi/lime/lime:10/QKQ1.200830.002/V12.0.6.0.QJQCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.5.0.QJQCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/lime/lime:10/QKQ1.200830.002/V12.0.6.0.QJQCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/lime/lime:10/QKQ1.200830.002/V12.0.5.0.QJQCNXM:user/release-keys or Redmi/lime/lime:10/QKQ1.200830.002/V12.0.6.0.QJQCNXM: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:7fffb8a40c27158df471d03e87f2e17a1a631620",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:7e6f84546b4b802ee813da1e78549d84c07bf715") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:7fffb8a40c27158df471d03e87f2e17a1a631620\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:7e6f84546b4b802ee813da1e78549d84c07bf715\" 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"), "52,1,225,252,261,281,282,542,546,558,559,578,579,1896,1897,3097,8507,8513,8514,32395,32396,32476,32478,32768,32770,32963,32979,33047,33051,33064,33065,33228,33241,33249,33253,33258,98306,98499,163842,164035,229378,229571,294914,295107,753678,753680,786433,786448,788936,791571,804115,804281,804282") == "c9521510ed382f13954111d417b37973f1133697" || 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"), "52,1,225,252,261,281,282,542,546,558,559,578,579,1896,1897,3097,8507,8513,8514,32395,32396,32476,32478,32768,32770,32963,32979,33047,33051,33064,33065,33228,33241,33249,33253,33258,98306,98499,163842,164035,229378,229571,294914,295107,753678,753680,786433,786448,788936,791571,804115,804281,804282") && 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,109,119,123,128,129,211,215,220,221,1895,1899,2376,2377,2640,2644,2648,2649,2651,2655,2657,2668,2691,2692,2728,2732,2782,2783,2919,2920,2932,2936,2940,2941,2944,2950,2951,2952,2964,2968,2972,2979,2988,2989,3017,3023,3160,4086,4279,4603,4776,32770,32850,98306,98386,163842,163922,229378,229458,294914,294994,327691,327699,329962,330988,336235,336315,336316") == "66873e9001fdddd04261abd768eddb31fbce001b" || 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,109,119,123,128,129,211,215,220,221,1895,1899,2376,2377,2640,2644,2648,2649,2651,2655,2657,2668,2691,2692,2728,2732,2782,2783,2919,2920,2932,2936,2940,2941,2944,2950,2951,2952,2964,2968,2972,2979,2988,2989,3017,3023,3160,4086,4279,4603,4776,32770,32850,98306,98386,163842,163922,229378,229458,294914,294994,327691,327699,329962,330988,336235,336315,336316") && 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"), "244,1,54,70,74,78,79,86,92,93,94,740,744,751,752,758,764,765,766,768,769,775,781,782,783,898,904,905,906,918,924,925,926,929,930,936,942,943,944,967,973,974,975,1819,1820,15518,15519,15538,15539,31208,31209,31330,31331,31507,31508,31875,31876,32627,32628,32768,32770,32789,32814,36165,36166,36177,36183,36184,36185,36195,36196,36201,36205,36210,36211,36215,36221,36225,36226,36232,36238,36239,36240,36252,36258,36259,36260,36270,36276,36277,36278,36280,36287,36293,36294,38052,38053,38059,38060,38200,38201,39403,39410,39479,39486,39491,39498,39528,39535,39554,39561,39579,39586,39602,39603,39614,39616,39780,39781,39782,39783,39784,39785,39820,39822,39830,39831,40153,40157,40161,40165,40166,40170,40171,40175,40176,40180,40181,40185,40186,40190,40191,40195,40196,40200,40201,40203,40204,40205,40208,40214,40215,40219,40220,40224,40225,40229,40230,40234,40235,40239,40240,40244,40245,40247,40248,40252,40253,40257,40258,40262,40263,40267,40268,40272,40273,40277,40278,40282,40283,40287,40288,40290,40295,40299,40300,40304,40305,40309,40310,40314,40315,40317,40318,40322,40323,40325,40326,40330,40331,40335,40336,40338,40339,40343,40344,40348,40349,40351,40352,40356,40357,40462,40465,40513,40550,40808,41531,41532,45340,45341,46545,46546,51609,51610,53232,53235,55822,55867,56606,56608,56628,56632,57058,57059,61822,61823,61848,61850,61868,78308,78548,79795,79827,79828") == "f7682f5afbd5bed8edec8f87bfe27cdfcc40c831" || 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"), "244,1,54,70,74,78,79,86,92,93,94,740,744,751,752,758,764,765,766,768,769,775,781,782,783,898,904,905,906,918,924,925,926,929,930,936,942,943,944,967,973,974,975,1819,1820,15518,15519,15538,15539,31208,31209,31330,31331,31507,31508,31875,31876,32627,32628,32768,32770,32789,32814,36165,36166,36177,36183,36184,36185,36195,36196,36201,36205,36210,36211,36215,36221,36225,36226,36232,36238,36239,36240,36252,36258,36259,36260,36270,36276,36277,36278,36280,36287,36293,36294,38052,38053,38059,38060,38200,38201,39403,39410,39479,39486,39491,39498,39528,39535,39554,39561,39579,39586,39602,39603,39614,39616,39780,39781,39782,39783,39784,39785,39820,39822,39830,39831,40153,40157,40161,40165,40166,40170,40171,40175,40176,40180,40181,40185,40186,40190,40191,40195,40196,40200,40201,40203,40204,40205,40208,40214,40215,40219,40220,40224,40225,40229,40230,40234,40235,40239,40240,40244,40245,40247,40248,40252,40253,40257,40258,40262,40263,40267,40268,40272,40273,40277,40278,40282,40283,40287,40288,40290,40295,40299,40300,40304,40305,40309,40310,40314,40315,40317,40318,40322,40323,40325,40326,40330,40331,40335,40336,40338,40339,40343,40344,40348,40349,40351,40352,40356,40357,40462,40465,40513,40550,40808,41531,41532,45340,45341,46545,46546,51609,51610,53232,53235,55822,55867,56606,56608,56628,56632,57058,57059,61822,61823,61848,61850,61868,78308,78548,79795,79827,79828") && 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:7fffb8a40c27158df471d03e87f2e17a1a631620",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:7e6f84546b4b802ee813da1e78549d84c07bf715",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:7e6f84546b4b802ee813da1e78549d84c07bf715");

# ---- 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/imagefv.elf", "/dev/block/bootdevice/by-name/imagefv");
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/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem");
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/imagefv.elf", "/dev/block/bootdevice/by-name/imagefvbak");
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/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");
