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.1.0.QJGMIXM:user/release-keys");
ui_print("Target: POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.2.0.QJGMIXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.1.0.QJGMIXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.2.0.QJGMIXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.1.0.QJGMIXM:user/release-keys or POCO/surya_global/surya:10/QKQ1.200512.002/V12.0.2.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:febba62833eeea5520229c6cc4ee00b7b8af16da",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:de16d1c7d8bf4cb027a412023c1a240666e7a8c6") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:febba62833eeea5520229c6cc4ee00b7b8af16da\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:de16d1c7d8bf4cb027a412023c1a240666e7a8c6\" 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"), "228,1,190,218,227,280,281,541,545,557,558,577,578,1895,1896,2325,2326,26230,26231,27598,27599,32768,32770,32924,32944,33002,33003,33217,33219,33379,33383,33391,33392,33400,33404,33409,33410,33629,33630,33642,33646,33650,34888,34892,34893,34903,34913,34919,34925,34926,34927,34932,34938,34939,34940,35059,35065,35071,35072,36628,36629,36694,36695,37220,37224,37253,37254,37324,37326,37419,37423,37427,37428,37459,37465,37466,37467,37566,37572,37573,41250,41263,41269,41270,41271,41273,41274,44554,44558,44599,44600,44970,44974,44982,44983,45070,45076,45081,45082,45210,45214,45220,45221,45483,45484,45676,45680,45695,45696,50313,50317,50411,50412,50417,50423,50424,51139,51197,51198,51269,51273,51277,51278,52561,52565,52590,53047,53048,53049,54811,54815,54858,54859,54863,54869,54870,54871,54893,54897,54921,98306,98460,111670,111693,111694,112058,112064,112078,112079,113194,113198,113224,113225,113238,113244,113245,113246,129814,129815,131072,131092,133666,133667,134814,134818,135032,135033,135421,135427,135441,135442,135452,135456,135463,135464,140215,140216,143051,143055,143138,143139,145698,145702,145742,145743,145794,145798,145802,145803,145806,145812,145816,145817,145822,145828,145829,145830,149703,149707,149773,149774,149858,149862,149867,149868,149870,149876,149877,149878,149922,149928,149929,163842,163996,174785,175708,175710,175735,229378,229532,294914,295068,622609,622612,630817,632773,642800,642938,642939") == "e54193c218b4b5434b4652c096c1cf7bb166c850" || 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"), "228,1,190,218,227,280,281,541,545,557,558,577,578,1895,1896,2325,2326,26230,26231,27598,27599,32768,32770,32924,32944,33002,33003,33217,33219,33379,33383,33391,33392,33400,33404,33409,33410,33629,33630,33642,33646,33650,34888,34892,34893,34903,34913,34919,34925,34926,34927,34932,34938,34939,34940,35059,35065,35071,35072,36628,36629,36694,36695,37220,37224,37253,37254,37324,37326,37419,37423,37427,37428,37459,37465,37466,37467,37566,37572,37573,41250,41263,41269,41270,41271,41273,41274,44554,44558,44599,44600,44970,44974,44982,44983,45070,45076,45081,45082,45210,45214,45220,45221,45483,45484,45676,45680,45695,45696,50313,50317,50411,50412,50417,50423,50424,51139,51197,51198,51269,51273,51277,51278,52561,52565,52590,53047,53048,53049,54811,54815,54858,54859,54863,54869,54870,54871,54893,54897,54921,98306,98460,111670,111693,111694,112058,112064,112078,112079,113194,113198,113224,113225,113238,113244,113245,113246,129814,129815,131072,131092,133666,133667,134814,134818,135032,135033,135421,135427,135441,135442,135452,135456,135463,135464,140215,140216,143051,143055,143138,143139,145698,145702,145742,145743,145794,145798,145802,145803,145806,145812,145816,145817,145822,145828,145829,145830,149703,149707,149773,149774,149858,149862,149867,149868,149870,149876,149877,149878,149922,149928,149929,163842,163996,174785,175708,175710,175735,229378,229532,294914,295068,622609,622612,630817,632773,642800,642938,642939") && 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"), "384,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,1165,1502,1579,1580,1628,1668,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,23882,23883,24165,24166,26075,32770,32837,33203,33240,33241,33247,33248,33273,33274,33386,33387,33424,33425,33638,33666,33824,33825,33827,33829,33893,33894,33964,33965,34032,34033,34035,34036,34038,34039,34098,34100,34102,34103,34167,34168,34238,34239,34306,34307,34309,34310,34312,34315,34316,34317,34379,34380,34446,34447,34449,34450,34453,34457,34459,34460,34462,34464,34525,34526,34528,34530,34590,34591,34593,34594,34655,34656,34658,34659,34719,34720,34722,34723,34787,34788,34856,34857,34924,34925,34991,34992,34994,34995,35058,35059,35125,35126,35128,35129,35190,35191,35193,35194,35258,35259,35261,35262,35324,35325,35327,35328,35392,35393,35395,35396,35457,35458,35460,35461,35525,35526,35528,35529,35593,35594,35596,35597,35661,35662,35664,35665,35729,35730,35732,35733,35800,35801,35802,35803,35869,35870,35871,35872,35877,35878,35938,35939,36005,36006,36008,36009,36072,36073,36139,36140,36142,36143,36204,36205,36207,36208,36272,36273,36275,36276,36338,36339,36341,36342,36406,36407,36409,36410,36471,36472,36474,36475,36539,36540,36542,36543,36607,36608,36610,36611,36675,36676,36678,36679,36743,36744,36746,36747,36814,36815,36816,36817,36883,36884,36885,36886,36951,36952,37024,37025,37027,37028,37090,37091,37161,37162,37170,37174,37175,37176,37375,37376,37442,37443,37445,37446,37449,37454,43866,44066,44301,44302,44874,44875,45039,45040,45349,45350,45411,45412,45561,45562,45641,45642,45674,45675,45751,45752,45819,45820,45892,45893,45985,45986,46058,46059,46104,46105,46209,46210,46253,46254,46256,46257,46289,46290,46451,46452,46523,46524,46576,46577,46901,46902,46904,46905,46999,47000,47124,47125,47199,47200,47233,47234,47297,47298,47369,47370,47919,47921,47926,47928,47945,98306,98373,163842,163909,229378,229445,262165,262167,274351,275335,279700,279769,279770") == "2452c89d35f994f63d9f7840dc1fdfafbfb29f95" || 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"), "384,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,1165,1502,1579,1580,1628,1668,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,23882,23883,24165,24166,26075,32770,32837,33203,33240,33241,33247,33248,33273,33274,33386,33387,33424,33425,33638,33666,33824,33825,33827,33829,33893,33894,33964,33965,34032,34033,34035,34036,34038,34039,34098,34100,34102,34103,34167,34168,34238,34239,34306,34307,34309,34310,34312,34315,34316,34317,34379,34380,34446,34447,34449,34450,34453,34457,34459,34460,34462,34464,34525,34526,34528,34530,34590,34591,34593,34594,34655,34656,34658,34659,34719,34720,34722,34723,34787,34788,34856,34857,34924,34925,34991,34992,34994,34995,35058,35059,35125,35126,35128,35129,35190,35191,35193,35194,35258,35259,35261,35262,35324,35325,35327,35328,35392,35393,35395,35396,35457,35458,35460,35461,35525,35526,35528,35529,35593,35594,35596,35597,35661,35662,35664,35665,35729,35730,35732,35733,35800,35801,35802,35803,35869,35870,35871,35872,35877,35878,35938,35939,36005,36006,36008,36009,36072,36073,36139,36140,36142,36143,36204,36205,36207,36208,36272,36273,36275,36276,36338,36339,36341,36342,36406,36407,36409,36410,36471,36472,36474,36475,36539,36540,36542,36543,36607,36608,36610,36611,36675,36676,36678,36679,36743,36744,36746,36747,36814,36815,36816,36817,36883,36884,36885,36886,36951,36952,37024,37025,37027,37028,37090,37091,37161,37162,37170,37174,37175,37176,37375,37376,37442,37443,37445,37446,37449,37454,43866,44066,44301,44302,44874,44875,45039,45040,45349,45350,45411,45412,45561,45562,45641,45642,45674,45675,45751,45752,45819,45820,45892,45893,45985,45986,46058,46059,46104,46105,46209,46210,46253,46254,46256,46257,46289,46290,46451,46452,46523,46524,46576,46577,46901,46902,46904,46905,46999,47000,47124,47125,47199,47200,47233,47234,47297,47298,47369,47370,47919,47921,47926,47928,47945,98306,98373,163842,163909,229378,229445,262165,262167,274351,275335,279700,279769,279770") && 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,32838,98306,98374,163842,163910,229378,229446,262147,262156,278420,279266,283694,283763,283764") == "bdfb37a84b2e0035418eea4ca1bc21c5a998a86f" || 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,32838,98306,98374,163842,163910,229378,229446,262147,262156,278420,279266,283694,283763,283764") && 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:febba62833eeea5520229c6cc4ee00b7b8af16da",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:de16d1c7d8bf4cb027a412023c1a240666e7a8c6",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:de16d1c7d8bf4cb027a412023c1a240666e7a8c6");

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