getprop("ro.product.device") == "shiva" || abort("E3004: This package is for \"shiva\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: POCO/shiva/shiva:10/QP1A.190711.020/V11.0.5.0.QJRINXM:user/release-keys");
ui_print("Target: POCO/shiva/shiva:10/QP1A.190711.020/V12.0.1.0.QJRINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "POCO/shiva/shiva:10/QP1A.190711.020/V11.0.5.0.QJRINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "POCO/shiva/shiva:10/QP1A.190711.020/V12.0.1.0.QJRINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of POCO/shiva/shiva:10/QP1A.190711.020/V11.0.5.0.QJRINXM:user/release-keys or POCO/shiva/shiva:10/QP1A.190711.020/V12.0.1.0.QJRINXM: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:f6669e8f16f39bf8d36245ec00d403fa030b9aff",
                      "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:e9e5d749796f3ed8700b19474696cbbeb8044c67") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:f6669e8f16f39bf8d36245ec00d403fa030b9aff\" or \"EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:e9e5d749796f3ed8700b19474696cbbeb8044c67\" has unexpected contents.");
apply_patch_space(72941568) || abort("E3006: Not enough free space on /cache to apply patches.");
patch_partition_check("EMMC:/dev/block/platform/bootdevice/by-name/md1img:59070448:12218d3ac384033ca27f0ca57557dac5331c725c",
                      "EMMC:/dev/block/platform/bootdevice/by-name/md1img:59044784:ae6d70f04bd5841ccac339dab35651a539e79106") ||
    abort("E3005: \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59070448:12218d3ac384033ca27f0ca57557dac5331c725c\" or \"EMMC:/dev/block/platform/bootdevice/by-name/md1img:59044784:ae6d70f04bd5841ccac339dab35651a539e79106\" has unexpected contents.");
apply_patch_space(59070448) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1(map_partition("system"), "176,1,192,193,32770,32914,34157,34173,34175,34181,58283,58970,58972,58996,86582,87651,91118,91125,98306,98450,163842,163986,174739,174740,229378,229522,294914,295058,300793,300797,301900,301904,334866,334957,335563,335745,354212,354262,362727,362730,362758,362824,368325,368329,371780,371783,371917,371919,395778,395811,424878,425984,426005,429169,431644,431775,431925,432058,432875,433005,433155,433288,434917,435811,435910,436057,495907,495910,495943,495945,497197,497199,497312,497314,502442,502448,502451,502471,502507,502523,502524,502527,502537,502538,502540,502541,502542,502545,502550,502551,502559,502563,502574,502575,502601,502603,502609,502612,502614,502615,502624,502626,502641,502642,502644,502645,502660,502662,502722,502724,502740,502746,502748,502759,502795,502810,502812,502814,502824,502825,502827,502828,502830,502832,502834,502835,502846,502851,502861,502862,502890,502892,502898,502901,502903,502904,502913,502915,502931,502933,502960,502962,502966,502967,502991,502992,503009,503011,503027,503028,503037,503038,503062,503065,503111,503112,503133,503135,503147,503149,503228,503229,503564,503565,508569,508570,589511,589522,589527,589824,589832,589845,589853,591325,600696,600824,600825") == "e68e08bc41902ad2fb902de818fb908fdf419aa1" || 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"), "176,1,192,193,32770,32914,34157,34173,34175,34181,58283,58970,58972,58996,86582,87651,91118,91125,98306,98450,163842,163986,174739,174740,229378,229522,294914,295058,300793,300797,301900,301904,334866,334957,335563,335745,354212,354262,362727,362730,362758,362824,368325,368329,371780,371783,371917,371919,395778,395811,424878,425984,426005,429169,431644,431775,431925,432058,432875,433005,433155,433288,434917,435811,435910,436057,495907,495910,495943,495945,497197,497199,497312,497314,502442,502448,502451,502471,502507,502523,502524,502527,502537,502538,502540,502541,502542,502545,502550,502551,502559,502563,502574,502575,502601,502603,502609,502612,502614,502615,502624,502626,502641,502642,502644,502645,502660,502662,502722,502724,502740,502746,502748,502759,502795,502810,502812,502814,502824,502825,502827,502828,502830,502832,502834,502835,502846,502851,502861,502862,502890,502892,502898,502901,502903,502904,502913,502915,502931,502933,502960,502962,502966,502967,502991,502992,503009,503011,503027,503028,503037,503038,503062,503065,503111,503112,503133,503135,503147,503149,503228,503229,503564,503565,508569,508570,589511,589522,589527,589824,589832,589845,589853,591325,600696,600824,600825") && 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,10701,13553,32770,32806,98306,98342,131098,131101,148446,148906,151270,151315,151316") == "0f2336b4cf131d934c125a2ced24705e7227d448" || 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,10701,13553,32770,32806,98306,98342,131098,131101,148446,148906,151270,151315,151316") && 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"), "22,1,76,4412,4416,4507,6560,6662,32770,32829,98306,98365,163842,163901,229378,229437,229441,229444,242982,243716,247580,247643,247644") == "5bfb7b6e1cf70f74e3afae2648b33aa3c767cf7d" || 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"), "22,1,76,4412,4416,4507,6560,6662,32770,32829,98306,98365,163842,163901,229378,229437,229441,229444,242982,243716,247580,247643,247644") && 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:f6669e8f16f39bf8d36245ec00d403fa030b9aff",
                "EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:e9e5d749796f3ed8700b19474696cbbeb8044c67",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/boot:67108864:e9e5d749796f3ed8700b19474696cbbeb8044c67");
show_progress(0.600000, 600);

# --- Start patching dynamic partitions ---


# 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.");

# 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 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:59070448:12218d3ac384033ca27f0ca57557dac5331c725c",
                "EMMC:/dev/block/platform/bootdevice/by-name/md1img:59044784:ae6d70f04bd5841ccac339dab35651a539e79106",
                package_extract_file("patch/md1img.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/platform/bootdevice/by-name/md1img:59044784:ae6d70f04bd5841ccac339dab35651a539e79106");
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();
