-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash
executable file
·91 lines (73 loc) · 2.65 KB
/
flash
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
#!/usr/bin/env python3
import os
from sys import argv, stderr
from subprocess import CalledProcessError, run, DEVNULL, PIPE
def tool_exists(name):
"""Check whether `name` is on PATH and marked as executable."""
from shutil import which
return which(name) is not None
def fatal(*args, **kwargs):
print(*args, file=stderr, **kwargs)
exit(1)
def adb_wait_for_device():
print('Waiting for the device...')
run(['adb', 'wait-for-device'])
def heimdall_wait_for_device():
print('Waiting for download mode...')
run('until heimdall detect > /dev/null 2>&1; do sleep 1; done', shell=True)
def heimdall_in_download_mode():
return run(['heimdall', 'detect'], stdout=DEVNULL, stderr=DEVNULL).returncode == 0
def heimdall_flash_boot(boot):
run(['heimdall', 'flash', '--BOOT', boot], check=True)
def adb_reboot_download():
run(['adb', 'reboot', 'download'])
def adb_reboot():
run(['adb', 'reboot'])
def adb_get_kernel_version():
run(['adb', 'shell', 'cat', '/proc/version'])
def adb_uid():
return int(run(['adb', 'shell', 'id', '-u'], stdout=PIPE, check=True).stdout.decode('utf-8'))
def adb_check_su():
try:
run(['adb', 'shell', 'command', '-v', 'su'], check=True)
return True
except CalledProcessError:
return False
def flash(boot):
if tool_exists('adb'):
is_root = False
try:
if not heimdall_in_download_mode():
adb_wait_for_device()
is_root = (adb_uid() == 0) or adb_check_su()
except (FileNotFoundError, CalledProcessError):
pass
if is_root:
run(['adb', 'push',
boot, '/data/local/tmp'],
check=True)
run(['adb', 'shell',
"su -c 'dd if=/data/local/tmp/" + boot +
" of=/dev/block/by-name/boot'"],
check=True)
run(['adb', 'shell', 'rm', '-f', '/data/local/tmp/' + boot])
adb_reboot()
adb_wait_for_device()
adb_get_kernel_version()
elif tool_exists('heimdall'):
if not heimdall_in_download_mode():
adb_wait_for_device()
adb_reboot_download()
heimdall_wait_for_device()
heimdall_flash_boot(boot)
adb_wait_for_device()
adb_get_kernel_version()
else:
fatal("Please, use 'adb root' or install 'heimdall'")
else:
fatal("Please, install 'adb'")
if __name__ == '__main__':
boot_img = argv[1] if len(argv) > 1 else 'boot.img'
if not os.path.isfile(boot_img):
fatal("Usage: {} ./boot.img".format(argv[0]))
flash(boot_img)