From f8a4aafbeb1ef0d5e230d6d5ae6de6438ca94197 Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Wed, 8 Feb 2023 19:09:57 +0000 Subject: [PATCH] WIP --- openandroidinstaller/openandroidinstaller.py | 6 +- openandroidinstaller/tooling.py | 165 +++++++------------ openandroidinstaller/views/step_view.py | 1 - 3 files changed, 65 insertions(+), 107 deletions(-) diff --git a/openandroidinstaller/openandroidinstaller.py b/openandroidinstaller/openandroidinstaller.py index a6680738..21fb31d4 100644 --- a/openandroidinstaller/openandroidinstaller.py +++ b/openandroidinstaller/openandroidinstaller.py @@ -193,21 +193,21 @@ def log_version_infos(bin_path): """Log the version infos of adb, fastboot and heimdall.""" # adb adbversion = [ - line for line in run_command("adb", ["version"], bin_path, enable_logging=False) + line for line in run_command("adb version", bin_path, enable_logging=False) ] logger.info(f"{adbversion[1].strip()}") # fastboot fbversion = [ line for line in run_command( - "fastboot", ["--version"], bin_path, enable_logging=False + "fastboot --version", bin_path, enable_logging=False ) ] logger.info(f"{fbversion[1].strip()}") # heimdall hdversion = [ line - for line in run_command("heimdall", ["info"], bin_path, enable_logging=False) + for line in run_command("heimdall info", bin_path, enable_logging=False) ] logger.info(f"Heimdall version: {hdversion[1].strip()}") diff --git a/openandroidinstaller/tooling.py b/openandroidinstaller/tooling.py index 1c268b00..a97301f8 100644 --- a/openandroidinstaller/tooling.py +++ b/openandroidinstaller/tooling.py @@ -24,8 +24,9 @@ CompletedProcess, check_output, ) +import shlex from time import sleep -from typing import List, Optional +from typing import List, Optional, Union from loguru import logger @@ -33,10 +34,12 @@ def run_command( - tool: str, command: List[str], bin_path: Path, enable_logging: bool = True -) -> CompletedProcess: + command: str, bin_path: Path, enable_logging: bool = True +) -> Union[str, bool]: """Run a command with a tool (adb, fastboot, heimdall).""" - yield f"${' '.join([tool] + command )}" + yield f"${command}" + # split the command and extract the tool part + tool, *command = shlex.split(command) if tool not in ["adb", "fastboot", "heimdall"]: raise Exception(f"Unknown tool {tool}. Use adb, fastboot or heimdall.") if PLATFORM == "win32": @@ -63,89 +66,75 @@ def run_command( logger.info(line.strip()) yield line.strip() + # finally return if the command was successful yield p.returncode == 0 +def add_logging(step_desc: str, return_if_fail: bool=False): + def logging_decorator(func): + def logging(*args, **kwargs): + logger.info(step_desc) + for line in func(*args, **kwargs): + if (type(line) == bool) and not line: + logger.error(f"{step_desc} Failed!") + if return_if_fail: + return + yield line + + return logging + return logging_decorator + + +@add_logging("Rebooting device with adb.") def adb_reboot(bin_path: Path) -> bool: """Run adb reboot on the device and return success.""" - logger.info("Rebooting device with adb.") - for line in run_command("adb", ["reboot"], bin_path): + for line in run_command("adb reboot", bin_path): yield line - if (type(line) == bool) and line: - logger.debug("Reboot failed.") - yield False - else: - yield True -def adb_reboot_bootloader(bin_path: Path) -> bool: +@add_logging("Rebooting device into bootloader with adb.", return_if_fail=True) +def adb_reboot_bootloader(bin_path: Path) -> Union[str, bool]: """Reboot the device into bootloader and return success.""" - logger.info("Rebooting device into bootloader with adb.") - for line in run_command("adb", ["reboot", "bootloader"], bin_path): - if (type(line) == bool) and not line: - logger.error("Reboot into bootloader failed.") + for line in run_command("adb reboot bootloader", bin_path): yield line - sleep(1) -def adb_reboot_download(bin_path: Path) -> bool: +@add_logging("Rebooting device into download mode with adb.") +def adb_reboot_download(bin_path: Path) -> Union[str, bool]: """Reboot the device into download mode of samsung devices and return success.""" - logger.info("Rebooting device into download mode with adb.") - for line in run_command("adb", ["reboot", "download"], bin_path): + for line in run_command("adb reboot download", bin_path): yield line - if (type(line) == bool) and not line: - logger.error("Reboot into download mode failed.") - yield False - else: - # check if in download mode with heimdall? - yield True -def adb_sideload(bin_path: Path, target: str) -> bool: +@add_logging("Sideload the target to device with adb.") +def adb_sideload(bin_path: Path, target: str) -> Union[str, bool]: """Sideload the target to device and return success.""" - logger.info("Rebooting device into bootloader with adb.") - for line in run_command("adb", ["sideload", target], bin_path): + for line in run_command(f"adb sideload {target}", bin_path): + yield line + + +@add_logging("Activate sideloading in TWRP.", return_if_fail=True) +def activate_sideload(bin_path: Path) -> Union[str, bool]: + """Activate sideload with adb shell in twrp.""" + for line in run_command("adb shell twrp sideload", bin_path): yield line - if (type(line) == bool) and line: - logger.info(f"Sideloading {target} failed.") - yield False - else: - yield True def adb_twrp_copy_partitions(bin_path: Path, config_path: Path): # some devices like one plus 6t or motorola moto g7 power need the partitions copied to prevent a hard brick logger.info("Sideload copy_partitions script with adb.") # activate sideload - for line in run_command("adb", ["shell", "twrp", "sideload"], bin_path): + for line in activate_sideload(bin_path): yield line - if (type(line) == bool) and not line: - logger.error("Activating sideload failed.") - yield False - return # now sideload the script sleep(5) logger.info("Sideload the copy_partitions script") - for line in run_command( - "adb", - [ - "sideload", - f"{config_path.parent.joinpath(Path('copy-partitions-20220613-signed.zip'))}", - ], - bin_path, - ): + for line in adb_sideload(bin_path=bin_path, target=f"{config_path.parent.joinpath(Path('copy-partitions-20220613-signed.zip'))}"): yield line - if (type(line) == bool) and not line: - logger.error("Sideloading copy-partitions-20220613-signed.zip failed.") sleep(10) # reboot into the bootloader again - logger.info("Rebooting device into bootloader with adb.") - for line in run_command("adb", ["reboot", "bootloader"], bin_path): + for line in adb_reboot_bootloader(bin_path): yield line - if (type(line) == bool) and not line: - logger.error("Reboot into bootloader failed.") - yield False - return sleep(7) # Copy partitions end # return True @@ -333,64 +322,39 @@ def adb_twrp_install_addons(bin_path: Path, addons: List[str], is_ab: bool) -> b yield True -def fastboot_unlock_with_code(bin_path: Path, unlock_code: str) -> bool: +@add_logging("Unlock the device with fastboot and code.") +def fastboot_unlock_with_code(bin_path: Path, unlock_code: str) -> Union[str, bool]: """Unlock the device with fastboot and code given.""" - logger.info(f"Unlock the device with fastboot and code: {unlock_code}.") - for line in run_command("fastboot", ["oem", "unlock", f"{unlock_code}"], bin_path): + for line in run_command(f"fastboot oem unlock {unlock_code}", bin_path): yield line - if (type(line) == bool) and not line: - logger.error(f"Unlocking with code {unlock_code} failed.") - yield False - else: - yield True -def fastboot_unlock(bin_path: Path) -> bool: +@add_logging("Unlock the device with fastboot without code.") +def fastboot_unlock(bin_path: Path) -> Union[str, bool]: """Unlock the device with fastboot and without code.""" - logger.info("Unlock the device with fastboot.") - for line in run_command("fastboot", ["flashing", "unlock"], bin_path): + for line in run_command("fastboot flashing unlock", bin_path): yield line - if (type(line) == bool) and not line: - logger.error("Unlocking failed.") - yield False - else: - yield True -def fastboot_oem_unlock(bin_path: Path) -> bool: +@add_logging("OEM unlocking the device with fastboot.") +def fastboot_oem_unlock(bin_path: Path) -> Union[str, bool]: """OEM unlock the device with fastboot and without code.""" - logger.info("OEM unlocking the device with fastboot.") - for line in run_command("fastboot", ["oem", "unlock"], bin_path): + for line in run_command("fastboot oem unlock", bin_path): yield line - if (type(line) == bool) and not line: - logger.error("OEM unlocking failed.") - yield False - else: - yield True -def fastboot_get_unlock_data(bin_path: Path) -> bool: +@add_logging("Get unlock data with fastboot") +def fastboot_get_unlock_data(bin_path: Path) -> Union[str, bool]: """Get the unlock data with fastboot""" - logger.info("Get unlock data with fastboot") - for line in run_command("fastboot", ["oem", "get_unlock_data"], bin_path): + for line in run_command("fastboot oem get_unlock_data", bin_path): yield line - if (type(line) == bool) and not line: - logger.error("Getting unlock data failed.") - yield False - else: - yield True -def fastboot_reboot(bin_path: Path) -> bool: +@add_logging("Rebooting device with fastboot.") +def fastboot_reboot(bin_path: Path) -> Union[str, bool]: """Reboot with fastboot""" - logger.info("Rebooting device with fastboot.") - for line in run_command("fastboot", ["reboot"], bin_path): + for line in run_command("fastboot reboot", bin_path): yield line - if (type(line) == bool) and not line: - logger.error("Rebooting with fastboot failed.") - yield False - else: - yield True def fastboot_flash_recovery(bin_path: Path, recovery: str, is_ab: bool = True) -> bool: @@ -443,18 +407,13 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> bool: yield True -def heimdall_flash_recovery(bin_path: Path, recovery: str) -> bool: +@add_logging("Flash custom recovery with heimdall.") +def heimdall_flash_recovery(bin_path: Path, recovery: str) -> Union[str, bool]: """Temporarily, flash custom recovery with heimdall.""" - logger.info("Flash custom recovery with heimdall.") for line in run_command( - "heimdall", ["flash", "--no-reboot", "--RECOVERY", f"{recovery}"], bin_path + f"heimdall flash --no-reboot --RECOVERY {recovery}", bin_path ): yield line - if (type(line) == bool) and not line: - logger.error("Flashing recovery with heimdall failed.") - yield False - else: - yield True def search_device(platform: str, bin_path: Path) -> Optional[str]: diff --git a/openandroidinstaller/views/step_view.py b/openandroidinstaller/views/step_view.py index b3c334c2..b8961ec0 100644 --- a/openandroidinstaller/views/step_view.py +++ b/openandroidinstaller/views/step_view.py @@ -84,7 +84,6 @@ def build(self): # switch to enable advanced output - here it means show terminal input/output in tool def check_advanced_switch(e): - logger.info(self.advanced_switch.value) """Check the box to enable advanced output.""" if self.advanced_switch.value: logger.info("Enable advanced output.")