ui_print("Source: None");
ui_print("Target: None");

set_inactive_slot_as_unbootable() || abort("Failed to set inactive slot as unbootable!");

if_copy_done_cookie_exists("/cache/recovery/AB_COPY_DONE") || (
ui_print("Copying blocks of all A/B partitions from active to inactive slots...");
copy_all_source_partitions_except() || abort("E3009: Failed to copy all partitions from active to inactive slot");
);
write_copy_done_cookie(/cache/recovery/AB_COPY_DONE);

ui_print("Verifying current system...");
apply_patch_space(0) || abort("E3006: Not enough free space on /cache to apply patches.");

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

ui_print("Patching system image after verification.");
show_progress(0.900000, 0);
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("Verifying the updated system image...");
if range_sha1("/dev/block/bootdevice/by-name/system", "46,0,32767,32768,32770,32897,65535,65536,65538,98304,98306,98433,98435,98947,131071,131072,131074,163840,163842,163969,163971,164483,170323,196608,196610,229376,229378,229505,229507,262144,262146,294912,294914,295041,295043,327680,327682,360448,360450,393216,393218,425984,425986,458752,458754,491520,491522") == "50cc219286ce6e2ebd156cffc7427d2ca5413268" then
if range_sha1("/dev/block/bootdevice/by-name/system", "70,32767,32768,32770,32897,65535,65536,65538,66050,97792,98304,98306,98433,98435,98947,131071,131072,131074,131586,163328,163840,163842,163969,163971,164483,170323,170835,196096,196608,196610,197122,228864,229376,229378,229505,229507,230019,261632,262144,262146,262658,294400,294912,294914,295041,295043,295555,327168,327680,327682,328194,359936,360448,360450,360962,392704,393216,393218,393730,425472,425984,425986,426498,458240,458752,458754,459266,491008,491520,491522,492034") == "c8c8a4127208489a3ce93404b7240bfd96b8fb9f" then
ui_print("Verified the updated system image.");
else
  abort("E1003: system partition has unexpected non-zero contents after OTA update");
endif;
else
  abort("E1002: system partition has unexpected contents after OTA update");
endif;

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

ui_print("Patching firmware images...");
(package_extract_file("firmware-update/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem") && block_device_check("/dev/block/bootdevice/by-name/modem", "99252736", "05c2d5fdd0a0ab4db027c1a1be5cd763fd72614d")) ||
  abort("Failed to extract firmware-update/NON-HLOS.bin to /dev/block/bootdevice/by-name/modem");

set_inactive_slot_as_active() || abort("Failed to set inactive slot as active!");
delete_copy_done_cookie("/cache/recovery/AB_COPY_DONE");

set_progress(1.000000);
