From daf2b392f04e3b97ce52e52df08bd5a79ac35715 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 3 May 2021 23:17:29 +0200 Subject: [PATCH] shell: Add 'reset' command to reset the card At some points during an interactive session or a script one may want to reset the card. Change-Id: I992eb3e0ed52f7941a5fb44f28a42e22ebd49301 --- docs/shell.rst | 6 ++++++ pySim-shell.py | 6 ++++++ pySim/filesystem.py | 13 ++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/shell.rst b/docs/shell.rst index 51709996..08fd3762 100644 --- a/docs/shell.rst +++ b/docs/shell.rst @@ -193,6 +193,12 @@ verify_adm FIXME +reset +~~~~~ + +Perform card reset and display the card ATR. + + Linear Fixed EF commands ------------------------ diff --git a/pySim-shell.py b/pySim-shell.py index 5b5768a0..bbfe7e9e 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -313,6 +313,12 @@ class PySimCommands(CommandSet): elif context['DF_SKIP']: raise RuntimeError("unable to export %i dedicated files(s)" % context['ERR']) + def do_reset(self, opts): + """Reset the Card.""" + atr = self._cmd.rs.reset(self._cmd) + self._cmd.poutput('Card ATR: %s' % atr) + self._cmd.update_prompt() + @with_default_category('ISO7816 Commands') class Iso7816Commands(CommandSet): diff --git a/pySim/filesystem.py b/pySim/filesystem.py index b3e28efd..780da26d 100644 --- a/pySim/filesystem.py +++ b/pySim/filesystem.py @@ -34,7 +34,7 @@ import argparse from typing import cast, Optional, Iterable, List, Any, Dict, Tuple -from pySim.utils import sw_match, h2b, b2h, is_hex, auto_int, bertlv_parse_one +from pySim.utils import sw_match, h2b, b2h, i2h, is_hex, auto_int, bertlv_parse_one, Hexstr from pySim.construct import filter_dict from pySim.exceptions import * from pySim.jsonpath import js_path_find, js_path_modify @@ -1020,6 +1020,17 @@ class RuntimeState(object): print("error: could not determine card applications") return apps_taken + def reset(self, cmd_app=None) -> Hexstr: + """Perform physical card reset and obtain ATR. + Args: + cmd_app : Command Application State (for unregistering old file commands) + """ + self.card._scc._tp.reset_card() + atr = i2h(self.card._scc._tp.get_atr()) + # select MF to reset internal state and to verify card really works + self.select('MF', cmd_app) + return atr + def get_cwd(self) -> CardDF: """Obtain the current working directory.