getprop("ro.product.device") == "cannong" || abort("E3004: This package is for \"cannong\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/cannong_eea/cannong:11/RP1A.200720.011/V12.5.7.0.RJEEUXM:user/release-keys");
ui_print("Target: Redmi/cannong_eea/cannong:11/RP1A.200720.011/V12.5.8.0.RJEEUXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/cannong_eea/cannong:11/RP1A.200720.011/V12.5.7.0.RJEEUXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/cannong_eea/cannong:11/RP1A.200720.011/V12.5.8.0.RJEEUXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/cannong_eea/cannong:11/RP1A.200720.011/V12.5.7.0.RJEEUXM:user/release-keys or Redmi/cannong_eea/cannong:11/RP1A.200720.011/V12.5.8.0.RJEEUXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
patch_partition_check(concat("EMMC:","/dev/block/by-name/boot",":67108864:75875e334c1b6e08a550a5f5b74e5c4245603e83"),
                      concat("EMMC:","/dev/block/by-name/boot",":67108864:a30e0a26e80efa2b49c6c3c77c346d2dc84ddd5b")) ||
    abort(concat("E3005: \"",concat("EMMC:","/dev/block/by-name/boot",":67108864:75875e334c1b6e08a550a5f5b74e5c4245603e83"),"\" or \"",concat("EMMC:","/dev/block/by-name/boot",":67108864:a30e0a26e80efa2b49c6c3c77c346d2dc84ddd5b"),"\" has unexpected contents."));
apply_patch_space(75059200) || abort("E3006: Not enough free space on /cache to apply patches.");
if (range_sha1(map_partition("system"), "18,1,32770,32968,98306,98504,163842,164040,229378,229576,294914,295112,786446,786448,810534,812992,825874,826047,826048") == "69f892af20ed2ae2e79a148c3d75659e090a4ae2" || 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"), "18,1,32770,32968,98306,98504,163842,164040,229378,229576,294914,295112,786446,786448,810534,812992,825874,826047,826048") && 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"), "14,1,32770,32816,98306,98352,163842,163888,163917,163919,188289,188872,191868,191920,191921") == "cd4677533be1794cc7d86d1f2ed13a119c7a975e" || 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"), "14,1,32770,32816,98306,98352,163842,163888,163917,163919,188289,188872,191868,191920,191921") && 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"), "62,1,88,6245,6246,12449,12453,15187,15190,17935,17936,26118,26119,32768,32770,32842,32848,35720,35721,38760,38761,39123,39127,39646,39647,39659,39663,39667,39668,41508,41512,42948,42949,52124,52125,60245,60246,65536,65542,70093,70094,81173,81174,86563,86564,98304,98306,98378,98384,99316,163842,163914,229378,229450,294914,294986,294990,294992,296491,297387,302102,302175,302176") == "451972b13ab2c1050a694e5dd7274800c9c6e2b3" || 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"), "62,1,88,6245,6246,12449,12453,15187,15190,17935,17936,26118,26119,32768,32770,32842,32848,35720,35721,38760,38761,39123,39127,39646,39647,39659,39663,39667,39668,41508,41512,42948,42949,52124,52125,60245,60246,65536,65542,70093,70094,81173,81174,86563,86564,98304,98306,98378,98384,99316,163842,163914,229378,229450,294914,294986,294990,294992,296491,297387,302102,302175,302176") && 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 ----


# --- Start patching dynamic partitions ---


# Update dynamic partition metadata

assert(update_dynamic_partitions(package_extract_file("dynamic_partitions_op_list")));

# Patch partition system

ui_print("Patching system image after verification.");
show_progress(0.700000, 0);
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 vendor

ui_print("Patching vendor image after verification.");
show_progress(0.100000, 0);
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 product

ui_print("Patching product image after verification.");
show_progress(0.100000, 0);
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.");

# --- End patching dynamic partitions ---

ui_print("Patching boot image...");
show_progress(0.100000, 10);
patch_partition(concat("EMMC:","/dev/block/by-name/boot",":67108864:75875e334c1b6e08a550a5f5b74e5c4245603e83"),
                concat("EMMC:","/dev/block/by-name/boot",":67108864:a30e0a26e80efa2b49c6c3c77c346d2dc84ddd5b"),
                package_extract_file("boot.img.p")) ||
    abort(concat(        "E3008: Failed to apply patch to ",concat("EMMC:","/dev/block/by-name/boot",":67108864:a30e0a26e80efa2b49c6c3c77c346d2dc84ddd5b")));
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("audio_dsp.img", "/dev/block/by-name/audio_dsp");
package_extract_file("pi_img.img", "/dev/block/by-name/pi_img");
package_extract_file("cam_vpu3.img", "/dev/block/by-name/cam_vpu3");
package_extract_file("dtbo.img", "/dev/block/by-name/dtbo");
package_extract_file("logo.bin", "/dev/block/by-name/logo");
package_extract_file("cam_vpu1.img", "/dev/block/by-name/cam_vpu1");
package_extract_file("cam_vpu2.img", "/dev/block/by-name/cam_vpu2");
package_extract_file("md1img.img", "/dev/block/by-name/md1img");
package_extract_file("spmfw.img", "/dev/block/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/by-name/sspm_2");
package_extract_file("tee.img", "/dev/block/by-name/tee2");
package_extract_file("scp.img", "/dev/block/by-name/scp2");
package_extract_file("mcupm.img", "/dev/block/by-name/mcupm_2");
package_extract_file("lk.img", "/dev/block/by-name/lk2");
package_extract_file("dpm.img", "/dev/block/by-name/dpm_2");
if get_storage_type() then 
 assert(package_extract_file("preloader_ufs.img", "/dev/block/sdb"));
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("tee1", "tee2");
switch_active("scp1", "scp2");
switch_active("mcupm_1", "mcupm_2");
switch_active("lk", "lk2");
switch_active("dpm_1", "dpm_2");
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/by-name/sspm_1");
package_extract_file("tee.img", "/dev/block/by-name/tee1");
package_extract_file("scp.img", "/dev/block/by-name/scp1");
package_extract_file("mcupm.img", "/dev/block/by-name/mcupm_1");
package_extract_file("lk.img", "/dev/block/by-name/lk");
package_extract_file("dpm.img", "/dev/block/by-name/dpm_1");
if get_storage_type() then 
 assert(package_extract_file("preloader_ufs.img", "/dev/block/sda"));
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("tee2", "tee1");
switch_active("scp2", "scp1");
switch_active("mcupm_2", "mcupm_1");
switch_active("lk2", "lk");
switch_active("dpm_2", "dpm_1");
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/by-name/vbmeta_system");
package_extract_file("vbmeta.img", "/dev/block/by-name/vbmeta");
package_extract_file("vbmeta_vendor.img", "/dev/block/by-name/vbmeta_vendor");
post_ota_action();
set_progress(1.000000);
