getprop("ro.product.device") == "phoenix" || abort("E3004: This package is for \"phoenix\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/phoenix/phoenix:10/QKQ1.190825.002/V12.0.4.0.QGHCNXM:user/release-keys");
ui_print("Target: Redmi/phoenix/phoenix:10/QKQ1.190825.002/V12.0.5.0.QGHCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/phoenix/phoenix:10/QKQ1.190825.002/V12.0.4.0.QGHCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/phoenix/phoenix:10/QKQ1.190825.002/V12.0.5.0.QGHCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/phoenix/phoenix:10/QKQ1.190825.002/V12.0.4.0.QGHCNXM:user/release-keys or Redmi/phoenix/phoenix:10/QKQ1.190825.002/V12.0.5.0.QGHCNXM: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:f2eef282bf4e9b3a3cc72f9ef6ed54e525e2268b",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:b1d1ed5e7019286834ee5aa5d01ce670315cc20c") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:f2eef282bf4e9b3a3cc72f9ef6ed54e525e2268b\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:b1d1ed5e7019286834ee5aa5d01ce670315cc20c\" 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"), "42,1,240,271,280,282,283,529,533,545,546,565,566,1877,8095,8101,8102,8525,32770,32978,98306,98514,163842,164050,172052,172975,172977,173003,229378,229586,294914,295122,819202,819410,849827,851968,851976,851983,851991,852698,866209,866388,866389") == "994f4be680e24119b1ff402152f038c98ad39626" || 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"), "42,1,240,271,280,282,283,529,533,545,546,565,566,1877,8095,8101,8102,8525,32770,32978,98306,98514,163842,164050,172052,172975,172977,173003,229378,229586,294914,295122,819202,819410,849827,851968,851976,851983,851991,852698,866209,866388,866389") && 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"), "220,1,109,119,123,128,129,210,214,219,220,1926,1930,2408,2409,2414,2415,2676,2680,2684,2685,2687,2688,2690,2701,2723,2724,2760,2764,2814,2815,2951,2952,2955,2959,2963,2964,3287,3288,3293,3297,3310,3311,3319,3323,3327,3328,3444,3451,3487,3493,3612,3638,3821,3871,4273,4274,4326,4366,5389,6137,6278,6340,6487,6644,6719,6811,14763,14764,15525,15526,15530,15532,15535,15537,15540,15542,15545,15547,15550,15552,15555,15557,15561,15563,15564,15565,15567,15569,15570,15571,15573,15575,15576,15577,15585,15587,15588,15589,15591,15593,15594,15595,15599,15600,15604,15606,15609,15611,15614,15616,15619,15621,15624,15626,15627,15628,15637,15638,15639,15640,15661,15662,15663,15664,15669,15670,15674,15675,15679,15680,15684,15685,15690,15691,15695,15696,15697,15698,15701,15702,15703,15704,15707,15708,15709,15710,16062,16063,17084,17087,20989,20990,22261,22262,23889,23892,25039,25041,25989,25991,26161,26164,27532,27533,30711,30713,32768,32770,32850,32874,33136,33137,39826,39829,43557,43559,49119,49120,49289,49290,49298,49299,49312,49313,49407,49408,49529,49530,49533,49534,50695,50696,65536,65555,67309,67310,73645,73646,79928,79929,80037,80038,80560,80561,83402,98306,98386,163842,163922,229378,229458,294914,294994,327689,327699,330676,331730,336989,337069,337070") == "b6493a7802a4d03b122d456f7b27d9b922a7cc58" || 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"), "220,1,109,119,123,128,129,210,214,219,220,1926,1930,2408,2409,2414,2415,2676,2680,2684,2685,2687,2688,2690,2701,2723,2724,2760,2764,2814,2815,2951,2952,2955,2959,2963,2964,3287,3288,3293,3297,3310,3311,3319,3323,3327,3328,3444,3451,3487,3493,3612,3638,3821,3871,4273,4274,4326,4366,5389,6137,6278,6340,6487,6644,6719,6811,14763,14764,15525,15526,15530,15532,15535,15537,15540,15542,15545,15547,15550,15552,15555,15557,15561,15563,15564,15565,15567,15569,15570,15571,15573,15575,15576,15577,15585,15587,15588,15589,15591,15593,15594,15595,15599,15600,15604,15606,15609,15611,15614,15616,15619,15621,15624,15626,15627,15628,15637,15638,15639,15640,15661,15662,15663,15664,15669,15670,15674,15675,15679,15680,15684,15685,15690,15691,15695,15696,15697,15698,15701,15702,15703,15704,15707,15708,15709,15710,16062,16063,17084,17087,20989,20990,22261,22262,23889,23892,25039,25041,25989,25991,26161,26164,27532,27533,30711,30713,32768,32770,32850,32874,33136,33137,39826,39829,43557,43559,49119,49120,49289,49290,49298,49299,49312,49313,49407,49408,49529,49530,49533,49534,50695,50696,65536,65555,67309,67310,73645,73646,79928,79929,80037,80038,80560,80561,83402,98306,98386,163842,163922,229378,229458,294914,294994,327689,327699,330676,331730,336989,337069,337070") && 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"), "10,1,32770,32787,65559,65560,71380,71600,72739,72768,72769") == "af7f6655480e5ab5173ce868b223debf102c05c1" || 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"), "10,1,32770,32787,65559,65560,71380,71600,72739,72768,72769") && 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") == "7af23eab672ef8e0fe43cb9f1549d109285e2034" || 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:f2eef282bf4e9b3a3cc72f9ef6ed54e525e2268b",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:b1d1ed5e7019286834ee5aa5d01ce670315cc20c",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:b1d1ed5e7019286834ee5aa5d01ce670315cc20c");

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

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