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_in/surya:10/QKQ1.200512.002/V12.0.5.0.QJGINXM:user/release-keys");
ui_print("Target: POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.8.0.QJGINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.5.0.QJGINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.8.0.QJGINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.5.0.QJGINXM:user/release-keys or POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.8.0.QJGINXM: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:b3d728806da82a27de85223cb1a8ebffdbb071d9",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:d4a06bf47c63a2800384719279c2a21cdefe2ce8") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:b3d728806da82a27de85223cb1a8ebffdbb071d9\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:d4a06bf47c63a2800384719279c2a21cdefe2ce8\" 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"), "56,1,190,218,227,280,281,541,545,557,558,577,578,1889,2319,2325,2326,26230,26231,27598,27599,32768,32770,32924,32944,33002,33003,33217,33219,33379,33383,33391,33392,33400,33404,33409,33410,33630,33631,33643,33647,33651,98306,98460,163842,163996,229378,229532,294914,295068,622609,622612,631038,632994,643025,643163,643164") == "cf10cde9153dfb9d80720a1d604dc45582d24aad" || 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"), "56,1,190,218,227,280,281,541,545,557,558,577,578,1889,2319,2325,2326,26230,26231,27598,27599,32768,32770,32924,32944,33002,33003,33217,33219,33379,33383,33391,33392,33400,33404,33409,33410,33630,33631,33643,33647,33651,98306,98460,163842,163996,229378,229532,294914,295068,622609,622612,631038,632994,643025,643163,643164") && 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"), "112,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,752,777,1096,1146,1165,1502,1579,1580,12263,12264,12441,12492,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,14397,25648,25649,25931,25932,27841,28277,28335,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,276610,277607,282007,282077,282078") == "c7509ac7a7d09d80614b64ae71529854f68569a6" || 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"), "112,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,752,777,1096,1146,1165,1502,1579,1580,12263,12264,12441,12492,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,14397,25648,25649,25931,25932,27841,28277,28335,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,276610,277607,282007,282077,282078") && 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"), "88,1,93,109,113,117,118,4454,4458,5933,5934,6598,6601,6605,6606,6647,6648,6655,6661,6662,6663,7380,7384,7391,7392,7398,7404,7405,7406,7466,7470,7475,7476,8224,8225,11184,11188,15401,15402,18157,18158,21630,21631,22269,22273,23043,23044,25451,25452,25815,25819,26326,26327,26339,26345,26346,26347,28187,28191,29628,29629,32768,32770,32841,32853,36603,36604,37009,37010,44175,44176,52178,52179,53642,53643,64712,98306,98377,163842,163913,229378,229449,262148,262156,291083,292044,296674,296746,296747") == "291237ef15da0ad094c9118346c05bee47ae0a72" || 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"), "88,1,93,109,113,117,118,4454,4458,5933,5934,6598,6601,6605,6606,6647,6648,6655,6661,6662,6663,7380,7384,7391,7392,7398,7404,7405,7406,7466,7470,7475,7476,8224,8225,11184,11188,15401,15402,18157,18158,21630,21631,22269,22273,23043,23044,25451,25452,25815,25819,26326,26327,26339,26345,26346,26347,28187,28191,29628,29629,32768,32770,32841,32853,36603,36604,37009,37010,44175,44176,52178,52179,53642,53643,64712,98306,98377,163842,163913,229378,229449,262148,262156,291083,292044,296674,296746,296747") && 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:b3d728806da82a27de85223cb1a8ebffdbb071d9",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:d4a06bf47c63a2800384719279c2a21cdefe2ce8",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:d4a06bf47c63a2800384719279c2a21cdefe2ce8");

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


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