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_ru/merlin:10/QP1A.190711.020/V12.0.5.0.QJORUXM:user/release-keys");
ui_print("Target: Redmi/merlin_ru/merlin:10/QP1A.190711.020/V12.0.6.0.QJORUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/merlin_ru/merlin:10/QP1A.190711.020/V12.0.5.0.QJORUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/merlin_ru/merlin:10/QP1A.190711.020/V12.0.6.0.QJORUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/merlin_ru/merlin:10/QP1A.190711.020/V12.0.5.0.QJORUXM:user/release-keys or Redmi/merlin_ru/merlin:10/QP1A.190711.020/V12.0.6.0.QJORUXM: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:62d81ed8d7d0391676b647606fbb64f91ec6c7fb",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:6cf475bd5534f09c79048b77e81957064d10665d") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:62d81ed8d7d0391676b647606fbb64f91ec6c7fb\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:6cf475bd5534f09c79048b77e81957064d10665d\" has unexpected contents.");
apply_patch_space(67108864) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:81ccfae72df027599c940f5f3d0db47d795e38c8",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:fa322dad7cba644195eedcbdfdde6bb95921c550") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:81ccfae72df027599c940f5f3d0db47d795e38c8\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:fa322dad7cba644195eedcbdfdde6bb95921c550\" has unexpected contents.");
apply_patch_space(58163104) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "28,1,195,223,230,480,484,500,501,2229,32770,32930,98306,98466,163842,164002,229378,229538,294914,295074,654770,655360,655374,655379,655395,656791,667200,667340,667341") == "be44afde5db9aefa4b40afe9a340b0cfff240da7" || 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"), "28,1,195,223,230,480,484,500,501,2229,32770,32930,98306,98466,163842,164002,229378,229538,294914,295074,654770,655360,655374,655379,655395,656791,667200,667340,667341") && 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"), "220,1,78,815,821,839,840,845,846,848,849,1363,1364,1366,1367,1528,1529,1540,1544,1548,1549,1765,1766,1845,1846,1916,1917,2102,2103,2181,2182,2261,2262,2341,2342,3228,3229,5628,5629,6704,6708,6709,6711,6712,6714,8967,8969,8976,8978,14442,14443,22481,22482,23712,23714,24706,24707,24775,24776,32768,32770,32808,32859,39984,39985,40306,40307,49198,49199,52353,53619,54452,54453,54787,54788,55118,55119,55438,55439,55760,55761,56090,56091,56418,56419,56739,56740,65536,65583,68346,68347,69418,69419,69738,69739,70064,70065,70393,70394,70729,70730,71047,71048,71369,71370,71698,71699,74155,74164,75788,75789,89692,89892,92548,92549,94978,95853,98304,98306,98344,98382,100920,100921,108436,108654,109618,109619,110574,110786,110945,110946,118704,118705,127194,127195,131072,131101,133634,133635,138333,138334,138358,138359,139337,139338,139659,139660,139994,139995,140335,140336,140670,140671,141011,141012,141331,141332,141656,141657,141975,141976,142301,142302,142610,142611,142682,142683,142901,142902,143220,143221,143564,143565,143881,143882,144209,144210,144538,144539,144872,144873,145201,145202,145534,145535,145855,145856,146182,146183,146503,146504,146599,146600,150636,150638,150876,150878,150879,150883,150886,150888,150893,150894,150902,150903,150920,150921,150941,150945,150951,150952,153420,153422,155371,155372,155881,155883,158121,160629,160677,160678") == "315a5a706a1bb132e7007a76a12f16b5d92fb6a2" || 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"), "220,1,78,815,821,839,840,845,846,848,849,1363,1364,1366,1367,1528,1529,1540,1544,1548,1549,1765,1766,1845,1846,1916,1917,2102,2103,2181,2182,2261,2262,2341,2342,3228,3229,5628,5629,6704,6708,6709,6711,6712,6714,8967,8969,8976,8978,14442,14443,22481,22482,23712,23714,24706,24707,24775,24776,32768,32770,32808,32859,39984,39985,40306,40307,49198,49199,52353,53619,54452,54453,54787,54788,55118,55119,55438,55439,55760,55761,56090,56091,56418,56419,56739,56740,65536,65583,68346,68347,69418,69419,69738,69739,70064,70065,70393,70394,70729,70730,71047,71048,71369,71370,71698,71699,74155,74164,75788,75789,89692,89892,92548,92549,94978,95853,98304,98306,98344,98382,100920,100921,108436,108654,109618,109619,110574,110786,110945,110946,118704,118705,127194,127195,131072,131101,133634,133635,138333,138334,138358,138359,139337,139338,139659,139660,139994,139995,140335,140336,140670,140671,141011,141012,141331,141332,141656,141657,141975,141976,142301,142302,142610,142611,142682,142683,142901,142902,143220,143221,143564,143565,143881,143882,144209,144210,144538,144539,144872,144873,145201,145202,145534,145535,145855,145856,146182,146183,146503,146504,146599,146600,150636,150638,150876,150878,150879,150883,150886,150888,150893,150894,150902,150903,150920,150921,150941,150945,150951,150952,153420,153422,155371,155372,155881,155883,158121,160629,160677,160678") && 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"), "174,1,90,5826,5830,8769,8772,11722,11723,12268,12274,12291,12292,19143,19144,27369,27370,28009,28013,28783,28784,31640,31641,32005,32009,32516,32517,32529,32533,32537,32538,32768,32770,32844,32850,40610,40611,41011,41012,49825,49826,58259,58260,65536,65542,67848,67849,78833,78834,87148,87149,94724,94725,94731,94732,94860,94864,94868,94869,98304,98306,98380,98386,110228,110229,126795,126796,129239,129240,131072,131078,136060,136061,147103,147104,150214,150218,150227,150228,150229,150230,150246,150247,150248,150249,150254,150255,150294,150298,150302,150304,150309,150312,150316,150318,150323,150324,150369,150370,150429,150432,150881,150882,151253,151254,153360,153362,157219,157220,160347,160349,163840,163842,163916,163935,177207,177208,186247,186249,194237,194238,196608,196647,198864,198866,198913,198915,199056,199058,200454,200456,201542,201544,201620,201622,217926,217927,229376,229378,229452,229484,233344,233345,233834,233838,233852,233853,235228,235229,236633,236637,238534,238535,238960,238962,260720,260721,262144,262154,289129,289130,289160,289164,289168,289169,292879,292882,294912,294914,294988,294991,303661,308475,308549,308550") == "376a18c427bcff8f074258bf36060b1ac3125956" || 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"), "174,1,90,5826,5830,8769,8772,11722,11723,12268,12274,12291,12292,19143,19144,27369,27370,28009,28013,28783,28784,31640,31641,32005,32009,32516,32517,32529,32533,32537,32538,32768,32770,32844,32850,40610,40611,41011,41012,49825,49826,58259,58260,65536,65542,67848,67849,78833,78834,87148,87149,94724,94725,94731,94732,94860,94864,94868,94869,98304,98306,98380,98386,110228,110229,126795,126796,129239,129240,131072,131078,136060,136061,147103,147104,150214,150218,150227,150228,150229,150230,150246,150247,150248,150249,150254,150255,150294,150298,150302,150304,150309,150312,150316,150318,150323,150324,150369,150370,150429,150432,150881,150882,151253,151254,153360,153362,157219,157220,160347,160349,163840,163842,163916,163935,177207,177208,186247,186249,194237,194238,196608,196647,198864,198866,198913,198915,199056,199058,200454,200456,201542,201544,201620,201622,217926,217927,229376,229378,229452,229484,233344,233345,233834,233838,233852,233853,235228,235229,236633,236637,238534,238535,238960,238962,260720,260721,262144,262154,289129,289130,289160,289164,289168,289169,292879,292882,294912,294914,294988,294991,303661,308475,308549,308550") && 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:62d81ed8d7d0391676b647606fbb64f91ec6c7fb",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:6cf475bd5534f09c79048b77e81957064d10665d",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:6cf475bd5534f09c79048b77e81957064d10665d");
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);
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:58163104:81ccfae72df027599c940f5f3d0db47d795e38c8",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:fa322dad7cba644195eedcbdfdde6bb95921c550",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:58163104:fa322dad7cba644195eedcbdfdde6bb95921c550");
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();
