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_ru/surya:10/QKQ1.200512.002/V12.0.3.0.QJGRUXM:user/release-keys");
ui_print("Target: POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.5.0.QJGRUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.3.0.QJGRUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.5.0.QJGRUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.3.0.QJGRUXM:user/release-keys or POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.5.0.QJGRUXM: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:74b9f8b3f19445c93eb062cd8615407a9fb91303",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:0ff90e0f33c7b2183ceb80b363875bbefd2b286d") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:74b9f8b3f19445c93eb062cd8615407a9fb91303\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:0ff90e0f33c7b2183ceb80b363875bbefd2b286d\" 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"), "62,1,198,225,234,287,288,548,552,564,565,584,585,1896,2326,2332,2333,26237,26238,27605,27606,32768,32770,32932,32952,33017,33018,33232,33234,33394,33398,33406,33407,33415,33419,33424,33425,33645,33646,33658,33662,33666,98306,98468,163842,164004,195927,196490,196492,196509,229378,229540,294914,295076,622610,622612,655361,655380,665098,667134,677705,677850,677851") == "b638f88da16b2b5eca395a951b5935749e7d9154" || 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"), "62,1,198,225,234,287,288,548,552,564,565,584,585,1896,2326,2332,2333,26237,26238,27605,27606,32768,32770,32932,32952,33017,33018,33232,33234,33394,33398,33406,33407,33415,33419,33424,33425,33645,33646,33658,33662,33666,98306,98468,163842,164004,195927,196490,196492,196509,229378,229540,294914,295076,622610,622612,655361,655380,665098,667134,677705,677850,677851") && 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"), "60,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,1681,28358,28361,32770,32837,35060,35061,98306,98373,163842,163909,229378,229445,262165,262167,276613,277610,282010,282080,282081") == "ba5342c7fac42559507cb9c837bb0dc8f2411a41" || 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"), "60,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,1681,28358,28361,32770,32837,35060,35061,98306,98373,163842,163909,229378,229445,262165,262167,276613,277610,282010,282080,282081") && 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,32838,98306,98374,163842,163910,229378,229446,279405,280263,284707,284776,284777") == "7c743e477938a66206aa0f022f06295ba0c3983a" || 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,32838,98306,98374,163842,163910,229378,229446,279405,280263,284707,284776,284777") && 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:74b9f8b3f19445c93eb062cd8615407a9fb91303",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:0ff90e0f33c7b2183ceb80b363875bbefd2b286d",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:0ff90e0f33c7b2183ceb80b363875bbefd2b286d");

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

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

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