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_id/surya:10/QKQ1.200512.002/V12.0.2.0.QJGIDXM:user/release-keys");
ui_print("Target: POCO/surya_id/surya:10/QKQ1.200512.002/V12.0.3.0.QJGIDXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_id/surya:10/QKQ1.200512.002/V12.0.2.0.QJGIDXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_id/surya:10/QKQ1.200512.002/V12.0.3.0.QJGIDXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_id/surya:10/QKQ1.200512.002/V12.0.2.0.QJGIDXM:user/release-keys or POCO/surya_id/surya:10/QKQ1.200512.002/V12.0.3.0.QJGIDXM: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:b9ae611eb72039b99ee4518a8ad639ad00ac1e13",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:80dcbaa3d558d36de0d1ea66a9c1de062d0dabae") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:b9ae611eb72039b99ee4518a8ad639ad00ac1e13\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:80dcbaa3d558d36de0d1ea66a9c1de062d0dabae\" 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,203,231,240,293,294,554,558,570,571,590,591,1908,1909,2332,26237,26243,26244,27611,27612,32768,32770,32937,32957,33028,33029,33243,33245,33405,33409,33417,33418,33426,33430,33435,33436,33656,33657,33669,33673,33677,98306,98473,163842,164009,229378,229545,294914,295081,655363,655380,682922,685005,695859,696008,696009") == "ff257483f42c2fd87d7d545ff03b9729bb5a0e09" || 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,203,231,240,293,294,554,558,570,571,590,591,1908,1909,2332,26237,26243,26244,27611,27612,32768,32770,32937,32957,33028,33029,33243,33245,33405,33409,33417,33418,33426,33430,33435,33436,33656,33657,33669,33673,33677,98306,98473,163842,164009,229378,229545,294914,295081,655363,655380,682922,685005,695859,696008,696009") && 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"), "104,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,1579,1580,12268,12269,12922,12923,12927,12929,12948,12949,12953,12954,12959,12960,12961,12962,12965,12966,12967,12968,12971,12972,12973,12974,12977,12978,12979,12980,12983,12984,12985,12986,12989,12990,12991,12992,12996,12997,13002,13003,13007,13008,13012,13013,13017,13018,13491,13492,14396,19247,25653,25654,25936,25937,27890,28057,28059,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,276653,277650,282053,282123,282124") == "4b1fa9d7db722cbecfafa6cd4e0a179a4b0ea63f" || 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"), "104,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,1579,1580,12268,12269,12922,12923,12927,12929,12948,12949,12953,12954,12959,12960,12961,12962,12965,12966,12967,12968,12971,12972,12973,12974,12977,12978,12979,12980,12983,12984,12985,12986,12989,12990,12991,12992,12996,12997,13002,13003,13007,13008,13012,13013,13017,13018,13491,13492,14396,19247,25653,25654,25936,25937,27890,28057,28059,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,276653,277650,282053,282123,282124") && 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"), "294,1,85,101,105,109,110,4446,4450,4541,6593,6597,6598,6639,6640,6647,6653,6654,6655,7372,7376,7383,7384,7390,7396,7397,7398,7458,7462,7467,7468,10223,10224,13696,13697,14335,14339,15109,15110,15473,15477,15984,15985,15997,16003,16004,16005,17845,17849,19286,19287,26180,26181,26582,26583,32768,32770,32832,32845,33817,33818,41828,41829,43292,43293,54362,54370,54372,54373,54379,54385,54386,54387,54515,54521,54522,54523,54535,54541,54542,54543,54546,54547,54553,54559,54560,54561,54584,54590,54591,54592,55436,55437,65536,65549,65550,65551,69168,69169,84838,84839,84960,84961,85137,85138,85505,85506,86257,86258,92641,92642,97394,97395,98304,98306,98368,98381,99723,99724,99911,99912,99923,99929,99930,99931,99941,99942,99947,99951,99956,99957,99961,99967,99971,99972,99978,99984,99985,99986,99998,100004,100005,100006,100016,100022,100023,100024,100026,100033,100039,100040,101797,101798,101804,101805,101939,101940,103148,103155,103209,103216,103221,103228,103253,103260,103279,103286,103304,103311,103327,103328,103339,103341,103510,103511,103512,103513,103514,103515,103516,103517,103553,103555,103563,103564,103569,103570,103907,103911,103915,103917,103922,103926,103927,103931,103932,103936,103937,103941,103942,103946,103947,103951,103952,103956,103957,103959,103960,103961,103964,103970,103971,103975,103976,103980,103981,103985,103986,103988,103989,103993,103994,103998,103999,104001,104002,104006,104007,104011,104012,104016,104017,104021,104022,104026,104027,104031,104032,104036,104037,104041,104042,104044,104048,104052,104053,104057,104058,104062,104063,104067,104068,104070,104071,104075,104076,104078,104079,104083,104084,104086,104087,104091,104092,104096,104097,104099,104100,104104,104105,104256,104293,104551,105283,105284,108888,108889,110190,110191,114934,114935,119616,119618,119628,163842,163904,229378,229440,229446,229453,255611,256392,260458,260522,260523") == "d687d8cd9a70476a59a0a8064a5155b97de1e8e8" || 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"), "294,1,85,101,105,109,110,4446,4450,4541,6593,6597,6598,6639,6640,6647,6653,6654,6655,7372,7376,7383,7384,7390,7396,7397,7398,7458,7462,7467,7468,10223,10224,13696,13697,14335,14339,15109,15110,15473,15477,15984,15985,15997,16003,16004,16005,17845,17849,19286,19287,26180,26181,26582,26583,32768,32770,32832,32845,33817,33818,41828,41829,43292,43293,54362,54370,54372,54373,54379,54385,54386,54387,54515,54521,54522,54523,54535,54541,54542,54543,54546,54547,54553,54559,54560,54561,54584,54590,54591,54592,55436,55437,65536,65549,65550,65551,69168,69169,84838,84839,84960,84961,85137,85138,85505,85506,86257,86258,92641,92642,97394,97395,98304,98306,98368,98381,99723,99724,99911,99912,99923,99929,99930,99931,99941,99942,99947,99951,99956,99957,99961,99967,99971,99972,99978,99984,99985,99986,99998,100004,100005,100006,100016,100022,100023,100024,100026,100033,100039,100040,101797,101798,101804,101805,101939,101940,103148,103155,103209,103216,103221,103228,103253,103260,103279,103286,103304,103311,103327,103328,103339,103341,103510,103511,103512,103513,103514,103515,103516,103517,103553,103555,103563,103564,103569,103570,103907,103911,103915,103917,103922,103926,103927,103931,103932,103936,103937,103941,103942,103946,103947,103951,103952,103956,103957,103959,103960,103961,103964,103970,103971,103975,103976,103980,103981,103985,103986,103988,103989,103993,103994,103998,103999,104001,104002,104006,104007,104011,104012,104016,104017,104021,104022,104026,104027,104031,104032,104036,104037,104041,104042,104044,104048,104052,104053,104057,104058,104062,104063,104067,104068,104070,104071,104075,104076,104078,104079,104083,104084,104086,104087,104091,104092,104096,104097,104099,104100,104104,104105,104256,104293,104551,105283,105284,108888,108889,110190,110191,114934,114935,119616,119618,119628,163842,163904,229378,229440,229446,229453,255611,256392,260458,260522,260523") && 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:b9ae611eb72039b99ee4518a8ad639ad00ac1e13",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:80dcbaa3d558d36de0d1ea66a9c1de062d0dabae",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:80dcbaa3d558d36de0d1ea66a9c1de062d0dabae");

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

# Update dynamic partition metadata

assert(update_dynamic_partitions(package_extract_file("dynamic_partitions_op_list")));

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