From 825b56411585dc2b5cb27bd647e5f10431456166 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Wed, 10 Nov 2021 17:36:26 +0100 Subject: [PATCH] pySim-shell: export command: guess number of records when not specified The select response of an UICC will always return the number of records of a file. However, older SIM will not include the number of records in the select response. In those cases, simply guess the number of records by reading until the first invalid record is hit. Change-Id: Ib480797d881b9ec607ec6a86b73d452449f8cf87 Related: OS#5274 --- pySim-shell.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pySim-shell.py b/pySim-shell.py index 0519ec4f..8e8a1a61 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -517,10 +517,28 @@ class PySimCommands(CommandSet): result = self._cmd.rs.read_binary() self._cmd.poutput("update_binary " + str(result[0])) elif structure == 'cyclic' or structure == 'linear_fixed': - num_of_rec = fd['num_of_rec'] - for r in range(1, num_of_rec + 1): - result = self._cmd.rs.read_record(r) - self._cmd.poutput("update_record %d %s" % (r, str(result[0]))) + # Use number of records specified in select response + if 'num_of_rec' in fd: + num_of_rec = fd['num_of_rec'] + for r in range(1, num_of_rec + 1): + result = self._cmd.rs.read_record(r) + self._cmd.poutput("update_record %d %s" % (r, str(result[0]))) + # When the select response does not return the number of records, read until we hit the + # first record that cannot be read. + else: + r = 1 + while True: + try: + result = self._cmd.rs.read_record(r) + except SwMatchError as e: + # We are past the last valid record - stop + if e.sw_actual == "9402": + break + # Some other problem occurred + else: + raise e + self._cmd.poutput("update_record %d %s" % (r, str(result[0]))) + r = r + 1 elif structure == 'ber_tlv': tags = self._cmd.rs.retrieve_tags() for t in tags: