getprop("ro.product.device") == "surya" || abort("E3004: This package is for \"surya\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.8.0.QJGMIXM:user/release-keys");
ui_print("Target: POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.9.0.QJGMIXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.8.0.QJGMIXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.9.0.QJGMIXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.8.0.QJGMIXM:user/release-keys or POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.9.0.QJGMIXM: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:8c46df99bdccf1e5190602c24296ca7aa24dce2e",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:f78327b9b651da5af3d3d6621d0edb5316d7051e") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:8c46df99bdccf1e5190602c24296ca7aa24dce2e\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:f78327b9b651da5af3d3d6621d0edb5316d7051e\" 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"), "46,1,189,217,226,279,280,540,544,556,557,576,577,1894,1895,2318,32770,32923,50920,51607,51609,51633,98306,98459,163842,163995,168814,168937,229378,229531,294914,295067,361277,361387,384521,384571,391348,391773,403586,403588,622608,622612,626916,628859,638825,638960,638961") == "56a598352183f78af094299b52ab98b6cac21065" || 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"), "46,1,189,217,226,279,280,540,544,556,557,576,577,1894,1895,2318,32770,32923,50920,51607,51609,51633,98306,98459,163842,163995,168814,168937,229378,229531,294914,295067,361277,361387,384521,384571,391348,391773,403586,403588,622608,622612,626916,628859,638825,638960,638961") && 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"), "102,1,100,110,114,119,120,201,205,210,211,216,217,480,484,488,489,491,492,494,505,517,521,525,526,529,535,536,537,545,549,553,554,1096,1146,1579,1580,1628,1668,12263,12264,12744,12797,12917,12918,12922,12924,12943,12944,12948,12949,12954,12955,12956,12957,12960,12961,12962,12963,12966,12967,12968,12969,12972,12973,12974,12975,12978,12979,12980,12981,12984,12985,12986,12987,12991,12992,12997,12998,13002,13003,13007,13008,13012,13013,13486,13487,14391,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,276637,277634,282037,282106,282107") == "c96502990983e7cbb76999a900282c29c0a65e29" || 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"), "102,1,100,110,114,119,120,201,205,210,211,216,217,480,484,488,489,491,492,494,505,517,521,525,526,529,535,536,537,545,549,553,554,1096,1146,1579,1580,1628,1668,12263,12264,12744,12797,12917,12918,12922,12924,12943,12944,12948,12949,12954,12955,12956,12957,12960,12961,12962,12963,12966,12967,12968,12969,12972,12973,12974,12975,12978,12979,12980,12981,12984,12985,12986,12987,12991,12992,12997,12998,13002,13003,13007,13008,13012,13013,13486,13487,14391,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,276637,277634,282037,282106,282107") && 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"), "16,1,32770,32841,98306,98377,163842,163913,229378,229449,262148,262156,290715,291675,296298,296372,296373") == "f30a29537647d85dcffeb3a907e4f9803fd0772f" || 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"), "16,1,32770,32841,98306,98377,163842,163913,229378,229449,262148,262156,290715,291675,296298,296372,296373") && 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:8c46df99bdccf1e5190602c24296ca7aa24dce2e",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:f78327b9b651da5af3d3d6621d0edb5316d7051e",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:f78327b9b651da5af3d3d6621d0edb5316d7051e");

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


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