dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/28162 )
Change subject: pySim-shell: catch exceptions from walk() while exporting
......................................................................
pySim-shell: catch exceptions from walk() while exporting
When we run the exporter we also get an error summary at the end.
However, if walk() throws an eception this stops the exporter
immediately and we won't get the summpary. Lets catch exceptions from
walk as well so that we are able to end gracefully.
Change-Id: I3edc250ef2a84550c5b821a72e207e4d685790a5
---
M pySim-shell.py
1 file changed, 12 insertions(+), 5 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/62/28162/1
diff --git a/pySim-shell.py b/pySim-shell.py
index 8c6db2f..49c794f 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -669,10 +669,17 @@
context = {'ERR': 0, 'COUNT': 0, 'BAD': [],
'DF_SKIP': 0, 'DF_SKIP_REASON': []}
opts_export = {'JSON': opts.json, 'CREATE': opts.create}
+ exception_str_add = ""
+
if opts.filename:
self.export_ef(opts.filename, context, opts_export)
else:
- self.walk(0, self.export_ef, self.export_df, context, opts_export)
+ try:
+ self.walk(0, self.export_ef, self.export_df, context, opts_export)
+ except Exception as e:
+ print("# Stopping early here due to exception: " + str(e))
+ print("#")
+ exception_str_add = ", also had to stop early due to exception:" + str(e)
self._cmd.poutput(boxed_heading_str("Export summary"))
@@ -687,14 +694,14 @@
self._cmd.poutput("# " + b)
if context['ERR'] and context['DF_SKIP']:
- raise RuntimeError("unable to export %i elementary file(s) and %i dedicated file(s)" % (
- context['ERR'], context['DF_SKIP']))
+ raise RuntimeError("unable to export %i elementary file(s) and %i dedicated file(s)%s" % (
+ context['ERR'], context['DF_SKIP'], exception_str_add))
elif context['ERR']:
raise RuntimeError(
- "unable to export %i elementary file(s)" % context['ERR'])
+ "unable to export %i elementary file(s)%s" % (context['ERR'], exception_str_add))
elif context['DF_SKIP']:
raise RuntimeError(
- "unable to export %i dedicated files(s)" % context['ERR'])
+ "unable to export %i dedicated files(s)%s" % (context['ERR'], exception_str_add))
def do_reset(self, opts):
"""Reset the Card."""
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28162
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I3edc250ef2a84550c5b821a72e207e4d685790a5
Gerrit-Change-Number: 28162
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-MessageType: newchange
Attention is currently required from: dexter.
Hello Jenkins Builder,
I'd like you to reexamine a change. Please visit
https://gerrit.osmocom.org/c/pysim/+/28160
to look at the new patch set (#2).
Change subject: pySim-shell: extend walk() so that we can also have action of ADF or DF
......................................................................
pySim-shell: extend walk() so that we can also have action of ADF or DF
The walk() method that we use to traverse the whole file system tree is
currently only able to execute action callbacks on EFs. Lets add a
mechanism that allows us to have a second callback that is executed when
we hit a DF or ADF.
Change-Id: Iabcd78552a14a2d3f8f31273dda7731e1f640cdb
---
M pySim-shell.py
1 file changed, 15 insertions(+), 9 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/60/28160/2
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28160
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Iabcd78552a14a2d3f8f31273dda7731e1f640cdb
Gerrit-Change-Number: 28160
Gerrit-PatchSet: 2
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-MessageType: newpatchset
dexter has posted comments on this change. ( https://gerrit.osmocom.org/c/pysim/+/28161 )
Change subject: pySim-shell: add export option to add CREATE FILE APDUs
......................................................................
Patch Set 1:
(1 comment)
Patchset:
PS1:
(see commit message)
It turns out that the FCP template that is returned by the card may be quite variable. It may even lack the FID! This is a problem when we want to use the FCP template as a parameter for CREATE FILE. What we could do though is to parse the FCP template we got and extend the missing information if necessary. pySim-shell should have all the tools we need.
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28161
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: If529b86062f7c678c9c75f46ce9ca116cea8b47b
Gerrit-Change-Number: 28161
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-CC: Jenkins Builder
Gerrit-Comment-Date: Thu, 19 May 2022 08:42:22 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/28160 )
Change subject: pySim-shell: extend walk() so that we can also have action of ADF or DF
......................................................................
pySim-shell: extend walk() so that we can also have action of ADF or DF
The walk() method that we use to traverse the whole file system tree is
currently only able to execute action callbacks on EFs. Lets add a
mechanism that allows us to have a second callback that is executed when
we hit a DF or ADF.
Change-Id: Iabcd78552a14a2d3f8f31273dda7731e1f640cdb
---
M pySim-shell.py
1 file changed, 15 insertions(+), 9 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/60/28160/1
diff --git a/pySim-shell.py b/pySim-shell.py
index 5a51fcd..b1503a8 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -461,12 +461,18 @@
self._cmd.poutput(directory_str)
self._cmd.poutput("%d files" % len(selectables))
- def walk(self, indent=0, action=None, context=None, opts={}):
+ def walk(self, indent=0, action_ef=None, action_df=None, context=None, opts={}):
"""Recursively walk through the file system, starting at the currently selected DF"""
+
+ if isinstance(self._cmd.rs.selected_file, CardDF):
+ if action_df:
+ action_df(context, opts)
+
files = self._cmd.rs.selected_file.get_selectables(
flags=['FNAMES', 'ANAMES'])
for f in files:
- if not action:
+ # special case: When no action is performed, just output a directory
+ if not action_ef and action_df:
output_str = " " * indent + str(f) + (" " * 250)
output_str = output_str[0:25]
if isinstance(files[f], CardADF):
@@ -493,12 +499,12 @@
# If the DF was skipped, we never have entered the directory
# below, so we must not move up.
if skip_df == False:
- self.walk(indent + 1, action, context, opts)
+ self.walk(indent + 1, action_ef, action_df, context, opts)
fcp_dec = self._cmd.rs.select("..", self._cmd)
- elif action:
+ elif action_ef:
df_before_action = self._cmd.rs.selected_file
- action(f, context, opts)
+ action_ef(f, context, opts)
# When walking through the file system tree the action must not
# always restore the currently selected file to the file that
# was selected before executing the action() callback.
@@ -510,8 +516,8 @@
"""Display a filesystem-tree with all selectable files"""
self.walk()
- def export(self, filename, context, opts):
- """ Select and export a single file """
+ def export_ef(self, filename, context, opts):
+ """ Select and export a single elementry file (EF) """
context['COUNT'] += 1
df = self._cmd.rs.selected_file
as_json = opts['JSON']
@@ -622,9 +628,9 @@
'DF_SKIP': 0, 'DF_SKIP_REASON': []}
opts_export = {'JSON': opts.json}
if opts.filename:
- self.export(opts.filename, context, opts_export)
+ self.export_ef(opts.filename, context, opts_export)
else:
- self.walk(0, self.export, context, opts_export)
+ self.walk(0, self.export_ef, None, context, opts_export)
self._cmd.poutput(boxed_heading_str("Export summary"))
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28160
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Iabcd78552a14a2d3f8f31273dda7731e1f640cdb
Gerrit-Change-Number: 28160
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-MessageType: newchange
dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/28161 )
Change subject: pySim-shell: add export option to add CREATE FILE APDUs
......................................................................
pySim-shell: add export option to add CREATE FILE APDUs
Export currently only exports the contents of the files. When running
the resulting script it is expected that the files already exist. This
is normaly the case. However, when replaying the script on a blank card
the files do not exist yet.
The CREATE FILE parameters are created based on the FCP template that
the card returns when the file is selected. In some cases the returned
FCP template may lacl data that the card needs to create the file
properly. This means that the --create parameter must be used with
caution and possibly manual re-work of the result is necessary.
Change-Id: If529b86062f7c678c9c75f46ce9ca116cea8b47b
---
M pySim-shell.py
1 file changed, 45 insertions(+), 3 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/61/28161/1
diff --git a/pySim-shell.py b/pySim-shell.py
index b1503a8..5e379e7 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -521,6 +521,7 @@
context['COUNT'] += 1
df = self._cmd.rs.selected_file
as_json = opts['JSON']
+ create = opts['CREATE']
# The currently selected file (not the file we are going to export)
# must always be an ADF or DF. From this starting point we select
@@ -550,7 +551,11 @@
for f in df_path_list:
self._cmd.poutput("select " + str(f))
- self._cmd.poutput("select " + self._cmd.rs.selected_file.name)
+
+ if create:
+ self._cmd.poutput("apdu 00E00000%02x%s" % (int(len(self._cmd.rs.selected_file_fcp_hex)/2), str(self._cmd.rs.selected_file_fcp_hex)))
+ else:
+ self._cmd.poutput("select " + self._cmd.rs.selected_file.name)
if structure == 'transparent':
if as_json:
@@ -615,22 +620,59 @@
self._cmd.poutput("#")
+ def export_df(self, context, opts):
+ """ Export the currently selected dedicated file (DF or ADF) """
+ df = self._cmd.rs.selected_file
+
+ create = opts['CREATE']
+
+ # For consistency, ensure that at the time we call this function the
+ # selected file is a dedicated file.
+ if not isinstance(df, CardDF):
+ raise RuntimeError(
+ "currently selected file %s is not an EF" % str(df))
+
+ # This type of export only makes sense when we intend to add APDUs for
+ # file creation. In the other case the files are already present on the
+ # card, only the contents are updated.
+ if not create:
+ return;
+
+ df = self._cmd.rs.selected_file
+ df_path_list = df.fully_qualified_path(True)
+ df_path_list_fid = df.fully_qualified_path(False)
+
+ file_str = '/'.join(df_path_list) + "/"
+ self._cmd.poutput(boxed_heading_str(file_str))
+
+ structure = self._cmd.rs.selected_file_structure()
+ self._cmd.poutput("# RAW FCP Template: %s" % str(self._cmd.rs.selected_file_fcp_hex))
+ self._cmd.poutput("# Decoded FCP Template: %s" % str(self._cmd.rs.selected_file_fcp))
+
+ for f in df_path_list[:-1]:
+ self._cmd.poutput("select " + str(f))
+
+ self._cmd.poutput("apdu 00E00000%02x%s" % (int(len(self._cmd.rs.selected_file_fcp_hex)/2), str(self._cmd.rs.selected_file_fcp_hex)))
+ self._cmd.poutput("#")
+
export_parser = argparse.ArgumentParser()
export_parser.add_argument(
'--filename', type=str, default=None, help='only export specific file')
export_parser.add_argument(
'--json', action='store_true', help='export as JSON (less reliable)')
+ export_parser.add_argument(
+ '--create', action='store_true', help='add APDUs for file creation (less reliable')
@cmd2.with_argparser(export_parser)
def do_export(self, opts):
"""Export files to script that can be imported back later"""
context = {'ERR': 0, 'COUNT': 0, 'BAD': [],
'DF_SKIP': 0, 'DF_SKIP_REASON': []}
- opts_export = {'JSON': opts.json}
+ opts_export = {'JSON': opts.json, 'CREATE': opts.create}
if opts.filename:
self.export_ef(opts.filename, context, opts_export)
else:
- self.walk(0, self.export_ef, None, context, opts_export)
+ self.walk(0, self.export_ef, self.export_df, context, opts_export)
self._cmd.poutput(boxed_heading_str("Export summary"))
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28161
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: If529b86062f7c678c9c75f46ce9ca116cea8b47b
Gerrit-Change-Number: 28161
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-MessageType: newchange
dexter has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/28158 )
Change subject: pySim-shell: explain why we insist on a DF or ADF
......................................................................
pySim-shell: explain why we insist on a DF or ADF
Change-Id: I155cefb10864432d59a0a66410783b4c9772f8a4
---
M pySim-shell.py
1 file changed, 4 insertions(+), 0 deletions(-)
Approvals:
Jenkins Builder: Verified
dexter: Looks good to me, approved
diff --git a/pySim-shell.py b/pySim-shell.py
index 06977f0..33c18fd 100755
--- a/pySim-shell.py
+++ b/pySim-shell.py
@@ -515,6 +515,10 @@
context['COUNT'] += 1
df = self._cmd.rs.selected_file
+ # The currently selected file (not the file we are going to export)
+ # must always be an ADF or DF. From this starting point we select
+ # the EF we want to export. To maintain consistency we will then
+ # select the current DF again (see comment below).
if not isinstance(df, CardDF):
raise RuntimeError(
"currently selected file %s is not a DF or ADF" % str(df))
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28158
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I155cefb10864432d59a0a66410783b4c9772f8a4
Gerrit-Change-Number: 28158
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-MessageType: merged
dexter has posted comments on this change. ( https://gerrit.osmocom.org/c/pysim/+/28158 )
Change subject: pySim-shell: explain why we insist on a DF or ADF
......................................................................
Patch Set 1: Code-Review+2
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/28158
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I155cefb10864432d59a0a66410783b4c9772f8a4
Gerrit-Change-Number: 28158
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Thu, 19 May 2022 08:30:08 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment