getprop("ro.product.device") == "p8600" || abort("E3004: This package is for \"p8600\" devices; this is a \"" + getprop("ro.product.device") + "\".");
ui_print("Target: Android/p8600/p8600:8.0.0/MAIN/P4.4.4:userdebug/release-keys");
show_progress(0.650000, 0);
ui_print("Patching system image unconditionally...");
block_image_update("/dev/block/by-name/system", package_extract_file("system.transfer.list"), "system.new.dat", "system.patch.dat") ||
  abort("E1001: Failed to update system image.");
show_progress(0.100000, 0);
ui_print("Patching vendor image unconditionally...");
block_image_update("/dev/block/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.050000, 5);
package_extract_file("boot.img", "/dev/block/by-name/boot");
show_progress(0.200000, 10);

stdout("SHA1 of existing u-boot: ");
stdout(sha1_offset_count("/dev/block/by-name/bootloader", 1024, 536620));
stdout("\n");
ui_print("Checking if bootloader update is necessary...");
if
    sha1_offset_count("/dev/block/by-name/bootloader", 1024, 536620) != bd8bc3d3c7d3dca4e44f57be11aaf2e1a0263dd5
then
    ui_print("Updating bootloader...");
    if
        getprop("ro.boot.storage_type") == "emmc"
    then
        unlock_emmc_boot() || abort("Error unlocking emmc");
    endif;
    package_extract_file("u-boot-imx6q.imx", "/tmp/u-boot-imx6q.imx");
    simple_dd("/tmp/u-boot-imx6q.imx", "/dev/block/by-name/bootloader", 1024);
else
    ui_print("Bootloader update not needed");
endif;

if
    "/system" != mount("ext4", "emmc", "/dev/block/by-name/system", "/system")
then
    ui_print("Unable to mount /system, skipping firmware update of micro controllers");
else
    ui_print("Checking if SPI-USB Bridge AVX1 uC update is necessary...");
    if
        0 == run_program("/system/bin/updateSTM32.sh", "CHECK_UPDATE_NOT_NEEDED", "SUBUC_AVX1")
    then
       ui_print("SPI-USB Bridge AVX1 uC update not needed");
    else
        ui_print("Updating SPI-USB Bridge AVX1 uC...");
        ifelse(0 == run_program("/system/bin/updateSTM32.sh", "UPDATE", "SUBUC_AVX1"), ui_print("updateSTM32 for SPI-USB Bridge AVX1 uC succeeded"), ui_print("ERR: updateSTM32 for SPI-USB Bridge AVX1 failed"));
    endif;

    ui_print("Checking if SUBUC_AVX2 update is necessary...");
    if
        0 == run_program("/system/bin/updateSTM32.sh", "CHECK_UPDATE_NOT_NEEDED", "SUBUC_AVX2")
    then
       ui_print("SPI-USB Bridge AVX2 uC update not needed");
    else
        ui_print("Updating SPI-USB Bridge AVX2 uC...");
        ifelse(0 == run_program("/system/bin/updateSTM32.sh", "UPDATE", "SUBUC_AVX2"), ui_print("updateSTM32 for SPI-USB Bridge AVX2 uC succeeded"), ui_print("ERR: updateSTM32 for SPI-USB Bridge AVX2 failed"));
    endif;

    ui_print("Checking if System uC update is necessary...");

    if
        0 == run_program("/system/bin/updateSTM32.sh", "CHECK_UPDATE_NOT_NEEDED", "SYSUC")
    then
        ui_print("System uC update not needed");
    else
        ui_print("Updating System uC...");

        # Updating the System uC (an STM32L433) can/will leave the System uC running in its bootloader.
        # It is purposefully left running in its bootloader, because if the bootloader were commanded
        # to start the newly updated application, the System uC (which controls the power supplies of
        # the DM system) application would cause the DM's host processor (iMX6, the one running this
        # very script as a part of the (Android) recovery UI) to be abruptly reset. Instead, the
        # System uC is left running in its bootloader so that the DM's host processor (iMX6, the one
        # running this very script as a part of the (Android) recovery UI) can continue running the
        # (Android) recovery UI and perform a Linux domain shutdown/reboot as it normally does.
        # A simple shutdown/reboot of Linux only results in a shutdown/reboot of the DM's host
        # processor (iMX6) and does not result in a full DM system reset and/or does not result in
        # a reset of the System uC (which controls the power supplies of the DM system). The only way
        # for the System uC to start running its newly updated firmware is to command it to do so.
        # Need to enable the "Reset System On Shutdown" feature of the "protocol" driver associated
        # with the System uC device (ucspicom/sysuc). This is done by writing a "1" to the driver's
        # Reset System On Shutdown (RSOS) file. The driver also needs to know the address parameter
        # to be used with the bootloader's Go command, so additional data has to be specified, hence
        # the additional field of data "0x0800000", which is the address that the bootloader needs to
        # jump to, to start the new application.
        # If/When Linux processes a shutdown/reboot, the driver gets a 'shutdown' callback and it will
        # send an appropriate request to the System uC that should result in a full DM system reset.
        # (Note that Linux may or may not complete all its shutdown/reboot processing before the System
        # uC resets the host processor, but it should get pretty far into it.)
        if
            write_value("1,0x08000000", "/sys/class/ucspicom/sysuc/device/reset_system_on_shutdown") != "t"
        then
            ui_print("WARN: Unable to write value to file: /sys/class/ucspicom/sysuc/device/reset_system_on_shutdown");
        endif;

        ifelse(0 == run_program("/system/bin/updateSTM32.sh", "UPDATE", "SYSUC"), ui_print("updateSTM32 for System uC succeeded"), ui_print("ERR: updateSTM32 for System uC failed"));
    endif;

    unmount("/system");
endif;

set_progress(1.000000);
