getprop("ro.product.device") == "raphaelin" || abort("E3004: This package is for \"raphaelin\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Xiaomi/raphaelin/raphaelin:10/QKQ1.190825.002/V12.0.5.0.QFKINXM:user/release-keys");
ui_print("Target: Xiaomi/raphaelin/raphaelin:10/QKQ1.190825.002/V12.0.6.0.QFKINXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Xiaomi/raphaelin/raphaelin:10/QKQ1.190825.002/V12.0.5.0.QFKINXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Xiaomi/raphaelin/raphaelin:10/QKQ1.190825.002/V12.0.6.0.QFKINXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Xiaomi/raphaelin/raphaelin:10/QKQ1.190825.002/V12.0.5.0.QFKINXM:user/release-keys or Xiaomi/raphaelin/raphaelin:10/QKQ1.190825.002/V12.0.6.0.QFKINXM:user/release-keys; this device has " + getprop("ro.build.fingerprint") + ".");
show_progress(0.100000, 10);
patch_partition_check("EMMC:/dev/block/bootdevice/by-name/boot:134217728:52d14ae335cd44a8d89a65ae6076433d18a4408a",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:5d901b0ada2718af3531d74868dba99421103bbc") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:52d14ae335cd44a8d89a65ae6076433d18a4408a\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:5d901b0ada2718af3531d74868dba99421103bbc\" has unexpected contents.");
apply_patch_space(134217728) || abort("E3006: Not enough free space on /cache to apply patches.");
show_progress(0.100000, 70);
if (range_sha1("/dev/block/bootdevice/by-name/system", "130,1,618,728,742,775,782,784,785,1031,1036,1038,1039,1145,1146,1631,1632,2320,2321,2323,2325,2326,2327,2342,2343,2524,2525,3696,3697,3699,3700,3704,3705,3713,3714,3715,3716,5936,6043,9073,9074,9076,9077,9481,9482,9484,9485,9503,9663,10481,10483,16422,16423,16556,16557,19326,19328,23641,32770,32990,32991,33496,65537,66042,98306,98526,98527,99032,131073,131578,163842,164062,164063,164568,196609,197114,229378,229598,229599,230104,262145,262650,294914,295134,295135,295640,327681,328186,360449,360954,393217,393722,425985,426490,458753,459258,491521,492026,524289,524794,557057,557562,589825,590330,622593,623098,655361,655866,688129,688634,720897,721402,753665,754170,786433,786938,819202,819422,819423,819928,851969,852474,871178,884736,884738,884958,884959,903007,917316,917503,917504") == "f40508a48490ea039426606cc9d24e501a5e3080" || block_image_verify("/dev/block/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/bootdevice/by-name/system");
ifelse (block_image_recover("/dev/block/bootdevice/by-name/system", "130,1,618,728,742,775,782,784,785,1031,1036,1038,1039,1145,1146,1631,1632,2320,2321,2323,2325,2326,2327,2342,2343,2524,2525,3696,3697,3699,3700,3704,3705,3713,3714,3715,3716,5936,6043,9073,9074,9076,9077,9481,9482,9484,9485,9503,9663,10481,10483,16422,16423,16556,16557,19326,19328,23641,32770,32990,32991,33496,65537,66042,98306,98526,98527,99032,131073,131578,163842,164062,164063,164568,196609,197114,229378,229598,229599,230104,262145,262650,294914,295134,295135,295640,327681,328186,360449,360954,393217,393722,425985,426490,458753,459258,491521,492026,524289,524794,557057,557562,589825,590330,622593,623098,655361,655866,688129,688634,720897,721402,753665,754170,786433,786938,819202,819422,819423,819928,851969,852474,871178,884736,884738,884958,884959,903007,917316,917503,917504") && block_image_verify("/dev/block/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/bootdevice/by-name/vendor", "188,1,268,602,610,620,621,702,703,767,768,770,781,783,784,792,793,801,802,1719,1720,1772,1812,2839,3587,12141,12142,12259,12260,12648,12700,13052,13053,13685,13686,14674,14679,16932,16934,21779,21780,25606,25607,30316,30317,32768,32770,32864,32865,42087,42088,47081,47082,56359,56360,57382,57384,57418,57419,57796,57797,58159,58161,58644,58645,58703,58704,65536,65537,79150,79153,79171,79172,79217,79218,79236,79237,79239,79240,79288,79289,79331,79332,79542,79543,79568,79569,79603,79604,79728,79729,79878,79879,79892,79893,89136,89137,89140,89142,95278,95279,95282,95283,95851,95853,98304,98306,98400,98401,102799,102800,102802,102803,105843,105844,105845,105846,105867,105868,106293,106294,106350,106351,106712,106714,106715,106721,107017,107019,107027,107040,107041,107043,107053,107068,107077,107078,107080,107084,107222,107223,107574,107575,107913,107914,112552,112553,113060,113061,115843,115844,120478,120479,120986,120987,123546,123547,124005,124006,126794,126795,127772,127773,128216,131073,131578,163842,163936,163937,164442,196609,197114,229378,229472,229473,229978,262145,262650,294914,295008,295009,295514,322274,360448,360449,386991,393125,393215,393216") == "d513ef5cd38c99bc8271ea83cc1b6a79a1e30ff1" || block_image_verify("/dev/block/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/bootdevice/by-name/vendor");
ifelse (block_image_recover("/dev/block/bootdevice/by-name/vendor", "188,1,268,602,610,620,621,702,703,767,768,770,781,783,784,792,793,801,802,1719,1720,1772,1812,2839,3587,12141,12142,12259,12260,12648,12700,13052,13053,13685,13686,14674,14679,16932,16934,21779,21780,25606,25607,30316,30317,32768,32770,32864,32865,42087,42088,47081,47082,56359,56360,57382,57384,57418,57419,57796,57797,58159,58161,58644,58645,58703,58704,65536,65537,79150,79153,79171,79172,79217,79218,79236,79237,79239,79240,79288,79289,79331,79332,79542,79543,79568,79569,79603,79604,79728,79729,79878,79879,79892,79893,89136,89137,89140,89142,95278,95279,95282,95283,95851,95853,98304,98306,98400,98401,102799,102800,102802,102803,105843,105844,105845,105846,105867,105868,106293,106294,106350,106351,106712,106714,106715,106721,107017,107019,107027,107040,107041,107043,107053,107068,107077,107078,107080,107084,107222,107223,107574,107575,107913,107914,112552,112553,113060,113061,115843,115844,120478,120479,120986,120987,123546,123547,124005,124006,126794,126795,127772,127773,128216,131073,131578,163842,163936,163937,164442,196609,197114,229378,229472,229473,229978,262145,262650,294914,295008,295009,295514,322274,360448,360449,386991,393125,393215,393216") && block_image_verify("/dev/block/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/bootdevice/by-name/boot:134217728:52d14ae335cd44a8d89a65ae6076433d18a4408a",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:5d901b0ada2718af3531d74868dba99421103bbc",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:5d901b0ada2718af3531d74868dba99421103bbc");

# ---- radio update tasks ----

ui_print("Patching firmware images...");
package_extract_file("firmware-update/abl.elf", "/dev/block/bootdevice/by-name/abl");
package_extract_file("firmware-update/abl.elf", "/dev/block/bootdevice/by-name/ablbak");
show_progress(0.600000, 600);
ui_print("Patching system image after verification.");
block_image_update("/dev/block/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/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);

# ---- radio update tasks 2 ----

ui_print("Patching vbmeta dtbo logo binimages...");
package_extract_file("firmware-update/dtbo.img", "/dev/block/bootdevice/by-name/dtbo");
package_extract_file("firmware-update/vbmeta.img", "/dev/block/bootdevice/by-name/vbmeta");
