Path: blob/master/AndroidRunner/pyand/Fastboot.py
630 views
#!/usr/bin/env python312try:3import sys4import subprocess5from os import popen as pipe6except ImportError as e:7print("[!] Required module missing. %s" % e.args[0])8sys.exit(-1)91011class Fastboot(object):12__fastboot_path = None13__output = None14__error = None15__devices = None16__target = None1718def __init__(self, fastboot_path="fastboot"):19"""20By default we assume fastboot is in $PATH.21Alternatively, the path to fasboot can be supplied.22"""23self.__fastboot_path = fastboot_path24if not self.check_path():25self.__error = "[!] fastboot path not valid."2627def __clean__(self):28self.__output = None29self.__error = None3031def __read_output__(self, fd):32ret = ""33while 1:34line = fd.readline()35if not line:36break37ret += line3839if len(ret) == 0:40ret = None4142return ret4344def __build_command__(self, cmd):45"""46Build command parameters for Fastboot command47"""48if self.__devices is not None and len(self.__devices) > 1 and self.__target is None:49self.__error = "[!] Must set target device first"50return None5152if type(cmd) is tuple:53a = list(cmd)54elif type(cmd) is list:55a = cmd56else:57a = cmd.split(" ")58a.insert(0, self.__fastboot_path)59if self.__target is not None:60# add target device arguments to the command61a.insert(1, '-s')62a.insert(2, self.__target)6364return a6566def run_cmd(self, cmd):67"""68Run a command against the fastboot tool ($ fastboot <cmd>)69"""70self.__clean__()7172if self.__fastboot_path is None:73self.__error = "[!] Fastboot path not set"74return False7576try:77args = self.__build_command__(cmd)78if args is None:79return80cmdp = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)81self.__output, self.__error = cmdp.communicate()82retcode = cmdp.wait()83return self.__output84except OSError as error:85self.__error = str(error)8687return8889def check_path(self):90"""91Check if the Fastboot path is valid92"""93if self.run_cmd("help") is None:94print("[-] fastboot executable not found")95return False96return True9798def set_fastboot_path(self, fastboot_path):99"""100Set the Fastboot tool path101"""102self.__fastboot_path = fastboot_path103self.check_path()104105def get_fastboot_path(self):106"""107Returns the Fastboot tool path108"""109return self.__fastboot_path_path110111# noinspection PyUnusedLocal112def get_devices(self):113"""114Return a dictionary of fastboot connected devices along with an incremented Id.115fastboot devices116"""117error = 0118# Clear existing list of devices119self.__devices = None120self.run_cmd("devices")121if self.__error is not None:122return ''123try:124device_list = self.__output.replace('fastboot', '').split()125126if device_list[1:] == ['no', 'permissions']:127error = 2128self.__devices = None129except:130self.__devices = None131return self.__devices132i = 0133device_dict = {}134for device in device_list:135# Add list to dictionary with incrementing ID136device_dict[i] = device137i += 1138self.__devices = device_dict139return self.__devices140141def set_target_by_name(self, device):142"""143Specify the device name to target144example: set_target_device('emulator-5554')145"""146if device is None or device not in list(self.__devices.values()):147self.__error = 'Must get device list first'148print("[!] Device not found in device list")149return False150self.__target = device151return "[+] Target device set: %s" % self.get_target_device()152153def set_target_by_id(self, device):154"""155Specify the device ID to target.156The ID should be one from the device list.157"""158if device is None or device not in self.__devices:159self.__error = 'Must get device list first'160print("[!] Device not found in device list")161return False162self.__target = self.__devices[device]163return "[+] Target device set: %s" % self.get_target_device()164165def get_target_device(self):166"""167Returns the selected device to work with168"""169if self.__target is None:170print("[*] No device target set")171172return self.__target173174def flash_all(self, wipe=False):175"""176flash boot + recovery + system. Optionally wipe everything177"""178if wipe:179self.run_cmd('-w flashall')180else:181self.run_cmd('flashall')182183def format(self, partition):184"""185Format the specified partition186"""187self.run_cmd('format %s' % partition)188return self.__output189190def reboot_device(self):191"""192Reboot the device normally193"""194self.run_cmd('reboot')195return self.__output196197def reboot_device_bootloader(self):198"""199Reboot the device into bootloader200"""201self.run_cmd('reboot-bootloader')202return self.__output203204def oem_unlock(self):205"""206unlock bootloader207"""208self.run_cmd('oem unlock')209return self.__output210211def oem_lock(self):212"""213lock bootloader214"""215self.run_cmd('oem lock')216return self.__output217218219