getprop("ro.product.device") == "begonia" || abort("E3004: This package is for \"begonia\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/begonia/begonia:10/QP1A.190711.020/V12.0.2.0.QGGIDXM:user/release-keys");
ui_print("Target: Redmi/begonia/begonia:10/QP1A.190711.020/V12.0.3.0.QGGIDXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/begonia/begonia:10/QP1A.190711.020/V12.0.2.0.QGGIDXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/begonia/begonia:10/QP1A.190711.020/V12.0.3.0.QGGIDXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/begonia/begonia:10/QP1A.190711.020/V12.0.2.0.QGGIDXM:user/release-keys or Redmi/begonia/begonia:10/QP1A.190711.020/V12.0.3.0.QGGIDXM: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:526c1a4341f15b139bda64b4c317681d8f6427b7",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:b5b3e13318a402bdc23e6b35ccd608fd1e884750") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:526c1a4341f15b139bda64b4c317681d8f6427b7\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:b5b3e13318a402bdc23e6b35ccd608fd1e884750\" has unexpected contents.");
apply_patch_space(104562688) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:61737152:2b0f74b759c26f1cfbbdc43662af7afdc506c9ff",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:61733408:a8716fee71955add6faeb9021d9bba74d167da84") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:61737152:2b0f74b759c26f1cfbbdc43662af7afdc506c9ff\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:61733408:a8716fee71955add6faeb9021d9bba74d167da84\" has unexpected contents.");
apply_patch_space(61737152) || 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", "146,1,617,728,742,771,778,780,781,1031,1036,1038,1039,1145,1146,1622,1623,2296,2297,2299,2301,2302,2303,2318,2319,2499,2511,3612,3642,3644,3645,3649,3650,3658,3659,3660,3661,4957,5181,6339,6469,8673,8702,9160,9161,9163,32770,32990,32991,33496,65537,66042,98306,98526,98527,99032,131073,131578,143044,143050,143051,143370,163842,164062,164063,164568,196609,197114,229378,229598,229599,230104,262145,262650,279391,279395,294914,295134,295135,295640,327681,328186,360449,360954,370450,377528,377529,382101,393217,393722,425985,426490,458753,459258,491521,492026,524289,524794,527125,530619,530658,530661,531843,533404,537728,537729,537843,537844,537855,537856,537905,537907,537949,537950,544035,546621,557057,557562,589825,590330,622593,623098,655361,655866,688129,688634,720897,721402,753665,754170,786433,786938,819202,819422,819423,819928,851969,852474,867138,884736,884738,884958,884959,903007,917316,917503,917504") == "98af402ca80837c83cb7aa4ec535404e38d8d153" || 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", "146,1,617,728,742,771,778,780,781,1031,1036,1038,1039,1145,1146,1622,1623,2296,2297,2299,2301,2302,2303,2318,2319,2499,2511,3612,3642,3644,3645,3649,3650,3658,3659,3660,3661,4957,5181,6339,6469,8673,8702,9160,9161,9163,32770,32990,32991,33496,65537,66042,98306,98526,98527,99032,131073,131578,143044,143050,143051,143370,163842,164062,164063,164568,196609,197114,229378,229598,229599,230104,262145,262650,279391,279395,294914,295134,295135,295640,327681,328186,360449,360954,370450,377528,377529,382101,393217,393722,425985,426490,458753,459258,491521,492026,524289,524794,527125,530619,530658,530661,531843,533404,537728,537729,537843,537844,537855,537856,537905,537907,537949,537950,544035,546621,557057,557562,589825,590330,622593,623098,655361,655866,688129,688634,720897,721402,753665,754170,786433,786938,819202,819422,819423,819928,851969,852474,867138,884736,884738,884958,884959,903007,917316,917503,917504") && 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", "58,1,274,602,610,961,964,966,967,969,970,975,981,1229,1267,1751,1752,4007,4009,5079,5080,6111,6117,8370,8372,8378,8380,15988,15989,20661,32770,32864,32865,33370,65537,66042,98306,98400,98401,98906,131073,131578,163842,163936,163937,164442,196609,197114,227204,229376,229378,294912,294914,360448,360449,386991,393125,393215,393216") == "172b60b36c5172a5212575347d0cb0028755475b" || 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", "58,1,274,602,610,961,964,966,967,969,970,975,981,1229,1267,1751,1752,4007,4009,5079,5080,6111,6117,8370,8372,8378,8380,15988,15989,20661,32770,32864,32865,33370,65537,66042,98306,98400,98401,98906,131073,131578,163842,163936,163937,164442,196609,197114,227204,229376,229378,294912,294914,360448,360449,386991,393125,393215,393216") && 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);
patch_partition("EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:526c1a4341f15b139bda64b4c317681d8f6427b7",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:b5b3e13318a402bdc23e6b35ccd608fd1e884750",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:b5b3e13318a402bdc23e6b35ccd608fd1e884750");
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_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("audio_dsp.img", "/dev/block/platform/bootdevice/by-name/audio_dsp");
package_extract_file("cam_vpu3.img", "/dev/block/platform/bootdevice/by-name/cam_vpu3");
package_extract_file("dtbo.img", "/dev/block/platform/bootdevice/by-name/dtbo");
package_extract_file("cam_vpu2.img", "/dev/block/platform/bootdevice/by-name/cam_vpu2");
ui_print("Patching md1img image...");
show_progress(0.100000, 10);
patch_partition("EMMC:/dev/block/platform/bootdevice/by-name/md1img:61737152:2b0f74b759c26f1cfbbdc43662af7afdc506c9ff",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:61733408:a8716fee71955add6faeb9021d9bba74d167da84",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:61733408:a8716fee71955add6faeb9021d9bba74d167da84");
package_extract_file("spmfw.img", "/dev/block/platform/bootdevice/by-name/spmfw");
package_extract_file("cam_vpu1.img", "/dev/block/platform/bootdevice/by-name/cam_vpu1");
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("lk.img", "/dev/block/platform/bootdevice/by-name/lk2");
package_extract_file("gz.img", "/dev/block/platform/bootdevice/by-name/gz2");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp2");
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/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("lk", "lk2");
switch_active("gz1", "gz2");
switch_active("scp1", "scp2");
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("lk.img", "/dev/block/platform/bootdevice/by-name/lk");
package_extract_file("gz.img", "/dev/block/platform/bootdevice/by-name/gz1");
package_extract_file("scp.img", "/dev/block/platform/bootdevice/by-name/scp1");
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/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("lk2", "lk");
switch_active("gz2", "gz1");
switch_active("scp2", "scp1");
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.img", "/dev/block/platform/bootdevice/by-name/vbmeta");
post_ota_action();
