From a1a31471eec5c3c51e8224bf0f0f2647a4d3857f Mon Sep 17 00:00:00 2001 From: HappyZ Date: Fri, 14 Dec 2018 11:41:52 -0600 Subject: [PATCH 1/3] try to find default client key file paths --- dpt-tools.py | 2 -- python_api/libDPT.py | 63 +++++++++++++++++++++++++++++++++++- python_api/libInteractive.py | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/dpt-tools.py b/dpt-tools.py index 63067e6..6a479d4 100644 --- a/dpt-tools.py +++ b/dpt-tools.py @@ -107,8 +107,6 @@ def main(): "Make sure your id, key, and ip addresses are correct." ) exit(1) - dpt.client_id_fp = args.get('dpt_id', "") - dpt.key_fp = args.get('dpt_key', "") interactive(dpt, diagnosis=args.get('diagnosis', False)) diff --git a/python_api/libDPT.py b/python_api/libDPT.py index 05db8ab..331e94d 100644 --- a/python_api/libDPT.py +++ b/python_api/libDPT.py @@ -545,12 +545,73 @@ class DPT(): return self._get_api( "/system/controls/pastlog", cookies=self.cookies, isfile=True) - def authenticate(self, client_id_fp, key_fp, testmode=False): + def get_client_key_fps(self): + ''' + return the stored client key file paths + ''' + return self.client_id_fp, self.key_fp + + def set_client_key_fps(self, client_id_fp, key_fp): + ''' + store the client key file paths + ''' + self.client_id_fp = client_id_fp + self.key_fp = key_fp + + def auto_find_client_key_fps(self): + ''' + automatically find the client key file paths + inspired from https://github.com/janten/dpt-rp1-py/pull/52 + ''' + default_client_fp, default_key_fp = self.get_client_key_fps() + if os.path.isfile(default_client_fp) and os.path.isfile(default_key_fp): + return default_client_fp, default_key_fp + dpa_path = "." + # MacOS + try: + home_path = os.path.expanduser("~") + except BaseException: + return default_client_fp, default_key_fp + tmp_dpa_path = "{}/Library/Application Support/".format(home_path) + tmp_dpa_path += "Sony Corporation/Digital Paper App" + if os.path.isdir(tmp_dpa_path): + dpa_path = tmp_dpa_path + # windows + tmp_dpa_path = "{}/AppData/Roaming/".format(home_path) + tmp_dpa_path += "Sony Corporation/Digital Paper App" + if os.path.isdir(tmp_dpa_path): + dpa_path = tmp_dpa_path + # Linux + tmp_dpa_path = "{}/.dpapp".format(home_path) + if os.path.isdir(tmp_dpa_path): + dpa_path = tmp_dpa_path + tmp_client_fp = "{}/deviceid.dat".format(dpa_path) + tmp_key_fp = "{}/privatekey.dat".format(dpa_path) + if os.path.isfile(tmp_client_fp) and os.path.isfile(tmp_key_fp): + default_client_fp = tmp_client_fp + default_key_fp = tmp_key_fp + return default_client_fp, default_key_fp + + def reauthenticate(self): + ''' + reauthentication (must done after reboot) + ''' + return self.authenticate() + + + def authenticate(self, client_id_fp="", key_fp="", testmode=False): + ''' + authenticate is necessary to send url request + ''' + # find client_id_fp and key_fp optional + if not client_id_fp or not key_fp: + client_id_fp, key_fp = auto_find_client_key_fps() if not os.path.isfile(client_id_fp) or not os.path.isfile(key_fp): print( "! Err: did not find {0} or {1}" .format(client_id_fp, key_fp)) return False + self.set_client_key_fps(client_id_fp, key_fp) with open(client_id_fp) as f: client_id = f.read().strip() diff --git a/python_api/libInteractive.py b/python_api/libInteractive.py index 8ffb7de..d6fbfbd 100644 --- a/python_api/libInteractive.py +++ b/python_api/libInteractive.py @@ -125,7 +125,7 @@ def obtain_diagnosis_access(dpt): except BaseException as e: dpt.err_print(str(e)) return False - if not dpt.authenticate(dpt.client_id_fp, dpt.key_fp): + if not dpt.reauthenticate(): dpt.err_print("Cannot reauthenticate after reboot") dpt.err_print("Client id filepath: {}".format(dpt.client_id_fp)) dpt.err_print("Client key filepath: {}".format(dpt.key_fp)) From 416d265b2e6978866c42809fbd5d52445f278fc0 Mon Sep 17 00:00:00 2001 From: HappyZ Date: Fri, 14 Dec 2018 20:30:13 -0600 Subject: [PATCH 2/3] bug fix --- dpt-tools.py | 3 +-- python_api/libDPT.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dpt-tools.py b/dpt-tools.py index 6a479d4..7ba73b7 100644 --- a/dpt-tools.py +++ b/dpt-tools.py @@ -65,8 +65,7 @@ def main(): ''' main func to initalize dpt object ''' - p = argparse.ArgumentParser( - description="DPT Tools") + p = argparse.ArgumentParser(description="DPT Tools") p.add_argument( '--client-id', '-id', dest="dpt_id", diff --git a/python_api/libDPT.py b/python_api/libDPT.py index 331e94d..30491a7 100644 --- a/python_api/libDPT.py +++ b/python_api/libDPT.py @@ -598,14 +598,13 @@ class DPT(): ''' return self.authenticate() - def authenticate(self, client_id_fp="", key_fp="", testmode=False): ''' authenticate is necessary to send url request ''' # find client_id_fp and key_fp optional if not client_id_fp or not key_fp: - client_id_fp, key_fp = auto_find_client_key_fps() + client_id_fp, key_fp = self.auto_find_client_key_fps() if not os.path.isfile(client_id_fp) or not os.path.isfile(key_fp): print( "! Err: did not find {0} or {1}" From 2618e81589c41770c9e4d99ba7b8e3782921c7a6 Mon Sep 17 00:00:00 2001 From: HappyZ Date: Sat, 15 Dec 2018 10:04:13 -0600 Subject: [PATCH 3/3] add patch to prevent permanent brick just in case --- python_api/assets/updater_check.sh | 71 ++++++++++++++++++++++++++++++ python_api/libInteractive.py | 17 ++++++- 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100755 python_api/assets/updater_check.sh diff --git a/python_api/assets/updater_check.sh b/python_api/assets/updater_check.sh new file mode 100755 index 0000000..27d296e --- /dev/null +++ b/python_api/assets/updater_check.sh @@ -0,0 +1,71 @@ +#!/bin/sh + + +DDAT_MOUNT_PATH=/tmp/ddat +END_USER_UPDATER_PKG=${DDAT_MOUNT_PATH}/FwUpdater.pkg + + + + +# $1 : reboot=1, shutdown 0 +local_reboot() +{ + umount $DDAT_MOUNT_PATH + sync + sync + mount -o remount,ro / + if [ $1 -eq 1 ] + then + /sbin/reboot + else + /sbin/poweroff + fi + + while [ 1 ] + do + sleep 3 + done +} + + + +######################### +# End User Updater check +######################### + +mount -t tmpfs tmpfs /tmp +mkdir ${DDAT_MOUNT_PATH} +mount /dev/mmcblk0p16 ${DDAT_MOUNT_PATH} +if [ -f ${END_USER_UPDATER_PKG} ] +then + rawdata --get_dump=sig_key > /tmp/sig.key + rawdata --get_dump=dec_key > /tmp/dec.key + start_eufwupdater.sh ${END_USER_UPDATER_PKG} /tmp /tmp/sig.key /tmp/dec.key + ret=$? + if [ $ret -eq 0 ] + then + # remove pkg, change normal boot and reboot + change_boot_mode.sh normal + rm -rf ${END_USER_UPDATER_PKG} + local_reboot 1 + # elif [ $ret -eq 1 ] + # then + # # remain pkg, keep boot mode and shutdown + # local_reboot 0 + else + # remove pkg, change normal boot and shutdown + change_boot_mode.sh normal + rm -rf ${END_USER_UPDATER_PKG} + local_reboot 0 + fi +fi + +umount ${DDAT_MOUNT_PATH} + +######################### +# Diag check +######################### + +initctl start diag +exit 0 + diff --git a/python_api/libInteractive.py b/python_api/libInteractive.py index d6fbfbd..b355e7f 100644 --- a/python_api/libInteractive.py +++ b/python_api/libInteractive.py @@ -52,7 +52,8 @@ def validate_required_files(dpt, purpose='diagnosis'): ] elif purpose == 'eufwupdater': requiredFiles = [ - 'python_api/assets/start_eufwupdater.sh' + 'python_api/assets/start_eufwupdater.sh', + 'python_api/assets/updater_check.sh' ] else: requiredFiles = [ @@ -342,15 +343,27 @@ def diagnosis_patch_eufwupdater(dpt): ''' if not validate_required_files(dpt, purpose='eufwupdater'): return False + # patch start_eufwupdater.sh bashfp = diagnosis_push_file( dpt, localfp='python_api/assets/start_eufwupdater.sh', folder='/usr/local/bin', overwrite=True) if bashfp is None: - dpt.err_print("Failed to patch!!") + dpt.err_print("Failed to patch start_eufwupdater.sh!!") return False dpt.diagnosis_set_perm(bashfp, owner='1496.1496', perm='0775') + # patch updater_check.sh + bashfp = diagnosis_push_file( + dpt, + localfp='python_api/assets/updater_check.sh', + folder='/usr/local/bin', + overwrite=True) + if bashfp is None: + dpt.err_print("Failed to patch updater_check.sh!!") + return False + dpt.diagnosis_set_perm(bashfp, owner='1496.1496', perm='0775') + # success dpt.info_print("Success!") return True