add `pull-file` also

ready to test boot.img with adbd yet?
This commit is contained in:
HappyZ 2018-11-16 15:35:20 -06:00
parent 72de99a077
commit 0f61bfe432
4 changed files with 86 additions and 13 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ extracted_apk
secrets secrets
.DS_Store .DS_Store
*.pyc *.pyc
abootimg

View File

@ -70,13 +70,11 @@ def main():
p.add_argument( p.add_argument(
'--client-id', '-id', '--client-id', '-id',
dest="dpt_id", dest="dpt_id",
help="File containing the device's client id", help="File containing the device's client id")
required=True)
p.add_argument( p.add_argument(
'--key', '-k', '--key', '-k',
dest="dpt_key", dest="dpt_key",
help="File containing the device's private key", help="File containing the device's private key")
required=True)
p.add_argument( p.add_argument(
'--addr', '-ip', '--addr', '-ip',
dest="dpt_addr", dest="dpt_addr",

View File

@ -101,7 +101,18 @@ class DPT():
cmd = "[ -d {} ] && echo 'YESS' || echo 'NONO'".format(folderp) cmd = "[ -d {} ] && echo 'YESS' || echo 'NONO'".format(folderp)
return 'YESS' in self.diagnosis_write(cmd) return 'YESS' in self.diagnosis_write(cmd)
def diagnosis_write(self, cmd, echo=False): def diagnosis_backup_boot(self):
'''
back up boot partition to /tmp/ folder
'''
cmd = 'dd if=/dev/mmcblk0p8 of=/tmp/boot.img.bak bs=4M'
self.diagnosis_write(cmd, timeout=60)
if not self.diagnosis_isfile('/tmp/boot.img.bak'):
self.err_print('Failed to backup!')
return False
return True
def diagnosis_write(self, cmd, echo=False, timeout=99):
''' '''
write cmd and read feedbacks write cmd and read feedbacks
''' '''
@ -112,7 +123,7 @@ class DPT():
try: try:
self.serial.write(cmd.encode() + b'\n') self.serial.write(cmd.encode() + b'\n')
# change timeout to (nearly) blocking first to read # change timeout to (nearly) blocking first to read
self.serial.timeout = 99 self.serial.timeout = timeout
resp = self.serial.read_until(b'# ') resp = self.serial.read_until(b'# ')
# change back the original timeout # change back the original timeout
self.serial.timeout = self.serialReadTimeout self.serial.timeout = self.serialReadTimeout

View File

@ -3,6 +3,7 @@
# built-ins # built-ins
import os import os
import time import time
import subprocess
# import traceback # import traceback
@ -122,20 +123,75 @@ It behaves similarly to regular serial session with less flexibility (cannot use
This mode intends to automate some complicated procedures. This mode intends to automate some complicated procedures.
Supported commands: Supported commands:
`transfer-file` -- transfer file to DPT at 512bps `push-file` -- transfer file to DPT at 512bps
`pull-file` -- transfer file from DPT
`exit`/`quit` -- leave the tool `exit`/`quit` -- leave the tool
and many unix cmds (do not support less/head) and many unix cmds (do not support less/head)
""") """)
def diagnosis_transfer_file(dpt, chunkSize=128): def diagnosis_pull_file(dpt):
''' '''
transfer file through echo in diagnosis (serial) mode pull file from device to local via xxd and parsing in
python
do NOT pull large file using this, it will take forever
to finish..
'''
try:
# get and validate remote file path
remotefp = input('> DPT file path: ')
if not dpt.diagnosis_isfile(remotefp):
dpt.err_print('File {} does not exist!'.format(remotefp))
return False
# get local folder path
folder = input('> Local folder path: ')
if not os.path.isdir(folder):
resp = input('> {} not exist, create? [yes/no]: '.format(folder))
if resp == 'no':
return False
elif resp == 'yes':
os.makedirs(folder)
else:
dpt.err_print('Unrecognized input {}'.format(resp))
return False
# check if local fp exists
localfp = "{0}/{1}".format(folder, os.path.basename(remotefp))
if os.path.isfile(localfp):
resp = input('> {} exist, overwrite? [yes/no]: '.format(localfp))
if resp == 'no':
return False
elif not resp == 'yes':
dpt.err_print('Unrecognized input {}'.format(resp))
return False
# read from xxd, parse, and write to local file
startTime = int(time.time() * 1000)
cmd = "xxd -p {}".format(remotefp)
with open("{}.tmp".format(localfp), 'w') as f:
for each in dpt.diagnosis_write(cmd, timeout=999).splitlines():
f.write(each)
subprocess.call(
['xxd', '-r', '-p', "{}.tmp".format(localfp), '>', localfp]
)
duration = int(time.time() * 1000) - startTime
dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0))
if os.path.isfile(localfp):
dpt.info_print("Success")
os.remove("{}.tmp".format(localfp))
return True
except BaseException as e:
dpt.err_print(str(e))
return False
def diagnosis_push_file(dpt, chunkSize=128):
'''
push file from local to device through echo in diagnosis
(serial) mode
using echo is dumb and slow but very reliable using echo is dumb and slow but very reliable
limited to 128 bytes per sec since we send raw bytes in limited to 128 bytes per sec since we send raw bytes in
string (each byte sent = 4 bytes), and terminal at best string (each byte sent = 4 bytes), and terminal at best
allows 1024 bytes to send allows 1024 bytes to send
do NOT transfer large file using this, it will take do NOT push large file using this, it will take
forever to finish.. forever to finish..
''' '''
try: try:
@ -167,6 +223,7 @@ def diagnosis_transfer_file(dpt, chunkSize=128):
elif not resp == 'yes': elif not resp == 'yes':
dpt.err_print('Unrecognized input {}'.format(resp)) dpt.err_print('Unrecognized input {}'.format(resp))
return False return False
# write through echo
firstRun = True firstRun = True
symbol = '>' symbol = '>'
startTime = int(time.time() * 1000) startTime = int(time.time() * 1000)
@ -187,6 +244,9 @@ def diagnosis_transfer_file(dpt, chunkSize=128):
firstRun = False firstRun = False
duration = int(time.time() * 1000) - startTime duration = int(time.time() * 1000) - startTime
dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0)) dpt.info_print('Finished in {0:.2f}sec'.format(duration / 1000.0))
if dpt.diagnosis_isfile(remotefp):
dpt.info_print("Success")
return True
except BaseException as e: except BaseException as e:
dpt.err_print(str(e)) dpt.err_print(str(e))
return False return False
@ -214,8 +274,11 @@ def diagnosis_cmd(dpt):
elif cmd == 'help': elif cmd == 'help':
print_diagnosis_info() print_diagnosis_info()
continue continue
elif cmd == 'transfer-file': elif cmd == 'push-file':
diagnosis_transfer_file(dpt) diagnosis_push_file(dpt)
continue
elif cmd == 'pull-file':
diagnosis_pull_file(dpt)
continue continue
rawresp = dpt.diagnosis_write(cmd) rawresp = dpt.diagnosis_write(cmd)
# ignore first and last echos # ignore first and last echos