-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
471 lines (465 loc) · 19.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
try:
import subprocess, platform, sys, time, os
except:
print("Check that all dependencies are installed...")
def init(type):
global debug
if type == "cold":
debug = False
global supportedDevices
print("Rootella is starting cold.")
instance = open("supportedDevices.txt", "r")
supportedDevices = []
for line in instance.readlines():
supportedDevices.append(line.rstrip())
else:
print("Rootella is starting warm")
global version
time.sleep(0.5)
version = "1.0 ALPHA"
print("Rootella " + version)
print(" ██╗ ██╗ ██████╗ ██████╗ ██████╗ ████████╗███████╗██╗ ██╗ █████╗ ")
print("████████╗██╔══██╗██╔═══██╗██╔═══██╗╚══██╔══╝██╔════╝██║ ██║ ██╔══██╗")
print("╚██╔═██╔╝██████╔╝██║ ██║██║ ██║ ██║ █████╗ ██║ ██║ ███████║")
print("████████╗██╔══██╗██║ ██║██║ ██║ ██║ ██╔══╝ ██║ ██║ ██╔══██║")
print("╚██╔═██╔╝██║ ██║╚██████╔╝╚██████╔╝ ██║ ███████╗███████╗███████╗██║ ██║")
print(" ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝")
time.sleep(1)
if debug == True:
print("Bebug mode started virtual device mouted.")
if platform.system() == "Windows":
print("#################################\nWindows Detected. Rootella only works on Linux and MacOS.\nWe suggest Nexus Root Toolkit for Windows\nor you can use Ubuntu on Windows.\n#################################")
sys.exit()
if os.getuid() != 0:
sys.exit("############\nPlease run Rootella as sudo\nsudo python rootella.py\n############")
else:
if type == "cold":
execute("adb start-server")
global devices
devices = []
else:
print("ADB Already running.")
startupChecks()
def execute(arg):
s = subprocess.check_output(arg.split()).decode("utf-8")
if arg[0] != "fastboot":
s = str(s[25:])
s = str(s).rstrip()
s = s.split('\t')
return s
def clear():
size = executeR("stty size")
for i in range(int(size[0])):
print("")
def r(string):
clear()
size = executeR("stty size")
width2 = round(int(size[1])/2.5)
width = ""
for i in range(width2):
width = width + " "
print(width + " ██╗ ██╗ ██████╗")
print(width + "████████╗██╔══██╗")
print(width + "╚██╔═██╔╝██████╔╝")
print(width + "████████╗██╔══██╗")
print(width + "╚██╔═██╔╝██║ ██║")
print(width + " ╚═╝ ╚═╝ ╚═╝ ╚═╝")
print(width + string)
size = round(int(size[0]))
size = size/2.5
for i in range(int(size)):
print("")
def executeR(arg):
try:
s = subprocess.check_output(arg.split()).decode("utf-8")
s = s.rstrip()
s = s.split(' ')
return s
except:
return False
def stdout(message):
sys.stdout.write(message)
sys.stdout.write('\b' * len(message)) # \b: non-deleting backspace
def install(imageType, fileLocation):
r("Flashing recovery")
try:
data = executeR("sudo fastboot flash recovery " + str(fileLocation))
return True
except:
r("Something's not right.")
print("The install failed, We'll try to reboot your device into recovery. Make sure it's working fine.")
return False
def bootloader(type):
if type == "unlock":
r("Take a look at your phone.")
print("Please use the volume buttons and power button to select 'Yes' on the bootloader message. ")
try:
info = executeR("sudo fastboot oem unlock")
r("Bootloader unlocked!")
print("We've unlocked your bootloader, hit the power button on your phone to boot into recovery to flash your ROM :D")
print("Note: Factory resetting will have disabled ADB access on your device, you'll have to reenable it to use this tool.")
input("Press enter to continue")
init("warm")
return True
except:
return False
elif type == "lock":
try:
info = executeR("sudo fastboot oem lock")
return True
except:
return False
def reboot():
print("###########################")
print("Reboot your Device\n1.Reboot System (Boot into Android)\n2.Reboot Fastboot (Reboot into bootloader)\n3.Reboot Recovery (Reboot into recovery)")
print("###########################")
reply = int(input("Selection> "))
if reply == 1:
r("Rebooting device...")
executeR("adb reboot")
r("Waiting for device")
for i in range(50):
system = deviceLoc()
if type(system) != bool:
menu()
else:
time.sleep(5)
startupChecks()
elif reply == 2:
r("Booting to fastboot")
execute("sudo adb reboot bootloader")
startupChecks()
elif reply == 3:
r("Rebooting to recovery")
executeR("adb reboot recovery")
r("Waiting for device")
for i in range(10):
system = deviceLoc()
if type(system) != bool:
menu()
else:
time.sleep(5)
startupChecks()
else:
r("Unknown Selection")
time.sleep(1)
menu()
def menu():
global debug
global devices
global system
cmds = 8
if debug == True:
cmds = cmds + 1
size = executeR("stty size")
print(size)
for i in range(int(size[0])):
print("\n")
hash = "#"
for i in range(int(size[1])):
hash = hash + "#"
hash = hash[1:]
print(hash)
if devices[0] == "RUNNING":
if debug == False:
productinfo = executeR("sudo adb -s " + str(devices[1]) + " shell getprop ro.product.model")
deviceName = ""
loop = 0
for i in productinfo:
if loop == 0:
deviceName = i
else:
deviceName = deviceName + " " + i
loop = loop + 1
if debug == True:
string = "PLATFORM: " + platform.system().rstrip().upper() + " | DEBUG | " + devices[1]
else:
string = "PLATFORM: " + platform.system().rstrip().upper() + " | " + deviceName
stdout(string.rjust(int(size[1])))
else:
string = "DEVICE VERIFICATION OFF"
stdout(string.rjust(int(size[1])))
stdout('#rootella V: {0}'.format(version))
sys.stdout.flush()
print()
print("0. Quit")
print("1. Reboot")
print("2. Check connectivity")
print("3. Temporarily boot TWRP")
print("4. Flash custom recovery")
print("5. Unlock system bootloader")
print("6. Lock system bootloader")
print("990. Restart Rootella")
if debug == True:
print("999. Debug Menu")
for i in range(int(size[0]) - 4 - cmds):
print("")
print(hash)
select = int(input("Selection> "))
if select == 0:
print("Shutting down the ADB daemon...")
executeR("sudo adb kill-server")
sys.exit("Thanks for using Rootella, see you soon!")
if select == 1:
reboot()
if select == 2:
print("Checking with ADB... Please wait.")
startupChecks()
if select == 3:
if os.path.isfile("twrp.img") == False:
clear()
print("====TWRP Quick Boot====\nAs we currently don't have a TWRP downloader,\nPlease download the required TWRP\nimage for " + system[0] + " and name the file\n'twrp.img'")
input("Press ENTER to continue... ")
menu()
r("Rebooting to fastboot")
executeR("adb reboot bootloader")
r("Waiting for device")
for i in range(10):
system = deviceLoc()
if type(system) != bool:
if system[0] == "FASTBOOT":
print("Device online... Booting into TWRP.")
info = executeR("sudo fastboot boot twrp.img")
time.sleep(1)
r("Waiting for device")
for i in range(10):
system = deviceLoc()
if type(system) != bool:
if system[0] == "RUNNING":
r("TWRP booted!")
time.sleep(2)
menu()
else:
time.sleep(5)
else:
time.sleep(5)
if select == 4:
r("Custom Recovery")
print("This tool will flash a custom recovery to your device, the bootloader must be unlocked, if it's not, this tool will fail.")
answer = input("Continue? [y/N]> ").lower()
if answer == "y":
r("Rebooting to fastboot")
executeR("adb reboot bootloader")
r("Waiting for device")
for i in range(10):
system = deviceLoc()
if type(system) != bool:
fileLoc = input("File location (.img)> ")
if os.path.isfile(fileLoc):
if install("recovery", fileLoc):
r("Recovery flashed!")
time.sleep(2)
r("Rebooting device...")
executeR("sudo fastboot reboot")
r("Waiting for device")
print("We'll wait for a while, if you wiped your device it will take a while to reboot. The device finder may timeout.")
time.sleep(1)
for i in range(20):
system = deviceLoc()
if type(system) != bool:
menu()
else:
time.sleep(5)
startupChecks()
else:
menu()
else:
print("File not found.")
else:
time.sleep(5)
else:
menu()
if select == 5:
r("Bootloader unlock")
print("This tool will unlock your device's bootloader")
print("THIS WILL ERASE YOUR DATA AND PROBABLY VOID YOUR DEVICE WARRENTLY.")
answer = input("Are you sure you want to do this? [y/N]> ").lower()
if answer == "y":
print("Okay, let's go, I've got the torch...")
time.sleep(0.26)
r("Rebooting to fastboot")
executeR("adb reboot bootloader")
r("Waiting for device")
for i in range(10):
system = deviceLoc()
if type(system) != bool:
if system[0] == "FASTBOOT":
try:
bootloader("unlock")
except:
r("Something's gone wrong...")
print("Something bad has happend, check your device to make sure it's working.")
input("Press ENTER to continue")
init(warm)
else:
r("Something not right.")
print("Something has gone wrong. We'll reboot your phone, make sure it's working.")
executeR("sudo fastboot reboot")
else:
time.sleep(5)
if select == 6:
r("Bootloader lock")
print("This tool will re-lock your bootloader, it WILL NOT wipe your device, after doing this you won't be able to flash content to your phone.")
answer = input("Are you sure you want to continue? [y/N]> ").lower()
if answer == "y":
r("Rebooting to fastboot")
executeR("adb reboot bootloader")
r("Waiting for device")
for i in range(10):
system = deviceLoc()
if type(system) != bool:
if system[0] == "FASTBOOT":
r("Locking your bootloader.")
try:
bootloader("lock")
r("Bootloader locked.")
time.sleep(1)
print("Start your device (press the power button), wait for it to boot and hit enter.")
input("Press ENTER to continue. ")
init("warm")
except:
r("Bootloader Failed to lock")
input("Check your phone, it may've locked and told Rootella wrong, then reboot it and press ENTER")
menu()
else:
r("Something's not right.")
print("Your device didn't go to bootloader. We don't know why. Make sure your phone is okay.")
input("Press ENTER to continue. ")
else:
time.sleep(5)
if select == 990:
print("Restart now...")
time.sleep(1)
init("warm")
if select == 999:
if debug == True:
print("--Debug Menu--\n1. Add another device and restart\n2. Update Entry\n3. Device List\n4. Exit Debug\n--------------")
answer = int(input("Option> "))
if answer == 1:
answer = input("Device name> ")
devices.append('ADB')
devices.append(answer.upper())
devices.append("AUTHORIZED")
print("Calling restart...")
time.sleep(1)
init("warm")
if answer == 2:
print(devices)
loc = int(input("Entry?> "))
new = str(input("New content?> "))
devices[loc] = new
print(devices)
print("Devices list updated.")
answer = input("Preform startup checks? [Y/N]?> ").upper()
if answer == "Y":
startupChecks()
else:
menu()
if answer == 3:
print("Devices (Includes virtual devices): ")
print(devices)
input("Press enter to continue")
menu()
if answer == 4:
print("Exiting debug mode. Rootella will do a full cold restart and search for ADB/Fastboot devices...")
time.sleep(2)
debug = False
init("cold")
def deviceLoc():
running = execute("adb devices")
if len(running) == 1:
fastboot = subprocess.check_output("sudo fastboot devices".split()).decode("utf-8")
fastboot = fastboot.split("\t")
if len(fastboot) < 2:
return False
fastboot[1] = fastboot[1].rstrip()
arr = ["FASTBOOT"]
for i in fastboot:
arr.append(i)
return arr
else:
arr = ['RUNNING']
for i in running:
arr.append(i)
if arr == ['RUNNING', '']:
return False
return arr
def startupChecks():
global devices
global debug
if debug == True:
print("Device list (Contains Virtual Device): ")
print(devices)
time.sleep(1)
if debug == False:
devices = deviceLoc()
if devices == False and debug == False:
r("Device not found")
print("No devices where detected. Please make sure it's connected, on and developer options are enabled.")
answer = input("Press enter to continue. CTRL C to exit. Or type DEBUG to force the startup. \n").upper()
if answer == "DEBUG":
if devices != False:
devices.append('RUNNING', 'VIRTUALDEVICE01', 'AUTHORIZED')
print(devices)
else:
devices = ['RUNNING', 'VIRTUALDEVICE01', 'AUTHORIZED']
debug = True
print("Restarting Rootella...")
time.sleep(1)
init("warm")
devices = []
startupChecks()
if len(devices) > 3:
print("We've detected more than one device\nPlease only connect the device you want to use the toolkit with. Unplug the others.")
input("Press ENTER to continue...\n################################")
if debug == True:
print("You're running in debug mode with multiple devices, we'll automatically remove the device named " + devices[1] + " and rescan.")
devices.remove(devices[0])
devices.remove(devices[1])
devices.remove(devices[2])
startupChecks()
if devices[2] == "unauthorized":
print("#################################")
print("Looks like your device is unauthorized\nPlease authorize this computer on your phone now.\nWe suggest ticking 'Always allow from this computer' to stop errors/crashes.")
input("Press ENTER to continue...\n################################")
if debug == True:
print("Debug mode set, changing unauthorized to authorized automatically.")
devices[2] = "AUTHORIZED"
startupChecks()
if devices[0] == "FASTBOOT":
r("Device verification error.")
time.sleep(1)
print("As the device is in fastboot, we couldn't verify that it's comptable. We suggest you boot into recovery or Android.")
answer = input("Press ENTER to continue without verification or type reboot to boot into android> ")
if answer == "reboot":
r("Rebooting device")
execute("sudo fastboot reboot")
time.sleep(10)
init("warm")
else:
menu()
else:
global system
global supportedDevices
if debug == False:
system = executeR("sudo adb -s "+str(devices[1])+" shell getprop ro.product.device")
if system[0] in supportedDevices:
r(system[0] + " connected!")
time.sleep(1)
menu()
else:
if system[0] == "/sbin/sh:":
r("Please reboot your device")
print("Please reboot your device into Android, we can't verify the device as it's either in temp boot or using an old recovery.")
input("Press ENTER to continue... ")
init("warm")
else:
r(system[0] + " not supported")
print("Your connected device, " + system[0] + " is currently not supported. Sorry!")
input("Press ENTER to continue or CTRL + C to exit... ")
init("warm")
else:
r("Localhost connected!")
time.sleep(1)
menu()
init("cold")