blob: bd71114a353fb97d58d2282c24157c6127840b35 [file] [log] [blame]
Alexander Chemeris067f69c2017-07-18 16:44:26 +03001# -*- coding: utf-8 -*-
2
Harald Welteb2edd142021-01-08 23:29:35 +01003""" Various constants from ETSI TS 151.011 +
4Representation of the GSM SIM/USIM/ISIM filesystem hierarchy.
5
6The File (and its derived classes) uses the classes of pySim.filesystem in
7order to describe the files specified in the relevant ETSI + 3GPP specifications.
Alexander Chemeris067f69c2017-07-18 16:44:26 +03008"""
9
10#
11# Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com>
Harald Welteb2edd142021-01-08 23:29:35 +010012# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
Alexander Chemeris067f69c2017-07-18 16:44:26 +030013#
14# This program is free software: you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation, either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program. If not, see <http://www.gnu.org/licenses/>.
26#
27
28MF_num = '3F00'
29
30DF_num = {
31'TELECOM': '7F10',
32
33'GSM': '7F20',
34'IS-41': '7F22',
35'FP-CTS': '7F23',
36
37'GRAPHICS': '5F50',
38
39'IRIDIUM': '5F30',
40'GLOBST': '5F31',
41'ICO': '5F32',
42'ACeS': '5F33',
43
44'EIA/TIA-553': '5F40',
45'CTS': '5F60',
46'SOLSA': '5F70',
47
48'MExE': '5F3C',
49}
50
51EF_num = {
52# MF
53'ICCID': '2FE2',
54'ELP': '2F05',
Sebastian Viviani0dc8f692020-05-29 00:14:55 +010055'DIR': '2F00',
Alexander Chemeris067f69c2017-07-18 16:44:26 +030056
57# DF_TELECOM
58'ADN': '6F3A',
59'FDN': '6F3B',
60'SMS': '6F3C',
61'CCP': '6F3D',
62'MSISDN': '6F40',
63'SMSP': '6F42',
64'SMSS': '6F43',
65'LND': '6F44',
66'SMSR': '6F47',
67'SDN': '6F49',
68'EXT1': '6F4A',
69'EXT2': '6F4B',
70'EXT3': '6F4C',
71'BDN': '6F4D',
72'EXT4': '6F4E',
73'CMI': '6F58',
74'ECCP': '6F4F',
75
76# DF_GRAPHICS
77'IMG': '4F20',
78
79# DF_SoLSA
80'SAI': '4F30',
81'SLL': '4F31',
82
83# DF_MExE
84'MExE-ST': '4F40',
85'ORPK': '4F41',
86'ARPK': '4F42',
87'TPRPK': '4F43',
88
89# DF_GSM
90'LP': '6F05',
91'IMSI': '6F07',
92'Kc': '6F20',
93'DCK': '6F2C',
94'PLMNsel': '6F30',
95'HPPLMN': '6F31',
96'CNL': '6F32',
97'ACMmax': '6F37',
98'SST': '6F38',
99'ACM': '6F39',
100'GID1': '6F3E',
101'GID2': '6F3F',
102'PUCT': '6F41',
103'CBMI': '6F45',
104'SPN': '6F46',
105'CBMID': '6F48',
106'BCCH': '6F74',
107'ACC': '6F78',
108'FPLMN': '6F7B',
109'LOCI': '6F7E',
110'AD': '6FAD',
111'PHASE': '6FAE',
112'VGCS': '6FB1',
113'VGCSS': '6FB2',
114'VBS': '6FB3',
115'VBSS': '6FB4',
116'eMLPP': '6FB5',
117'AAeM': '6FB6',
118'ECC': '6FB7',
119'CBMIR': '6F50',
120'NIA': '6F51',
121'KcGPRS': '6F52',
122'LOCIGPRS': '6F53',
123'SUME': '6F54',
124'PLMNwAcT': '6F60',
125'OPLMNwAcT': '6F61',
126# Figure 8 names it HPLMNAcT, but in the text it's names it HPLMNwAcT
127'HPLMNAcT': '6F62',
128'HPLMNwAcT': '6F62',
129'CPBCCH': '6F63',
130'INVSCAN': '6F64',
131'PNN': '6FC5',
132'OPL': '6FC6',
133'MBDN': '6FC7',
134'EXT6': '6FC8',
135'MBI': '6FC9',
136'MWIS': '6FCA',
137'CFIS': '6FCB',
138'EXT7': '6FCC',
139'SPDI': '6FCD',
140'MMSN': '6FCE',
141'EXT8': '6FCF',
142'MMSICP': '6FD0',
143'MMSUP': '6FD1',
144'MMSUCP': '6FD2',
145}
146
147DF = {
148'TELECOM': [MF_num, DF_num['TELECOM']],
149
150'GSM': [MF_num, DF_num['GSM']],
151'IS-41': [MF_num, DF_num['IS-41']],
152'FP-CTS': [MF_num, DF_num['FP-CTS']],
153
154'GRAPHICS': [MF_num, DF_num['GRAPHICS']],
155
156'IRIDIUM': [MF_num, DF_num['IRIDIUM']],
157'GLOBST': [MF_num, DF_num['GLOBST']],
158'ICO': [MF_num, DF_num['ICO']],
159'ACeS': [MF_num, DF_num['ACeS']],
160
161'EIA/TIA-553': [MF_num, DF_num['EIA/TIA-553']],
162'CTS': [MF_num, DF_num['CTS']],
163'SoLSA': [MF_num, DF_num['SOLSA']],
164
165'MExE': [MF_num, DF_num['MExE']],
166}
167
168
169EF = {
170'ICCID': [MF_num, EF_num['ICCID']],
171'ELP': [MF_num, EF_num['ELP']],
Sebastian Viviani0dc8f692020-05-29 00:14:55 +0100172'DIR': [MF_num, EF_num['DIR']],
Alexander Chemeris067f69c2017-07-18 16:44:26 +0300173
174'ADN': DF['TELECOM']+[EF_num['ADN']],
175'FDN': DF['TELECOM']+[EF_num['FDN']],
176'SMS': DF['TELECOM']+[EF_num['SMS']],
177'CCP': DF['TELECOM']+[EF_num['CCP']],
178'MSISDN': DF['TELECOM']+[EF_num['MSISDN']],
179'SMSP': DF['TELECOM']+[EF_num['SMSP']],
180'SMSS': DF['TELECOM']+[EF_num['SMSS']],
181'LND': DF['TELECOM']+[EF_num['LND']],
182'SMSR': DF['TELECOM']+[EF_num['SMSR']],
183'SDN': DF['TELECOM']+[EF_num['SDN']],
184'EXT1': DF['TELECOM']+[EF_num['EXT1']],
185'EXT2': DF['TELECOM']+[EF_num['EXT2']],
186'EXT3': DF['TELECOM']+[EF_num['EXT3']],
187'BDN': DF['TELECOM']+[EF_num['BDN']],
188'EXT4': DF['TELECOM']+[EF_num['EXT4']],
189'CMI': DF['TELECOM']+[EF_num['CMI']],
190'ECCP': DF['TELECOM']+[EF_num['ECCP']],
191
192'IMG': DF['GRAPHICS']+[EF_num['IMG']],
193
194'SAI': DF['SoLSA']+[EF_num['SAI']],
195'SLL': DF['SoLSA']+[EF_num['SLL']],
196
197'MExE-ST': DF['MExE']+[EF_num['MExE-ST']],
198'ORPK': DF['MExE']+[EF_num['ORPK']],
199'ARPK': DF['MExE']+[EF_num['ARPK']],
200'TPRPK': DF['MExE']+[EF_num['TPRPK']],
201
202'LP': DF['GSM']+[EF_num['LP']],
203'IMSI': DF['GSM']+[EF_num['IMSI']],
204'Kc': DF['GSM']+[EF_num['Kc']],
205'DCK': DF['GSM']+[EF_num['DCK']],
206'PLMNsel': DF['GSM']+[EF_num['PLMNsel']],
207'HPPLMN': DF['GSM']+[EF_num['HPPLMN']],
208'CNL': DF['GSM']+[EF_num['CNL']],
209'ACMmax': DF['GSM']+[EF_num['ACMmax']],
210'SST': DF['GSM']+[EF_num['SST']],
211'ACM': DF['GSM']+[EF_num['ACM']],
212'GID1': DF['GSM']+[EF_num['GID1']],
213'GID2': DF['GSM']+[EF_num['GID2']],
214'PUCT': DF['GSM']+[EF_num['PUCT']],
215'CBMI': DF['GSM']+[EF_num['CBMI']],
216'SPN': DF['GSM']+[EF_num['SPN']],
217'CBMID': DF['GSM']+[EF_num['CBMID']],
218'BCCH': DF['GSM']+[EF_num['BCCH']],
219'ACC': DF['GSM']+[EF_num['ACC']],
220'FPLMN': DF['GSM']+[EF_num['FPLMN']],
221'LOCI': DF['GSM']+[EF_num['LOCI']],
222'AD': DF['GSM']+[EF_num['AD']],
223'PHASE': DF['GSM']+[EF_num['PHASE']],
224'VGCS': DF['GSM']+[EF_num['VGCS']],
225'VGCSS': DF['GSM']+[EF_num['VGCSS']],
226'VBS': DF['GSM']+[EF_num['VBS']],
227'VBSS': DF['GSM']+[EF_num['VBSS']],
228'eMLPP': DF['GSM']+[EF_num['eMLPP']],
229'AAeM': DF['GSM']+[EF_num['AAeM']],
230'ECC': DF['GSM']+[EF_num['ECC']],
231'CBMIR': DF['GSM']+[EF_num['CBMIR']],
232'NIA': DF['GSM']+[EF_num['NIA']],
233'KcGPRS': DF['GSM']+[EF_num['KcGPRS']],
234'LOCIGPRS': DF['GSM']+[EF_num['LOCIGPRS']],
235'SUME': DF['GSM']+[EF_num['SUME']],
236'PLMNwAcT': DF['GSM']+[EF_num['PLMNwAcT']],
237'OPLMNwAcT': DF['GSM']+[EF_num['OPLMNwAcT']],
238# Figure 8 names it HPLMNAcT, but in the text it's names it HPLMNwAcT
239'HPLMNAcT': DF['GSM']+[EF_num['HPLMNAcT']],
240'HPLMNwAcT': DF['GSM']+[EF_num['HPLMNAcT']],
241'CPBCCH': DF['GSM']+[EF_num['CPBCCH']],
242'INVSCAN': DF['GSM']+[EF_num['INVSCAN']],
243'PNN': DF['GSM']+[EF_num['PNN']],
244'OPL': DF['GSM']+[EF_num['OPL']],
245'MBDN': DF['GSM']+[EF_num['MBDN']],
246'EXT6': DF['GSM']+[EF_num['EXT6']],
247'MBI': DF['GSM']+[EF_num['MBI']],
248'MWIS': DF['GSM']+[EF_num['MWIS']],
249'CFIS': DF['GSM']+[EF_num['CFIS']],
250'EXT7': DF['GSM']+[EF_num['EXT7']],
251'SPDI': DF['GSM']+[EF_num['SPDI']],
252'MMSN': DF['GSM']+[EF_num['MMSN']],
253'EXT8': DF['GSM']+[EF_num['EXT8']],
254'MMSICP': DF['GSM']+[EF_num['MMSICP']],
255'MMSUP': DF['GSM']+[EF_num['MMSUP']],
256'MMSUCP': DF['GSM']+[EF_num['MMSUCP']],
257}
Supreeth Herlebf5d6022020-03-20 15:18:27 +0100258
259# Mapping between SIM Service Number and its description
260EF_SST_map = {
261 1: 'CHV1 disable function',
262 2: 'Abbreviated Dialling Numbers (ADN)',
263 3: 'Fixed Dialling Numbers (FDN)',
264 4: 'Short Message Storage (SMS)',
265 5: 'Advice of Charge (AoC)',
266 6: 'Capability Configuration Parameters (CCP)',
267 7: 'PLMN selector',
268 8: 'RFU',
269 9: 'MSISDN',
270 10: 'Extension1',
271 11: 'Extension2',
272 12: 'SMS Parameters',
273 13: 'Last Number Dialled (LND)',
274 14: 'Cell Broadcast Message Identifier',
275 15: 'Group Identifier Level 1',
276 16: 'Group Identifier Level 2',
277 17: 'Service Provider Name',
278 18: 'Service Dialling Numbers (SDN)',
279 19: 'Extension3',
280 20: 'RFU',
281 21: 'VGCS Group Identifier List (EFVGCS and EFVGCSS)',
282 22: 'VBS Group Identifier List (EFVBS and EFVBSS)',
283 23: 'enhanced Multi-Level Precedence and Pre-emption Service',
284 24: 'Automatic Answer for eMLPP',
285 25: 'Data download via SMS-CB',
286 26: 'Data download via SMS-PP',
287 27: 'Menu selection',
288 28: 'Call control',
289 29: 'Proactive SIM',
290 30: 'Cell Broadcast Message Identifier Ranges',
291 31: 'Barred Dialling Numbers (BDN)',
292 32: 'Extension4',
293 33: 'De-personalization Control Keys',
294 34: 'Co-operative Network List',
295 35: 'Short Message Status Reports',
296 36: 'Network\'s indication of alerting in the MS',
297 37: 'Mobile Originated Short Message control by SIM',
298 38: 'GPRS',
299 39: 'Image (IMG)',
300 40: 'SoLSA (Support of Local Service Area)',
301 41: 'USSD string data object supported in Call Control',
302 42: 'RUN AT COMMAND command',
303 43: 'User controlled PLMN Selector with Access Technology',
304 44: 'Operator controlled PLMN Selector with Access Technology',
305 45: 'HPLMN Selector with Access Technology',
306 46: 'CPBCCH Information',
307 47: 'Investigation Scan',
308 48: 'Extended Capability Configuration Parameters',
309 49: 'MExE',
310 50: 'Reserved and shall be ignored',
311 51: 'PLMN Network Name',
312 52: 'Operator PLMN List',
313 53: 'Mailbox Dialling Numbers',
314 54: 'Message Waiting Indication Status',
315 55: 'Call Forwarding Indication Status',
316 56: 'Service Provider Display Information',
317 57: 'Multimedia Messaging Service (MMS)',
318 58: 'Extension 8',
319 59: 'MMS User Connectivity Parameters',
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +0700320}
321
322# 10.3.18 "EF.AD (Administrative data) "
323EF_AD_mode_map = {
324 '00' : 'normal operation',
325 '80' : 'type approval operations',
326 '01' : 'normal operation + specific facilities',
327 '81' : 'type approval operations + specific facilities',
328 '02' : 'maintenance (off line)',
329 '04' : 'cell test operation',
330}
Harald Welteb2edd142021-01-08 23:29:35 +0100331
332
333from pySim.utils import *
334from struct import pack, unpack
335
336from pySim.filesystem import *
337import pySim.ts_102_221
338
339######################################################################
340# DF.TELECOM
341######################################################################
342
343# TS 51.011 Section 10.5.1
344class EF_ADN(LinFixedEF):
345 def __init__(self, fid='6f3a', sfid=None, name='EF.ADN', desc='Abbreviated Dialing Numbers'):
346 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={14, 30})
347 def _decode_record_bin(self, raw_bin_data):
348 alpha_id_len = len(raw_bin_data) - 14
349 alpha_id = raw_bin_data[:alpha_id_len]
350 u = unpack('!BB10sBB', raw_bin_data[-14:])
351 return {'alpha_id': alpha_id, 'len_of_bcd': u[0], 'ton_npi': u[1],
352 'dialing_nr': u[2], 'cap_conf_id': u[3], 'ext1_record_id': u[4]}
353
354# TS 51.011 Section 10.5.5
Harald Welteec7d0da2021-04-02 22:01:19 +0200355class EF_SMS(LinFixedEF):
356 def __init__(self, fid='6f3c', sfid=None, name='EF.SMS', desc='Short messages'):
357 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={176,176})
358 def _decode_record_bin(self, raw_bin_data):
359 def decode_status(status):
360 if status & 0x01 == 0x00:
361 return (None, 'free_space')
362 elif status & 0x07 == 0x01:
363 return ('mt', 'message_read')
364 elif status & 0x07 == 0x03:
365 return ('mt', 'message_to_be_read')
366 elif status & 0x07 == 0x07:
367 return ('mo', 'message_to_be_sent')
368 elif status & 0x1f == 0x05:
369 return ('mo', 'sent_status_not_requested')
370 elif status & 0x1f == 0x0d:
371 return ('mo', 'sent_status_req_but_not_received')
372 elif status & 0x1f == 0x15:
373 return ('mo', 'sent_status_req_rx_not_stored_smsr')
374 elif status & 0x1f == 0x1d:
375 return ('mo', 'sent_status_req_rx_stored_smsr')
376 else:
377 return (None, 'rfu')
378
379 status = decode_status(raw_bin_data[0])
380 remainder = raw_bin_data[1:]
381 return {'direction': status[0], 'status': status[1], 'remainder': b2h(remainder)}
382
383
384# TS 51.011 Section 10.5.5
Harald Welteb2edd142021-01-08 23:29:35 +0100385class EF_MSISDN(LinFixedEF):
Harald Welteec7d0da2021-04-02 22:01:19 +0200386 def __init__(self, fid='6f40', sfid=None, name='EF.MSISDN', desc='MSISDN'):
Harald Welteb2edd142021-01-08 23:29:35 +0100387 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={15, None})
388 def _decode_record_hex(self, raw_hex_data):
389 return {'msisdn': dec_msisdn(raw_hex_data)}
390 def _encode_record_hex(self, abstract):
391 return enc_msisdn(abstract['msisdn'])
392
393# TS 51.011 Section 10.5.6
394class EF_SMSP(LinFixedEF):
395 def __init__(self, fid='6f42', sfid=None, name='EF.SMSP', desc='Short message service parameters'):
396 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={28, None})
397
398class DF_TELECOM(CardDF):
399 def __init__(self, fid='7f10', name='DF.TELECOM', desc=None):
400 super().__init__(fid=fid, name=name, desc=desc)
401 files = [
402 EF_ADN(),
Harald Welteec7d0da2021-04-02 22:01:19 +0200403 EF_ADN(fid='6f3b', name='EF_FDN', desc='Fixed dialling numbers'),
404 EF_SMS(),
405 LinFixedEF(fid='6f3d', name='EF.CCP', desc='Capability Configuration Parameters', rec_len={14,14}),
406 LinFixedEF(fid='6f4f', name='EF.ECCP', desc='Extended Capability Configuration Parameters', rec_len={15,32}),
Harald Welteb2edd142021-01-08 23:29:35 +0100407 EF_MSISDN(),
408 EF_SMSP(),
Harald Welteec7d0da2021-04-02 22:01:19 +0200409 TransparentEF(fid='6f43', name='EF.SMSS', desc='SMS status', size={2,8}),
410 # LND, SDN, EXT1, EXT2, EXT3, BDN, EXT4
411 LinFixedEF(fid='6f47', name='EF.SMSR', desc='SMS status reports', rec_len={30,30}),
412 # CMI
Harald Welteb2edd142021-01-08 23:29:35 +0100413 ]
414 self.add_files(files)
415
416 def decode_select_response(self, data_hex):
417 return decode_select_response(data_hex)
418
419######################################################################
420# DF.GSM
421######################################################################
422
423# TS 51.011 Section 10.3.1
424class EF_LP(TransRecEF):
425 def __init__(self, fid='6f05', sfid=None, name='EF.LP', size={1,None}, rec_len=1,
426 desc='Language Preference'):
427 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
428 def _decode_record_bin(self, in_bin):
429 return b2h(in_bin)
430 def _encode_record_bin(self, in_json):
431 return h2b(in_json)
432
433# TS 51.011 Section 10.3.2
434class EF_IMSI(TransparentEF):
435 def __init__(self, fid='6f07', sfid=None, name='EF.IMSI', desc='IMSI', size={9,9}):
436 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
437 def _decode_hex(self, raw_hex):
438 return {'imsi': dec_imsi(raw_hex)}
439 def _encode_hex(self, abstract):
440 return enc_imsi(abstract['imsi'])
441
442# TS 51.011 Section 10.3.4
443class EF_PLMNsel(TransRecEF):
444 def __init__(self, fid='6f30', sfid=None, name='EF.PLMNsel', desc='PLMN selector',
445 size={24,None}, rec_len=3):
446 super().__init__(fid, name=name, sfid=sfid, desc=desc, size=size, rec_len=rec_len)
447 def _decode_record_hex(self, in_hex):
448 if in_hex[:6] == "ffffff":
449 return None
450 else:
451 return dec_plmn(in_hex)
452 def _encode_record_hex(self, in_json):
453 if in_json == None:
454 return "ffffff"
455 else:
456 return enc_plmn(in_json['mcc'], in_json['mnc'])
457
458# TS 51.011 Section 10.3.7
459class EF_ServiceTable(TransparentEF):
460 def __init__(self, fid, sfid, name, desc, size, table):
461 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
462 self.table = table
463 def _decode_bin(self, raw_bin):
464 ret = {}
465 for i in range(0, len(raw_bin)*4):
466 service_nr = i+1
467 byte = int(raw_bin[i//4])
468 bit_offset = (i % 4) * 2
469 bits = (byte >> bit_offset) & 3
470 ret[service_nr] = {
471 'description': self.table[service_nr] or None,
472 'allocated': True if bits & 1 else False,
473 'activated': True if bits & 2 else False,
474 }
475 return ret
476 # TODO: encoder
477
478# TS 51.011 Section 10.3.11
479class EF_SPN(TransparentEF):
480 def __init__(self, fid='6f46', sfid=None, name='EF.SPN', desc='Service Provider Name', size={17,17}):
481 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
482 def _decode_hex(self, raw_hex):
483 return {'spn': dec_spn(raw_hex)}
484 def _encode_hex(self, abstract):
485 return enc_spn(abstract['spn'])
486
487# TS 51.011 Section 10.3.13
488class EF_CBMI(TransRecEF):
489 def __init__(self, fid='6f45', sfid=None, name='EF.CBMI', size={2,None}, rec_len=2,
490 desc='Cell Broadcast message identifier selection'):
491 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
492
493# TS 51.011 Section 10.3.15
494class EF_ACC(TransparentEF):
495 def __init__(self, fid='6f78', sfid=None, name='EF.ACC', desc='Access Control Class', size={2,2}):
496 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
497 def _decode_bin(self, raw_bin):
498 return {'acc': unpack('!H', raw_bin)[0]}
499 def _encode_bin(self, abstract):
500 return pack('!H', abstract['acc'])
501
502# TS 51.011 Section 10.3.18
503class EF_AD(TransparentEF):
504 OP_MODE = {
Harald Weltec9baa4d2021-04-06 20:19:01 +0200505 0x00: 'normal',
506 0x80: 'type_approval',
507 0x01: 'normal_and_specific_facilities',
508 0x81: 'type_approval_and_specific_facilities',
509 0x02: 'maintenance_off_line',
510 0x04: 'cell_test',
Harald Welteb2edd142021-01-08 23:29:35 +0100511 }
Harald Weltec9baa4d2021-04-06 20:19:01 +0200512 OP_MODE_reverse = dict(map(reversed, OP_MODE.items()))
Harald Welteb2edd142021-01-08 23:29:35 +0100513 def __init__(self, fid='6fad', sfid=None, name='EF.AD', desc='Administrative Data', size={3,4}):
514 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
515 def _decode_bin(self, raw_bin):
516 u = unpack('!BH', raw_bin[:3])
Harald Weltec9baa4d2021-04-06 20:19:01 +0200517 ofm = True if u[1] & 1 else False
518 res = {'ms_operation_mode': self.OP_MODE.get(u[0], u[0]), 'specific_facilities': { 'ofm': ofm } }
519 if len(raw_bin) > 3:
520 res['len_of_mnc_in_imsi'] = int(raw_bin[3]) & 0xf
521 return res
522 def _encode_bin(self, abstract):
523 op_mode = self.OP_MODE_reverse[abstract['ms_operation_mode']]
524 res = pack('!BH', op_mode, abstract['specific_facilities']['ofm'])
525 if 'len_of_mnc_in_imsi' in abstract:
526 res += pack('!B', abstract['len_of_mnc_in_imsi'])
527 return res
Harald Welteb2edd142021-01-08 23:29:35 +0100528
529# TS 51.011 Section 10.3.13
530class EF_CBMID(EF_CBMI):
531 def __init__(self, fid='6f48', sfid=None, name='EF.CBMID', size={2,None}, rec_len=2,
532 desc='Cell Broadcast Message Identifier for Data Download'):
533 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
534
Harald Welte89e59542021-04-02 21:33:13 +0200535# TS 51.011 Section 10.3.27
536class EF_ECC(TransRecEF):
537 def __init__(self, fid='6fb7', sfid=None, name='EF.ECC', size={3,15}, rec_len=3,
538 desc='Emergency Call Codes'):
539 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Welteb2edd142021-01-08 23:29:35 +0100540
541# TS 51.011 Section 10.3.28
542class EF_CBMIR(TransRecEF):
543 def __init__(self, fid='6f50', sfid=None, name='EF.CBMIR', size={4,None}, rec_len=4,
544 desc='Cell Broadcast message identifier range selection'):
545 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
546
547
548# TS 51.011 Section 10.3.35..37
549class EF_xPLMNwAcT(TransRecEF):
550 def __init__(self, fid, sfid=None, name=None, desc=None, size={40,None}, rec_len=5):
551 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
552 def _decode_record_hex(self, in_hex):
553 if in_hex[:6] == "ffffff":
554 return None
555 else:
556 return dec_xplmn_w_act(in_hex)
557 def _encode_record_hex(self, in_json):
558 if in_json == None:
559 return "ffffff0000"
560 else:
561 hplmn = enc_plmn(in_json['mcc'], in_json['mnc'])
562 act = self.enc_act(in_json['act'])
563 return hplmn + act
564 @staticmethod
565 def enc_act(in_list):
566 u16 = 0
567 # first the simple ones
568 if 'UTRAN' in in_list:
569 u16 |= 0x8000
570 if 'NG-RAN' in in_list:
571 u16 |= 0x0800
572 if 'GSM COMPACT' in in_list:
573 u16 |= 0x0040
574 if 'cdma2000 HRPD' in in_list:
575 u16 |= 0x0020
576 if 'cdma2000 1xRTT' in in_list:
577 u16 |= 0x0010
578 # E-UTRAN
Vadim Yanitskiy5452d642021-03-07 21:45:34 +0100579 if 'E-UTRAN WB-S1' in in_list:
580 u16 |= 0x6000
581 if 'E-UTRAN NB-S1' in in_list:
582 u16 |= 0x5000
Harald Welteb2edd142021-01-08 23:29:35 +0100583 # GSM mess
584 if 'GSM' in in_list and 'EC-GSM-IoT' in in_list:
585 u16 |= 0x008C
586 elif 'GSM' in in_list:
587 u16 |= 0x0084
588 elif 'EC-GSM-IuT' in in_list:
589 u16 |= 0x0088
590 return '%04X'%(u16)
591
592
593class DF_GSM(CardDF):
594 def __init__(self, fid='7f20', name='DF.GSM', desc='GSM Network related files'):
595 super().__init__(fid=fid, name=name, desc=desc)
596 files = [
597 EF_LP(),
598 EF_IMSI(),
599 TransparentEF('5f20', None, 'EF.Kc', 'Ciphering key Kc'),
600 EF_PLMNsel(),
601 TransparentEF('6f31', None, 'EF.HPPLMN', 'Higher Priority PLMN search period'),
602 # ACMmax
603 EF_ServiceTable('6f37', None, 'EF.SST', 'SIM service table', table=EF_SST_map, size={2,16}),
604 CyclicEF('6f39', None, 'EF.ACM', 'Accumulated call meter', rec_len={4,3}),
605 TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1'),
606 TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2'),
607 EF_SPN(),
608 TransparentEF('6f41', None, 'EF.PUCT', 'Price per unit and currency table', size={5,5}),
609 EF_CBMI(),
610 TransparentEF('6f7f', None, 'EF.BCCH', 'Broadcast control channels', size={16,16}),
611 EF_ACC(),
612 EF_PLMNsel('6f7b', None, 'EF.FPLMN', 'Forbidden PLMNs', size={12,12}),
613 TransparentEF('6f7e', None, 'EF.LOCI', 'Locationn information', size={11,11}),
614 EF_AD(),
615 TransparentEF('6fa3', None, 'EF.Phase', 'Phase identification', size={1,1}),
616 # TODO EF.VGCS VGCSS, VBS, VBSS, eMLPP, AAeM
617 EF_CBMID(),
618 EF_ECC(),
619 EF_CBMIR(),
620 # DCK, CNL, NIA, KcGRS, LOCIGPRS, SUME
621 EF_xPLMNwAcT('6f60', None, 'EF.PLMNwAcT',
622 'User controlled PLMN Selector with Access Technology'),
623 EF_xPLMNwAcT('6f61', None, 'EF.OPLMNwAcT',
624 'Operator controlled PLMN Selector with Access Technology'),
625 EF_xPLMNwAcT('6f62', None, 'EF.HPLMNwAcT', 'HPLMN Selector with Access Technology'),
626 # CPBCCH, InvScan, PNN, OPL, MBDN, MBI, MWIS, CFIS, EXT5, EXT6, EXT7, SPDI, MMSN, EXT8
627 # MMSICP, MMSUP, MMSUCP
628 ]
629 self.add_files(files)
630
631 def decode_select_response(self, data_hex):
632 return decode_select_response(data_hex)
633
634def decode_select_response(resp_hex):
635 resp_bin = h2b(resp_hex)
636 if resp_bin[0] == 0x62:
637 return pySim.ts_102_221.decode_select_response(resp_hex)
638 struct_of_file_map = {
639 0: 'transparent',
640 1: 'linear_fixed',
641 3: 'cyclic'
642 }
643 type_of_file_map = {
644 1: 'mf',
645 2: 'df',
646 4: 'working_ef'
647 }
648 ret = {
649 'file_descriptor': {},
650 'proprietary_info': {},
651 }
652 ret['file_id'] = b2h(resp_bin[4:6])
653 ret['proprietary_info']['available_memory'] = int.from_bytes(resp_bin[2:4], 'big')
654 file_type = type_of_file_map[resp_bin[6]] if resp_bin[6] in type_of_file_map else resp_bin[6]
655 ret['file_descriptor']['file_type'] = file_type
656 if file_type in ['mf', 'df']:
657 ret['file_characteristics'] = b2h(resp_bin[13])
658 ret['num_direct_child_df'] = int(resp_bin[14], 16)
659 ret['num_direct_child_ef'] = int(resp_bin[15], 16)
660 ret['num_chv_unbkock_adm_codes'] = int(resp_bin[16])
661 # CHV / UNBLOCK CHV stats
662 elif file_type in ['working_ef']:
663 file_struct = struct_of_file_map[resp_bin[13]] if resp_bin[13] in struct_of_file_map else resp_bin[13]
664 ret['file_descriptor']['structure'] = file_struct
665 ret['access_conditions'] = b2h(resp_bin[8:10])
666 if resp_bin[11] & 0x01 == 0:
667 ret['life_cycle_status_int'] = 'operational_activated'
668 elif resp_bin[11] & 0x04:
669 ret['life_cycle_status_int'] = 'operational_deactivated'
670 else:
671 ret['life_cycle_status_int'] = 'terminated'
672
673 return ret
674
675CardProfileSIM = CardProfile('SIM', desc='GSM SIM Card', files_in_mf=[DF_TELECOM(), DF_GSM()])