getprop("ro.product.device") == "lotus" || abort("E3004: This package is for \"lotus\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: xiaomi/lotus/lotus:8.1.0/O11019/V10.3.3.0.OFICNXM:user/release-keys");
ui_print("Target: xiaomi/lotus/lotus:8.1.0/O11019/V10.3.4.0.OFICNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "xiaomi/lotus/lotus:8.1.0/O11019/V10.3.3.0.OFICNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "xiaomi/lotus/lotus:8.1.0/O11019/V10.3.4.0.OFICNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of xiaomi/lotus/lotus:8.1.0/O11019/V10.3.3.0.OFICNXM:user/release-keys or xiaomi/lotus/lotus:8.1.0/O11019/V10.3.4.0.OFICNXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
show_progress(0.100000, 5);
apply_patch_check("EMMC:/dev/block/platform/bootdevice/by-name/boot:33554432:1a52ecc0db2636fc70846273a2b90208584b7715:33554432:51c27fe6baf5cb99994ccb14e096e64b05f5af10", "1a52ecc0db2636fc70846273a2b90208584b7715", "51c27fe6baf5cb99994ccb14e096e64b05f5af10") || abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:33554432:1a52ecc0db2636fc70846273a2b90208584b7715:33554432:51c27fe6baf5cb99994ccb14e096e64b05f5af10\" has unexpected contents.");
apply_patch_space(301424640) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1("/dev/block/platform/bootdevice/by-name/system", "68,1,453,696,32770,32958,32959,33464,65537,66042,98306,98494,98495,99000,131073,131578,163842,164030,164031,164536,166032,166040,196609,197114,229378,229566,229567,230072,256256,257471,262145,262650,294914,295102,295103,295608,327681,328186,328187,344570,360449,360954,393217,393722,425985,426490,458753,459258,491521,492026,524289,524794,557057,557562,579429,589824,589825,592204,622593,623098,655361,655866,685234,753664,753665,774003,786268,786431,786432") == "b81621ed2a55b11f8364075987c3124a206ae1c5" || block_image_verify("/dev/block/platform/bootdevice/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat")) then
ui_print("Verified system image...");
else
check_first_block("/dev/block/platform/bootdevice/by-name/system");
ifelse (block_image_recover("/dev/block/platform/bootdevice/by-name/system", "68,1,453,696,32770,32958,32959,33464,65537,66042,98306,98494,98495,99000,131073,131578,163842,164030,164031,164536,166032,166040,196609,197114,229378,229566,229567,230072,256256,257471,262145,262650,294914,295102,295103,295608,327681,328186,328187,344570,360449,360954,393217,393722,425985,426490,458753,459258,491521,492026,524289,524794,557057,557562,579429,589824,589825,592204,622593,623098,655361,655866,685234,753664,753665,774003,786268,786431,786432") && block_image_verify("/dev/block/platform/bootdevice/by-name/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("/dev/block/platform/bootdevice/by-name/vendor", "24,1,152,555,32770,32817,32818,33323,65537,66042,66043,70138,98306,98353,98354,98859,106040,163840,163842,163889,163890,193485,196553,196607,196608") == "ec2cab602acdfd5f154efc71f281f0cb397cf35d" || block_image_verify("/dev/block/platform/bootdevice/by-name/vendor", package_extract_file("vendor.transfer.list"), "vendor.new.dat", "vendor.patch.dat")) then
ui_print("Verified vendor image...");
else
check_first_block("/dev/block/platform/bootdevice/by-name/vendor");
ifelse (block_image_recover("/dev/block/platform/bootdevice/by-name/vendor", "24,1,152,555,32770,32817,32818,33323,65537,66042,66043,70138,98306,98353,98354,98859,106040,163840,163842,163889,163890,193485,196553,196607,196608") && block_image_verify("/dev/block/platform/bootdevice/by-name/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;

# ---- start making changes here ----

ui_print("Patching boot image...");
show_progress(0.100000, 10);
apply_patch("EMMC:/dev/block/platform/bootdevice/by-name/boot:33554432:1a52ecc0db2636fc70846273a2b90208584b7715:33554432:51c27fe6baf5cb99994ccb14e096e64b05f5af10",
            "-", 51c27fe6baf5cb99994ccb14e096e64b05f5af10, 33554432,
            1a52ecc0db2636fc70846273a2b90208584b7715, package_extract_file("patch/boot.img.p")) || abort("Failed to apply patch \"patch/boot.img.p \".");
show_progress(0.600000, 600);
ui_print("Patching system image after verification.");
block_image_update("/dev/block/platform/bootdevice/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat") ||
  abort("E1001: Failed to update system image.");
ui_print("Patching vendor image after verification.");
block_image_update("/dev/block/platform/bootdevice/by-name/vendor", package_extract_file("vendor.transfer.list"), "vendor.new.dat", "vendor.patch.dat") ||
  abort("E2001: Failed to update vendor image.");
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("md1img.img", "/dev/block/platform/bootdevice/by-name/md1img");
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("tee.img", "/dev/block/platform/bootdevice/by-name/tee2");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk2");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp2");
assert(set_emmc_writable("/sys/block/mmcblk0boot1/force_ro"),
       package_extract_file("preloader.img", "/dev/block/mmcblk0boot1"));
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "2");
endif;

switch_active("sspm_1", "sspm_2");
switch_active("tee1", "tee2");
switch_active("lk", "lk2");
switch_active("scp1", "scp2");
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("tee.img", "/dev/block/platform/bootdevice/by-name/tee1");
package_extract_file("lk.img", "/dev/block/platform/bootdevice/by-name/lk");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp1");
assert(set_emmc_writable("/sys/block/mmcblk0boot0/force_ro"),
       package_extract_file("preloader.img", "/dev/block/mmcblk0boot0"));
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "4");
endif;

switch_active("sspm_2", "sspm_1");
switch_active("tee2", "tee1");
switch_active("lk2", "lk");
switch_active("scp2", "scp1");
switch_active("preloader2", "preloader");
),
ui_print("main loader images are already updated");
);
ifelse (
less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "6") ,
(
if less_than_int(get_mtupdate_stage("/cache/recovery/last_mtupdate_stage"), "5") then

ui_print("start to update vbmeta and odmdtbo image");
package_extract_file("vbmeta.img", "/dev/block/platform/bootdevice/by-name/vbmeta");
package_extract_file("odmdtbo.img", "/dev/block/platform/bootdevice/by-name/odmdtbo");
set_mtupdate_stage("/cache/recovery/last_mtupdate_stage", "5");
endif;

),
ui_print("vbmeta and odmdtbo images are already updated");
);
delete("/cache/recovery/last_mtupdate_stage");
show_progress(0.100000, 2);
