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.5.0.QJDCNXM:user/release-keys");
ui_print("Target: Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.6.0.QJDCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.6.0.QJDCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDCNXM:user/release-keys or Redmi/apollo/apollo:10/QKQ1.200419.002/V12.0.6.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:5ed79cc39740d6bb0d5868902f37bff09e5d1ca6",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:a8d307b88b2f7195ace1902126dfa985bee1e25e") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:5ed79cc39740d6bb0d5868902f37bff09e5d1ca6\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:a8d307b88b2f7195ace1902126dfa985bee1e25e\" 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,32991,98306,98527,163842,164063,229378,229599,294914,295135,819202,819423,884738,884959,884974,884975,905105,907857,922242,922431,922432") == "a17eaceade34946d09543ecc290b864cdf900569" || 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,32991,98306,98527,163842,164063,229378,229599,294914,295135,819202,819423,884738,884959,884974,884975,905105,907857,922242,922431,922432") && 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"), "114,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,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,163842,163970,229378,229506,294914,295042,524289,524303,526017,527614,535975,536092,536093") == "f68c33213ec32fba14b14e3576654855a41445c2" || 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"), "114,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,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,163842,163970,229378,229506,294914,295042,524289,524303,526017,527614,535975,536092,536093") && 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"), "306,1,57,73,77,81,82,89,93,97,98,104,108,112,113,128,132,136,137,197,201,206,207,209,210,212,213,246,250,254,255,261,265,269,270,385,389,393,394,406,410,414,415,418,419,425,429,433,434,457,461,465,466,15031,15032,30713,30714,32125,32126,32768,32770,32797,32817,35519,35520,35531,35535,35539,35540,35550,35551,35556,35560,35565,35566,35570,35576,35581,35582,35588,35592,35596,35597,35609,35613,35617,35618,35620,35625,35634,35635,38745,38750,38807,38812,38820,38825,38853,38858,38880,38885,38906,38911,38930,38931,38942,38944,39108,39109,39110,39111,39112,39113,39114,39115,39151,39152,39156,39158,39166,39167,39169,39172,39509,39513,39517,39519,39524,39526,39530,39532,39536,39538,39542,39544,39548,39550,39554,39556,39560,39562,39566,39577,39581,39583,39587,39589,39593,39595,39599,39601,39605,39607,39611,39613,39617,39619,39623,39625,39629,39631,39635,39637,39641,39643,39647,39649,39653,39655,39659,39661,39665,39667,39671,39673,39678,39680,39684,39686,39690,39692,39696,39698,39702,39704,39708,39710,39714,39716,39720,39722,39726,39728,39732,39734,39738,39740,39744,39746,39750,39753,39757,39759,39764,39766,39770,39772,39776,39778,39782,39784,39788,39790,39794,39796,39800,39802,39806,39817,39821,39823,39827,39829,39833,39835,39839,39841,39845,39847,39851,39853,39857,39859,39863,39865,39869,39871,39875,39877,39881,39883,39887,39889,39893,39895,39899,39901,39905,39907,39911,39913,39918,39920,39924,39926,39930,39932,39936,39938,39942,39944,39948,39950,39954,39956,39960,39962,39966,39968,39972,39974,39978,39980,39984,39986,40027,40313,41023,41024,44590,44591,55339,55341,57401,57403,65536,65563,73831,73832,83174,83176,83262,83264,88925,88927,90128,90130,95309,95311,95329,98306,98333,112242,112586,114373,114411,114412") == "a37d6bc96b2d6846ae07d23e8c79f4ba82c38b82" || 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"), "306,1,57,73,77,81,82,89,93,97,98,104,108,112,113,128,132,136,137,197,201,206,207,209,210,212,213,246,250,254,255,261,265,269,270,385,389,393,394,406,410,414,415,418,419,425,429,433,434,457,461,465,466,15031,15032,30713,30714,32125,32126,32768,32770,32797,32817,35519,35520,35531,35535,35539,35540,35550,35551,35556,35560,35565,35566,35570,35576,35581,35582,35588,35592,35596,35597,35609,35613,35617,35618,35620,35625,35634,35635,38745,38750,38807,38812,38820,38825,38853,38858,38880,38885,38906,38911,38930,38931,38942,38944,39108,39109,39110,39111,39112,39113,39114,39115,39151,39152,39156,39158,39166,39167,39169,39172,39509,39513,39517,39519,39524,39526,39530,39532,39536,39538,39542,39544,39548,39550,39554,39556,39560,39562,39566,39577,39581,39583,39587,39589,39593,39595,39599,39601,39605,39607,39611,39613,39617,39619,39623,39625,39629,39631,39635,39637,39641,39643,39647,39649,39653,39655,39659,39661,39665,39667,39671,39673,39678,39680,39684,39686,39690,39692,39696,39698,39702,39704,39708,39710,39714,39716,39720,39722,39726,39728,39732,39734,39738,39740,39744,39746,39750,39753,39757,39759,39764,39766,39770,39772,39776,39778,39782,39784,39788,39790,39794,39796,39800,39802,39806,39817,39821,39823,39827,39829,39833,39835,39839,39841,39845,39847,39851,39853,39857,39859,39863,39865,39869,39871,39875,39877,39881,39883,39887,39889,39893,39895,39899,39901,39905,39907,39911,39913,39918,39920,39924,39926,39930,39932,39936,39938,39942,39944,39948,39950,39954,39956,39960,39962,39966,39968,39972,39974,39978,39980,39984,39986,40027,40313,41023,41024,44590,44591,55339,55341,57401,57403,65536,65563,73831,73832,83174,83176,83262,83264,88925,88927,90128,90130,95309,95311,95329,98306,98333,112242,112586,114373,114411,114412") && 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") == "d072727ee6e5caf5023c0d920114db33e93344f7" || 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:5ed79cc39740d6bb0d5868902f37bff09e5d1ca6",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:a8d307b88b2f7195ace1902126dfa985bee1e25e",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:a8d307b88b2f7195ace1902126dfa985bee1e25e");

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