getprop("ro.product.device") == "citrus" || abort("E3004: This package is for \"citrus\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: POCO/citrus_eea/citrus:10/QKQ1.200830.002/V12.0.5.0.QJFEUXM:user/release-keys");
ui_print("Target: POCO/citrus_eea/citrus:10/QKQ1.200830.002/V12.0.6.0.QJFEUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/citrus_eea/citrus:10/QKQ1.200830.002/V12.0.5.0.QJFEUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/citrus_eea/citrus:10/QKQ1.200830.002/V12.0.6.0.QJFEUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/citrus_eea/citrus:10/QKQ1.200830.002/V12.0.5.0.QJFEUXM:user/release-keys or POCO/citrus_eea/citrus:10/QKQ1.200830.002/V12.0.6.0.QJFEUXM: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:100663296:3d5d9055fb452d2407b14b282469365bd09cf6b4",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:1a0893c562cb425f0ab4a59e5665f5bf204c8a17") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:3d5d9055fb452d2407b14b282469365bd09cf6b4\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:1a0893c562cb425f0ab4a59e5665f5bf204c8a17\" has unexpected contents.");
apply_patch_space(100663296) || 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,32935,67162,67849,67851,67875,98306,98471,163842,164007,229378,229543,294914,295079,655377,655379,676126,678216,688963,689109,689110") == "ae15e9c1f69a1f05d53de3f167fc4594a9e74031" || 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,32935,67162,67849,67851,67875,98306,98471,163842,164007,229378,229543,294914,295079,655377,655379,676126,678216,688963,689109,689110") && 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"), "20,1,32770,32842,46912,46950,98306,98378,163842,163914,229378,229450,294914,294986,295003,295006,297182,298120,302847,302920,302921") == "f2becabbf681349f571040515e429a8c9e182874" || 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"), "20,1,32770,32842,46912,46950,98306,98378,163842,163914,229378,229450,294914,294986,295003,295006,297182,298120,302847,302920,302921") && 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,5383,5384,5400,5404,5408,5409,9699,9700,9745,9749,11224,11225,11889,11892,11896,11897,11938,11939,11946,11952,11953,11954,12671,12675,12682,12683,12689,12695,12696,12697,15452,15453,18925,18926,21333,21334,21697,21701,22208,22209,22221,22227,22228,22229,24069,24073,25510,25511,32404,32405,32768,32770,32841,32853,32891,32892,40057,40058,48060,48061,49524,49525,60594,60595,62853,62854,63230,63231,63309,63311,65536,65548,66691,66693,74173,74174,74180,74181,74187,74193,74194,74195,74323,74329,74330,74331,74343,74349,74350,74351,74354,74355,74361,74367,74368,74369,74392,74398,74399,74400,75244,75245,88943,88944,88963,88964,90930,90931,98304,98306,98377,98389,104834,104835,105011,105012,105379,105380,106131,106132,112515,112516,117268,117269,119520,119521,119708,119709,119720,119726,119727,119728,119738,119739,119744,119748,119753,119754,119758,119764,119768,119769,119775,119781,119782,119783,119795,119801,119802,119803,119813,119819,119820,119821,119823,119830,119836,119837,121594,121595,121601,121602,121736,121737,122945,122952,123021,123028,123033,123040,123070,123077,123096,123103,123121,123128,123144,123145,123156,123158,123328,123329,123330,123331,123332,123333,123334,123335,123373,123375,123383,123384,123390,123391,123752,123756,123760,123764,123765,123767,123772,123776,123777,123781,123782,123786,123787,123791,123792,123796,123797,123801,123802,123806,123807,123811,123812,123814,123815,123816,123819,123825,123826,123830,123831,123835,123836,123840,123841,123845,123846,123850,123851,123855,123856,123858,123859,123863,123864,123868,123869,123873,123874,123878,123879,123883,123884,123888,123889,123893,123894,123898,123899,123901,123905,123909,123910,123914,123915,123919,123920,123924,123925,123927,123928,123932,123933,123935,123936,123940,123941,123945,123946,123948,123949,123953,123954,123958,123959,123961,123962,123966,123967,123977,123980,124132,124169,124427,125150,125151,128959,128960,130164,130165,131072,131084,135240,135241,136863,136866,139769,139771,139781,163842,163913,229378,229449,262148,262156,291327,292289,296923,296995,296996") == "d41da58ea8269e1c13858b10b4cfb443254628dd" || 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
ifelse (block_image_recover(map_partition("product"), "316,1,93,5383,5384,5400,5404,5408,5409,9699,9700,9745,9749,11224,11225,11889,11892,11896,11897,11938,11939,11946,11952,11953,11954,12671,12675,12682,12683,12689,12695,12696,12697,15452,15453,18925,18926,21333,21334,21697,21701,22208,22209,22221,22227,22228,22229,24069,24073,25510,25511,32404,32405,32768,32770,32841,32853,32891,32892,40057,40058,48060,48061,49524,49525,60594,60595,62853,62854,63230,63231,63309,63311,65536,65548,66691,66693,74173,74174,74180,74181,74187,74193,74194,74195,74323,74329,74330,74331,74343,74349,74350,74351,74354,74355,74361,74367,74368,74369,74392,74398,74399,74400,75244,75245,88943,88944,88963,88964,90930,90931,98304,98306,98377,98389,104834,104835,105011,105012,105379,105380,106131,106132,112515,112516,117268,117269,119520,119521,119708,119709,119720,119726,119727,119728,119738,119739,119744,119748,119753,119754,119758,119764,119768,119769,119775,119781,119782,119783,119795,119801,119802,119803,119813,119819,119820,119821,119823,119830,119836,119837,121594,121595,121601,121602,121736,121737,122945,122952,123021,123028,123033,123040,123070,123077,123096,123103,123121,123128,123144,123145,123156,123158,123328,123329,123330,123331,123332,123333,123334,123335,123373,123375,123383,123384,123390,123391,123752,123756,123760,123764,123765,123767,123772,123776,123777,123781,123782,123786,123787,123791,123792,123796,123797,123801,123802,123806,123807,123811,123812,123814,123815,123816,123819,123825,123826,123830,123831,123835,123836,123840,123841,123845,123846,123850,123851,123855,123856,123858,123859,123863,123864,123868,123869,123873,123874,123878,123879,123883,123884,123888,123889,123893,123894,123898,123899,123901,123905,123909,123910,123914,123915,123919,123920,123924,123925,123927,123928,123932,123933,123935,123936,123940,123941,123945,123946,123948,123949,123953,123954,123958,123959,123961,123962,123966,123967,123977,123980,124132,124169,124427,125150,125151,128959,128960,130164,130165,131072,131084,135240,135241,136863,136866,139769,139771,139781,163842,163913,229378,229449,262148,262156,291327,292289,296923,296995,296996") && 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:100663296:3d5d9055fb452d2407b14b282469365bd09cf6b4",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:1a0893c562cb425f0ab4a59e5665f5bf204c8a17",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:1a0893c562cb425f0ab4a59e5665f5bf204c8a17");

# ---- 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/featenabler.mbn", "/dev/block/bootdevice/by-name/featenabler");
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/dspso.bin", "/dev/block/bootdevice/by-name/dsp");
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/rpm.mbn", "/dev/block/bootdevice/by-name/rpm");
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/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/storsec.mbn", "/dev/block/bootdevice/by-name/storsecbak");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
package_extract_file("firmware-update/rpm.mbn", "/dev/block/bootdevice/by-name/rpmbak");
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/dtbo.img", "/dev/block/bootdevice/by-name/dtbo");
package_extract_file("firmware-update/vbmeta.img", "/dev/block/bootdevice/by-name/vbmeta");
package_extract_file("firmware-update/vbmeta_system.img", "/dev/block/bootdevice/by-name/vbmeta_system");
