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_eea/surya:10/QKQ1.200512.002/V12.0.3.0.QJGEUXM:user/release-keys");
ui_print("Target: POCO/surya_eea/surya:10/QKQ1.200512.002/V12.0.4.0.QJGEUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/surya_eea/surya:10/QKQ1.200512.002/V12.0.3.0.QJGEUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/surya_eea/surya:10/QKQ1.200512.002/V12.0.4.0.QJGEUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/surya_eea/surya:10/QKQ1.200512.002/V12.0.3.0.QJGEUXM:user/release-keys or POCO/surya_eea/surya:10/QKQ1.200512.002/V12.0.4.0.QJGEUXM: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:4b953485ebcfb75ed0668569e31ededd7657acf2",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:dcfc3aa1ab7bf3e64ea715ce94bf7904645833ab") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:4b953485ebcfb75ed0668569e31ededd7657acf2\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:dcfc3aa1ab7bf3e64ea715ce94bf7904645833ab\" 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"), "52,1,196,224,233,286,287,547,551,563,564,583,584,1901,1902,2325,26230,26236,26237,27604,27605,32768,32770,32930,32950,33014,33015,33229,33231,33391,33395,33403,33404,33412,33416,33421,98306,98466,163842,164002,229378,229538,294914,295074,622609,622612,653927,655360,655361,655945,666341,666481,666482") == "11cfdd33add8730aba70b3f705d78c06a9d2a458" || 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"), "52,1,196,224,233,286,287,547,551,563,564,583,584,1901,1902,2325,26230,26236,26237,27604,27605,32768,32770,32930,32950,33014,33015,33229,33231,33391,33395,33403,33404,33412,33416,33421,98306,98466,163842,164002,229378,229538,294914,295074,622609,622612,653927,655360,655361,655945,666341,666481,666482") && 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"), "108,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,12268,12269,12446,12497,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") == "78bde2e9c556549a9c9c1a0084978f27c2c6bb57" || 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"), "108,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,12268,12269,12446,12497,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"), "308,1,92,5382,5383,5399,5403,5407,5408,9698,9699,9744,9748,9839,11891,11895,11896,11937,11938,11945,11951,11952,12674,12681,12682,12688,12694,12695,12696,12756,12760,12765,12766,15521,15522,18994,18995,21402,21403,21766,21770,22277,22278,22290,22296,22297,22298,24138,24142,25579,25580,32473,32474,32768,32770,32840,32852,32959,32960,40125,40126,48128,48129,49592,49593,60662,60663,64876,64877,65536,65548,73319,73321,73322,73323,73324,73325,73604,73612,73614,73615,73621,73627,73628,73763,73764,73765,73777,73783,73784,73785,73788,73789,73795,73801,73802,73803,73826,73832,73833,73834,74678,74679,88377,88378,88397,88398,98304,98306,98376,98388,98389,98390,104273,104274,104450,104451,104818,104819,105570,105571,111954,111955,116707,116708,118958,118959,119146,119147,119158,119164,119165,119166,119176,119177,119182,119186,119191,119192,119196,119202,119206,119207,119213,119219,119220,119221,119233,119239,119240,119241,119251,119257,119258,119259,119261,119268,119274,119275,121032,121033,121039,121040,121174,121175,122383,122390,122444,122451,122456,122463,122488,122495,122514,122521,122539,122546,122562,122563,122574,122576,122746,122747,122748,122749,122750,122751,122752,122753,122790,122792,122800,122801,122807,122808,123146,123150,123154,123158,123159,123161,123166,123170,123171,123175,123176,123180,123181,123185,123186,123190,123191,123195,123196,123200,123201,123203,123204,123205,123208,123214,123215,123219,123220,123224,123225,123229,123230,123232,123233,123237,123238,123242,123243,123245,123246,123250,123251,123255,123256,123260,123261,123265,123266,123270,123271,123275,123276,123280,123281,123285,123286,123288,123292,123296,123297,123301,123302,123306,123307,123311,123312,123314,123315,123319,123320,123322,123323,123327,123328,123330,123331,123335,123336,123340,123341,123343,123344,123348,123349,123504,123541,123799,124531,124532,128136,128137,131072,131085,134194,134195,135973,135976,138352,138365,138879,138881,138891,163842,163912,229378,229448,262148,262156,287388,288337,292908,292980,292981") == "a544a1b9cd6d88ee3a4fd31f5163a1b8efd62e5a" || 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"), "308,1,92,5382,5383,5399,5403,5407,5408,9698,9699,9744,9748,9839,11891,11895,11896,11937,11938,11945,11951,11952,12674,12681,12682,12688,12694,12695,12696,12756,12760,12765,12766,15521,15522,18994,18995,21402,21403,21766,21770,22277,22278,22290,22296,22297,22298,24138,24142,25579,25580,32473,32474,32768,32770,32840,32852,32959,32960,40125,40126,48128,48129,49592,49593,60662,60663,64876,64877,65536,65548,73319,73321,73322,73323,73324,73325,73604,73612,73614,73615,73621,73627,73628,73763,73764,73765,73777,73783,73784,73785,73788,73789,73795,73801,73802,73803,73826,73832,73833,73834,74678,74679,88377,88378,88397,88398,98304,98306,98376,98388,98389,98390,104273,104274,104450,104451,104818,104819,105570,105571,111954,111955,116707,116708,118958,118959,119146,119147,119158,119164,119165,119166,119176,119177,119182,119186,119191,119192,119196,119202,119206,119207,119213,119219,119220,119221,119233,119239,119240,119241,119251,119257,119258,119259,119261,119268,119274,119275,121032,121033,121039,121040,121174,121175,122383,122390,122444,122451,122456,122463,122488,122495,122514,122521,122539,122546,122562,122563,122574,122576,122746,122747,122748,122749,122750,122751,122752,122753,122790,122792,122800,122801,122807,122808,123146,123150,123154,123158,123159,123161,123166,123170,123171,123175,123176,123180,123181,123185,123186,123190,123191,123195,123196,123200,123201,123203,123204,123205,123208,123214,123215,123219,123220,123224,123225,123229,123230,123232,123233,123237,123238,123242,123243,123245,123246,123250,123251,123255,123256,123260,123261,123265,123266,123270,123271,123275,123276,123280,123281,123285,123286,123288,123292,123296,123297,123301,123302,123306,123307,123311,123312,123314,123315,123319,123320,123322,123323,123327,123328,123330,123331,123335,123336,123340,123341,123343,123344,123348,123349,123504,123541,123799,124531,124532,128136,128137,131072,131085,134194,134195,135973,135976,138352,138365,138879,138881,138891,163842,163912,229378,229448,262148,262156,287388,288337,292908,292980,292981") && 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:4b953485ebcfb75ed0668569e31ededd7657acf2",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:dcfc3aa1ab7bf3e64ea715ce94bf7904645833ab",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:dcfc3aa1ab7bf3e64ea715ce94bf7904645833ab");

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


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

# Update dynamic partition metadata

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

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