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/V11.0.4.0.QJXINXM:user/release-keys");
ui_print("Target: Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.1.0.QJXINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.4.0.QJXINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.1.0.QJXINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.4.0.QJXINXM:user/release-keys or Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V12.0.1.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:d2fda1d4b4b74f9f98c8375edec144d9da579c8c",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:d2fda1d4b4b74f9f98c8375edec144d9da579c8c\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd\" 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"), "104,1,32770,32922,70244,71313,74780,74787,98306,98458,108917,123462,123463,127262,127263,127481,127482,128197,128199,128406,163842,163994,180874,180875,229378,229530,294914,295066,304115,304118,305238,305241,341989,342080,342789,342971,366064,366114,385592,385595,385616,385682,385922,386064,389884,389888,393750,393753,393818,393820,418626,418659,438196,442466,442961,443092,443242,443375,444140,444262,444412,444543,449149,449150,449175,449179,449190,449195,449236,449241,449283,450177,487945,489544,489545,491520,491551,491759,494523,504034,504035,505342,505343,515091,515093,515100,515104,516057,516058,516064,516065,516070,516071,519247,530527,531062,622472,622592,622599,622613,622619,624408,634304,634438,634439") == "1e86f4b020614a612283e8e8e61857a0a31e6c50" || 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"), "104,1,32770,32922,70244,71313,74780,74787,98306,98458,108917,123462,123463,127262,127263,127481,127482,128197,128199,128406,163842,163994,180874,180875,229378,229530,294914,295066,304115,304118,305238,305241,341989,342080,342789,342971,366064,366114,385592,385595,385616,385682,385922,386064,389884,389888,393750,393753,393818,393820,418626,418659,438196,442466,442961,443092,443242,443375,444140,444262,444412,444543,449149,449150,449175,449179,449190,449195,449236,449241,449283,450177,487945,489544,489545,491520,491551,491759,494523,504034,504035,505342,505343,515091,515093,515100,515104,516057,516058,516064,516065,516070,516071,519247,530527,531062,622472,622592,622599,622613,622619,624408,634304,634438,634439") && 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"), "24,1,19381,26799,26800,28515,28516,29214,32770,32845,98306,98381,163842,163917,229378,229453,294914,294989,295007,295010,308023,309005,313903,313980,313981") == "987fdbd584c01dd511a7005f39e9c09d5fc7afa6" || 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"), "24,1,19381,26799,26800,28515,28516,29214,32770,32845,98306,98381,163842,163917,229378,229453,294914,294989,295007,295010,308023,309005,313903,313980,313981") && 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,32831,98306,98367,163842,163903,229378,229439,229445,229452,252823,253592,257614,257678,257679") == "990b0842e016514e987bff6eb3a09ed1d9063a17" || 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,32831,98306,98367,163842,163903,229378,229439,229445,229452,252823,253592,257614,257678,257679") && 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:d2fda1d4b4b74f9f98c8375edec144d9da579c8c",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd");

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


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

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