getprop("ro.product.device") == "curtana" || abort("E3004: This package is for \"curtana\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/curtana/curtana:10/QKQ1.191215.002/V11.0.8.0.QJWINXM:user/release-keys");
ui_print("Target: Redmi/curtana/curtana:10/QKQ1.191215.002/V12.0.1.0.QJWINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/curtana/curtana:10/QKQ1.191215.002/V11.0.8.0.QJWINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/curtana/curtana:10/QKQ1.191215.002/V12.0.1.0.QJWINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/curtana/curtana:10/QKQ1.191215.002/V11.0.8.0.QJWINXM:user/release-keys or Redmi/curtana/curtana:10/QKQ1.191215.002/V12.0.1.0.QJWINXM: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:134217728:215b5f54a570268a0f1cf96879655fd08a983d59",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:34855d6a0540ba4218eccd61dfbe12ccd1965fb0") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:215b5f54a570268a0f1cf96879655fd08a983d59\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:34855d6a0540ba4218eccd61dfbe12ccd1965fb0\" has unexpected contents.");
apply_patch_space(134217728) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "104,1,32770,32922,70244,71313,74780,74787,98306,98458,108917,123462,123463,127262,127263,127481,127482,128197,128199,128406,163842,163994,180874,180875,229378,229530,294914,295066,304115,304118,305238,305241,341989,342080,342789,342971,366064,366114,385592,385595,385616,385682,385922,386064,389884,389888,393750,393753,393818,393820,418626,418659,438196,442466,442961,443092,443242,443375,444140,444262,444412,444543,449149,449150,449175,449179,449190,449195,449236,449241,449283,450177,488011,489610,489611,491520,491551,491825,494589,504100,504101,505408,505409,515157,515159,515166,515170,516123,516124,516130,516131,516136,516137,519313,530593,531128,622554,622592,622600,622613,622620,624512,634408,634545,634546") == "afce0b02614cb6946146349fb1034d0cd60ef780" || 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"), "104,1,32770,32922,70244,71313,74780,74787,98306,98458,108917,123462,123463,127262,127263,127481,127482,128197,128199,128406,163842,163994,180874,180875,229378,229530,294914,295066,304115,304118,305238,305241,341989,342080,342789,342971,366064,366114,385592,385595,385616,385682,385922,386064,389884,389888,393750,393753,393818,393820,418626,418659,438196,442466,442961,443092,443242,443375,444140,444262,444412,444543,449149,449150,449175,449179,449190,449195,449236,449241,449283,450177,488011,489610,489611,491520,491551,491825,494589,504100,504101,505408,505409,515157,515159,515166,515170,516123,516124,516130,516131,516136,516137,519313,530593,531128,622554,622592,622600,622613,622620,624512,634408,634545,634546") && 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"), "128,1,4166,4243,4244,4292,4332,5329,6060,6201,6267,6760,6855,14807,14808,14987,15038,15606,15607,15611,15613,15616,15618,15621,15623,15626,15628,15631,15633,15636,15638,15642,15644,15645,15646,15648,15650,15651,15652,15654,15656,15657,15658,15666,15668,15669,15670,15672,15674,15675,15676,15680,15681,15685,15687,15690,15692,15695,15697,15700,15702,15705,15707,15708,15709,15718,15719,15720,15721,15742,15743,15744,15745,15750,15751,15755,15756,15760,15761,15765,15766,15771,15772,15776,15777,15778,15779,15782,15783,15784,15785,15788,15789,15790,15791,15794,15795,16143,16144,16395,16396,17170,17174,23574,23575,23755,23756,32768,32770,32855,32883,34672,34673,39239,39240,41653,98306,98391,163842,163927,229378,229463,294914,294999,327692,327700,349759,350889,356345") == "122dca6e53c141fd469852f3c41eb23ddfea2256" || 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"), "128,1,4166,4243,4244,4292,4332,5329,6060,6201,6267,6760,6855,14807,14808,14987,15038,15606,15607,15611,15613,15616,15618,15621,15623,15626,15628,15631,15633,15636,15638,15642,15644,15645,15646,15648,15650,15651,15652,15654,15656,15657,15658,15666,15668,15669,15670,15672,15674,15675,15676,15680,15681,15685,15687,15690,15692,15695,15697,15700,15702,15705,15707,15708,15709,15718,15719,15720,15721,15742,15743,15744,15745,15750,15751,15755,15756,15760,15761,15765,15766,15771,15772,15776,15777,15778,15779,15782,15783,15784,15785,15788,15789,15790,15791,15794,15795,16143,16144,16395,16396,17170,17174,23574,23575,23755,23756,32768,32770,32855,32883,34672,34673,39239,39240,41653,98306,98391,163842,163927,229378,229463,294914,294999,327692,327700,349759,350889,356345") && 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"), "14,1,32770,32829,98306,98365,163842,163901,229378,229437,241344,242087,245926,245989,245990") == "6705cc1064d1c88834ff400dbe6764243f190561" || 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
check_first_block(map_partition("product"));
ifelse (block_image_recover(map_partition("product"), "14,1,32770,32829,98306,98365,163842,163901,229378,229437,241344,242087,245926,245989,245990") && 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:134217728:215b5f54a570268a0f1cf96879655fd08a983d59",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:34855d6a0540ba4218eccd61dfbe12ccd1965fb0",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:34855d6a0540ba4218eccd61dfbe12ccd1965fb0");

# ---- 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/BTFM.bin", "/dev/block/bootdevice/by-name/bluetooth");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tz");
package_extract_file("firmware-update/aop.mbn", "/dev/block/bootdevice/by-name/aop");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_config");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsec");
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/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfg");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xbl");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymaster");
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/tz.mbn", "/dev/block/bootdevice/by-name/tzbak");
package_extract_file("firmware-update/aop.mbn", "/dev/block/bootdevice/by-name/aopbak");
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/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymasterbak");
show_progress(0.600000, 600);

# --- Start patching dynamic partitions ---


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

# 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 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/vbmeta_system.img", "/dev/block/bootdevice/by-name/vbmeta_system");
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");
