Change in pysim[master]: Allow update_binary function to write more than 255 bytes

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at

laforge gerrit-no-reply at
Tue Aug 3 07:09:23 UTC 2021

laforge has submitted this change. ( )

Change subject: Allow update_binary function to write more than 255 bytes

Allow update_binary function to write more than 255 bytes

The T0 protocol (selected in transport/ does not support extended APDU, so 255 bytes is the maximum number of bytes that can be transmitted at a time.  We can divide large data into 255 byte chunks.  The read_binary function already has code to read more than 255 bytes, so we can just adapt it to the update_binary function.

Change-Id: Icc240d5c8c04198640eb118565ea99f10ba27466
M pySim/
1 file changed, 15 insertions(+), 3 deletions(-)

  Hoernchen: Looks good to me, but someone else must approve
  laforge: Looks good to me, approved
  Jenkins Builder: Verified

diff --git a/pySim/ b/pySim/
index 76b7cd5..9657162 100644
--- a/pySim/
+++ b/pySim/
@@ -170,11 +170,23 @@
 				return None, sw
-		pdu = self.cla_byte + 'd6%04x%02x' % (offset, data_length) + data
-		res = self._tp.send_apdu_checksw(pdu)
+		total_data = ''
+		total_sw = "9000"
+		chunk_offset = offset
+		while chunk_offset < data_length:
+			chunk_len = min(255, data_length - chunk_offset)
+			# chunk_offset is bytes, but data slicing is hex chars, so we need to multiply by 2
+			pdu = self.cla_byte + 'd6%04x%02x' % (chunk_offset, chunk_len) + data[chunk_offset*2 : (chunk_offset+chunk_len)*2]
+			chunk_data, chunk_sw = self._tp.send_apdu(pdu)
+			if chunk_sw == total_sw:
+				total_data += chunk_data
+				chunk_offset += chunk_len
+			else:
+				total_sw = chunk_sw
+				raise ValueError('Failed to write chunk (chunk_offset %d, chunk_len %d)' % (chunk_offset, chunk_len))
 		if verify:
 			self.verify_binary(ef, data, offset)
-		return res
+		return total_data, total_sw
 	def verify_binary(self, ef, data:str, offset:int=0):
 		"""Verify contents of transparent EF.

To view, visit
To unsubscribe, or for help writing mail filters, visit

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Icc240d5c8c04198640eb118565ea99f10ba27466
Gerrit-Change-Number: 25114
Gerrit-PatchSet: 1
Gerrit-Owner: andrew-ma <andrewm098 at>
Gerrit-Reviewer: Hoernchen <ewild at>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at>
Gerrit-Reviewer: laforge <laforge at>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the gerrit-log mailing list