blob: b46e62d6d13aad64ba30dde6d895f8534ed5aed8 [file] [log] [blame]
Alexander Chemeris067f69c2017-07-18 16:44:26 +03001# -*- coding: utf-8 -*-
2
Harald Welte14105dc2021-05-31 08:48:51 +02003# without this, pylint will fail when inner classes are used
4# within the 'nested' kwarg of our TlvMeta metaclass on python 3.7 :(
5# pylint: disable=undefined-variable
6
Harald Welteb2edd142021-01-08 23:29:35 +01007""" Various constants from ETSI TS 151.011 +
8Representation of the GSM SIM/USIM/ISIM filesystem hierarchy.
9
10The File (and its derived classes) uses the classes of pySim.filesystem in
11order to describe the files specified in the relevant ETSI + 3GPP specifications.
Alexander Chemeris067f69c2017-07-18 16:44:26 +030012"""
13
14#
15# Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com>
Harald Welteb2edd142021-01-08 23:29:35 +010016# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
Alexander Chemeris067f69c2017-07-18 16:44:26 +030017#
18# This program is free software: you can redistribute it and/or modify
19# it under the terms of the GNU General Public License as published by
20# the Free Software Foundation, either version 2 of the License, or
21# (at your option) any later version.
22#
23# This program is distributed in the hope that it will be useful,
24# but WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26# GNU General Public License for more details.
27#
28# You should have received a copy of the GNU General Public License
29# along with this program. If not, see <http://www.gnu.org/licenses/>.
30#
31
Harald Weltec91085e2022-02-10 18:05:45 +010032from pySim.profile import match_sim
33from pySim.profile import CardProfile
34from pySim.filesystem import *
35import enum
36from pySim.construct import *
37from construct import Optional as COptional
38from construct import *
39from struct import pack, unpack
40from typing import Tuple
41from pySim.tlv import *
42from pySim.utils import *
Alexander Chemeris067f69c2017-07-18 16:44:26 +030043MF_num = '3F00'
44
45DF_num = {
Harald Weltec91085e2022-02-10 18:05:45 +010046 'TELECOM': '7F10',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030047
Harald Weltec91085e2022-02-10 18:05:45 +010048 'GSM': '7F20',
49 'IS-41': '7F22',
50 'FP-CTS': '7F23',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030051
Harald Weltec91085e2022-02-10 18:05:45 +010052 'GRAPHICS': '5F50',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030053
Harald Weltec91085e2022-02-10 18:05:45 +010054 'IRIDIUM': '5F30',
55 'GLOBST': '5F31',
56 'ICO': '5F32',
57 'ACeS': '5F33',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030058
Harald Weltec91085e2022-02-10 18:05:45 +010059 'EIA/TIA-553': '5F40',
60 'CTS': '5F60',
61 'SOLSA': '5F70',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030062
Harald Weltec91085e2022-02-10 18:05:45 +010063 'MExE': '5F3C',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030064}
65
66EF_num = {
Harald Weltec91085e2022-02-10 18:05:45 +010067 # MF
68 'ICCID': '2FE2',
69 'ELP': '2F05',
70 'DIR': '2F00',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030071
Harald Weltec91085e2022-02-10 18:05:45 +010072 # DF_TELECOM
73 'ADN': '6F3A',
74 'FDN': '6F3B',
75 'SMS': '6F3C',
76 'CCP': '6F3D',
77 'MSISDN': '6F40',
78 'SMSP': '6F42',
79 'SMSS': '6F43',
80 'LND': '6F44',
81 'SMSR': '6F47',
82 'SDN': '6F49',
83 'EXT1': '6F4A',
84 'EXT2': '6F4B',
85 'EXT3': '6F4C',
86 'BDN': '6F4D',
87 'EXT4': '6F4E',
88 'CMI': '6F58',
89 'ECCP': '6F4F',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030090
Harald Weltec91085e2022-02-10 18:05:45 +010091 # DF_GRAPHICS
92 'IMG': '4F20',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030093
Harald Weltec91085e2022-02-10 18:05:45 +010094 # DF_SoLSA
95 'SAI': '4F30',
96 'SLL': '4F31',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030097
Harald Weltec91085e2022-02-10 18:05:45 +010098 # DF_MExE
99 'MExE-ST': '4F40',
100 'ORPK': '4F41',
101 'ARPK': '4F42',
102 'TPRPK': '4F43',
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300103
Harald Weltec91085e2022-02-10 18:05:45 +0100104 # DF_GSM
105 'LP': '6F05',
106 'IMSI': '6F07',
107 'Kc': '6F20',
108 'DCK': '6F2C',
109 'PLMNsel': '6F30',
110 'HPPLMN': '6F31',
111 'CNL': '6F32',
112 'ACMmax': '6F37',
113 'SST': '6F38',
114 'ACM': '6F39',
115 'GID1': '6F3E',
116 'GID2': '6F3F',
117 'PUCT': '6F41',
118 'CBMI': '6F45',
119 'SPN': '6F46',
120 'CBMID': '6F48',
121 'BCCH': '6F74',
122 'ACC': '6F78',
123 'FPLMN': '6F7B',
124 'LOCI': '6F7E',
125 'AD': '6FAD',
126 'PHASE': '6FAE',
127 'VGCS': '6FB1',
128 'VGCSS': '6FB2',
129 'VBS': '6FB3',
130 'VBSS': '6FB4',
131 'eMLPP': '6FB5',
132 'AAeM': '6FB6',
133 'ECC': '6FB7',
134 'CBMIR': '6F50',
135 'NIA': '6F51',
136 'KcGPRS': '6F52',
137 'LOCIGPRS': '6F53',
138 'SUME': '6F54',
139 'PLMNwAcT': '6F60',
140 'OPLMNwAcT': '6F61',
141 # Figure 8 names it HPLMNAcT, but in the text it's names it HPLMNwAcT
142 'HPLMNAcT': '6F62',
143 'HPLMNwAcT': '6F62',
144 'CPBCCH': '6F63',
145 'INVSCAN': '6F64',
146 'PNN': '6FC5',
147 'OPL': '6FC6',
148 'MBDN': '6FC7',
149 'EXT6': '6FC8',
150 'MBI': '6FC9',
151 'MWIS': '6FCA',
152 'CFIS': '6FCB',
153 'EXT7': '6FCC',
154 'SPDI': '6FCD',
155 'MMSN': '6FCE',
156 'EXT8': '6FCF',
157 'MMSICP': '6FD0',
158 'MMSUP': '6FD1',
159 'MMSUCP': '6FD2',
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300160}
161
162DF = {
Harald Weltec91085e2022-02-10 18:05:45 +0100163 'TELECOM': [MF_num, DF_num['TELECOM']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300164
Harald Weltec91085e2022-02-10 18:05:45 +0100165 'GSM': [MF_num, DF_num['GSM']],
166 'IS-41': [MF_num, DF_num['IS-41']],
167 'FP-CTS': [MF_num, DF_num['FP-CTS']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300168
Harald Weltec91085e2022-02-10 18:05:45 +0100169 'GRAPHICS': [MF_num, DF_num['GRAPHICS']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300170
Harald Weltec91085e2022-02-10 18:05:45 +0100171 'IRIDIUM': [MF_num, DF_num['IRIDIUM']],
172 'GLOBST': [MF_num, DF_num['GLOBST']],
173 'ICO': [MF_num, DF_num['ICO']],
174 'ACeS': [MF_num, DF_num['ACeS']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300175
Harald Weltec91085e2022-02-10 18:05:45 +0100176 'EIA/TIA-553': [MF_num, DF_num['EIA/TIA-553']],
177 'CTS': [MF_num, DF_num['CTS']],
178 'SoLSA': [MF_num, DF_num['SOLSA']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300179
Harald Weltec91085e2022-02-10 18:05:45 +0100180 'MExE': [MF_num, DF_num['MExE']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300181}
182
183
184EF = {
Harald Weltec91085e2022-02-10 18:05:45 +0100185 'ICCID': [MF_num, EF_num['ICCID']],
186 'ELP': [MF_num, EF_num['ELP']],
187 'DIR': [MF_num, EF_num['DIR']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300188
Harald Weltec91085e2022-02-10 18:05:45 +0100189 'ADN': DF['TELECOM']+[EF_num['ADN']],
190 'FDN': DF['TELECOM']+[EF_num['FDN']],
191 'SMS': DF['TELECOM']+[EF_num['SMS']],
192 'CCP': DF['TELECOM']+[EF_num['CCP']],
193 'MSISDN': DF['TELECOM']+[EF_num['MSISDN']],
194 'SMSP': DF['TELECOM']+[EF_num['SMSP']],
195 'SMSS': DF['TELECOM']+[EF_num['SMSS']],
196 'LND': DF['TELECOM']+[EF_num['LND']],
197 'SMSR': DF['TELECOM']+[EF_num['SMSR']],
198 'SDN': DF['TELECOM']+[EF_num['SDN']],
199 'EXT1': DF['TELECOM']+[EF_num['EXT1']],
200 'EXT2': DF['TELECOM']+[EF_num['EXT2']],
201 'EXT3': DF['TELECOM']+[EF_num['EXT3']],
202 'BDN': DF['TELECOM']+[EF_num['BDN']],
203 'EXT4': DF['TELECOM']+[EF_num['EXT4']],
204 'CMI': DF['TELECOM']+[EF_num['CMI']],
205 'ECCP': DF['TELECOM']+[EF_num['ECCP']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300206
Harald Weltec91085e2022-02-10 18:05:45 +0100207 'IMG': DF['GRAPHICS']+[EF_num['IMG']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300208
Harald Weltec91085e2022-02-10 18:05:45 +0100209 'SAI': DF['SoLSA']+[EF_num['SAI']],
210 'SLL': DF['SoLSA']+[EF_num['SLL']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300211
Harald Weltec91085e2022-02-10 18:05:45 +0100212 'MExE-ST': DF['MExE']+[EF_num['MExE-ST']],
213 'ORPK': DF['MExE']+[EF_num['ORPK']],
214 'ARPK': DF['MExE']+[EF_num['ARPK']],
215 'TPRPK': DF['MExE']+[EF_num['TPRPK']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300216
Harald Weltec91085e2022-02-10 18:05:45 +0100217 'LP': DF['GSM']+[EF_num['LP']],
218 'IMSI': DF['GSM']+[EF_num['IMSI']],
219 'Kc': DF['GSM']+[EF_num['Kc']],
220 'DCK': DF['GSM']+[EF_num['DCK']],
221 'PLMNsel': DF['GSM']+[EF_num['PLMNsel']],
222 'HPPLMN': DF['GSM']+[EF_num['HPPLMN']],
223 'CNL': DF['GSM']+[EF_num['CNL']],
224 'ACMmax': DF['GSM']+[EF_num['ACMmax']],
225 'SST': DF['GSM']+[EF_num['SST']],
226 'ACM': DF['GSM']+[EF_num['ACM']],
227 'GID1': DF['GSM']+[EF_num['GID1']],
228 'GID2': DF['GSM']+[EF_num['GID2']],
229 'PUCT': DF['GSM']+[EF_num['PUCT']],
230 'CBMI': DF['GSM']+[EF_num['CBMI']],
231 'SPN': DF['GSM']+[EF_num['SPN']],
232 'CBMID': DF['GSM']+[EF_num['CBMID']],
233 'BCCH': DF['GSM']+[EF_num['BCCH']],
234 'ACC': DF['GSM']+[EF_num['ACC']],
235 'FPLMN': DF['GSM']+[EF_num['FPLMN']],
236 'LOCI': DF['GSM']+[EF_num['LOCI']],
237 'AD': DF['GSM']+[EF_num['AD']],
238 'PHASE': DF['GSM']+[EF_num['PHASE']],
239 'VGCS': DF['GSM']+[EF_num['VGCS']],
240 'VGCSS': DF['GSM']+[EF_num['VGCSS']],
241 'VBS': DF['GSM']+[EF_num['VBS']],
242 'VBSS': DF['GSM']+[EF_num['VBSS']],
243 'eMLPP': DF['GSM']+[EF_num['eMLPP']],
244 'AAeM': DF['GSM']+[EF_num['AAeM']],
245 'ECC': DF['GSM']+[EF_num['ECC']],
246 'CBMIR': DF['GSM']+[EF_num['CBMIR']],
247 'NIA': DF['GSM']+[EF_num['NIA']],
248 'KcGPRS': DF['GSM']+[EF_num['KcGPRS']],
249 'LOCIGPRS': DF['GSM']+[EF_num['LOCIGPRS']],
250 'SUME': DF['GSM']+[EF_num['SUME']],
251 'PLMNwAcT': DF['GSM']+[EF_num['PLMNwAcT']],
252 'OPLMNwAcT': DF['GSM']+[EF_num['OPLMNwAcT']],
253 # Figure 8 names it HPLMNAcT, but in the text it's names it HPLMNwAcT
254 'HPLMNAcT': DF['GSM']+[EF_num['HPLMNAcT']],
255 'HPLMNwAcT': DF['GSM']+[EF_num['HPLMNAcT']],
256 'CPBCCH': DF['GSM']+[EF_num['CPBCCH']],
257 'INVSCAN': DF['GSM']+[EF_num['INVSCAN']],
258 'PNN': DF['GSM']+[EF_num['PNN']],
259 'OPL': DF['GSM']+[EF_num['OPL']],
260 'MBDN': DF['GSM']+[EF_num['MBDN']],
261 'EXT6': DF['GSM']+[EF_num['EXT6']],
262 'MBI': DF['GSM']+[EF_num['MBI']],
263 'MWIS': DF['GSM']+[EF_num['MWIS']],
264 'CFIS': DF['GSM']+[EF_num['CFIS']],
265 'EXT7': DF['GSM']+[EF_num['EXT7']],
266 'SPDI': DF['GSM']+[EF_num['SPDI']],
267 'MMSN': DF['GSM']+[EF_num['MMSN']],
268 'EXT8': DF['GSM']+[EF_num['EXT8']],
269 'MMSICP': DF['GSM']+[EF_num['MMSICP']],
270 'MMSUP': DF['GSM']+[EF_num['MMSUP']],
271 'MMSUCP': DF['GSM']+[EF_num['MMSUCP']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300272}
Supreeth Herlebf5d6022020-03-20 15:18:27 +0100273
274# Mapping between SIM Service Number and its description
275EF_SST_map = {
Harald Weltec91085e2022-02-10 18:05:45 +0100276 1: 'CHV1 disable function',
277 2: 'Abbreviated Dialling Numbers (ADN)',
278 3: 'Fixed Dialling Numbers (FDN)',
279 4: 'Short Message Storage (SMS)',
280 5: 'Advice of Charge (AoC)',
281 6: 'Capability Configuration Parameters (CCP)',
282 7: 'PLMN selector',
283 8: 'RFU',
284 9: 'MSISDN',
285 10: 'Extension1',
286 11: 'Extension2',
287 12: 'SMS Parameters',
288 13: 'Last Number Dialled (LND)',
289 14: 'Cell Broadcast Message Identifier',
290 15: 'Group Identifier Level 1',
291 16: 'Group Identifier Level 2',
292 17: 'Service Provider Name',
293 18: 'Service Dialling Numbers (SDN)',
294 19: 'Extension3',
295 20: 'RFU',
296 21: 'VGCS Group Identifier List (EFVGCS and EFVGCSS)',
297 22: 'VBS Group Identifier List (EFVBS and EFVBSS)',
298 23: 'enhanced Multi-Level Precedence and Pre-emption Service',
299 24: 'Automatic Answer for eMLPP',
300 25: 'Data download via SMS-CB',
301 26: 'Data download via SMS-PP',
302 27: 'Menu selection',
303 28: 'Call control',
304 29: 'Proactive SIM',
305 30: 'Cell Broadcast Message Identifier Ranges',
306 31: 'Barred Dialling Numbers (BDN)',
307 32: 'Extension4',
308 33: 'De-personalization Control Keys',
309 34: 'Co-operative Network List',
310 35: 'Short Message Status Reports',
311 36: 'Network\'s indication of alerting in the MS',
312 37: 'Mobile Originated Short Message control by SIM',
313 38: 'GPRS',
314 39: 'Image (IMG)',
315 40: 'SoLSA (Support of Local Service Area)',
316 41: 'USSD string data object supported in Call Control',
317 42: 'RUN AT COMMAND command',
318 43: 'User controlled PLMN Selector with Access Technology',
319 44: 'Operator controlled PLMN Selector with Access Technology',
320 45: 'HPLMN Selector with Access Technology',
321 46: 'CPBCCH Information',
322 47: 'Investigation Scan',
323 48: 'Extended Capability Configuration Parameters',
324 49: 'MExE',
325 50: 'Reserved and shall be ignored',
326 51: 'PLMN Network Name',
327 52: 'Operator PLMN List',
328 53: 'Mailbox Dialling Numbers',
329 54: 'Message Waiting Indication Status',
330 55: 'Call Forwarding Indication Status',
331 56: 'Service Provider Display Information',
332 57: 'Multimedia Messaging Service (MMS)',
333 58: 'Extension 8',
334 59: 'MMS User Connectivity Parameters',
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +0700335}
336
Harald Welteb2edd142021-01-08 23:29:35 +0100337
338######################################################################
339# DF.TELECOM
340######################################################################
341
342# TS 51.011 Section 10.5.1
Harald Weltec91085e2022-02-10 18:05:45 +0100343
Harald Welteb2edd142021-01-08 23:29:35 +0100344class EF_ADN(LinFixedEF):
345 def __init__(self, fid='6f3a', sfid=None, name='EF.ADN', desc='Abbreviated Dialing Numbers'):
Harald Weltec91085e2022-02-10 18:05:45 +0100346 super().__init__(fid, sfid=sfid, name=name,
347 desc=desc, rec_len={14, 30})
348
Harald Welteb2edd142021-01-08 23:29:35 +0100349 def _decode_record_bin(self, raw_bin_data):
350 alpha_id_len = len(raw_bin_data) - 14
351 alpha_id = raw_bin_data[:alpha_id_len]
352 u = unpack('!BB10sBB', raw_bin_data[-14:])
353 return {'alpha_id': alpha_id, 'len_of_bcd': u[0], 'ton_npi': u[1],
354 'dialing_nr': u[2], 'cap_conf_id': u[3], 'ext1_record_id': u[4]}
355
356# TS 51.011 Section 10.5.5
Harald Weltec91085e2022-02-10 18:05:45 +0100357
358
Harald Welteec7d0da2021-04-02 22:01:19 +0200359class EF_SMS(LinFixedEF):
360 def __init__(self, fid='6f3c', sfid=None, name='EF.SMS', desc='Short messages'):
Harald Weltec91085e2022-02-10 18:05:45 +0100361 super().__init__(fid, sfid=sfid, name=name,
362 desc=desc, rec_len={176, 176})
363
Harald Welteec7d0da2021-04-02 22:01:19 +0200364 def _decode_record_bin(self, raw_bin_data):
365 def decode_status(status):
366 if status & 0x01 == 0x00:
367 return (None, 'free_space')
368 elif status & 0x07 == 0x01:
369 return ('mt', 'message_read')
370 elif status & 0x07 == 0x03:
371 return ('mt', 'message_to_be_read')
372 elif status & 0x07 == 0x07:
373 return ('mo', 'message_to_be_sent')
374 elif status & 0x1f == 0x05:
375 return ('mo', 'sent_status_not_requested')
376 elif status & 0x1f == 0x0d:
377 return ('mo', 'sent_status_req_but_not_received')
378 elif status & 0x1f == 0x15:
379 return ('mo', 'sent_status_req_rx_not_stored_smsr')
380 elif status & 0x1f == 0x1d:
381 return ('mo', 'sent_status_req_rx_stored_smsr')
382 else:
383 return (None, 'rfu')
384
385 status = decode_status(raw_bin_data[0])
386 remainder = raw_bin_data[1:]
387 return {'direction': status[0], 'status': status[1], 'remainder': b2h(remainder)}
388
389
390# TS 51.011 Section 10.5.5
Harald Welteb2edd142021-01-08 23:29:35 +0100391class EF_MSISDN(LinFixedEF):
Harald Welteec7d0da2021-04-02 22:01:19 +0200392 def __init__(self, fid='6f40', sfid=None, name='EF.MSISDN', desc='MSISDN'):
Harald Weltec91085e2022-02-10 18:05:45 +0100393 super().__init__(fid, sfid=sfid, name=name,
394 desc=desc, rec_len={15, 34})
395
Harald Welteb2edd142021-01-08 23:29:35 +0100396 def _decode_record_hex(self, raw_hex_data):
397 return {'msisdn': dec_msisdn(raw_hex_data)}
Harald Weltec91085e2022-02-10 18:05:45 +0100398
Harald Welteb2edd142021-01-08 23:29:35 +0100399 def _encode_record_hex(self, abstract):
Philipp Maierf9cbe092021-04-23 19:37:36 +0200400 msisdn = abstract['msisdn']
401 if type(msisdn) == str:
402 encoded_msisdn = enc_msisdn(msisdn)
403 else:
Harald Weltec91085e2022-02-10 18:05:45 +0100404 encoded_msisdn = enc_msisdn(msisdn[2], msisdn[0], msisdn[1])
405 alpha_identifier = (list(self.rec_len)[
406 0] - len(encoded_msisdn) // 2) * "ff"
Philipp Maierb46cb3f2021-04-20 22:38:21 +0200407 return alpha_identifier + encoded_msisdn
Harald Welteb2edd142021-01-08 23:29:35 +0100408
409# TS 51.011 Section 10.5.6
Harald Weltec91085e2022-02-10 18:05:45 +0100410
411
Harald Welteb2edd142021-01-08 23:29:35 +0100412class EF_SMSP(LinFixedEF):
413 def __init__(self, fid='6f42', sfid=None, name='EF.SMSP', desc='Short message service parameters'):
Harald Weltec91085e2022-02-10 18:05:45 +0100414 super().__init__(fid, sfid=sfid, name=name,
415 desc=desc, rec_len={28, None})
Harald Welteb2edd142021-01-08 23:29:35 +0100416
Harald Welte790b2702021-04-11 00:01:35 +0200417# TS 51.011 Section 10.5.7
Harald Weltec91085e2022-02-10 18:05:45 +0100418
419
Harald Welte790b2702021-04-11 00:01:35 +0200420class EF_SMSS(TransparentEF):
421 class MemCapAdapter(Adapter):
422 def _decode(self, obj, context, path):
423 return False if obj & 1 else True
Harald Weltec91085e2022-02-10 18:05:45 +0100424
Harald Welte790b2702021-04-11 00:01:35 +0200425 def _encode(self, obj, context, path):
426 return 0 if obj else 1
Harald Weltec91085e2022-02-10 18:05:45 +0100427
428 def __init__(self, fid='6f43', sfid=None, name='EF.SMSS', desc='SMS status', size={2, 8}):
Harald Welte790b2702021-04-11 00:01:35 +0200429 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100430 self._construct = Struct(
431 'last_used_tpmr'/Int8ub, 'memory_capacity_exceeded'/self.MemCapAdapter(Int8ub))
Harald Welte790b2702021-04-11 00:01:35 +0200432
433# TS 51.011 Section 10.5.8
Harald Weltec91085e2022-02-10 18:05:45 +0100434
435
Harald Welte790b2702021-04-11 00:01:35 +0200436class EF_SMSR(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100437 def __init__(self, fid='6f47', sfid=None, name='EF.SMSR', desc='SMS status reports', rec_len={30, 30}):
Harald Welte790b2702021-04-11 00:01:35 +0200438 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100439 self._construct = Struct(
440 'sms_record_id'/Int8ub, 'sms_status_report'/HexAdapter(Bytes(29)))
441
Harald Welte790b2702021-04-11 00:01:35 +0200442
443class EF_EXT(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100444 def __init__(self, fid, sfid=None, name='EF.EXT', desc='Extension', rec_len={13, 13}):
Harald Welte790b2702021-04-11 00:01:35 +0200445 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100446 self._construct = Struct(
447 'record_type'/Int8ub, 'extension_data'/HexAdapter(Bytes(11)), 'identifier'/Int8ub)
Harald Welte790b2702021-04-11 00:01:35 +0200448
449# TS 51.011 Section 10.5.16
Harald Weltec91085e2022-02-10 18:05:45 +0100450
451
Harald Welte790b2702021-04-11 00:01:35 +0200452class EF_CMI(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100453 def __init__(self, fid='6f58', sfid=None, name='EF.CMI', rec_len={2, 21},
Harald Weltec9cdce32021-04-11 10:28:28 +0200454 desc='Comparison Method Information'):
Harald Welte790b2702021-04-11 00:01:35 +0200455 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100456 self._construct = Struct(
457 'alpha_id'/Bytes(this._.total_len-1), 'comparison_method_id'/Int8ub)
458
Harald Welte790b2702021-04-11 00:01:35 +0200459
Harald Welteb2edd142021-01-08 23:29:35 +0100460class DF_TELECOM(CardDF):
461 def __init__(self, fid='7f10', name='DF.TELECOM', desc=None):
462 super().__init__(fid=fid, name=name, desc=desc)
463 files = [
Harald Weltec91085e2022-02-10 18:05:45 +0100464 EF_ADN(),
465 EF_ADN(fid='6f3b', name='EF.FDN', desc='Fixed dialling numbers'),
466 EF_SMS(),
467 LinFixedEF(fid='6f3d', name='EF.CCP',
468 desc='Capability Configuration Parameters', rec_len={14, 14}),
469 LinFixedEF(fid='6f4f', name='EF.ECCP',
470 desc='Extended Capability Configuration Parameters', rec_len={15, 32}),
471 EF_MSISDN(),
472 EF_SMSP(),
473 EF_SMSS(),
474 # LND, SDN
475 EF_EXT('6f4a', None, 'EF.EXT1', 'Extension1 (ADN/SSC)'),
476 EF_EXT('6f4b', None, 'EF.EXT2', 'Extension2 (FDN/SSC)'),
477 EF_EXT('6f4c', None, 'EF.EXT3', 'Extension3 (SDN)'),
478 EF_ADN(fid='6f4d', name='EF.BDN', desc='Barred Dialling Numbers'),
479 EF_EXT('6f4e', None, 'EF.EXT4', 'Extension4 (BDN/SSC)'),
480 EF_SMSR(),
481 EF_CMI(),
482 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100483 self.add_files(files)
484
Harald Welteb2edd142021-01-08 23:29:35 +0100485######################################################################
486# DF.GSM
487######################################################################
488
489# TS 51.011 Section 10.3.1
Harald Weltec91085e2022-02-10 18:05:45 +0100490
491
Harald Welteb2edd142021-01-08 23:29:35 +0100492class EF_LP(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100493 def __init__(self, fid='6f05', sfid=None, name='EF.LP', size={1, None}, rec_len=1,
Harald Welteb2edd142021-01-08 23:29:35 +0100494 desc='Language Preference'):
495 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100496
Harald Welteb2edd142021-01-08 23:29:35 +0100497 def _decode_record_bin(self, in_bin):
498 return b2h(in_bin)
Harald Weltec91085e2022-02-10 18:05:45 +0100499
Harald Welteb2edd142021-01-08 23:29:35 +0100500 def _encode_record_bin(self, in_json):
501 return h2b(in_json)
502
503# TS 51.011 Section 10.3.2
Harald Weltec91085e2022-02-10 18:05:45 +0100504
505
Harald Welteb2edd142021-01-08 23:29:35 +0100506class EF_IMSI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100507 def __init__(self, fid='6f07', sfid=None, name='EF.IMSI', desc='IMSI', size={9, 9}):
Harald Welteb2edd142021-01-08 23:29:35 +0100508 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Bjoern Riemere91405e2021-12-17 15:16:35 +0100509 # add those commands to the general commands of a TransparentEF
510 self.shell_commands += [self.AddlShellCommands(self)]
Harald Weltec91085e2022-02-10 18:05:45 +0100511
Harald Welteb2edd142021-01-08 23:29:35 +0100512 def _decode_hex(self, raw_hex):
513 return {'imsi': dec_imsi(raw_hex)}
Harald Weltec91085e2022-02-10 18:05:45 +0100514
Harald Welteb2edd142021-01-08 23:29:35 +0100515 def _encode_hex(self, abstract):
516 return enc_imsi(abstract['imsi'])
Harald Weltec91085e2022-02-10 18:05:45 +0100517
Bjoern Riemere91405e2021-12-17 15:16:35 +0100518 @with_default_category('File-Specific Commands')
519 class AddlShellCommands(CommandSet):
Harald Weltec91085e2022-02-10 18:05:45 +0100520 def __init__(self, ef: TransparentEF):
Bjoern Riemere91405e2021-12-17 15:16:35 +0100521 super().__init__()
Harald Weltec91085e2022-02-10 18:05:45 +0100522 self._ef = ef
Bjoern Riemere91405e2021-12-17 15:16:35 +0100523
524 def do_update_imsi_plmn(self, arg: str):
525 """Change the plmn part of the IMSI"""
526 plmn = arg.strip()
527 if len(plmn) == 5 or len(plmn) == 6:
528 (data, sw) = self._cmd.rs.read_binary_dec()
529 if sw == '9000' and len(data['imsi'])-len(plmn) == 10:
530 imsi = data['imsi']
531 msin = imsi[len(plmn):]
Harald Weltec91085e2022-02-10 18:05:45 +0100532 (data, sw) = self._cmd.rs.update_binary_dec(
533 {'imsi': plmn+msin})
Bjoern Riemere91405e2021-12-17 15:16:35 +0100534 if sw == '9000' and data:
Harald Weltec91085e2022-02-10 18:05:45 +0100535 self._cmd.poutput_json(
536 self._cmd.rs.selected_file.decode_hex(data))
Bjoern Riemere91405e2021-12-17 15:16:35 +0100537 else:
538 raise ValueError("PLMN length does not match IMSI length")
539 else:
540 raise ValueError("PLMN has wrong length!")
541
Harald Welteb2edd142021-01-08 23:29:35 +0100542
543# TS 51.011 Section 10.3.4
544class EF_PLMNsel(TransRecEF):
545 def __init__(self, fid='6f30', sfid=None, name='EF.PLMNsel', desc='PLMN selector',
Harald Weltec91085e2022-02-10 18:05:45 +0100546 size={24, None}, rec_len=3):
Harald Welteb2edd142021-01-08 23:29:35 +0100547 super().__init__(fid, name=name, sfid=sfid, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100548
Harald Welteb2edd142021-01-08 23:29:35 +0100549 def _decode_record_hex(self, in_hex):
550 if in_hex[:6] == "ffffff":
551 return None
552 else:
553 return dec_plmn(in_hex)
Harald Weltec91085e2022-02-10 18:05:45 +0100554
Harald Welteb2edd142021-01-08 23:29:35 +0100555 def _encode_record_hex(self, in_json):
556 if in_json == None:
557 return "ffffff"
558 else:
559 return enc_plmn(in_json['mcc'], in_json['mnc'])
560
Harald Welte790b2702021-04-11 00:01:35 +0200561# TS 51.011 Section 10.3.6
Harald Weltec91085e2022-02-10 18:05:45 +0100562
563
Harald Welte790b2702021-04-11 00:01:35 +0200564class EF_ACMmax(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100565 def __init__(self, fid='6f37', sfid=None, name='EF.ACMmax', size={3, 3},
Harald Welte790b2702021-04-11 00:01:35 +0200566 desc='ACM maximum value'):
567 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
568 self._construct = Struct('acm_max'/Int24ub)
569
Harald Welteb2edd142021-01-08 23:29:35 +0100570# TS 51.011 Section 10.3.7
Harald Weltec91085e2022-02-10 18:05:45 +0100571
572
Harald Welteb2edd142021-01-08 23:29:35 +0100573class EF_ServiceTable(TransparentEF):
574 def __init__(self, fid, sfid, name, desc, size, table):
575 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
576 self.table = table
Harald Weltec91085e2022-02-10 18:05:45 +0100577
Harald Welte7a8aa862021-10-14 20:46:19 +0200578 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +0100579 def _bit_byte_offset_for_service(service: int) -> Tuple[int, int]:
Harald Welte7a8aa862021-10-14 20:46:19 +0200580 i = service - 1
581 byte_offset = i//4
582 bit_offset = (i % 4) * 2
583 return (byte_offset, bit_offset)
Harald Weltec91085e2022-02-10 18:05:45 +0100584
Harald Welteb2edd142021-01-08 23:29:35 +0100585 def _decode_bin(self, raw_bin):
586 ret = {}
587 for i in range(0, len(raw_bin)*4):
588 service_nr = i+1
589 byte = int(raw_bin[i//4])
590 bit_offset = (i % 4) * 2
591 bits = (byte >> bit_offset) & 3
592 ret[service_nr] = {
Harald Weltec91085e2022-02-10 18:05:45 +0100593 'description': self.table[service_nr] if service_nr in self.table else None,
594 'allocated': True if bits & 1 else False,
595 'activated': True if bits & 2 else False,
596 }
Harald Welteb2edd142021-01-08 23:29:35 +0100597 return ret
Harald Weltec91085e2022-02-10 18:05:45 +0100598
Harald Welte7a8aa862021-10-14 20:46:19 +0200599 def _encode_bin(self, in_json):
600 # compute the required binary size
601 bin_len = 0
602 for srv in in_json.keys():
603 service_nr = int(srv)
Harald Weltec91085e2022-02-10 18:05:45 +0100604 (byte_offset, bit_offset) = EF_ServiceTable._bit_byte_offset_for_service(
605 service_nr)
Harald Welte7a8aa862021-10-14 20:46:19 +0200606 if byte_offset >= bin_len:
607 bin_len = byte_offset+1
608 # encode the actual data
609 out = bytearray(b'\x00' * bin_len)
610 for srv in in_json.keys():
611 service_nr = int(srv)
Harald Weltec91085e2022-02-10 18:05:45 +0100612 (byte_offset, bit_offset) = EF_ServiceTable._bit_byte_offset_for_service(
613 service_nr)
Harald Welte7a8aa862021-10-14 20:46:19 +0200614 bits = 0
615 if in_json[srv]['allocated'] == True:
616 bits |= 1
617 if in_json[srv]['activated'] == True:
618 bits |= 2
619 out[byte_offset] |= ((bits & 3) << bit_offset)
620 return out
Harald Welteb2edd142021-01-08 23:29:35 +0100621
622# TS 51.011 Section 10.3.11
Harald Weltec91085e2022-02-10 18:05:45 +0100623
624
Harald Welteb2edd142021-01-08 23:29:35 +0100625class EF_SPN(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100626 def __init__(self, fid='6f46', sfid=None, name='EF.SPN', desc='Service Provider Name', size={17, 17}):
Harald Welteb2edd142021-01-08 23:29:35 +0100627 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Robert Falkenbergb07a3e92021-05-07 15:23:20 +0200628 self._construct = BitStruct(
629 # Byte 1
630 'rfu'/BitsRFU(6),
631 'hide_in_oplmn'/Flag,
632 'show_in_hplmn'/Flag,
633 # Bytes 2..17
634 'spn'/Bytewise(GsmString(16))
635 )
Harald Welteb2edd142021-01-08 23:29:35 +0100636
637# TS 51.011 Section 10.3.13
Harald Weltec91085e2022-02-10 18:05:45 +0100638
639
Harald Welteb2edd142021-01-08 23:29:35 +0100640class EF_CBMI(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100641 def __init__(self, fid='6f45', sfid=None, name='EF.CBMI', size={2, None}, rec_len=2,
Harald Welteb2edd142021-01-08 23:29:35 +0100642 desc='Cell Broadcast message identifier selection'):
643 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Welte14105dc2021-05-31 08:48:51 +0200644 self._construct = GreedyRange(Int16ub)
Harald Welteb2edd142021-01-08 23:29:35 +0100645
646# TS 51.011 Section 10.3.15
Harald Weltec91085e2022-02-10 18:05:45 +0100647
648
Harald Welteb2edd142021-01-08 23:29:35 +0100649class EF_ACC(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100650 def __init__(self, fid='6f78', sfid=None, name='EF.ACC', desc='Access Control Class', size={2, 2}):
Harald Welteb2edd142021-01-08 23:29:35 +0100651 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100652
Harald Welteb2edd142021-01-08 23:29:35 +0100653 def _decode_bin(self, raw_bin):
654 return {'acc': unpack('!H', raw_bin)[0]}
Harald Weltec91085e2022-02-10 18:05:45 +0100655
Harald Welteb2edd142021-01-08 23:29:35 +0100656 def _encode_bin(self, abstract):
657 return pack('!H', abstract['acc'])
658
Harald Welte790b2702021-04-11 00:01:35 +0200659# TS 51.011 Section 10.3.16
Harald Weltec91085e2022-02-10 18:05:45 +0100660
661
Harald Welte790b2702021-04-11 00:01:35 +0200662class EF_LOCI(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100663 def __init__(self, fid='6f7e', sfid=None, name='EF.LOCI', desc='Location Information', size={11, 11}):
Harald Welte790b2702021-04-11 00:01:35 +0200664 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
665 self._construct = Struct('tmsi'/Bytes(4), 'lai'/Bytes(5), 'tmsi_time'/Int8ub,
666 'lu_status'/Enum(Byte, updated=0, not_updated=1, plmn_not_allowed=2,
667 location_area_not_allowed=3))
668
Harald Welteb2edd142021-01-08 23:29:35 +0100669# TS 51.011 Section 10.3.18
Harald Weltec91085e2022-02-10 18:05:45 +0100670
671
Harald Welteb2edd142021-01-08 23:29:35 +0100672class EF_AD(TransparentEF):
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200673 class OP_MODE(enum.IntEnum):
Harald Weltec91085e2022-02-10 18:05:45 +0100674 normal = 0x00
675 type_approval = 0x80
676 normal_and_specific_facilities = 0x01
677 type_approval_and_specific_facilities = 0x81
678 maintenance_off_line = 0x02
679 cell_test = 0x04
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200680 #OP_MODE_DICT = {int(v) : str(v) for v in EF_AD.OP_MODE}
681 #OP_MODE_DICT_REVERSED = {str(v) : int(v) for v in EF_AD.OP_MODE}
682
Harald Weltec91085e2022-02-10 18:05:45 +0100683 def __init__(self, fid='6fad', sfid=None, name='EF.AD', desc='Administrative Data', size={3, 4}):
Harald Welteb2edd142021-01-08 23:29:35 +0100684 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200685 self._construct = BitStruct(
686 # Byte 1
687 'ms_operation_mode'/Bytewise(Enum(Byte, EF_AD.OP_MODE)),
688 # Byte 2
689 'rfu1'/Bytewise(ByteRFU),
690 # Byte 3
691 'rfu2'/BitsRFU(7),
692 'ofm'/Flag,
693 # Byte 4 (optional),
694 'extensions'/COptional(Struct(
695 'rfu3'/BitsRFU(4),
696 'mnc_len'/BitsInteger(4),
697 # Byte 5..N-4 (optional, RFU)
698 'extensions'/Bytewise(GreedyBytesRFU)
699 ))
700 )
Harald Welteb2edd142021-01-08 23:29:35 +0100701
Harald Welte790b2702021-04-11 00:01:35 +0200702# TS 51.011 Section 10.3.20 / 10.3.22
Harald Weltec91085e2022-02-10 18:05:45 +0100703
704
Harald Welte790b2702021-04-11 00:01:35 +0200705class EF_VGCS(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100706 def __init__(self, fid='6fb1', sfid=None, name='EF.VGCS', size={4, 200}, rec_len=4,
Harald Welte790b2702021-04-11 00:01:35 +0200707 desc='Voice Group Call Service'):
708 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
709 self._construct = BcdAdapter(Bytes(4))
710
711# TS 51.011 Section 10.3.21 / 10.3.23
Harald Weltec91085e2022-02-10 18:05:45 +0100712
713
Harald Welte790b2702021-04-11 00:01:35 +0200714class EF_VGCSS(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100715 def __init__(self, fid='6fb2', sfid=None, name='EF.VGCSS', size={7, 7},
Harald Welte790b2702021-04-11 00:01:35 +0200716 desc='Voice Group Call Service Status'):
717 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100718 self._construct = BitStruct(
719 'flags'/Bit[50], Padding(6, pattern=b'\xff'))
Harald Welte790b2702021-04-11 00:01:35 +0200720
721# TS 51.011 Section 10.3.24
Harald Weltec91085e2022-02-10 18:05:45 +0100722
723
Harald Welte790b2702021-04-11 00:01:35 +0200724class EF_eMLPP(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100725 def __init__(self, fid='6fb5', sfid=None, name='EF.eMLPP', size={2, 2},
Harald Welte790b2702021-04-11 00:01:35 +0200726 desc='enhanced Multi Level Pre-emption and Priority'):
727 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100728 FlagsConstruct = FlagsEnum(
729 Byte, A=1, B=2, zero=4, one=8, two=16, three=32, four=64)
730 self._construct = Struct(
731 'levels'/FlagsConstruct, 'fast_call_setup_cond'/FlagsConstruct)
Harald Welte790b2702021-04-11 00:01:35 +0200732
733# TS 51.011 Section 10.3.25
Harald Weltec91085e2022-02-10 18:05:45 +0100734
735
Harald Welte790b2702021-04-11 00:01:35 +0200736class EF_AAeM(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100737 def __init__(self, fid='6fb6', sfid=None, name='EF.AAeM', size={1, 1},
Harald Welte790b2702021-04-11 00:01:35 +0200738 desc='Automatic Answer for eMLPP Service'):
739 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100740 FlagsConstruct = FlagsEnum(
741 Byte, A=1, B=2, zero=4, one=8, two=16, three=32, four=64)
Harald Welte790b2702021-04-11 00:01:35 +0200742 self._construct = Struct('auto_answer_prio_levels'/FlagsConstruct)
743
744# TS 51.011 Section 10.3.26
Harald Weltec91085e2022-02-10 18:05:45 +0100745
746
Harald Welteb2edd142021-01-08 23:29:35 +0100747class EF_CBMID(EF_CBMI):
Harald Weltec91085e2022-02-10 18:05:45 +0100748 def __init__(self, fid='6f48', sfid=None, name='EF.CBMID', size={2, None}, rec_len=2,
Harald Welteb2edd142021-01-08 23:29:35 +0100749 desc='Cell Broadcast Message Identifier for Data Download'):
750 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Welte14105dc2021-05-31 08:48:51 +0200751 self._construct = GreedyRange(Int16ub)
Harald Welteb2edd142021-01-08 23:29:35 +0100752
Harald Welte89e59542021-04-02 21:33:13 +0200753# TS 51.011 Section 10.3.27
Harald Weltec91085e2022-02-10 18:05:45 +0100754
755
Harald Welte89e59542021-04-02 21:33:13 +0200756class EF_ECC(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100757 def __init__(self, fid='6fb7', sfid=None, name='EF.ECC', size={3, 15}, rec_len=3,
Harald Welte89e59542021-04-02 21:33:13 +0200758 desc='Emergency Call Codes'):
759 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Welte14105dc2021-05-31 08:48:51 +0200760 self._construct = GreedyRange(BcdAdapter(Bytes(3)))
Harald Welteb2edd142021-01-08 23:29:35 +0100761
762# TS 51.011 Section 10.3.28
Harald Weltec91085e2022-02-10 18:05:45 +0100763
764
Harald Welteb2edd142021-01-08 23:29:35 +0100765class EF_CBMIR(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100766 def __init__(self, fid='6f50', sfid=None, name='EF.CBMIR', size={4, None}, rec_len=4,
Harald Welteb2edd142021-01-08 23:29:35 +0100767 desc='Cell Broadcast message identifier range selection'):
768 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Welte14105dc2021-05-31 08:48:51 +0200769 self._construct = GreedyRange(Struct('lower'/Int16ub, 'upper'/Int16ub))
Harald Welteb2edd142021-01-08 23:29:35 +0100770
Harald Welte790b2702021-04-11 00:01:35 +0200771# TS 51.011 Section 10.3.29
Harald Weltec91085e2022-02-10 18:05:45 +0100772
773
Harald Welte790b2702021-04-11 00:01:35 +0200774class EF_DCK(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100775 def __init__(self, fid='6f2c', sfid=None, name='EF.DCK', size={16, 16},
Harald Welte790b2702021-04-11 00:01:35 +0200776 desc='Depersonalisation Control Keys'):
777 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
778 self._construct = Struct('network'/BcdAdapter(Bytes(4)),
779 'network_subset'/BcdAdapter(Bytes(4)),
780 'service_provider'/BcdAdapter(Bytes(4)),
781 'corporate'/BcdAdapter(Bytes(4)))
782# TS 51.011 Section 10.3.30
Harald Weltec91085e2022-02-10 18:05:45 +0100783
784
Harald Welte790b2702021-04-11 00:01:35 +0200785class EF_CNL(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100786 def __init__(self, fid='6f32', sfid=None, name='EF.CNL', size={6, None}, rec_len=6,
Harald Welte790b2702021-04-11 00:01:35 +0200787 desc='Co-operative Network List'):
788 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100789
Harald Welte790b2702021-04-11 00:01:35 +0200790 def _decode_record_hex(self, in_hex):
791 (in_plmn, sub, svp, corp) = unpack('!3sBBB', h2b(in_hex))
792 res = dec_plmn(b2h(in_plmn))
793 res['network_subset'] = sub
794 res['service_provider_id'] = svp
795 res['corporate_id'] = corp
796 return res
Harald Weltec91085e2022-02-10 18:05:45 +0100797
Harald Welte790b2702021-04-11 00:01:35 +0200798 def _encode_record_hex(self, in_json):
799 plmn = enc_plmn(in_json['mcc'], in_json['mnc'])
Vadim Yanitskiy1a95d2b2021-05-02 01:42:09 +0200800 return b2h(pack('!3sBBB',
801 h2b(plmn),
802 in_json['network_subset'],
803 in_json['service_provider_id'],
804 in_json['corporate_id']))
Harald Welte790b2702021-04-11 00:01:35 +0200805
806# TS 51.011 Section 10.3.31
Harald Weltec91085e2022-02-10 18:05:45 +0100807
808
Harald Welte790b2702021-04-11 00:01:35 +0200809class EF_NIA(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100810 def __init__(self, fid='6f51', sfid=None, name='EF.NIA', rec_len={1, 32},
Harald Welte790b2702021-04-11 00:01:35 +0200811 desc='Network\'s Indication of Alerting'):
812 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100813 self._construct = Struct(
814 'alerting_category'/Int8ub, 'category'/GreedyBytes)
Harald Welte790b2702021-04-11 00:01:35 +0200815
816# TS 51.011 Section 10.3.32
Harald Weltec91085e2022-02-10 18:05:45 +0100817
818
Harald Welte790b2702021-04-11 00:01:35 +0200819class EF_Kc(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100820 def __init__(self, fid='6f20', sfid=None, name='EF.Kc', desc='Ciphering key Kc', size={9, 9}):
Harald Welte790b2702021-04-11 00:01:35 +0200821 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
822 self._construct = Struct('kc'/HexAdapter(Bytes(8)), 'cksn'/Int8ub)
823
824# TS 51.011 Section 10.3.33
Harald Weltec91085e2022-02-10 18:05:45 +0100825
826
Harald Welte790b2702021-04-11 00:01:35 +0200827class EF_LOCIGPRS(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100828 def __init__(self, fid='6f53', sfid=None, name='EF.LOCIGPRS', desc='GPRS Location Information', size={14, 14}):
Harald Welte790b2702021-04-11 00:01:35 +0200829 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
830 self._construct = Struct('ptmsi'/Bytes(4), 'ptmsi_sig'/Int8ub, 'rai'/Bytes(6),
831 'rau_status'/Enum(Byte, updated=0, not_updated=1, plmn_not_allowed=2,
832 routing_area_not_allowed=3))
Harald Welteb2edd142021-01-08 23:29:35 +0100833
834# TS 51.011 Section 10.3.35..37
Harald Weltec91085e2022-02-10 18:05:45 +0100835
836
Harald Welteb2edd142021-01-08 23:29:35 +0100837class EF_xPLMNwAcT(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100838 def __init__(self, fid, sfid=None, name=None, desc=None, size={40, None}, rec_len=5):
Harald Welteb2edd142021-01-08 23:29:35 +0100839 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100840
Harald Welteb2edd142021-01-08 23:29:35 +0100841 def _decode_record_hex(self, in_hex):
842 if in_hex[:6] == "ffffff":
843 return None
844 else:
845 return dec_xplmn_w_act(in_hex)
Harald Weltec91085e2022-02-10 18:05:45 +0100846
Harald Welteb2edd142021-01-08 23:29:35 +0100847 def _encode_record_hex(self, in_json):
848 if in_json == None:
849 return "ffffff0000"
850 else:
851 hplmn = enc_plmn(in_json['mcc'], in_json['mnc'])
852 act = self.enc_act(in_json['act'])
853 return hplmn + act
Harald Weltec91085e2022-02-10 18:05:45 +0100854
Harald Welteb2edd142021-01-08 23:29:35 +0100855 @staticmethod
856 def enc_act(in_list):
857 u16 = 0
858 # first the simple ones
859 if 'UTRAN' in in_list:
860 u16 |= 0x8000
861 if 'NG-RAN' in in_list:
862 u16 |= 0x0800
863 if 'GSM COMPACT' in in_list:
864 u16 |= 0x0040
865 if 'cdma2000 HRPD' in in_list:
866 u16 |= 0x0020
867 if 'cdma2000 1xRTT' in in_list:
868 u16 |= 0x0010
869 # E-UTRAN
Philipp Maiere7d41792021-04-29 16:20:07 +0200870 if 'E-UTRAN' in in_list:
871 u16 |= 0x4000
Vadim Yanitskiy5452d642021-03-07 21:45:34 +0100872 if 'E-UTRAN WB-S1' in in_list:
873 u16 |= 0x6000
874 if 'E-UTRAN NB-S1' in in_list:
875 u16 |= 0x5000
Harald Welteb2edd142021-01-08 23:29:35 +0100876 # GSM mess
877 if 'GSM' in in_list and 'EC-GSM-IoT' in in_list:
878 u16 |= 0x008C
879 elif 'GSM' in in_list:
880 u16 |= 0x0084
881 elif 'EC-GSM-IuT' in in_list:
882 u16 |= 0x0088
Harald Weltec91085e2022-02-10 18:05:45 +0100883 return '%04X' % (u16)
Harald Welteb2edd142021-01-08 23:29:35 +0100884
Harald Welte790b2702021-04-11 00:01:35 +0200885# TS 51.011 Section 10.3.38
Harald Weltec91085e2022-02-10 18:05:45 +0100886
887
Harald Welte790b2702021-04-11 00:01:35 +0200888class EF_CPBCCH(TransRecEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100889 def __init__(self, fid='6f63', sfid=None, name='EF.CPBCCH', size={2, 14}, rec_len=2,
Harald Welte790b2702021-04-11 00:01:35 +0200890 desc='CPBCCH Information'):
891 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
892 self._construct = Struct('cpbcch'/Int16ub)
893
894# TS 51.011 Section 10.3.39
Harald Weltec91085e2022-02-10 18:05:45 +0100895
896
Harald Welte790b2702021-04-11 00:01:35 +0200897class EF_InvScan(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100898 def __init__(self, fid='6f64', sfid=None, name='EF.InvScan', size={1, 1},
Harald Welte790b2702021-04-11 00:01:35 +0200899 desc='IOnvestigation Scan'):
900 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Weltec91085e2022-02-10 18:05:45 +0100901 self._construct = FlagsEnum(
902 Byte, in_limited_service_mode=1, after_successful_plmn_selection=2)
Harald Welte790b2702021-04-11 00:01:35 +0200903
Harald Welte14105dc2021-05-31 08:48:51 +0200904# TS 51.011 Section 4.2.58
Harald Weltec91085e2022-02-10 18:05:45 +0100905
906
Harald Welte14105dc2021-05-31 08:48:51 +0200907class EF_PNN(LinFixedEF):
908 class FullNameForNetwork(BER_TLV_IE, tag=0x43):
909 # TS 24.008 10.5.3.5a
910 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100911
Harald Welte14105dc2021-05-31 08:48:51 +0200912 class ShortNameForNetwork(BER_TLV_IE, tag=0x45):
913 # TS 24.008 10.5.3.5a
914 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100915
Harald Welte14105dc2021-05-31 08:48:51 +0200916 class NetworkNameCollection(TLV_IE_Collection, nested=[FullNameForNetwork, ShortNameForNetwork]):
917 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100918
Harald Welte14105dc2021-05-31 08:48:51 +0200919 def __init__(self, fid='6fc5', sfid=None, name='EF.PNN', desc='PLMN Network Name'):
920 super().__init__(fid, sfid=sfid, name=name, desc=desc)
921 self._tlv = EF_PNN.NetworkNameCollection
922
Harald Welte790b2702021-04-11 00:01:35 +0200923# TS 51.011 Section 10.3.42
Harald Weltec91085e2022-02-10 18:05:45 +0100924
925
Harald Welte790b2702021-04-11 00:01:35 +0200926class EF_OPL(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100927 def __init__(self, fid='6fc6', sfid=None, name='EF.OPL', rec_len={8, 8}, desc='Operator PLMN List'):
Harald Welte790b2702021-04-11 00:01:35 +0200928 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
929 self._construct = Struct('lai'/Bytes(5), 'pnn_record_id'/Int8ub)
930
931# TS 51.011 Section 10.3.44 + TS 31.102 4.2.62
Harald Weltec91085e2022-02-10 18:05:45 +0100932
933
Harald Welte790b2702021-04-11 00:01:35 +0200934class EF_MBI(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100935 def __init__(self, fid='6fc9', sfid=None, name='EF.MBI', rec_len={4, 5}, desc='Mailbox Identifier'):
Harald Welte790b2702021-04-11 00:01:35 +0200936 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
937 self._construct = Struct('mbi_voicemail'/Int8ub, 'mbi_fax'/Int8ub, 'mbi_email'/Int8ub,
938 'mbi_other'/Int8ub, 'mbi_videocall'/COptional(Int8ub))
939
940# TS 51.011 Section 10.3.45 + TS 31.102 4.2.63
Harald Weltec91085e2022-02-10 18:05:45 +0100941
942
Harald Welte790b2702021-04-11 00:01:35 +0200943class EF_MWIS(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100944 def __init__(self, fid='6fca', sfid=None, name='EF.MWIS', rec_len={5, 6},
Harald Welte790b2702021-04-11 00:01:35 +0200945 desc='Message Waiting Indication Status'):
946 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
947 self._construct = Struct('mwi_status'/FlagsEnum(Byte, voicemail=1, fax=2, email=4, other=8, videomail=16),
948 'num_waiting_voicemail'/Int8ub,
949 'num_waiting_fax'/Int8ub, 'num_waiting_email'/Int8ub,
950 'num_waiting_other'/Int8ub, 'num_waiting_videomail'/COptional(Int8ub))
951
Harald Welte14105dc2021-05-31 08:48:51 +0200952# TS 51.011 Section 10.3.66
Harald Weltec91085e2022-02-10 18:05:45 +0100953
954
Harald Welte14105dc2021-05-31 08:48:51 +0200955class EF_SPDI(TransparentEF):
956 class ServiceProviderPLMN(BER_TLV_IE, tag=0x80):
957 # flexible numbers of 3-byte PLMN records
958 _construct = GreedyRange(BcdAdapter(Bytes(3)))
Harald Weltec91085e2022-02-10 18:05:45 +0100959
Harald Welte14105dc2021-05-31 08:48:51 +0200960 class SPDI(BER_TLV_IE, tag=0xA3, nested=[ServiceProviderPLMN]):
961 pass
962 def __init__(self, fid='6fcd', sfid=None, name='EF.SPDI',
Harald Weltec91085e2022-02-10 18:05:45 +0100963 desc='Service Provider Display Information'):
Harald Welte14105dc2021-05-31 08:48:51 +0200964 super().__init__(fid, sfid=sfid, name=name, desc=desc)
965 self._tlv = EF_SPDI.SPDI
966
Harald Welte790b2702021-04-11 00:01:35 +0200967# TS 51.011 Section 10.3.51
Harald Weltec91085e2022-02-10 18:05:45 +0100968
969
Harald Welte790b2702021-04-11 00:01:35 +0200970class EF_MMSN(LinFixedEF):
Harald Weltec91085e2022-02-10 18:05:45 +0100971 def __init__(self, fid='6fce', sfid=None, name='EF.MMSN', rec_len={4, 20}, desc='MMS Notification'):
Harald Welte790b2702021-04-11 00:01:35 +0200972 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
973 self._construct = Struct('mms_status'/Bytes(2), 'mms_implementation'/Bytes(1),
974 'mms_notification'/Bytes(this._.total_len-4), 'ext_record_nr'/Byte)
975
Harald Welte14105dc2021-05-31 08:48:51 +0200976# TS 51.011 Annex K.1
Harald Weltec91085e2022-02-10 18:05:45 +0100977
978
Harald Welte14105dc2021-05-31 08:48:51 +0200979class MMS_Implementation(BER_TLV_IE, tag=0x80):
980 _construct = FlagsEnum(Byte, WAP=1)
981
Harald Welte790b2702021-04-11 00:01:35 +0200982# TS 51.011 Section 10.3.53
Harald Weltec91085e2022-02-10 18:05:45 +0100983
984
Harald Welte790b2702021-04-11 00:01:35 +0200985class EF_MMSICP(TransparentEF):
Harald Welte14105dc2021-05-31 08:48:51 +0200986 class MMS_Relay_Server(BER_TLV_IE, tag=0x81):
987 # 3GPP TS 23.140
988 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100989
Harald Welte14105dc2021-05-31 08:48:51 +0200990 class Interface_to_CN(BER_TLV_IE, tag=0x82):
991 # 3GPP TS 23.140
992 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100993
Harald Welte14105dc2021-05-31 08:48:51 +0200994 class Gateway(BER_TLV_IE, tag=0x83):
995 # Address, Type of address, Port, Service, AuthType, AuthId, AuthPass / 3GPP TS 23.140
996 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100997
Harald Welte14105dc2021-05-31 08:48:51 +0200998 class MMS_ConnectivityParamters(TLV_IE_Collection,
Harald Weltec91085e2022-02-10 18:05:45 +0100999 nested=[MMS_Implementation, MMS_Relay_Server, Interface_to_CN, Gateway]):
Harald Welte14105dc2021-05-31 08:48:51 +02001000 pass
Harald Weltec91085e2022-02-10 18:05:45 +01001001 def __init__(self, fid='6fd0', sfid=None, name='EF.MMSICP', size={1, None},
Harald Welte790b2702021-04-11 00:01:35 +02001002 desc='MMS Issuer Connectivity Parameters'):
1003 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welte14105dc2021-05-31 08:48:51 +02001004 self._tlv = EF_MMSICP.MMS_ConnectivityParamters
Harald Welte790b2702021-04-11 00:01:35 +02001005
1006# TS 51.011 Section 10.3.54
Harald Weltec91085e2022-02-10 18:05:45 +01001007
1008
Harald Welte790b2702021-04-11 00:01:35 +02001009class EF_MMSUP(LinFixedEF):
Harald Welte14105dc2021-05-31 08:48:51 +02001010 class MMS_UserPref_ProfileName(BER_TLV_IE, tag=0x81):
1011 pass
Harald Weltec91085e2022-02-10 18:05:45 +01001012
Harald Welte14105dc2021-05-31 08:48:51 +02001013 class MMS_UserPref_Info(BER_TLV_IE, tag=0x82):
1014 pass
Harald Weltec91085e2022-02-10 18:05:45 +01001015
Harald Welte14105dc2021-05-31 08:48:51 +02001016 class MMS_User_Preferences(TLV_IE_Collection,
Harald Weltec91085e2022-02-10 18:05:45 +01001017 nested=[MMS_Implementation, MMS_UserPref_ProfileName, MMS_UserPref_Info]):
Harald Welte14105dc2021-05-31 08:48:51 +02001018 pass
Harald Weltec91085e2022-02-10 18:05:45 +01001019 def __init__(self, fid='6fd1', sfid=None, name='EF.MMSUP', rec_len={1, None},
Harald Welte790b2702021-04-11 00:01:35 +02001020 desc='MMS User Preferences'):
1021 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len)
Harald Welte6113fe92022-01-21 15:51:35 +01001022 self._tlv = EF_MMSUP.MMS_User_Preferences
Harald Welte790b2702021-04-11 00:01:35 +02001023
1024# TS 51.011 Section 10.3.55
Harald Weltec91085e2022-02-10 18:05:45 +01001025
1026
Harald Welte790b2702021-04-11 00:01:35 +02001027class EF_MMSUCP(TransparentEF):
Harald Weltec91085e2022-02-10 18:05:45 +01001028 def __init__(self, fid='6fd2', sfid=None, name='EF.MMSUCP', size={1, None},
Harald Welte790b2702021-04-11 00:01:35 +02001029 desc='MMS User Connectivity Parameters'):
1030 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
1031
Harald Welteb2edd142021-01-08 23:29:35 +01001032
1033class DF_GSM(CardDF):
1034 def __init__(self, fid='7f20', name='DF.GSM', desc='GSM Network related files'):
1035 super().__init__(fid=fid, name=name, desc=desc)
1036 files = [
Harald Weltec91085e2022-02-10 18:05:45 +01001037 EF_LP(),
1038 EF_IMSI(),
1039 EF_Kc(),
1040 EF_PLMNsel(),
1041 TransparentEF('6f31', None, 'EF.HPPLMN',
1042 'Higher Priority PLMN search period'),
1043 EF_ACMmax(),
1044 EF_ServiceTable('6f38', None, 'EF.SST',
1045 'SIM service table', table=EF_SST_map, size={2, 16}),
1046 CyclicEF('6f39', None, 'EF.ACM',
1047 'Accumulated call meter', rec_len={3, 3}),
1048 TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1'),
1049 TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2'),
1050 EF_SPN(),
1051 TransparentEF('6f41', None, 'EF.PUCT',
1052 'Price per unit and currency table', size={5, 5}),
1053 EF_CBMI(),
1054 TransparentEF('6f7f', None, 'EF.BCCH',
1055 'Broadcast control channels', size={16, 16}),
1056 EF_ACC(),
1057 EF_PLMNsel('6f7b', None, 'EF.FPLMN',
1058 'Forbidden PLMNs', size={12, 12}),
1059 EF_LOCI(),
1060 EF_AD(),
1061 TransparentEF('6fa3', None, 'EF.Phase',
1062 'Phase identification', size={1, 1}),
1063 EF_VGCS(),
1064 EF_VGCSS(),
1065 EF_VGCS('6fb3', None, 'EF.VBS', 'Voice Broadcast Service'),
1066 EF_VGCSS('6fb4', None, 'EF.VBSS',
1067 'Voice Broadcast Service Status'),
1068 EF_eMLPP(),
1069 EF_AAeM(),
1070 EF_CBMID(),
1071 EF_ECC(),
1072 EF_CBMIR(),
1073 EF_DCK(),
1074 EF_CNL(),
1075 EF_NIA(),
1076 EF_Kc('6f52', None, 'EF.KcGPRS', 'GPRS Ciphering key KcGPRS'),
1077 EF_LOCIGPRS(),
1078 TransparentEF('6f54', None, 'EF.SUME', 'SetUpMenu Elements'),
1079 EF_xPLMNwAcT('6f60', None, 'EF.PLMNwAcT',
1080 'User controlled PLMN Selector with Access Technology'),
1081 EF_xPLMNwAcT('6f61', None, 'EF.OPLMNwAcT',
1082 'Operator controlled PLMN Selector with Access Technology'),
1083 EF_xPLMNwAcT('6f62', None, 'EF.HPLMNwAcT',
1084 'HPLMN Selector with Access Technology'),
1085 EF_CPBCCH(),
1086 EF_InvScan(),
1087 EF_PNN(),
1088 EF_OPL(),
1089 EF_ADN('6fc7', None, 'EF.MBDN', 'Mailbox Dialling Numbers'),
1090 EF_MBI(),
1091 EF_MWIS(),
1092 EF_ADN('6fcb', None, 'EF.CFIS',
1093 'Call Forwarding Indication Status'),
1094 EF_EXT('6fc8', None, 'EF.EXT6', 'Externsion6 (MBDN)'),
1095 EF_EXT('6fcc', None, 'EF.EXT7', 'Externsion7 (CFIS)'),
1096 EF_SPDI(),
1097 EF_MMSN(),
1098 EF_EXT('6fcf', None, 'EF.EXT8', 'Extension8 (MMSN)'),
1099 EF_MMSICP(),
1100 EF_MMSUP(),
1101 EF_MMSUCP(),
1102 ]
Harald Welteb2edd142021-01-08 23:29:35 +01001103 self.add_files(files)
1104
Harald Weltec91085e2022-02-10 18:05:45 +01001105
Philipp Maierc8387dc2021-10-29 17:59:50 +02001106class CardProfileSIM(CardProfile):
Philipp Maiera028c7d2021-11-08 16:12:03 +01001107
1108 ORDER = 2
1109
Philipp Maierc8387dc2021-10-29 17:59:50 +02001110 def __init__(self):
Philipp Maiera4df9422021-11-10 17:13:40 +01001111 sw = {
Harald Weltec91085e2022-02-10 18:05:45 +01001112 'Normal': {
1113 '9000': 'Normal ending of the command',
1114 '91xx': 'normal ending of the command, with extra information from the proactive SIM containing a command for the ME',
1115 '9exx': 'length XX of the response data given in case of a SIM data download error',
1116 '9fxx': 'length XX of the response data',
Philipp Maiera4df9422021-11-10 17:13:40 +01001117 },
Harald Weltec91085e2022-02-10 18:05:45 +01001118 'Postponed processing': {
1119 '9300': 'SIM Application Toolkit is busy. Command cannot be executed at present, further normal commands are allowed',
Philipp Maiera4df9422021-11-10 17:13:40 +01001120 },
Harald Weltec91085e2022-02-10 18:05:45 +01001121 'Memory management': {
1122 '920x': 'command successful but after using an internal update retry routine X times',
1123 '9240': 'memory problem',
Philipp Maiera4df9422021-11-10 17:13:40 +01001124 },
Harald Weltec91085e2022-02-10 18:05:45 +01001125 'Referencing management': {
1126 '9400': 'no EF selected',
1127 '9402': 'out of range (invalid address)',
1128 '9404': 'file ID not found or pattern not found',
1129 '9408': 'file is inconsistent with the command',
Philipp Maiera4df9422021-11-10 17:13:40 +01001130 },
Harald Weltec91085e2022-02-10 18:05:45 +01001131 'Security management': {
1132 '9802': 'no CHV initialized',
1133 '9804': 'access condition not fulfilled, unsuccessful CHV verification or authentication failed',
1134 '9808': 'in contradiction with CHV status',
1135 '9810': 'in contradiction with invalidation status',
1136 '9840': 'unsuccessful verification, CHV blocked, UNBLOCK CHV blocked',
1137 '9850': 'increase cannot be performed, Max value reached',
Philipp Maiera4df9422021-11-10 17:13:40 +01001138 },
Harald Weltec91085e2022-02-10 18:05:45 +01001139 'Application independent errors': {
1140 '67xx': 'incorrect parameter P3',
1141 '6bxx': 'incorrect parameter P1 or P2',
1142 '6dxx': 'unknown instruction code given in the command',
1143 '6exx': 'wrong instruction class given in the command',
1144 '6fxx': 'technical problem with no diagnostic given',
Philipp Maiera4df9422021-11-10 17:13:40 +01001145 },
Harald Weltec91085e2022-02-10 18:05:45 +01001146 }
Philipp Maiera4df9422021-11-10 17:13:40 +01001147
Harald Weltec91085e2022-02-10 18:05:45 +01001148 super().__init__('SIM', desc='GSM SIM Card', cla="a0",
1149 sel_ctrl="0000", files_in_mf=[DF_TELECOM(), DF_GSM()], sw=sw)
Philipp Maier4ab971c2021-11-11 11:53:49 +01001150
Philipp Maier5998a3a2021-11-16 15:16:39 +01001151 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +01001152 def decode_select_response(resp_hex: str) -> object:
Philipp Maier4ab971c2021-11-11 11:53:49 +01001153 resp_bin = h2b(resp_hex)
1154 struct_of_file_map = {
1155 0: 'transparent',
1156 1: 'linear_fixed',
1157 3: 'cyclic'
Harald Weltec91085e2022-02-10 18:05:45 +01001158 }
Philipp Maier4ab971c2021-11-11 11:53:49 +01001159 type_of_file_map = {
1160 1: 'mf',
1161 2: 'df',
1162 4: 'working_ef'
Harald Weltec91085e2022-02-10 18:05:45 +01001163 }
Philipp Maier4ab971c2021-11-11 11:53:49 +01001164 ret = {
1165 'file_descriptor': {},
1166 'proprietary_info': {},
Harald Weltec91085e2022-02-10 18:05:45 +01001167 }
Philipp Maier4ab971c2021-11-11 11:53:49 +01001168 ret['file_id'] = b2h(resp_bin[4:6])
Harald Weltec91085e2022-02-10 18:05:45 +01001169 ret['proprietary_info']['available_memory'] = int.from_bytes(
1170 resp_bin[2:4], 'big')
1171 file_type = type_of_file_map[resp_bin[6]
1172 ] if resp_bin[6] in type_of_file_map else resp_bin[6]
Philipp Maier4ab971c2021-11-11 11:53:49 +01001173 ret['file_descriptor']['file_type'] = file_type
1174 if file_type in ['mf', 'df']:
1175 ret['file_characteristics'] = b2h(resp_bin[13:14])
1176 ret['num_direct_child_df'] = resp_bin[14]
1177 ret['num_direct_child_ef'] = resp_bin[15]
1178 ret['num_chv_unblock_adm_codes'] = int(resp_bin[16])
1179 # CHV / UNBLOCK CHV stats
1180 elif file_type in ['working_ef']:
Harald Weltec91085e2022-02-10 18:05:45 +01001181 file_struct = struct_of_file_map[resp_bin[13]
1182 ] if resp_bin[13] in struct_of_file_map else resp_bin[13]
Philipp Maier4ab971c2021-11-11 11:53:49 +01001183 ret['file_descriptor']['structure'] = file_struct
1184 ret['access_conditions'] = b2h(resp_bin[8:10])
1185 if resp_bin[11] & 0x01 == 0:
1186 ret['life_cycle_status_int'] = 'operational_activated'
1187 elif resp_bin[11] & 0x04:
1188 ret['life_cycle_status_int'] = 'operational_deactivated'
1189 else:
1190 ret['life_cycle_status_int'] = 'terminated'
1191 return ret
Philipp Maiera028c7d2021-11-08 16:12:03 +01001192
1193 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +01001194 def match_with_card(scc: SimCardCommands) -> bool:
Philipp Maiera028c7d2021-11-08 16:12:03 +01001195 return match_sim(scc)