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.2.0.QJXINXM:user/release-keys");
ui_print("Target: Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.4.0.QJXINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.2.0.QJXINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.4.0.QJXINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.2.0.QJXINXM:user/release-keys or Redmi/excalibur_in/excalibur:10/QKQ1.191215.002/V11.0.4.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:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:4efd28c32269d6bb68e138323fb062080de19857") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:4efd28c32269d6bb68e138323fb062080de19857\" 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"), "22,1,32770,32920,98306,98456,163842,163992,229378,229528,294914,295064,524187,524288,524423,524464,589832,589846,612790,614673,624414,624548,624549") == "ccd3be1659c2adebb741a9e95b2c6512b251e8ad" || 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"), "22,1,32770,32920,98306,98456,163842,163992,229378,229528,294914,295064,524187,524288,524423,524464,589832,589846,612790,614673,624414,624548,624549") && 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"), "70,1,106,116,120,125,126,207,211,216,217,1922,1926,2404,2405,2410,2411,2674,2678,2682,2683,2685,2691,2734,2742,2746,2747,2770,2771,2807,2811,2861,2862,2874,2878,2882,2883,2886,2892,2893,2894,3217,3218,3223,3227,3240,3247,3256,3257,3421,3444,3760,3810,3829,32770,32845,98306,98381,163842,163917,229378,229453,294914,294989,295007,295010,306988,307967,312848,312924,312925") == "e8bfa3f42fb63b569ca1a51fbf573d871b94f1e6" || 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"), "70,1,106,116,120,125,126,207,211,216,217,1922,1926,2404,2405,2410,2411,2674,2678,2682,2683,2685,2691,2734,2742,2746,2747,2770,2771,2807,2811,2861,2862,2874,2878,2882,2883,2886,2892,2893,2894,3217,3218,3223,3227,3240,3247,3256,3257,3421,3444,3760,3810,3829,32770,32845,98306,98381,163842,163917,229378,229453,294914,294989,295007,295010,306988,307967,312848,312924,312925") && 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"), "20,1,84,100,104,108,32770,32831,98306,98367,163842,163903,229378,229439,229445,229452,252444,253212,257227,257291,257292") == "c446c1a91db3a8bf23370206d589fe9ec516e164" || 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"), "20,1,84,100,104,108,32770,32831,98306,98367,163842,163903,229378,229439,229445,229452,252444,253212,257227,257291,257292") && 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:8f8f08f4cab034fdf7535331c3c83c5131d3c4bd",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:4efd28c32269d6bb68e138323fb062080de19857",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:4efd28c32269d6bb68e138323fb062080de19857");

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