getprop("ro.product.device") == "gauguin" || abort("E3004: This package is for \"gauguin\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Source: Redmi/gauguin/gauguin:10/QKQ1.200628.002/V12.0.7.0.QJSCNXM:user/release-keys");
ui_print("Target: Redmi/gauguin/gauguin:10/QKQ1.200628.002/V12.0.8.0.QJSCNXM:user/release-keys");
ui_print("Verifying current system...");
getprop("ro.build.fingerprint") == "Redmi/gauguin/gauguin:10/QKQ1.200628.002/V12.0.7.0.QJSCNXM:user/release-keys" ||
    getprop("ro.build.fingerprint") == "Redmi/gauguin/gauguin:10/QKQ1.200628.002/V12.0.8.0.QJSCNXM:user/release-keys" ||
    abort("E3001: Package expects build fingerprint of Redmi/gauguin/gauguin:10/QKQ1.200628.002/V12.0.7.0.QJSCNXM:user/release-keys or Redmi/gauguin/gauguin:10/QKQ1.200628.002/V12.0.8.0.QJSCNXM: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:f2546528a4622517445b0c8f052eac13d45519c6",
                      "EMMC:/dev/block/bootdevice/by-name/boot:134217728:dd692ad5d898c5a89076672a9a2fd76d660bb17e") ||
    abort("E3005: \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:f2546528a4622517445b0c8f052eac13d45519c6\" or \"EMMC:/dev/block/bootdevice/by-name/boot:134217728:dd692ad5d898c5a89076672a9a2fd76d660bb17e\" 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(map_partition("system"), "26,1,32770,32971,98306,98507,163842,164043,208532,208574,208743,208797,229378,229579,294914,295115,786437,786448,819202,819403,819404,819419,821801,824339,837401,837575,837576") == "e699fc7de5fe55f4be3478402eb1589fedd5a82c" || 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"), "26,1,32770,32971,98306,98507,163842,164043,208532,208574,208743,208797,229378,229579,294914,295115,786437,786448,819202,819403,819404,819419,821801,824339,837401,837575,837576") && 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"), "42,1,32770,32870,84274,84275,91574,91710,98306,98406,116507,117138,163842,163942,165741,166264,166265,166280,218489,218509,229378,229478,236056,236798,293332,294043,294914,295014,322844,324669,325192,325405,325754,326445,375887,376066,393226,393234,410436,411732,418259,418353,418354") == "617f7f9b2f81103d8fd44a42f6770240ddcb3533" || 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"), "42,1,32770,32870,84274,84275,91574,91710,98306,98406,116507,117138,163842,163942,165741,166264,166265,166280,218489,218509,229378,229478,236056,236798,293332,294043,294914,295014,322844,324669,325192,325405,325754,326445,375887,376066,393226,393234,410436,411732,418259,418353,418354") && 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"), "230,1,54,70,74,78,79,86,92,93,94,100,106,107,108,123,129,130,131,191,195,200,201,203,204,206,207,213,219,220,221,336,342,343,344,356,362,363,364,367,368,374,380,381,382,405,411,412,413,1257,1258,14956,14957,14976,14977,30646,30647,30768,30769,30945,30946,31313,31314,32065,32066,32768,32770,32789,32814,35603,35604,35615,35621,35622,35623,35633,35634,35639,35643,35648,35649,35653,35659,35663,35664,35670,35676,35677,35678,35690,35696,35697,35698,35700,35707,35713,35714,37472,37473,37479,37480,37620,37621,38823,38830,38884,38891,38896,38903,38928,38935,38955,38962,38980,38987,39003,39004,39015,39017,39180,39181,39182,39183,39184,39185,39220,39222,39230,39231,39502,39506,39510,39514,39515,39519,39520,39524,39525,39529,39530,39534,39535,39539,39540,39544,39545,39558,39559,39563,39564,39568,39569,39573,39574,39576,39577,39581,39582,39586,39587,39589,39590,39594,39595,39599,39600,39604,39605,39609,39610,39614,39615,39619,39620,39624,39625,39629,39630,39632,39637,39641,39642,39646,39647,39651,39652,39656,39657,39659,39660,39664,39665,39667,39668,39672,39673,39675,39676,39680,39681,39685,39686,39688,39689,39693,39694,39728,39731,39840,39896,40194,40899,40900,44653,44654,45817,45818,50747,50748,52374,52377,52792,52793,54965,65560,65561,77598,77836,79074,79103,79104") == "a1e1bc3a5eedd462307a4590b27f136175bb177f" || 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
check_first_block(map_partition("product"));
ifelse (block_image_recover(map_partition("product"), "230,1,54,70,74,78,79,86,92,93,94,100,106,107,108,123,129,130,131,191,195,200,201,203,204,206,207,213,219,220,221,336,342,343,344,356,362,363,364,367,368,374,380,381,382,405,411,412,413,1257,1258,14956,14957,14976,14977,30646,30647,30768,30769,30945,30946,31313,31314,32065,32066,32768,32770,32789,32814,35603,35604,35615,35621,35622,35623,35633,35634,35639,35643,35648,35649,35653,35659,35663,35664,35670,35676,35677,35678,35690,35696,35697,35698,35700,35707,35713,35714,37472,37473,37479,37480,37620,37621,38823,38830,38884,38891,38896,38903,38928,38935,38955,38962,38980,38987,39003,39004,39015,39017,39180,39181,39182,39183,39184,39185,39220,39222,39230,39231,39502,39506,39510,39514,39515,39519,39520,39524,39525,39529,39530,39534,39535,39539,39540,39544,39545,39558,39559,39563,39564,39568,39569,39573,39574,39576,39577,39581,39582,39586,39587,39589,39590,39594,39595,39599,39600,39604,39605,39609,39610,39614,39615,39619,39620,39624,39625,39629,39630,39632,39637,39641,39642,39646,39647,39651,39652,39656,39657,39659,39660,39664,39665,39667,39668,39672,39673,39675,39676,39680,39681,39685,39686,39688,39689,39693,39694,39728,39731,39840,39896,40194,40899,40900,44653,44654,45817,45818,50747,50748,52374,52377,52792,52793,54965,65560,65561,77598,77836,79074,79103,79104") && 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;
if (range_sha1(map_partition("odm"), "8,1,11,12,217,220,226,242,243") == "823f961cef66a54cefc0d32de32fe5eb179a1d14" || block_image_verify(map_partition("odm"), package_extract_file("odm.transfer.list"), "odm.new.dat", "odm.patch.dat")) then
ui_print("Verified odm image...");
else
check_first_block(map_partition("odm"));
ifelse (block_image_recover(map_partition("odm"), "8,1,11,12,217,220,226,242,243") && block_image_verify(map_partition("odm"), package_extract_file("odm.transfer.list"), "odm.new.dat", "odm.patch.dat"), ui_print("odm recovered successfully."), abort("E2004: odm partition fails to recover"));
endif;

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


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

ui_print("Patching firmware images...");
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64");
package_extract_file("firmware-update/abl_ecc.elf", "/dev/block/bootdevice/by-name/abl");
package_extract_file("firmware-update/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlib");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hyp");
package_extract_file("firmware-update/BTFM.bin", "/dev/block/bootdevice/by-name/bluetooth");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tz");
package_extract_file("firmware-update/aop.mbn", "/dev/block/bootdevice/by-name/aop");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_config");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsec");
package_extract_file("firmware-update/uefi_sec.mbn", "/dev/block/bootdevice/by-name/uefisecapp");
package_extract_file("firmware-update/qupv3fw.elf", "/dev/block/bootdevice/by-name/qupfw");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfg");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xbl");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymaster");
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64bak");
package_extract_file("firmware-update/abl_ecc.elf", "/dev/block/bootdevice/by-name/ablbak");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlibbak");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hypbak");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tzbak");
package_extract_file("firmware-update/aop.mbn", "/dev/block/bootdevice/by-name/aopbak");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_configbak");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsecbak");
package_extract_file("firmware-update/qupv3fw.elf", "/dev/block/bootdevice/by-name/qupfwbak");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfgbak");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
ui_print("Patching boot image...");
show_progress(0.100000, 10);
patch_partition("EMMC:/dev/block/bootdevice/by-name/boot:134217728:f2546528a4622517445b0c8f052eac13d45519c6",
                "EMMC:/dev/block/bootdevice/by-name/boot:134217728:dd692ad5d898c5a89076672a9a2fd76d660bb17e",
                package_extract_file("boot.img.p")) ||
    abort("E3008: Failed to apply patch to EMMC:/dev/block/bootdevice/by-name/boot:134217728:dd692ad5d898c5a89076672a9a2fd76d660bb17e");

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

ui_print("Patching firmware images...");
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64");
package_extract_file("firmware-update/abl_ecc.elf", "/dev/block/bootdevice/by-name/abl");
package_extract_file("firmware-update/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlib");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hyp");
package_extract_file("firmware-update/BTFM.bin", "/dev/block/bootdevice/by-name/bluetooth");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tz");
package_extract_file("firmware-update/aop.mbn", "/dev/block/bootdevice/by-name/aop");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_config");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsec");
package_extract_file("firmware-update/uefi_sec.mbn", "/dev/block/bootdevice/by-name/uefisecapp");
package_extract_file("firmware-update/qupv3fw.elf", "/dev/block/bootdevice/by-name/qupfw");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfg");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xbl");
package_extract_file("firmware-update/km4.mbn", "/dev/block/bootdevice/by-name/keymaster");
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64bak");
package_extract_file("firmware-update/abl_ecc.elf", "/dev/block/bootdevice/by-name/ablbak");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlibbak");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hypbak");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tzbak");
package_extract_file("firmware-update/aop.mbn", "/dev/block/bootdevice/by-name/aopbak");
package_extract_file("firmware-update/xbl_config.elf", "/dev/block/bootdevice/by-name/xbl_configbak");
package_extract_file("firmware-update/storsec.mbn", "/dev/block/bootdevice/by-name/storsecbak");
package_extract_file("firmware-update/qupv3fw.elf", "/dev/block/bootdevice/by-name/qupfwbak");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfgbak");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
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.");

# Patch partition odm

ui_print("Patching odm image after verification.");
block_image_update(map_partition("odm"), package_extract_file("odm.transfer.list"), "odm.new.dat", "odm.patch.dat") ||
  abort("E2001: Failed to update odm image.");

# --- End patching dynamic partitions ---

show_progress(0.100000, 2);

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

ui_print("Patching vbmeta dtbo logo binimages...");
package_extract_file("firmware-update/vbmeta_system.img", "/dev/block/bootdevice/by-name/vbmeta_system");
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");
