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_id/citrus:10/QKQ1.200830.002/V12.0.4.0.QJFIDXM:user/release-keys");
ui_print("Target: POCO/citrus_id/citrus:10/QKQ1.200830.002/V12.0.5.0.QJFIDXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/citrus_id/citrus:10/QKQ1.200830.002/V12.0.4.0.QJFIDXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/citrus_id/citrus:10/QKQ1.200830.002/V12.0.5.0.QJFIDXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/citrus_id/citrus:10/QKQ1.200830.002/V12.0.4.0.QJFIDXM:user/release-keys or POCO/citrus_id/citrus:10/QKQ1.200830.002/V12.0.5.0.QJFIDXM: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:a69021598f786ca24eaafaa9ee791d71f52d2873",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:03d17046b341b3f65ab0ae3491db1788e3084287") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:a69021598f786ca24eaafaa9ee791d71f52d2873\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:03d17046b341b3f65ab0ae3491db1788e3084287\" 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"), "24,1,32770,32943,67178,67865,67867,67891,98306,98479,163842,164015,195278,195413,229378,229551,294914,295087,688132,688147,706639,708821,720053,720204,720205") == "b4d0459fbc9fc1b5464b91e9984940f589db10df" || 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"), "24,1,32770,32943,67178,67865,67867,67891,98306,98479,163842,164015,195278,195413,229378,229551,294914,295087,688132,688147,706639,708821,720053,720204,720205") && 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"), "66,1,103,113,117,122,123,205,209,214,215,1889,1893,2370,2371,2634,2638,2642,2643,2645,2649,2651,2662,2685,2686,2722,2726,2776,2777,2938,2939,2951,2955,2959,2960,2963,2969,2970,2971,2983,2987,2991,2998,3007,3008,3019,3042,3179,32770,32843,46910,46948,98306,98379,163842,163915,229378,229451,294914,294987,295004,295007,298403,299345,304091,304165,304166") == "77f28abd750e1a2f5d2079767ae71a2278f22acd" || 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"), "66,1,103,113,117,122,123,205,209,214,215,1889,1893,2370,2371,2634,2638,2642,2643,2645,2649,2651,2662,2685,2686,2722,2726,2776,2777,2938,2939,2951,2955,2959,2960,2963,2969,2970,2971,2983,2987,2991,2998,3007,3008,3019,3042,3179,32770,32843,46910,46948,98306,98379,163842,163915,229378,229451,294914,294987,295004,295007,298403,299345,304091,304165,304166") && 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"), "352,1,86,102,106,110,111,4447,4451,5926,5927,6591,6594,6598,6599,6640,6641,6648,6654,6655,6656,7373,7377,7384,7385,7391,7397,7398,7399,10154,10155,13627,13628,14266,14270,15040,15041,15404,15408,15915,15916,15928,15934,15935,15936,17776,17780,19217,19218,26111,26112,26513,26514,32768,32770,32833,32846,33749,33750,41760,41761,43224,43225,54294,54295,54301,54302,54308,54314,54315,54316,54444,54450,54451,54452,54464,54470,54471,54472,54475,54476,54482,54488,54489,54490,54513,54519,54520,54521,55365,55366,65536,65549,65550,65551,69097,69098,84767,84768,84889,84890,85066,85067,85434,85435,86186,86187,92570,92571,97323,97324,98304,98306,98369,98382,99653,99654,99841,99842,99853,99859,99860,99861,99871,99872,99877,99881,99886,99887,99891,99897,99901,99902,99908,99914,99915,99916,99928,99934,99935,99936,99946,99952,99953,99954,99956,99963,99969,99970,101727,101728,101734,101735,101869,101870,103078,103085,103154,103161,103166,103173,103203,103210,103229,103236,103254,103261,103277,103278,103289,103291,103460,103461,103462,103463,103464,103465,103466,103467,103504,103506,103514,103515,103520,103521,103881,103885,103889,103891,103896,103900,103901,103905,103906,103910,103911,103915,103916,103920,103921,103925,103926,103930,103931,103935,103936,103938,103939,103940,103943,103949,103950,103954,103955,103959,103960,103964,103965,103969,103970,103974,103975,103979,103980,103982,103983,103987,103988,103992,103993,103997,103998,104002,104003,104007,104008,104012,104013,104017,104018,104022,104023,104025,104029,104033,104034,104038,104039,104043,104044,104048,104049,104051,104052,104056,104057,104059,104060,104064,104065,104069,104070,104072,104073,104077,104078,104082,104083,104085,104086,104090,104091,104097,104100,104252,104289,104547,105270,105271,109079,109080,110284,110285,115348,115349,119874,119876,119886,119932,122747,122749,128098,128100,131072,131090,132050,132051,136100,136102,137632,137634,137860,137861,139551,139561,143071,143072,147782,147783,147786,147787,148389,148391,148817,148819,148998,149000,149045,149047,149187,149189,150445,150447,151524,151526,151986,151987,153615,153616,154260,154261,157893,157894,160527,160528,163840,163842,163905,163942,165393,165394,165419,165421,165439,229378,229441,229447,229454,258072,258861,262966,263031,263032") == "f78c4c8496d878553d4051d79053c959cd700253" || 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"), "352,1,86,102,106,110,111,4447,4451,5926,5927,6591,6594,6598,6599,6640,6641,6648,6654,6655,6656,7373,7377,7384,7385,7391,7397,7398,7399,10154,10155,13627,13628,14266,14270,15040,15041,15404,15408,15915,15916,15928,15934,15935,15936,17776,17780,19217,19218,26111,26112,26513,26514,32768,32770,32833,32846,33749,33750,41760,41761,43224,43225,54294,54295,54301,54302,54308,54314,54315,54316,54444,54450,54451,54452,54464,54470,54471,54472,54475,54476,54482,54488,54489,54490,54513,54519,54520,54521,55365,55366,65536,65549,65550,65551,69097,69098,84767,84768,84889,84890,85066,85067,85434,85435,86186,86187,92570,92571,97323,97324,98304,98306,98369,98382,99653,99654,99841,99842,99853,99859,99860,99861,99871,99872,99877,99881,99886,99887,99891,99897,99901,99902,99908,99914,99915,99916,99928,99934,99935,99936,99946,99952,99953,99954,99956,99963,99969,99970,101727,101728,101734,101735,101869,101870,103078,103085,103154,103161,103166,103173,103203,103210,103229,103236,103254,103261,103277,103278,103289,103291,103460,103461,103462,103463,103464,103465,103466,103467,103504,103506,103514,103515,103520,103521,103881,103885,103889,103891,103896,103900,103901,103905,103906,103910,103911,103915,103916,103920,103921,103925,103926,103930,103931,103935,103936,103938,103939,103940,103943,103949,103950,103954,103955,103959,103960,103964,103965,103969,103970,103974,103975,103979,103980,103982,103983,103987,103988,103992,103993,103997,103998,104002,104003,104007,104008,104012,104013,104017,104018,104022,104023,104025,104029,104033,104034,104038,104039,104043,104044,104048,104049,104051,104052,104056,104057,104059,104060,104064,104065,104069,104070,104072,104073,104077,104078,104082,104083,104085,104086,104090,104091,104097,104100,104252,104289,104547,105270,105271,109079,109080,110284,110285,115348,115349,119874,119876,119886,119932,122747,122749,128098,128100,131072,131090,132050,132051,136100,136102,137632,137634,137860,137861,139551,139561,143071,143072,147782,147783,147786,147787,148389,148391,148817,148819,148998,149000,149045,149047,149187,149189,150445,150447,151524,151526,151986,151987,153615,153616,154260,154261,157893,157894,160527,160528,163840,163842,163905,163942,165393,165394,165419,165421,165439,229378,229441,229447,229454,258072,258861,262966,263031,263032") && 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:a69021598f786ca24eaafaa9ee791d71f52d2873",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:03d17046b341b3f65ab0ae3491db1788e3084287",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:03d17046b341b3f65ab0ae3491db1788e3084287");

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


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