getprop("ro.product.device") == "apollo" || abort("E3004: This package is for \"apollo\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.3.0.QJDCNXM:user/release-keys");
ui_print("Target: Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.3.0.QJDCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.3.0.QJDCNXM:user/release-keys or Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDCNXM: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:a8d307b88b2f7195ace1902126dfa985bee1e25e",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:bb7affe97d30393cb5521260ad2bf3d5af7ca921") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:a8d307b88b2f7195ace1902126dfa985bee1e25e\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:bb7affe97d30393cb5521260ad2bf3d5af7ca921\" 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,32989,98306,98525,163842,164061,229378,229597,294914,295133,819202,819421,884738,884957,884972,884973,898157,900888,915162,915350,915351") == "5f4ecea0b844ee8194bd97f2f3c8051125cfa2d9" || 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,32989,98306,98525,163842,164061,229378,229597,294914,295133,819202,819421,884738,884957,884972,884973,898157,900888,915162,915350,915351") && 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"), "250,1,153,163,167,172,173,254,258,263,264,268,272,276,277,283,287,291,292,297,298,561,565,570,571,573,577,6351,6352,18252,18254,22947,22948,22950,22961,23489,23493,23498,23499,23635,23636,23648,23652,23656,23657,23660,23664,23668,23669,23672,23673,23885,24316,24524,24874,25048,25098,25104,25445,26478,26481,28960,29200,32768,32770,32898,32914,37589,37590,37646,37647,37958,38010,38694,38695,39302,39303,40360,40366,65536,65555,81797,81798,82426,82428,88101,88102,98304,98306,98434,98449,111091,111092,116978,116979,125017,125022,131072,131087,134906,134908,134945,134946,139655,139657,140314,140315,140401,140402,161778,161779,161793,161794,161798,161800,161818,161819,161864,161865,161883,161884,162027,162030,162689,162690,163454,163455,163464,163465,163840,163842,163970,163985,165943,165946,165947,165948,165973,165974,165987,165988,166012,166013,166068,166072,166134,166135,166781,166782,166788,166793,167268,167269,171490,171491,171506,171507,171554,171556,178164,178165,179728,183976,184061,184062,184064,184065,184466,184468,184520,184521,184683,184684,187691,187692,187813,187814,187815,187816,187842,187843,188269,188270,188295,188296,188398,188399,188512,188513,188918,188919,188980,188981,189120,189121,189242,189243,189311,189312,189480,189481,189596,189597,189642,189643,189859,189860,189901,189902,189939,189940,190117,190118,190251,190252,190640,190641,190752,190753,190807,190808,190858,190859,190957,190958,191020,191022,191023,191029,191125,191127,191134,191146,191147,191149,191156,191168,191188,191189,191767,229378,229506,294914,295042,524289,524303,525879,527475,535835,535952,535953") == "47cc1d81cb29b3f1e8c5ee3529d23a9eeb260697" || 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"), "250,1,153,163,167,172,173,254,258,263,264,268,272,276,277,283,287,291,292,297,298,561,565,570,571,573,577,6351,6352,18252,18254,22947,22948,22950,22961,23489,23493,23498,23499,23635,23636,23648,23652,23656,23657,23660,23664,23668,23669,23672,23673,23885,24316,24524,24874,25048,25098,25104,25445,26478,26481,28960,29200,32768,32770,32898,32914,37589,37590,37646,37647,37958,38010,38694,38695,39302,39303,40360,40366,65536,65555,81797,81798,82426,82428,88101,88102,98304,98306,98434,98449,111091,111092,116978,116979,125017,125022,131072,131087,134906,134908,134945,134946,139655,139657,140314,140315,140401,140402,161778,161779,161793,161794,161798,161800,161818,161819,161864,161865,161883,161884,162027,162030,162689,162690,163454,163455,163464,163465,163840,163842,163970,163985,165943,165946,165947,165948,165973,165974,165987,165988,166012,166013,166068,166072,166134,166135,166781,166782,166788,166793,167268,167269,171490,171491,171506,171507,171554,171556,178164,178165,179728,183976,184061,184062,184064,184065,184466,184468,184520,184521,184683,184684,187691,187692,187813,187814,187815,187816,187842,187843,188269,188270,188295,188296,188398,188399,188512,188513,188918,188919,188980,188981,189120,189121,189242,189243,189311,189312,189480,189481,189596,189597,189642,189643,189859,189860,189901,189902,189939,189940,190117,190118,190251,190252,190640,190641,190752,190753,190807,190808,190858,190859,190957,190958,191020,191022,191023,191029,191125,191127,191134,191146,191147,191149,191156,191168,191188,191189,191767,229378,229506,294914,295042,524289,524303,525879,527475,535835,535952,535953") && 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"), "14,1,32770,32797,82087,82220,98306,98333,98350,98354,111000,111337,113104,113142,113143") == "3b8a719b66da65f71bfde82acbd3d1c2cbf74c49" || 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"), "14,1,32770,32797,82087,82220,98306,98333,98350,98354,111000,111337,113104,113142,113143") && 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"), "10,1,2,9,13,2026,2243,32231,32745,32767,32768") == "e645c8823f85d41f49e8d5b51195ba894d2d65bc" || 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"), "10,1,2,9,13,2026,2243,32231,32745,32767,32768") && 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:a8d307b88b2f7195ace1902126dfa985bee1e25e",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:bb7affe97d30393cb5521260ad2bf3d5af7ca921",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:bb7affe97d30393cb5521260ad2bf3d5af7ca921");

# ---- 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/xbl_config_5.elf", "/dev/block/bootdevice/by-name/xbl_config_5");
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/xbl_5.elf", "/dev/block/bootdevice/by-name/xbl_5");
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/featenabler.mbn", "/dev/block/bootdevice/by-name/featenabler");
package_extract_file("firmware-update/xbl_config_4.elf", "/dev/block/bootdevice/by-name/xbl_config_4");
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/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/storsec.mbn", "/dev/block/bootdevice/by-name/storsec");
package_extract_file("firmware-update/xbl_4.elf", "/dev/block/bootdevice/by-name/xbl_4");
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/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/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/storsec.mbn", "/dev/block/bootdevice/by-name/storsecbak");
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.");

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

# ---- update tasks: exaid part 2----


package_extract_file("exaid.img", "/dev/block/bootdevice/by-name/exaid");
    
