getprop("ro.product.device") == "apollo" || abort("E3004: This package is for \"apollo\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/apolloin/apollo:10/QKQ1.200419.002/V12.0.4.0.QJDINXM:user/release-keys");
ui_print("Target: Redmi/apolloin/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/apolloin/apollo:10/QKQ1.200419.002/V12.0.4.0.QJDINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/apolloin/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/apolloin/apollo:10/QKQ1.200419.002/V12.0.4.0.QJDINXM:user/release-keys or Redmi/apolloin/apollo:10/QKQ1.200419.002/V12.0.5.0.QJDINXM: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:d5151d30af31eeb6d1919618222e5c2442ee5427",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:dba4857444be01a74f76847d86c9502f7810bd9d") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:d5151d30af31eeb6d1919618222e5c2442ee5427\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:dba4857444be01a74f76847d86c9502f7810bd9d\" 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"), "36,1,205,233,242,244,245,506,510,526,527,1859,2296,32768,32770,32937,32959,34334,59189,59876,59878,59902,98306,98473,163842,164009,229378,229545,294914,295081,655369,655382,685991,688078,698982,699129,699130") == "d1fc738273ff21e435af18c93c21d69f6a60a974" || 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"), "36,1,205,233,242,244,245,506,510,526,527,1859,2296,32768,32770,32937,32959,34334,59189,59876,59878,59902,98306,98473,163842,164009,229378,229545,294914,295081,655369,655382,685991,688078,698982,699129,699130") && 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"), "86,1,141,151,155,160,161,242,246,251,252,256,260,264,265,271,275,279,280,285,286,549,553,558,559,561,565,567,578,1106,1110,1115,1116,1276,1277,1289,1293,1297,1298,1301,1305,1309,1310,1313,1314,1526,1957,2165,2515,2689,2739,2745,3086,3108,3109,4120,4123,15076,15077,15444,15496,16180,16181,16788,16789,17840,17848,32768,32770,32885,32902,60022,98306,98421,163842,163957,229378,229493,294914,295029,458764,458768,469812,471249,478718,478824,478825") == "84054544b0a9886d372e692fe2a9424d403c46bf" || 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"), "86,1,141,151,155,160,161,242,246,251,252,256,260,264,265,271,275,279,280,285,286,549,553,558,559,561,565,567,578,1106,1110,1115,1116,1276,1277,1289,1293,1297,1298,1301,1305,1309,1310,1313,1314,1526,1957,2165,2515,2689,2739,2745,3086,3108,3109,4120,4123,15076,15077,15444,15496,16180,16181,16788,16789,17840,17848,32768,32770,32885,32902,60022,98306,98421,163842,163957,229378,229493,294914,295029,458764,458768,469812,471249,478718,478824,478825") && 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"), "352,1,95,5385,5386,5402,5406,5410,5411,9747,9751,11892,11895,14047,14048,14055,14059,14063,14064,14070,14074,14078,14079,14094,14098,14102,14103,14163,14167,14172,14173,17884,17888,22101,22102,28341,28342,30749,30750,31114,31118,31625,31626,31638,31642,31646,31647,32768,32770,32844,32855,33574,33578,35015,35016,41909,41910,42311,42312,49477,49478,57528,57529,65536,65547,69424,69425,70084,70085,77563,77564,83671,83672,83678,83679,83681,83682,83688,83692,83696,83697,83825,83829,83833,83834,83846,83850,83854,83855,83858,83859,83865,83869,83873,83874,83897,83901,83905,83906,98304,98306,98380,98391,98558,98559,114240,114241,115652,115653,122036,122037,129278,129279,129290,129294,129298,129299,129309,129310,129315,129319,129324,129325,129329,129335,129340,129341,129347,129351,129355,129356,129368,129372,129376,129377,129379,129384,129393,129394,131072,131083,132515,132520,132577,132582,132590,132595,132623,132628,132650,132655,132676,132681,132700,132701,132712,132714,132884,132885,132886,132887,132888,132889,132890,132891,132929,132931,132939,132940,132945,132946,133284,133288,133292,133294,133298,133300,133305,133307,133311,133313,133317,133319,133323,133325,133329,133331,133335,133337,133341,133343,133347,133358,133362,133364,133368,133370,133374,133376,133380,133382,133386,133388,133392,133394,133398,133400,133404,133406,133410,133412,133416,133418,133422,133424,133428,133430,133434,133436,133440,133442,133446,133448,133452,133454,133459,133461,133465,133467,133471,133473,133477,133479,133483,133485,133489,133491,133495,133497,133501,133503,133507,133509,133513,133515,133519,133521,133525,133527,133531,133534,133538,133540,133544,133546,133551,133553,133557,133559,133563,133565,133569,133571,133575,133577,133581,133583,133587,133589,133593,133604,133608,133610,133614,133616,133620,133622,133626,133628,133632,133634,133638,133640,133644,133646,133650,133652,133656,133658,133662,133664,133668,133670,133674,133676,133680,133682,133686,133688,133692,133694,133698,133700,133705,133707,133711,133713,133717,133719,133723,133725,133729,133731,133735,133737,133741,133743,133747,133749,133753,133755,133759,133761,133765,133767,133771,133773,133814,134100,134810,134811,138377,138378,139714,139715,144290,144291,149400,149402,149412,163842,163916,229378,229452,294914,294988,294991,294999,302824,303739,308555,308628,308629") == "7f2b059ac663bc1b685d38fa9683fb72e05e554b" || 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"), "352,1,95,5385,5386,5402,5406,5410,5411,9747,9751,11892,11895,14047,14048,14055,14059,14063,14064,14070,14074,14078,14079,14094,14098,14102,14103,14163,14167,14172,14173,17884,17888,22101,22102,28341,28342,30749,30750,31114,31118,31625,31626,31638,31642,31646,31647,32768,32770,32844,32855,33574,33578,35015,35016,41909,41910,42311,42312,49477,49478,57528,57529,65536,65547,69424,69425,70084,70085,77563,77564,83671,83672,83678,83679,83681,83682,83688,83692,83696,83697,83825,83829,83833,83834,83846,83850,83854,83855,83858,83859,83865,83869,83873,83874,83897,83901,83905,83906,98304,98306,98380,98391,98558,98559,114240,114241,115652,115653,122036,122037,129278,129279,129290,129294,129298,129299,129309,129310,129315,129319,129324,129325,129329,129335,129340,129341,129347,129351,129355,129356,129368,129372,129376,129377,129379,129384,129393,129394,131072,131083,132515,132520,132577,132582,132590,132595,132623,132628,132650,132655,132676,132681,132700,132701,132712,132714,132884,132885,132886,132887,132888,132889,132890,132891,132929,132931,132939,132940,132945,132946,133284,133288,133292,133294,133298,133300,133305,133307,133311,133313,133317,133319,133323,133325,133329,133331,133335,133337,133341,133343,133347,133358,133362,133364,133368,133370,133374,133376,133380,133382,133386,133388,133392,133394,133398,133400,133404,133406,133410,133412,133416,133418,133422,133424,133428,133430,133434,133436,133440,133442,133446,133448,133452,133454,133459,133461,133465,133467,133471,133473,133477,133479,133483,133485,133489,133491,133495,133497,133501,133503,133507,133509,133513,133515,133519,133521,133525,133527,133531,133534,133538,133540,133544,133546,133551,133553,133557,133559,133563,133565,133569,133571,133575,133577,133581,133583,133587,133589,133593,133604,133608,133610,133614,133616,133620,133622,133626,133628,133632,133634,133638,133640,133644,133646,133650,133652,133656,133658,133662,133664,133668,133670,133674,133676,133680,133682,133686,133688,133692,133694,133698,133700,133705,133707,133711,133713,133717,133719,133723,133725,133729,133731,133735,133737,133741,133743,133747,133749,133753,133755,133759,133761,133765,133767,133771,133773,133814,134100,134810,134811,138377,138378,139714,139715,144290,144291,149400,149402,149412,163842,163916,229378,229452,294914,294988,294991,294999,302824,303739,308555,308628,308629") && 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,2037,32231,32745,32767,32768") == "789dca72aa4a262f369b89ce3d5264134cd753b2" || 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,2037,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:d5151d30af31eeb6d1919618222e5c2442ee5427",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:dba4857444be01a74f76847d86c9502f7810bd9d",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:dba4857444be01a74f76847d86c9502f7810bd9d");

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


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

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