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_ru/citrus:10/QKQ1.200830.002/V12.0.1.0.QJFRUXM:user/release-keys");
ui_print("Target: POCO/citrus_ru/citrus:10/QKQ1.200830.002/V12.0.3.0.QJFRUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/citrus_ru/citrus:10/QKQ1.200830.002/V12.0.1.0.QJFRUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/citrus_ru/citrus:10/QKQ1.200830.002/V12.0.3.0.QJFRUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/citrus_ru/citrus:10/QKQ1.200830.002/V12.0.1.0.QJFRUXM:user/release-keys or POCO/citrus_ru/citrus:10/QKQ1.200830.002/V12.0.3.0.QJFRUXM: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:d1c8ea294ca4c7d8c45f286648fb343185f8c7c6",
                      "EMMC:/dev/block/bootdevice/by-name/boot:100663296:0016b2db473793506b45f6e34849a35953a8ff27") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:d1c8ea294ca4c7d8c45f286648fb343185f8c7c6\" or \"EMMC:/dev/block/bootdevice/by-name/boot:100663296:0016b2db473793506b45f6e34849a35953a8ff27\" 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"), "108,1,202,229,238,258,523,535,536,555,556,1867,26205,26211,26212,27579,27580,32768,32770,32937,32956,32995,32996,33210,33212,33375,33388,33396,33400,33405,38284,38296,38300,38304,39619,39621,39622,39623,39624,39634,39644,39650,39656,39657,39658,39663,39669,39670,39671,39791,39797,39803,39804,41603,41604,41655,41656,41742,41743,41843,41844,41911,41912,42296,42300,42339,42340,43304,43305,43955,43957,45888,45890,45898,45899,45921,45922,45926,45927,46360,46361,46392,46398,46399,46400,46499,46505,46506,49332,49346,49352,49353,49354,49356,98306,98473,163842,164009,229378,229545,294914,295081,655378,655379,683654,685767,696633,696782,696783") == "96ea01c4751e3cf4b5dcf2e3615268704d9c034c" || 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"), "108,1,202,229,238,258,523,535,536,555,556,1867,26205,26211,26212,27579,27580,32768,32770,32937,32956,32995,32996,33210,33212,33375,33388,33396,33400,33405,38284,38296,38300,38304,39619,39621,39622,39623,39624,39634,39644,39650,39656,39657,39658,39663,39669,39670,39671,39791,39797,39803,39804,41603,41604,41655,41656,41742,41743,41843,41844,41911,41912,42296,42300,42339,42340,43304,43305,43955,43957,45888,45890,45898,45899,45921,45922,45926,45927,46360,46361,46392,46398,46399,46400,46499,46505,46506,49332,49346,49352,49353,49354,49356,98306,98473,163842,164009,229378,229545,294914,295081,655378,655379,683654,685767,696633,696782,696783") && 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"), "18,1,32770,32842,98306,98378,163842,163914,229378,229450,294914,294986,295002,295006,297046,297984,302709,302781,302782") == "fd415eb1fbea2aa5318f0df4720c7f83f4b8d770" || 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"), "18,1,32770,32842,98306,98378,163842,163914,229378,229450,294914,294986,295002,295006,297046,297984,302709,302781,302782") && 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"), "306,1,90,106,110,114,115,4451,4455,5930,5931,6595,6598,6602,6603,6644,6645,6652,6658,6659,6660,7377,7381,7388,7389,7395,7401,7402,7403,10158,10159,13631,13632,14270,14274,15044,15045,17452,17453,17816,17820,18327,18328,18340,18346,18347,18348,25241,25242,25643,25644,32768,32770,32839,32850,32883,32884,40894,40895,42358,42359,53428,53429,55687,55688,56064,56065,56143,56145,59513,59515,65536,65547,67006,67007,67013,67014,67020,67026,67027,67028,67156,67162,67163,67164,67176,67182,67183,67184,67187,67188,67194,67200,67201,67202,67225,67231,67232,67233,68077,68078,81776,81777,81796,81797,83763,83764,97582,97583,97759,97760,98127,98128,98304,98306,98375,98386,98961,98962,105345,105346,110098,110099,112350,112351,112538,112539,112550,112556,112557,112558,112568,112569,112574,112578,112583,112584,112588,112594,112598,112599,112605,112611,112612,112613,112625,112631,112632,112633,112643,112649,112650,112651,112653,112660,112666,112667,114424,114425,114431,114432,114566,114567,115775,115782,115851,115858,115863,115870,115900,115907,115926,115933,115951,115958,115974,115975,115986,115988,116157,116158,116159,116160,116161,116162,116163,116164,116202,116204,116212,116213,116215,116217,116218,116219,116580,116584,116588,116592,116593,116595,116600,116604,116605,116609,116610,116614,116615,116619,116620,116624,116625,116629,116630,116634,116635,116639,116640,116642,116643,116644,116647,116653,116654,116658,116659,116663,116664,116668,116669,116673,116674,116678,116679,116683,116684,116686,116687,116691,116692,116696,116697,116701,116702,116706,116707,116711,116712,116716,116717,116721,116722,116726,116727,116729,116733,116737,116738,116742,116743,116747,116748,116752,116753,116755,116756,116760,116761,116763,116764,116768,116769,116773,116774,116776,116777,116781,116782,116786,116787,116789,116790,116794,116795,116805,116808,116960,116997,117255,117978,117979,121787,121788,131072,131085,132593,132595,132605,163842,163911,229378,229447,283364,284234,288741,288811,288812") == "c6799e6f5912e5d575aa59a76e9ca7e002ca8956" || 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"), "306,1,90,106,110,114,115,4451,4455,5930,5931,6595,6598,6602,6603,6644,6645,6652,6658,6659,6660,7377,7381,7388,7389,7395,7401,7402,7403,10158,10159,13631,13632,14270,14274,15044,15045,17452,17453,17816,17820,18327,18328,18340,18346,18347,18348,25241,25242,25643,25644,32768,32770,32839,32850,32883,32884,40894,40895,42358,42359,53428,53429,55687,55688,56064,56065,56143,56145,59513,59515,65536,65547,67006,67007,67013,67014,67020,67026,67027,67028,67156,67162,67163,67164,67176,67182,67183,67184,67187,67188,67194,67200,67201,67202,67225,67231,67232,67233,68077,68078,81776,81777,81796,81797,83763,83764,97582,97583,97759,97760,98127,98128,98304,98306,98375,98386,98961,98962,105345,105346,110098,110099,112350,112351,112538,112539,112550,112556,112557,112558,112568,112569,112574,112578,112583,112584,112588,112594,112598,112599,112605,112611,112612,112613,112625,112631,112632,112633,112643,112649,112650,112651,112653,112660,112666,112667,114424,114425,114431,114432,114566,114567,115775,115782,115851,115858,115863,115870,115900,115907,115926,115933,115951,115958,115974,115975,115986,115988,116157,116158,116159,116160,116161,116162,116163,116164,116202,116204,116212,116213,116215,116217,116218,116219,116580,116584,116588,116592,116593,116595,116600,116604,116605,116609,116610,116614,116615,116619,116620,116624,116625,116629,116630,116634,116635,116639,116640,116642,116643,116644,116647,116653,116654,116658,116659,116663,116664,116668,116669,116673,116674,116678,116679,116683,116684,116686,116687,116691,116692,116696,116697,116701,116702,116706,116707,116711,116712,116716,116717,116721,116722,116726,116727,116729,116733,116737,116738,116742,116743,116747,116748,116752,116753,116755,116756,116760,116761,116763,116764,116768,116769,116773,116774,116776,116777,116781,116782,116786,116787,116789,116790,116794,116795,116805,116808,116960,116997,117255,117978,117979,121787,121788,131072,131085,132593,132595,132605,163842,163911,229378,229447,283364,284234,288741,288811,288812") && 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:d1c8ea294ca4c7d8c45f286648fb343185f8c7c6",
                "EMMC:/dev/block/bootdevice/by-name/boot:100663296:0016b2db473793506b45f6e34849a35953a8ff27",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:100663296:0016b2db473793506b45f6e34849a35953a8ff27");

# ---- 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/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem");
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/imagefv.elf", "/dev/block/bootdevice/by-name/imagefv");
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/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/imagefv.elf", "/dev/block/bootdevice/by-name/imagefvbak");
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");
