Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
S2-group
GitHub Repository: S2-group/android-runner
Path: blob/master/AndroidRunner/pyand/Fastboot.py
630 views
1
#!/usr/bin/env python3
2
3
try:
4
import sys
5
import subprocess
6
from os import popen as pipe
7
except ImportError as e:
8
print("[!] Required module missing. %s" % e.args[0])
9
sys.exit(-1)
10
11
12
class Fastboot(object):
13
__fastboot_path = None
14
__output = None
15
__error = None
16
__devices = None
17
__target = None
18
19
def __init__(self, fastboot_path="fastboot"):
20
"""
21
By default we assume fastboot is in $PATH.
22
Alternatively, the path to fasboot can be supplied.
23
"""
24
self.__fastboot_path = fastboot_path
25
if not self.check_path():
26
self.__error = "[!] fastboot path not valid."
27
28
def __clean__(self):
29
self.__output = None
30
self.__error = None
31
32
def __read_output__(self, fd):
33
ret = ""
34
while 1:
35
line = fd.readline()
36
if not line:
37
break
38
ret += line
39
40
if len(ret) == 0:
41
ret = None
42
43
return ret
44
45
def __build_command__(self, cmd):
46
"""
47
Build command parameters for Fastboot command
48
"""
49
if self.__devices is not None and len(self.__devices) > 1 and self.__target is None:
50
self.__error = "[!] Must set target device first"
51
return None
52
53
if type(cmd) is tuple:
54
a = list(cmd)
55
elif type(cmd) is list:
56
a = cmd
57
else:
58
a = cmd.split(" ")
59
a.insert(0, self.__fastboot_path)
60
if self.__target is not None:
61
# add target device arguments to the command
62
a.insert(1, '-s')
63
a.insert(2, self.__target)
64
65
return a
66
67
def run_cmd(self, cmd):
68
"""
69
Run a command against the fastboot tool ($ fastboot <cmd>)
70
"""
71
self.__clean__()
72
73
if self.__fastboot_path is None:
74
self.__error = "[!] Fastboot path not set"
75
return False
76
77
try:
78
args = self.__build_command__(cmd)
79
if args is None:
80
return
81
cmdp = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
82
self.__output, self.__error = cmdp.communicate()
83
retcode = cmdp.wait()
84
return self.__output
85
except OSError as error:
86
self.__error = str(error)
87
88
return
89
90
def check_path(self):
91
"""
92
Check if the Fastboot path is valid
93
"""
94
if self.run_cmd("help") is None:
95
print("[-] fastboot executable not found")
96
return False
97
return True
98
99
def set_fastboot_path(self, fastboot_path):
100
"""
101
Set the Fastboot tool path
102
"""
103
self.__fastboot_path = fastboot_path
104
self.check_path()
105
106
def get_fastboot_path(self):
107
"""
108
Returns the Fastboot tool path
109
"""
110
return self.__fastboot_path_path
111
112
# noinspection PyUnusedLocal
113
def get_devices(self):
114
"""
115
Return a dictionary of fastboot connected devices along with an incremented Id.
116
fastboot devices
117
"""
118
error = 0
119
# Clear existing list of devices
120
self.__devices = None
121
self.run_cmd("devices")
122
if self.__error is not None:
123
return ''
124
try:
125
device_list = self.__output.replace('fastboot', '').split()
126
127
if device_list[1:] == ['no', 'permissions']:
128
error = 2
129
self.__devices = None
130
except:
131
self.__devices = None
132
return self.__devices
133
i = 0
134
device_dict = {}
135
for device in device_list:
136
# Add list to dictionary with incrementing ID
137
device_dict[i] = device
138
i += 1
139
self.__devices = device_dict
140
return self.__devices
141
142
def set_target_by_name(self, device):
143
"""
144
Specify the device name to target
145
example: set_target_device('emulator-5554')
146
"""
147
if device is None or device not in list(self.__devices.values()):
148
self.__error = 'Must get device list first'
149
print("[!] Device not found in device list")
150
return False
151
self.__target = device
152
return "[+] Target device set: %s" % self.get_target_device()
153
154
def set_target_by_id(self, device):
155
"""
156
Specify the device ID to target.
157
The ID should be one from the device list.
158
"""
159
if device is None or device not in self.__devices:
160
self.__error = 'Must get device list first'
161
print("[!] Device not found in device list")
162
return False
163
self.__target = self.__devices[device]
164
return "[+] Target device set: %s" % self.get_target_device()
165
166
def get_target_device(self):
167
"""
168
Returns the selected device to work with
169
"""
170
if self.__target is None:
171
print("[*] No device target set")
172
173
return self.__target
174
175
def flash_all(self, wipe=False):
176
"""
177
flash boot + recovery + system. Optionally wipe everything
178
"""
179
if wipe:
180
self.run_cmd('-w flashall')
181
else:
182
self.run_cmd('flashall')
183
184
def format(self, partition):
185
"""
186
Format the specified partition
187
"""
188
self.run_cmd('format %s' % partition)
189
return self.__output
190
191
def reboot_device(self):
192
"""
193
Reboot the device normally
194
"""
195
self.run_cmd('reboot')
196
return self.__output
197
198
def reboot_device_bootloader(self):
199
"""
200
Reboot the device into bootloader
201
"""
202
self.run_cmd('reboot-bootloader')
203
return self.__output
204
205
def oem_unlock(self):
206
"""
207
unlock bootloader
208
"""
209
self.run_cmd('oem unlock')
210
return self.__output
211
212
def oem_lock(self):
213
"""
214
lock bootloader
215
"""
216
self.run_cmd('oem lock')
217
return self.__output
218
219