getprop("ro.product.device") == "phoenixin" || abort("E3004: This package is for \"phoenixin\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: POCO/phoenixin/phoenixin:10/QKQ1.190825.002/V12.0.1.0.QGHINXM:user/release-keys");
ui_print("Target: POCO/phoenixin/phoenixin:10/QKQ1.190825.002/V12.0.2.0.QGHINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/phoenixin/phoenixin:10/QKQ1.190825.002/V12.0.1.0.QGHINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/phoenixin/phoenixin:10/QKQ1.190825.002/V12.0.2.0.QGHINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/phoenixin/phoenixin:10/QKQ1.190825.002/V12.0.1.0.QGHINXM:user/release-keys or POCO/phoenixin/phoenixin:10/QKQ1.190825.002/V12.0.2.0.QGHINXM: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:362b5606f7c37bfa6e318b5e34d2076cbb2fa9c9",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:ba858894b7c97bd0149cc8d4308155600ebbb1cc") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:362b5606f7c37bfa6e318b5e34d2076cbb2fa9c9\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:ba858894b7c97bd0149cc8d4308155600ebbb1cc\" 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"), "50,1,32770,32935,98306,98471,101990,116703,116704,121772,121774,121978,158537,159460,159462,159487,163842,164007,229378,229543,294914,295079,379899,380041,507686,514103,514104,524288,524314,528617,528619,528626,528630,529627,529628,529634,529635,529640,529641,531594,531595,533037,558665,559200,655375,655378,677025,679116,689878,690024,690025") == "ddbf177a0dfc313f120e35109112f26d80bf5724" || 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"), "50,1,32770,32935,98306,98471,101990,116703,116704,121772,121774,121978,158537,159460,159462,159487,163842,164007,229378,229543,294914,295079,379899,380041,507686,514103,514104,524288,524314,528617,528619,528626,528630,529627,529628,529634,529635,529640,529641,531594,531595,533037,558665,559200,655375,655378,677025,679116,689878,690024,690025") && 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,32849,98306,98385,163842,163921,229378,229457,294914,294993,295007,295013,325336,326371,331545,331623,331624") == "9cccc9fcc664e7467c647a527d329570d9bd8dad" || 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,32849,98306,98385,163842,163921,229378,229457,294914,294993,295007,295013,325336,326371,331545,331623,331624") && 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"), "16,1,32770,32828,98306,98364,163842,163900,229378,229436,229443,229448,239650,240379,244190,244253,244254") == "1f2a9529a4e43b1a9ac28fc959bcd5c2b9789f1a" || 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"), "16,1,32770,32828,98306,98364,163842,163900,229378,229436,229443,229448,239650,240379,244190,244253,244254") && 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;
if (range_sha1(map_partition("odm"), "8,1,11,12,203,206,212,228,229") == "824402e4fa179efcaec4383139bf77b229f877a5" || block_image_verify(map_partition("odm"), package_extract_file("odm.transfer.list"), "odm.new.dat", "odm.patch.dat")) then
ui_print("Verified odm image...");
else
check_first_block(map_partition("odm"));
ifelse (block_image_recover(map_partition("odm"), "8,1,11,12,203,206,212,228,229") && block_image_verify(map_partition("odm"), package_extract_file("odm.transfer.list"), "odm.new.dat", "odm.patch.dat"), ui_print("odm recovered successfully."), abort("E2004: odm 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:362b5606f7c37bfa6e318b5e34d2076cbb2fa9c9",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:ba858894b7c97bd0149cc8d4308155600ebbb1cc",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:ba858894b7c97bd0149cc8d4308155600ebbb1cc");

# ---- 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/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/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/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/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/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/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
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.");

# Patch partition odm

ui_print("Patching odm image after verification.");
block_image_update(map_partition("odm"), package_extract_file("odm.transfer.list"), "odm.new.dat", "odm.patch.dat") ||
  abort("E2001: Failed to update odm 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");
