Skip to content

Commit 2e6dc03

Browse files
committed
Allow update_binary function to write more than 255 bytes
The T0 protocol (selected in transport/pcsc.py) 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
1 parent bb73e51 commit 2e6dc03

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

pySim/commands.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,23 @@ def update_binary(self, ef, data:str, offset:int=0, verify:bool=False, conserve:
170170
return None, sw
171171

172172
self.select_path(ef)
173-
pdu = self.cla_byte + 'd6%04x%02x' % (offset, data_length) + data
174-
res = self._tp.send_apdu_checksw(pdu)
173+
total_data = ''
174+
total_sw = "9000"
175+
chunk_offset = offset
176+
while chunk_offset < data_length:
177+
chunk_len = min(255, data_length - chunk_offset)
178+
# chunk_offset is bytes, but data slicing is hex chars, so we need to multiply by 2
179+
pdu = self.cla_byte + 'd6%04x%02x' % (chunk_offset, chunk_len) + data[chunk_offset*2 : (chunk_offset+chunk_len)*2]
180+
chunk_data, chunk_sw = self._tp.send_apdu(pdu)
181+
if chunk_sw == total_sw:
182+
total_data += chunk_data
183+
chunk_offset += chunk_len
184+
else:
185+
total_sw = chunk_sw
186+
raise ValueError('Failed to write chunk (chunk_offset %d, chunk_len %d)' % (chunk_offset, chunk_len))
175187
if verify:
176188
self.verify_binary(ef, data, offset)
177-
return res
189+
return total_data, total_sw
178190

179191
def verify_binary(self, ef, data:str, offset:int=0):
180192
"""Verify contents of transparent EF.

0 commit comments

Comments
 (0)