getprop("ro.product.device") == "excalibur" || abort("E3004: This package is for \"excalibur\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.1.0.QJXINXM:user/release-keys");
ui_print("Target: Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.2.0.QJXINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.1.0.QJXINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.2.0.QJXINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.1.0.QJXINXM:user/release-keys or Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.2.0.QJXINXM: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:b37087eeda56bec14b3a66fc4562e966e5e5955e",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:d2fda1d4b4b74f9f98c8375edec144d9da579c8c") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:b37087eeda56bec14b3a66fc4562e966e5e5955e\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:d2fda1d4b4b74f9f98c8375edec144d9da579c8c\" 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"), "28,1,32770,32927,56144,56831,56833,56857,98306,98463,163842,163999,179898,180461,180463,180480,229378,229535,294914,295071,501395,501571,622603,622613,644525,646522,656767,656908,656909") == "eb05238c8addc149931118add419f2c83330155a" || 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"), "28,1,32770,32927,56144,56831,56833,56857,98306,98463,163842,163999,179898,180461,180463,180480,229378,229535,294914,295071,501395,501571,622603,622613,644525,646522,656767,656908,656909") && 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"), "168,1,104,114,118,123,124,205,209,214,215,1920,1924,2402,2403,2408,2409,2672,2676,2680,2681,2683,2684,2686,2692,2735,2743,2747,2748,2771,2772,2808,2812,2862,2863,2875,2879,2883,2884,2887,2893,2894,2895,3218,3219,3224,3228,3241,3248,3257,3258,3422,3445,3522,3548,3761,3811,3830,4157,4234,4235,14767,14768,14942,14993,15561,15562,15566,15568,15571,15573,15576,15578,15581,15583,15586,15588,15591,15593,15597,15599,15600,15601,15603,15605,15606,15607,15609,15611,15612,15613,15621,15623,15624,15625,15627,15629,15630,15631,15635,15636,15640,15642,15645,15647,15650,15652,15655,15657,15660,15662,15663,15664,15673,15674,15675,15676,15697,15698,15699,15700,15705,15706,15710,15711,15715,15716,15720,15721,15726,15727,15731,15732,15733,15734,15737,15738,15739,15740,15743,15744,15745,15746,15749,15750,16098,16099,16350,16351,17125,17129,19382,32770,32843,98306,98379,163842,163915,229378,229451,294914,294987,295005,295008,298425,299378,304124,304198,304199") == "0ce7e402b8e9cddfea7f3dea3f9b550cb2c1c44a" || 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"), "168,1,104,114,118,123,124,205,209,214,215,1920,1924,2402,2403,2408,2409,2672,2676,2680,2681,2683,2684,2686,2692,2735,2743,2747,2748,2771,2772,2808,2812,2862,2863,2875,2879,2883,2884,2887,2893,2894,2895,3218,3219,3224,3228,3241,3248,3257,3258,3422,3445,3522,3548,3761,3811,3830,4157,4234,4235,14767,14768,14942,14993,15561,15562,15566,15568,15571,15573,15576,15578,15581,15583,15586,15588,15591,15593,15597,15599,15600,15601,15603,15605,15606,15607,15609,15611,15612,15613,15621,15623,15624,15625,15627,15629,15630,15631,15635,15636,15640,15642,15645,15647,15650,15652,15655,15657,15660,15662,15663,15664,15673,15674,15675,15676,15697,15698,15699,15700,15705,15706,15710,15711,15715,15716,15720,15721,15726,15727,15731,15732,15733,15734,15737,15738,15739,15740,15743,15744,15745,15746,15749,15750,16098,16099,16350,16351,17125,17129,19382,32770,32843,98306,98379,163842,163915,229378,229451,294914,294987,295005,295008,298425,299378,304124,304198,304199") && 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"), "298,1,85,101,105,109,110,4446,4450,5925,5926,6590,6593,6597,6598,6639,6640,6647,6653,6654,6655,7368,7372,7379,7380,7386,7392,7393,7394,7454,7458,7463,7464,8212,8213,11172,11176,15389,15390,18145,18146,21618,21619,22257,22261,23031,23032,23395,23399,23906,23907,23919,23925,23926,23927,25767,25771,27208,27209,32768,32770,32834,32845,34175,34176,34581,34582,41747,41748,49750,49751,51214,51215,62284,62292,62298,62304,62305,62306,62434,62440,62441,62442,62454,62460,62461,62462,62465,62466,62472,62478,62479,62480,62503,62509,62510,62511,63355,63356,65536,65547,65548,65549,77085,77086,92755,92756,92877,92878,93054,93055,93422,93423,94174,94175,98304,98306,98370,98381,100634,100635,105388,105389,107640,107641,107828,107829,107840,107846,107847,107848,107858,107859,107864,107868,107873,107874,107878,107884,107888,107889,107895,107901,107902,107903,107915,107921,107922,107923,107933,107939,107940,107941,107943,107950,107956,107957,109714,109715,109721,109722,109856,109857,111065,111072,111126,111133,111138,111145,111170,111177,111197,111204,111222,111229,111245,111246,111254,111256,111426,111427,111428,111429,111430,111431,111432,111433,111470,111472,111480,111481,111483,111485,111486,111487,111758,111762,111766,111768,111773,111777,111778,111782,111783,111787,111788,111792,111793,111797,111798,111802,111803,111807,111808,111810,111817,111821,111822,111826,111827,111831,111832,111836,111837,111839,111840,111844,111845,111849,111850,111852,111853,111857,111858,111862,111863,111867,111868,111872,111873,111877,111878,111882,111883,111887,111888,111890,111894,111898,111899,111903,111904,111908,111909,111913,111914,111916,111917,111921,111922,111924,111925,111929,111930,111932,111933,111937,111938,111942,111943,111945,111946,111950,111951,112098,113070,113071,113684,113722,116640,116641,118615,118662,127018,127020,127030,127074,129886,129888,129904,163842,163906,229378,229442,263168,263977,268163,268230,268231") == "03cff2a38a4d6bff42c27add0392a6e4ac24eaed" || 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"), "298,1,85,101,105,109,110,4446,4450,5925,5926,6590,6593,6597,6598,6639,6640,6647,6653,6654,6655,7368,7372,7379,7380,7386,7392,7393,7394,7454,7458,7463,7464,8212,8213,11172,11176,15389,15390,18145,18146,21618,21619,22257,22261,23031,23032,23395,23399,23906,23907,23919,23925,23926,23927,25767,25771,27208,27209,32768,32770,32834,32845,34175,34176,34581,34582,41747,41748,49750,49751,51214,51215,62284,62292,62298,62304,62305,62306,62434,62440,62441,62442,62454,62460,62461,62462,62465,62466,62472,62478,62479,62480,62503,62509,62510,62511,63355,63356,65536,65547,65548,65549,77085,77086,92755,92756,92877,92878,93054,93055,93422,93423,94174,94175,98304,98306,98370,98381,100634,100635,105388,105389,107640,107641,107828,107829,107840,107846,107847,107848,107858,107859,107864,107868,107873,107874,107878,107884,107888,107889,107895,107901,107902,107903,107915,107921,107922,107923,107933,107939,107940,107941,107943,107950,107956,107957,109714,109715,109721,109722,109856,109857,111065,111072,111126,111133,111138,111145,111170,111177,111197,111204,111222,111229,111245,111246,111254,111256,111426,111427,111428,111429,111430,111431,111432,111433,111470,111472,111480,111481,111483,111485,111486,111487,111758,111762,111766,111768,111773,111777,111778,111782,111783,111787,111788,111792,111793,111797,111798,111802,111803,111807,111808,111810,111817,111821,111822,111826,111827,111831,111832,111836,111837,111839,111840,111844,111845,111849,111850,111852,111853,111857,111858,111862,111863,111867,111868,111872,111873,111877,111878,111882,111883,111887,111888,111890,111894,111898,111899,111903,111904,111908,111909,111913,111914,111916,111917,111921,111922,111924,111925,111929,111930,111932,111933,111937,111938,111942,111943,111945,111946,111950,111951,112098,113070,113071,113684,113722,116640,116641,118615,118662,127018,127020,127030,127074,129886,129888,129904,163842,163906,229378,229442,263168,263977,268163,268230,268231") && 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:134217728:b37087eeda56bec14b3a66fc4562e966e5e5955e",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:d2fda1d4b4b74f9f98c8375edec144d9da579c8c",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:d2fda1d4b4b74f9f98c8375edec144d9da579c8c");

# ---- 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/BTFM.bin", "/dev/block/bootdevice/by-name/bluetooth");
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/storsec.mbn", "/dev/block/bootdevice/by-name/storsec");
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/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfg");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xbl");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymaster");
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/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/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");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymasterbak");
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/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");
