From 87950f0c125d5cdd37ee416d3e82e9bfb7bd62b9 Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Sat, 25 Feb 2023 09:09:23 +0000 Subject: [PATCH] Simplify waiting by using adb wait-for-recovery and wait-for-sideload as well as heimdall detect to wait for download mode --- openandroidinstaller/tooling.py | 56 +++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/openandroidinstaller/tooling.py b/openandroidinstaller/tooling.py index da438efe..00731216 100644 --- a/openandroidinstaller/tooling.py +++ b/openandroidinstaller/tooling.py @@ -106,7 +106,9 @@ def adb_reboot_bootloader(bin_path: Path) -> TerminalResponse: """Reboot the device into bootloader and return success.""" for line in run_command("adb reboot bootloader", bin_path): yield line - sleep(1) + # wait for the bootloader to become available + for line in fastboot_wait_for_bootloader(bin_path=bin_path): + yield line @add_logging("Rebooting device into download mode with adb.") @@ -114,6 +116,7 @@ def adb_reboot_download(bin_path: Path) -> TerminalResponse: """Reboot the device into download mode of samsung devices and return success.""" for line in run_command("adb reboot download", bin_path): yield line + yield heimdall_wait_for_download_available(bin_path=bin_path) @add_logging("Sideload the target to device with adb.") @@ -128,6 +131,22 @@ def activate_sideload(bin_path: Path) -> TerminalResponse: """Activate sideload with adb shell in twrp.""" for line in run_command("adb shell twrp sideload", bin_path): yield line + for line in adb_wait_for_sideload(bin_path=bin_path): + yield line + + +@add_logging("Wait for recovery") +def adb_wait_for_recovery(bin_path: Path) -> TerminalResponse: + """Use adb to wait for the recovery to become available.""" + for line in run_command("adb wait-for-recovery", bin_path): + yield line + + +@add_logging("Wait for sideload") +def adb_wait_for_sideload(bin_path: Path) -> TerminalResponse: + """Use adb to wait for the sideload to become available.""" + for line in run_command("adb wait-for-sideload", bin_path): + yield line def adb_twrp_copy_partitions(bin_path: Path, config_path: Path) -> TerminalResponse: @@ -137,7 +156,6 @@ def adb_twrp_copy_partitions(bin_path: Path, config_path: Path) -> TerminalRespo for line in activate_sideload(bin_path): yield line # now sideload the script - sleep(5) logger.info("Sideload the copy_partitions script") for line in adb_sideload( bin_path=bin_path, @@ -148,7 +166,6 @@ def adb_twrp_copy_partitions(bin_path: Path, config_path: Path) -> TerminalRespo # reboot into the bootloader again for line in adb_reboot_bootloader(bin_path): yield line - sleep(7) # Copy partitions end # yield True @@ -180,7 +197,6 @@ def adb_twrp_wipe_and_install( Only works for twrp recovery. """ logger.info("Wipe and format data with twrp, then install os image.") - sleep(7) # now perform a factory reset for line in adb_twrp_format_data(bin_path): yield line @@ -197,7 +213,6 @@ def adb_twrp_wipe_and_install( for line in activate_sideload(bin_path=bin_path): yield line # now flash os image - sleep(5) logger.info("Sideload and install os image.") for line in adb_sideload(bin_path=bin_path, target=target): yield line @@ -226,13 +241,11 @@ def adb_twrp_wipe_and_install( # reboot into the bootloader again for line in adb_reboot_bootloader(bin_path): yield line - sleep(3) # boot to TWRP again for line in fastboot_flash_recovery( bin_path=bin_path, recovery=recovery, is_ab=is_ab ): yield line - sleep(7) else: # if not an a/b-device just stay in twrp pass @@ -249,14 +262,13 @@ def adb_twrp_install_addons( Only works for twrp recovery. """ logger.info("Install addons with twrp.") - sleep(5) + sleep(0.5) logger.info("Sideload and install addons.") for addon in addons: # activate sideload logger.info("Activate sideload.") for line in activate_sideload(bin_path=bin_path): yield line - sleep(5) # now flash os image for line in adb_sideload(bin_path=bin_path, target=addon): yield line @@ -267,7 +279,6 @@ def adb_twrp_install_addons( # reboot into the bootloader again for line in adb_reboot_bootloader(bin_path=bin_path): yield line - sleep(3) # switch active boot partition for line in fastboot_switch_partition(bin_path=bin_path): yield line @@ -285,6 +296,13 @@ def adb_twrp_install_addons( yield line +@add_logging("Wait for bootloader") +def fastboot_wait_for_bootloader(bin_path: Path) -> TerminalResponse: + """Use adb to wait for the bootloader to become available.""" + for line in run_command("fastboot devices", bin_path): + yield line + + @add_logging("Switch active boot partitions.", return_if_fail=True) def fastboot_switch_partition(bin_path: Path) -> TerminalResponse: """Switch the active boot partition with fastboot.""" @@ -336,10 +354,14 @@ def fastboot_flash_recovery( logger.info("Boot custom recovery with fastboot.") for line in run_command(f"fastboot boot {recovery}", bin_path): yield line + for line in adb_wait_for_recovery(bin_path=bin_path): + yield line else: logger.info("Flash custom recovery with fastboot.") for line in run_command(f"fastboot flash recovery {recovery}", bin_path): yield line + for line in adb_wait_for_recovery(bin_path=bin_path): + yield line if (type(line) == bool) and not line: logger.error("Flashing recovery failed.") yield False @@ -349,6 +371,8 @@ def fastboot_flash_recovery( logger.info("Boot into TWRP with fastboot.") for line in run_command("fastboot reboot recovery", bin_path): yield line + for line in adb_wait_for_recovery(bin_path=bin_path): + yield line def fastboot_flash_boot(bin_path: Path, recovery: str) -> TerminalResponse: @@ -365,6 +389,8 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> TerminalResponse: logger.info("Boot into TWRP with fastboot.") for line in run_command("fastboot reboot", bin_path): yield line + for line in adb_wait_for_recovery(bin_path=bin_path): + yield line if (type(line) == bool) and not line: logger.error("Booting recovery failed.") yield False @@ -372,6 +398,16 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> TerminalResponse: yield True +def heimdall_wait_for_download_available(bin_path: Path) -> bool: + """Use heimdall detect to wait for download mode to become available on the device.""" + logger.info("Wait for download mode to become available.") + while True: + sleep(1) + for line in run_command("heimdall detect", bin_path=bin_path): + if (type(line) == bool) and line: + return True + + @add_logging("Flash custom recovery with heimdall.") def heimdall_flash_recovery(bin_path: Path, recovery: str) -> TerminalResponse: """Temporarily, flash custom recovery with heimdall."""