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.1.0.QJGRUXM:user/release-keys");
ui_print("Target: POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.3.0.QJGRUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.1.0.QJGRUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.3.0.QJGRUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.1.0.QJGRUXM:user/release-keys or POCO/surya_ru/surya:10/QKQ1.200512.002/V12.0.3.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:0ff90e0f33c7b2183ceb80b363875bbefd2b286d",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:1bc394e1ff7da00b39645fc7f193128fa882020f") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:0ff90e0f33c7b2183ceb80b363875bbefd2b286d\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:1bc394e1ff7da00b39645fc7f193128fa882020f\" 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"), "20,1,32770,32931,98306,98467,163842,164003,229378,229539,294914,295075,622610,622612,655361,655380,661389,663414,673926,674071,674072") == "e2918eb85d1e2ec4c33f33b1eae8f8031eb0c925" || 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"), "20,1,32770,32931,98306,98467,163842,164003,229378,229539,294914,295075,622610,622612,655361,655380,661389,663414,673926,674071,674072") && 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"), "52,1,100,110,114,119,120,201,205,210,211,216,217,480,484,488,489,491,492,494,505,517,521,525,535,536,537,545,549,553,554,1165,1502,1579,1580,1628,1668,1676,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,274446,275430,279797,279867,279868") == "4957cac75e4145d725fbbfb4beb857be2091b281" || 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"), "52,1,100,110,114,119,120,201,205,210,211,216,217,480,484,488,489,491,492,494,505,517,521,525,535,536,537,545,549,553,554,1165,1502,1579,1580,1628,1668,1676,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,274446,275430,279797,279867,279868") && 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"), "298,1,89,105,109,113,114,4450,4454,4545,6597,6601,6602,6643,6644,6651,6657,6658,7380,7387,7388,7394,7400,7401,7402,7462,7466,7471,7472,10227,10228,13700,13701,14339,14343,15113,15114,17521,17522,17885,17889,18396,18397,18409,18415,18416,18417,25310,25311,25712,25713,32768,32770,32838,32849,32951,32952,40962,40963,42426,42427,53496,53497,57710,57711,65536,65547,66152,66154,66155,66156,66157,66158,66437,66445,66447,66448,66454,66460,66461,66596,66597,66598,66610,66616,66617,66618,66621,66622,66628,66634,66635,66636,66659,66665,66666,66667,67511,67512,81210,81211,81230,81231,96900,96901,97022,97023,97199,97200,97567,97568,98304,98306,98374,98385,98400,98401,104784,104785,109537,109538,111788,111789,111976,111977,111988,111994,111995,111996,112006,112007,112012,112016,112021,112022,112026,112032,112036,112037,112043,112049,112050,112051,112063,112069,112070,112071,112081,112087,112088,112089,112091,112098,112104,112105,113862,113863,113869,113870,114004,114005,115213,115220,115274,115281,115286,115293,115318,115325,115344,115351,115369,115376,115392,115393,115404,115406,115575,115576,115577,115578,115579,115580,115581,115582,115619,115621,115629,115630,115635,115636,115974,115978,115982,115986,115987,115989,115994,115998,115999,116003,116004,116008,116009,116013,116014,116018,116019,116023,116024,116028,116029,116031,116032,116033,116036,116042,116043,116047,116048,116052,116053,116057,116058,116060,116061,116065,116066,116070,116071,116073,116074,116078,116079,116083,116084,116088,116089,116093,116094,116098,116099,116103,116104,116108,116109,116113,116114,116116,116120,116124,116125,116129,116130,116134,116135,116139,116140,116142,116143,116147,116148,116150,116151,116155,116156,116158,116159,116163,116164,116168,116169,116171,116172,116176,116177,116332,116369,116627,117359,117360,120964,120965,131072,131085,131176,131189,131193,131197,131199,163842,163910,229378,229446,279273,280130,284571,284642,284643") == "e2cc3e1e8fd766c3b14ce91b5673365f98d52429" || 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"), "298,1,89,105,109,113,114,4450,4454,4545,6597,6601,6602,6643,6644,6651,6657,6658,7380,7387,7388,7394,7400,7401,7402,7462,7466,7471,7472,10227,10228,13700,13701,14339,14343,15113,15114,17521,17522,17885,17889,18396,18397,18409,18415,18416,18417,25310,25311,25712,25713,32768,32770,32838,32849,32951,32952,40962,40963,42426,42427,53496,53497,57710,57711,65536,65547,66152,66154,66155,66156,66157,66158,66437,66445,66447,66448,66454,66460,66461,66596,66597,66598,66610,66616,66617,66618,66621,66622,66628,66634,66635,66636,66659,66665,66666,66667,67511,67512,81210,81211,81230,81231,96900,96901,97022,97023,97199,97200,97567,97568,98304,98306,98374,98385,98400,98401,104784,104785,109537,109538,111788,111789,111976,111977,111988,111994,111995,111996,112006,112007,112012,112016,112021,112022,112026,112032,112036,112037,112043,112049,112050,112051,112063,112069,112070,112071,112081,112087,112088,112089,112091,112098,112104,112105,113862,113863,113869,113870,114004,114005,115213,115220,115274,115281,115286,115293,115318,115325,115344,115351,115369,115376,115392,115393,115404,115406,115575,115576,115577,115578,115579,115580,115581,115582,115619,115621,115629,115630,115635,115636,115974,115978,115982,115986,115987,115989,115994,115998,115999,116003,116004,116008,116009,116013,116014,116018,116019,116023,116024,116028,116029,116031,116032,116033,116036,116042,116043,116047,116048,116052,116053,116057,116058,116060,116061,116065,116066,116070,116071,116073,116074,116078,116079,116083,116084,116088,116089,116093,116094,116098,116099,116103,116104,116108,116109,116113,116114,116116,116120,116124,116125,116129,116130,116134,116135,116139,116140,116142,116143,116147,116148,116150,116151,116155,116156,116158,116159,116163,116164,116168,116169,116171,116172,116176,116177,116332,116369,116627,117359,117360,120964,120965,131072,131085,131176,131189,131193,131197,131199,163842,163910,229378,229446,279273,280130,284571,284642,284643") && 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:0ff90e0f33c7b2183ceb80b363875bbefd2b286d",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:1bc394e1ff7da00b39645fc7f193128fa882020f",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:1bc394e1ff7da00b39645fc7f193128fa882020f");

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


# 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/ffu.img", "/dev/block/bootdevice/by-name/ffu");
package_extract_file("firmware-update/vbmeta.img", "/dev/block/bootdevice/by-name/vbmeta");
