getprop("ro.product.device") == "merlin" || abort("E3004: This package is for \"merlin\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/merlin/merlin:10/QP1A.190711.020/V12.0.7.0.QJOCNXM:user/release-keys");
ui_print("Target: Redmi/merlin/merlin:10/QP1A.190711.020/V12.0.8.0.QJOCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/merlin/merlin:10/QP1A.190711.020/V12.0.7.0.QJOCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/merlin/merlin:10/QP1A.190711.020/V12.0.8.0.QJOCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/merlin/merlin:10/QP1A.190711.020/V12.0.7.0.QJOCNXM:user/release-keys or Redmi/merlin/merlin:10/QP1A.190711.020/V12.0.8.0.QJOCNXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
show_progress(0.100000, 10);
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:e9c8439bd3fdcb4177aa2ecef47f42d257160d72",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:82360f17b1d82e79ce3b21268ee4b8bcc05fabd6") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:e9c8439bd3fdcb4177aa2ecef47f42d257160d72\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:82360f17b1d82e79ce3b21268ee4b8bcc05fabd6\" has unexpected contents.");
apply_patch_space(181923840) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:b07f1aabc7c1d9298489099a3216ec5ae3af32a2",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:3c0d272aeb0853fd5ebbcbb7b8058d9452dc0837") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:b07f1aabc7c1d9298489099a3216ec5ae3af32a2\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:3c0d272aeb0853fd5ebbcbb7b8058d9452dc0837\" has unexpected contents.");
apply_patch_space(58123424) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "30,1,32770,32965,98306,98501,163842,164037,229378,229573,288258,294912,294914,295109,295125,304632,315863,320760,320761,327680,327696,332689,753677,753680,786433,786448,799165,801563,814265,814434,814435") == "8573097046a28fa1a5c66c9c0373650b36f43c54" || 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"), "30,1,32770,32965,98306,98501,163842,164037,229378,229573,288258,294912,294914,295109,295125,304632,315863,320760,320761,327680,327696,332689,753677,753680,786433,786448,799165,801563,814265,814434,814435") && 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"), "230,1,81,786,792,810,811,816,817,819,820,1306,1307,1309,1310,1446,1447,1458,1462,1466,1467,1683,1684,1763,1764,1834,1835,2020,2021,2099,2100,2179,2180,2259,2260,3146,3147,4636,4637,7384,7385,8460,8464,8465,8467,32768,32770,32818,32848,34543,34544,41079,41080,49118,49123,59052,59054,60046,60047,60115,60116,60440,60441,60784,60785,60898,60899,61799,61801,64446,64448,64782,64783,65536,65567,65596,65598,65599,65600,65601,65602,65603,65604,65607,65608,65612,65614,75282,75283,75604,75605,85856,85857,89011,90277,91110,91111,91445,91446,91776,91777,92096,92097,92418,92419,92748,92749,93076,93077,93397,93398,98304,98306,98354,98391,105044,105045,106116,106117,106436,106437,106762,106763,107091,107092,107427,107428,107745,107746,108067,108068,108396,108397,110853,110862,112486,112487,129064,129264,131072,131102,131924,131925,132149,132150,134381,135256,140247,140248,147763,147981,148945,148946,149901,150113,150134,150135,158628,158629,163840,163842,163890,163913,168488,168489,174898,174899,180920,180921,181255,181256,181596,181597,181931,181932,182272,182273,182592,182593,182917,182918,183236,183237,183321,183322,183563,183564,183872,183873,184162,184163,184481,184482,184825,184826,185142,185143,185470,185471,185799,185800,186133,186134,186462,186463,186795,186796,187116,187117,187443,187444,187764,187765,191791,191793,192137,192140,192193,192195,196608,196624,196631,196648,196656,196657,197166,197168,199485,202649,202704,202705") == "756451acc0b4aae4dc8aefa33e0d0d6c4f062d34" || 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"), "230,1,81,786,792,810,811,816,817,819,820,1306,1307,1309,1310,1446,1447,1458,1462,1466,1467,1683,1684,1763,1764,1834,1835,2020,2021,2099,2100,2179,2180,2259,2260,3146,3147,4636,4637,7384,7385,8460,8464,8465,8467,32768,32770,32818,32848,34543,34544,41079,41080,49118,49123,59052,59054,60046,60047,60115,60116,60440,60441,60784,60785,60898,60899,61799,61801,64446,64448,64782,64783,65536,65567,65596,65598,65599,65600,65601,65602,65603,65604,65607,65608,65612,65614,75282,75283,75604,75605,85856,85857,89011,90277,91110,91111,91445,91446,91776,91777,92096,92097,92418,92419,92748,92749,93076,93077,93397,93398,98304,98306,98354,98391,105044,105045,106116,106117,106436,106437,106762,106763,107091,107092,107427,107428,107745,107746,108067,108068,108396,108397,110853,110862,112486,112487,129064,129264,131072,131102,131924,131925,132149,132150,134381,135256,140247,140248,147763,147981,148945,148946,149901,150113,150134,150135,158628,158629,163840,163842,163890,163913,168488,168489,174898,174899,180920,180921,181255,181256,181596,181597,181931,181932,182272,182273,182592,182593,182917,182918,183236,183237,183321,183322,183563,183564,183872,183873,184162,184163,184481,184482,184825,184826,185142,185143,185470,185471,185799,185800,186133,186134,186462,186463,186795,186796,187116,187117,187443,187444,187764,187765,191791,191793,192137,192140,192193,192195,196608,196624,196631,196648,196656,196657,197166,197168,199485,202649,202704,202705") && 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"), "144,1,37,570,576,593,594,596,597,712,716,720,721,15286,15287,30968,30969,32380,32381,32768,32770,32782,32797,35901,35902,39012,39016,39019,39020,39032,39033,39034,39035,39080,39081,39140,39143,39592,39593,39964,39965,41450,41451,41541,41543,41545,41546,41548,41549,41551,41552,41554,41555,41557,41558,41560,41561,41563,41564,41566,41567,41569,41570,41572,41573,41575,41576,41595,41596,41601,41602,41621,41622,41637,41638,41642,41643,41659,41660,41665,41666,41684,41685,41698,41699,41703,41704,41721,41722,41727,41728,41747,41748,41762,41763,41765,41766,41768,41769,41771,41772,41774,41775,41777,41778,41780,41781,41783,41784,41786,41787,41789,41790,41792,41794,42530,42534,42550,42551,42617,42621,42625,42626,42630,42634,42638,42639,49189,49190,51811,51812,52277,52281,52295,52296,52962,52966,52982,52983,53010,53014,53180,54026,54053,54054") == "839eb201c7d88568820fbb0590bac031c51d4e43" || 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
ifelse (block_image_recover(map_partition("product"), "144,1,37,570,576,593,594,596,597,712,716,720,721,15286,15287,30968,30969,32380,32381,32768,32770,32782,32797,35901,35902,39012,39016,39019,39020,39032,39033,39034,39035,39080,39081,39140,39143,39592,39593,39964,39965,41450,41451,41541,41543,41545,41546,41548,41549,41551,41552,41554,41555,41557,41558,41560,41561,41563,41564,41566,41567,41569,41570,41572,41573,41575,41576,41595,41596,41601,41602,41621,41622,41637,41638,41642,41643,41659,41660,41665,41666,41684,41685,41698,41699,41703,41704,41721,41722,41727,41728,41747,41748,41762,41763,41765,41766,41768,41769,41771,41772,41774,41775,41777,41778,41780,41781,41783,41784,41786,41787,41789,41790,41792,41794,42530,42534,42550,42551,42617,42621,42625,42626,42630,42634,42638,42639,49189,49190,51811,51812,52277,52281,52295,52296,52962,52966,52982,52983,53010,53014,53180,54026,54053,54054") && 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/platform/bootdevice/by-name/boot:67108864:e9c8439bd3fdcb4177aa2ecef47f42d257160d72",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:82360f17b1d82e79ce3b21268ee4b8bcc05fabd6",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:82360f17b1d82e79ce3b21268ee4b8bcc05fabd6");
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.");

# --- End patching dynamic partitions ---

show_progress(0.100000, 2);
show_mtupdate_stage("/cache/recovery/last_mtupdate_stage");
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "1") ,
(
ui_print("start to update general image");
package_extract_file("logo.bin", "/dev/block/platform/bootdevice/by-name/logo");
package_extract_file("dtbo.img", "/dev/block/platform/bootdevice/by-name/dtbo");
package_extract_file("exaid.img", "/dev/block/platform/bootdevice/by-name/exaid");
ui_print("Patching md1img image...");
show_progress(0.100000, 10);
patch_partition("EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:b07f1aabc7c1d9298489099a3216ec5ae3af32a2",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:3c0d272aeb0853fd5ebbcbb7b8058d9452dc0837",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:58123424:3c0d272aeb0853fd5ebbcbb7b8058d9452dc0837");
package_extract_file("spmfw.img", "/dev/block/platform/bootdevice/by-name/spmfw");
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "1");
),
ui_print("general images are already updated");
);
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "3") ,
(
if less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "2") then

ui_print("start to update alt loader image");
package_extract_file("sspm.img", "/dev/block/platform/bootdevice/by-name/sspm_2");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp2");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk2");
package_extract_file("tee.img", "/dev/block/platform/bootdevice/by-name/tee2");
if get_storage_type() then 
 assert(package_extract_file("preloader_ufs.img", "/dev/block/mmcblk0boot1"));
else 
 assert(set_emmc_writable("/sys/block/mmcblk0boot1/force_ro"),
        package_extract_file("preloader_emmc.img", "/dev/block/mmcblk0boot1"));
endif;
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "2");
endif;

switch_active("sspm_1", "sspm_2");
switch_active("scp1", "scp2");
switch_active("lk", "lk2");
switch_active("tee1", "tee2");
switch_active("preloader", "preloader2");
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "3");
),
ui_print("alt loder images are already updated");
);
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "5") ,
(
if less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "4") then

ui_print("start to update main loader image");
package_extract_file("sspm.img", "/dev/block/platform/bootdevice/by-name/sspm_1");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp1");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk");
package_extract_file("tee.img", "/dev/block/platform/bootdevice/by-name/tee1");
if get_storage_type() then 
 assert(package_extract_file("preloader_ufs.img", "/dev/block/mmcblk0boot0"));
else 
 assert(set_emmc_writable("/sys/block/mmcblk0boot0/force_ro"),
        package_extract_file("preloader_emmc.img", "/dev/block/mmcblk0boot0"));
endif;
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "4");
endif;

switch_active("sspm_2", "sspm_1");
switch_active("scp2", "scp1");
switch_active("lk2", "lk");
switch_active("tee2", "tee1");
switch_active("preloader2", "preloader");
),
ui_print("main loader images are already updated");
);
delete("/cache/recovery/last_mtupdate_stage");
package_extract_file("vbmeta_system.img", "/dev/block/platform/bootdevice/by-name/vbmeta_system");
package_extract_file("vbmeta.img", "/dev/block/platform/bootdevice/by-name/vbmeta");
package_extract_file("vbmeta_vendor.img", "/dev/block/platform/bootdevice/by-name/vbmeta_vendor");
post_ota_action();
