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.2.0.QJGINXM:user/release-keys");
ui_print("Target: POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.5.0.QJGINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.2.0.QJGINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.5.0.QJGINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.2.0.QJGINXM:user/release-keys or POCO/surya_in/surya:10/QKQ1.200512.002/V12.0.5.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:d4a06bf47c63a2800384719279c2a21cdefe2ce8",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:cd53cfd69b7ba182019157a92d1a5dc73e79871c") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:d4a06bf47c63a2800384719279c2a21cdefe2ce8\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:cd53cfd69b7ba182019157a92d1a5dc73e79871c\" 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"), "22,1,32770,32923,98306,98459,163842,163995,185352,185915,185917,185934,229378,229531,294914,295067,622610,622612,628112,630059,640043,640181,640182") == "ddb9cbfee6e17622d51e0f162c133719f0ad40b0" || 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"), "22,1,32770,32923,98306,98459,163842,163995,185352,185915,185917,185934,229378,229531,294914,295067,622610,622612,628112,630059,640043,640181,640182") && 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"), "98,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,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,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,274835,275820,280192,280262,280263") == "09d1ef6f3bffb8a087f89da02c4f95f2975eafbe" || 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"), "98,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,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,32770,32837,98306,98373,163842,163909,229378,229445,262165,262167,274835,275820,280192,280262,280263") && 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"), "316,1,93,109,113,117,118,4454,4458,4549,6601,6605,6606,6647,6648,6655,6661,6662,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,64713,65536,65548,68937,68938,77369,77371,77372,77373,77374,77375,77654,77662,77664,77665,77671,77677,77678,77813,77814,77815,77827,77833,77834,77835,77838,77839,77845,77851,77852,77853,77876,77882,77883,77884,78728,78729,92427,92428,92447,92448,98304,98306,98377,98389,98390,98391,108324,108325,108501,108502,108869,108870,109621,109622,116005,116006,120758,120759,123009,123010,123197,123198,123209,123215,123216,123217,123227,123228,123233,123237,123242,123243,123247,123253,123257,123258,123264,123270,123271,123272,123284,123290,123291,123292,123302,123308,123309,123310,123312,123319,123325,123326,125083,125084,125090,125091,125225,125226,126434,126441,126495,126502,126507,126514,126539,126546,126565,126572,126590,126597,126613,126614,126625,126627,126796,126797,126798,126799,126800,126801,126802,126803,126840,126842,126850,126851,126856,126857,127195,127199,127203,127207,127208,127210,127215,127219,127220,127224,127225,127229,127230,127234,127235,127239,127240,127244,127245,127249,127250,127252,127253,127254,127257,127263,127264,127268,127269,127273,127274,127278,127279,127281,127282,127286,127287,127291,127292,127294,127295,127299,127300,127304,127305,127309,127310,127314,127315,127319,127320,127324,127325,127329,127330,127334,127335,127337,127341,127345,127346,127350,127351,127355,127356,127360,127361,127363,127364,127368,127369,127371,127372,127376,127377,127379,127380,127384,127385,127389,127390,127392,127393,127397,127398,127553,127590,127848,128580,128581,131072,131084,132197,132198,133499,133500,138243,138244,140022,140025,142401,142414,142418,142422,142424,163842,163913,229378,229449,262148,262156,290885,291845,296472,296545,296546") == "076f99b197ab5e41410edbd89bf49bed4c885c3c" || 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"), "316,1,93,109,113,117,118,4454,4458,4549,6601,6605,6606,6647,6648,6655,6661,6662,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,64713,65536,65548,68937,68938,77369,77371,77372,77373,77374,77375,77654,77662,77664,77665,77671,77677,77678,77813,77814,77815,77827,77833,77834,77835,77838,77839,77845,77851,77852,77853,77876,77882,77883,77884,78728,78729,92427,92428,92447,92448,98304,98306,98377,98389,98390,98391,108324,108325,108501,108502,108869,108870,109621,109622,116005,116006,120758,120759,123009,123010,123197,123198,123209,123215,123216,123217,123227,123228,123233,123237,123242,123243,123247,123253,123257,123258,123264,123270,123271,123272,123284,123290,123291,123292,123302,123308,123309,123310,123312,123319,123325,123326,125083,125084,125090,125091,125225,125226,126434,126441,126495,126502,126507,126514,126539,126546,126565,126572,126590,126597,126613,126614,126625,126627,126796,126797,126798,126799,126800,126801,126802,126803,126840,126842,126850,126851,126856,126857,127195,127199,127203,127207,127208,127210,127215,127219,127220,127224,127225,127229,127230,127234,127235,127239,127240,127244,127245,127249,127250,127252,127253,127254,127257,127263,127264,127268,127269,127273,127274,127278,127279,127281,127282,127286,127287,127291,127292,127294,127295,127299,127300,127304,127305,127309,127310,127314,127315,127319,127320,127324,127325,127329,127330,127334,127335,127337,127341,127345,127346,127350,127351,127355,127356,127360,127361,127363,127364,127368,127369,127371,127372,127376,127377,127379,127380,127384,127385,127389,127390,127392,127393,127397,127398,127553,127590,127848,128580,128581,131072,131084,132197,132198,133499,133500,138243,138244,140022,140025,142401,142414,142418,142422,142424,163842,163913,229378,229449,262148,262156,290885,291845,296472,296545,296546") && 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:d4a06bf47c63a2800384719279c2a21cdefe2ce8",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:cd53cfd69b7ba182019157a92d1a5dc73e79871c",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:cd53cfd69b7ba182019157a92d1a5dc73e79871c");

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