blob: 422b35e7a018697d651d802b8ac1734cca1508ac [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
Harald Welte323a3502023-07-11 17:26:39 +020033from pySim.profile import CardProfile, CardProfileAddon
Harald Weltec91085e2022-02-10 18:05:45 +010034from pySim.filesystem import *
Harald Welte228ae8e2022-07-17 22:01:04 +020035from pySim.ts_31_102_telecom import DF_PHONEBOOK, DF_MULTIMEDIA, DF_MCS, DF_V2X
Harald Welte323a3502023-07-11 17:26:39 +020036from pySim.gsm_r import AddonGSMR
Harald Weltec91085e2022-02-10 18:05:45 +010037import enum
38from pySim.construct import *
39from construct import Optional as COptional
40from construct import *
41from struct import pack, unpack
42from typing import Tuple
43from pySim.tlv import *
44from pySim.utils import *
Supreeth Herlebf5d6022020-03-20 15:18:27 +010045
46# Mapping between SIM Service Number and its description
47EF_SST_map = {
Harald Weltec91085e2022-02-10 18:05:45 +010048 1: 'CHV1 disable function',
49 2: 'Abbreviated Dialling Numbers (ADN)',
50 3: 'Fixed Dialling Numbers (FDN)',
51 4: 'Short Message Storage (SMS)',
52 5: 'Advice of Charge (AoC)',
53 6: 'Capability Configuration Parameters (CCP)',
54 7: 'PLMN selector',
55 8: 'RFU',
56 9: 'MSISDN',
57 10: 'Extension1',
58 11: 'Extension2',
59 12: 'SMS Parameters',
60 13: 'Last Number Dialled (LND)',
61 14: 'Cell Broadcast Message Identifier',
62 15: 'Group Identifier Level 1',
63 16: 'Group Identifier Level 2',
64 17: 'Service Provider Name',
65 18: 'Service Dialling Numbers (SDN)',
66 19: 'Extension3',
67 20: 'RFU',
68 21: 'VGCS Group Identifier List (EFVGCS and EFVGCSS)',
69 22: 'VBS Group Identifier List (EFVBS and EFVBSS)',
70 23: 'enhanced Multi-Level Precedence and Pre-emption Service',
71 24: 'Automatic Answer for eMLPP',
72 25: 'Data download via SMS-CB',
73 26: 'Data download via SMS-PP',
74 27: 'Menu selection',
75 28: 'Call control',
76 29: 'Proactive SIM',
77 30: 'Cell Broadcast Message Identifier Ranges',
78 31: 'Barred Dialling Numbers (BDN)',
79 32: 'Extension4',
80 33: 'De-personalization Control Keys',
81 34: 'Co-operative Network List',
82 35: 'Short Message Status Reports',
83 36: 'Network\'s indication of alerting in the MS',
84 37: 'Mobile Originated Short Message control by SIM',
85 38: 'GPRS',
86 39: 'Image (IMG)',
87 40: 'SoLSA (Support of Local Service Area)',
88 41: 'USSD string data object supported in Call Control',
89 42: 'RUN AT COMMAND command',
90 43: 'User controlled PLMN Selector with Access Technology',
91 44: 'Operator controlled PLMN Selector with Access Technology',
92 45: 'HPLMN Selector with Access Technology',
93 46: 'CPBCCH Information',
94 47: 'Investigation Scan',
95 48: 'Extended Capability Configuration Parameters',
96 49: 'MExE',
97 50: 'Reserved and shall be ignored',
98 51: 'PLMN Network Name',
99 52: 'Operator PLMN List',
100 53: 'Mailbox Dialling Numbers',
101 54: 'Message Waiting Indication Status',
102 55: 'Call Forwarding Indication Status',
103 56: 'Service Provider Display Information',
104 57: 'Multimedia Messaging Service (MMS)',
105 58: 'Extension 8',
106 59: 'MMS User Connectivity Parameters',
Vadim Yanitskiydfe3dbb2020-07-28 05:26:02 +0700107}
108
Harald Welteb2edd142021-01-08 23:29:35 +0100109
110######################################################################
111# DF.TELECOM
112######################################################################
113
Harald Weltef11f1302023-01-24 17:59:59 +0100114
115# TS 51.011 Section 10.5.1 / Table 12
116class ExtendedBcdAdapter(Adapter):
117 """Replace some hex-characters with other ASCII characters"""
118 # we only translate a=* / b=# as they habe a clear representation
119 # in terms of USSD / SS service codes
120 def _decode(self, obj, context, path):
121 if not isinstance(obj, str):
122 return obj
123 return obj.lower().replace("a","*").replace("b","#")
124
125 def _encode(self, obj, context, path):
126 if not isinstance(obj, str):
127 return obj
128 return obj.replace("*","a").replace("#","b")
129
Harald Welteb2edd142021-01-08 23:29:35 +0100130# TS 51.011 Section 10.5.1
131class EF_ADN(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100132 _test_decode = [
133 ( '42204841203120536963ffffffff06810628560810ffffffffffffff',
134 { "alpha_id": "B HA 1 Sic", "len_of_bcd": 6, "ton_npi": { "ext": True, "type_of_number":
135 "unknown", "numbering_plan_id":
136 "isdn_e164" }, "dialing_nr":
137 "6082658001", "cap_conf_id": 255, "ext1_record_id": 255 }),
Harald Welte324175f2023-12-21 20:25:30 +0100138 ( '4B756E64656E626574726575756E67FFFFFF0791947112122721ffffffffffff',
139 {"alpha_id": "Kundenbetreuung", "len_of_bcd": 7, "ton_npi": {"ext": True, "type_of_number":
140 "international",
141 "numbering_plan_id": "isdn_e164"},
142 "dialing_nr": "491721217212", "cap_conf_id": 255, "ext1_record_id": 255} )
143 ]
Harald Welte865eea62023-01-27 19:26:12 +0100144
Harald Welte0dc6c202023-01-24 18:10:13 +0100145 def __init__(self, fid='6f3a', sfid=None, name='EF.ADN', desc='Abbreviated Dialing Numbers', ext=1, **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200146 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(14, 30), **kwargs)
Harald Welte0dc6c202023-01-24 18:10:13 +0100147 ext_name = 'ext%u_record_id' % ext
Harald Welte1e73d222022-02-13 11:32:50 +0100148 self._construct = Struct('alpha_id'/COptional(GsmStringAdapter(Rpad(Bytes(this._.total_len-14)), codec='ascii')),
149 'len_of_bcd'/Int8ub,
150 'ton_npi'/TonNpi,
Harald Weltef11f1302023-01-24 17:59:59 +0100151 'dialing_nr'/ExtendedBcdAdapter(BcdAdapter(Rpad(Bytes(10)))),
Harald Welte1e73d222022-02-13 11:32:50 +0100152 'cap_conf_id'/Int8ub,
Harald Welte0dc6c202023-01-24 18:10:13 +0100153 ext_name/Int8ub)
Harald Welteb2edd142021-01-08 23:29:35 +0100154
155# TS 51.011 Section 10.5.5
Harald Welteec7d0da2021-04-02 22:01:19 +0200156class EF_SMS(LinFixedEF):
Harald Welte6169c722022-02-12 09:05:15 +0100157 def __init__(self, fid='6f3c', sfid=None, name='EF.SMS', desc='Short messages', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200158 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(176, 176), **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100159
Harald Weltef6b37af2023-01-24 15:42:26 +0100160 def _decode_record_bin(self, raw_bin_data, **kwargs):
Harald Welteec7d0da2021-04-02 22:01:19 +0200161 def decode_status(status):
162 if status & 0x01 == 0x00:
163 return (None, 'free_space')
164 elif status & 0x07 == 0x01:
165 return ('mt', 'message_read')
166 elif status & 0x07 == 0x03:
167 return ('mt', 'message_to_be_read')
168 elif status & 0x07 == 0x07:
169 return ('mo', 'message_to_be_sent')
170 elif status & 0x1f == 0x05:
171 return ('mo', 'sent_status_not_requested')
172 elif status & 0x1f == 0x0d:
173 return ('mo', 'sent_status_req_but_not_received')
174 elif status & 0x1f == 0x15:
175 return ('mo', 'sent_status_req_rx_not_stored_smsr')
176 elif status & 0x1f == 0x1d:
177 return ('mo', 'sent_status_req_rx_stored_smsr')
178 else:
179 return (None, 'rfu')
180
181 status = decode_status(raw_bin_data[0])
182 remainder = raw_bin_data[1:]
183 return {'direction': status[0], 'status': status[1], 'remainder': b2h(remainder)}
184
185
186# TS 51.011 Section 10.5.5
Harald Welteb2edd142021-01-08 23:29:35 +0100187class EF_MSISDN(LinFixedEF):
Harald Welte6169c722022-02-12 09:05:15 +0100188 def __init__(self, fid='6f40', sfid=None, name='EF.MSISDN', desc='MSISDN', **kwargs):
Philipp Maier37e57e02023-09-07 12:43:12 +0200189 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(15, 34), leftpad=True, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100190
Harald Weltef6b37af2023-01-24 15:42:26 +0100191 def _decode_record_hex(self, raw_hex_data, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100192 return {'msisdn': dec_msisdn(raw_hex_data)}
Harald Weltec91085e2022-02-10 18:05:45 +0100193
Harald Weltef6b37af2023-01-24 15:42:26 +0100194 def _encode_record_hex(self, abstract, **kwargs):
Philipp Maierf9cbe092021-04-23 19:37:36 +0200195 msisdn = abstract['msisdn']
196 if type(msisdn) == str:
197 encoded_msisdn = enc_msisdn(msisdn)
198 else:
Harald Weltec91085e2022-02-10 18:05:45 +0100199 encoded_msisdn = enc_msisdn(msisdn[2], msisdn[0], msisdn[1])
Philipp Maier37e57e02023-09-07 12:43:12 +0200200 alpha_identifier = (list(self.rec_len)[0] - len(encoded_msisdn) // 2) * "ff"
Philipp Maierb46cb3f2021-04-20 22:38:21 +0200201 return alpha_identifier + encoded_msisdn
Harald Welteb2edd142021-01-08 23:29:35 +0100202
203# TS 51.011 Section 10.5.6
204class EF_SMSP(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100205 # FIXME: re-encode fails / missing alpha_id at start of output
206 _test_decode = [
207 ( '454e6574776f726b73fffffffffffffff1ffffffffffffffffffffffffffffffffffffffffffffffff0000a7',
208 { "alpha_id": "ENetworks", "parameter_indicators": { "tp_dest_addr": False, "tp_sc_addr": True,
209 "tp_pid": True, "tp_dcs": True, "tp_vp": True },
210 "tp_dest_addr": { "length": 255, "ton_npi": { "ext": True, "type_of_number": "reserved_for_extension",
211 "numbering_plan_id": "reserved_for_extension" },
212 "call_number": "" },
213 "tp_sc_addr": { "length": 255, "ton_npi": { "ext": True, "type_of_number": "reserved_for_extension",
214 "numbering_plan_id": "reserved_for_extension" },
215 "call_number": "" },
216 "tp_pid": "00", "tp_dcs": "00", "tp_vp_minutes": 1440 } ),
217 ]
Harald Weltebc0e2092022-02-13 10:54:58 +0100218 class ValidityPeriodAdapter(Adapter):
219 def _decode(self, obj, context, path):
220 if obj <= 143:
221 return obj + 1 * 5
222 elif obj <= 167:
223 return 12 * 60 + ((obj - 143) * 30)
224 elif obj <= 196:
225 return (obj - 166) * (24 * 60)
226 elif obj <= 255:
227 return (obj - 192) * (7 * 24 * 60)
228 else:
229 raise ValueError
230 def _encode(self, obj, context, path):
231 if obj <= 12*60:
232 return obj/5 - 1
233 elif obj <= 24*60:
Harald Welte136bdb02023-01-31 16:23:32 +0100234 return 143 + ((obj - (12 * 60)) // 30)
Harald Weltebc0e2092022-02-13 10:54:58 +0100235 elif obj <= 30 * 24 * 60:
236 return 166 + (obj / (24 * 60))
237 elif obj <= 63 * 7 * 24 * 60:
Harald Welte136bdb02023-01-31 16:23:32 +0100238 return 192 + (obj // (7 * 24 * 60))
Harald Weltebc0e2092022-02-13 10:54:58 +0100239 else:
240 raise ValueError
241
Harald Welte6169c722022-02-12 09:05:15 +0100242 def __init__(self, fid='6f42', sfid=None, name='EF.SMSP', desc='Short message service parameters', **kwargs):
Harald Welte99e4cc02022-07-21 15:25:47 +0200243 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(28, None), **kwargs)
Harald Weltebc0e2092022-02-13 10:54:58 +0100244 ScAddr = Struct('length'/Int8ub, 'ton_npi'/TonNpi, 'call_number'/BcdAdapter(Rpad(Bytes(10))))
245 self._construct = Struct('alpha_id'/COptional(GsmStringAdapter(Rpad(Bytes(this._.total_len-28)))),
246 'parameter_indicators'/InvertAdapter(FlagsEnum(Byte, tp_dest_addr=1, tp_sc_addr=2,
247 tp_pid=3, tp_dcs=4, tp_vp=5)),
248 'tp_dest_addr'/ScAddr,
249 'tp_sc_addr'/ScAddr,
250
251 'tp_pid'/HexAdapter(Bytes(1)),
252 'tp_dcs'/HexAdapter(Bytes(1)),
253 'tp_vp_minutes'/EF_SMSP.ValidityPeriodAdapter(Byte))
Harald Welteb2edd142021-01-08 23:29:35 +0100254
Harald Welte790b2702021-04-11 00:01:35 +0200255# TS 51.011 Section 10.5.7
256class EF_SMSS(TransparentEF):
257 class MemCapAdapter(Adapter):
258 def _decode(self, obj, context, path):
259 return False if obj & 1 else True
Harald Weltec91085e2022-02-10 18:05:45 +0100260
Harald Welte790b2702021-04-11 00:01:35 +0200261 def _encode(self, obj, context, path):
262 return 0 if obj else 1
Harald Weltec91085e2022-02-10 18:05:45 +0100263
Harald Welte13edf302022-07-21 15:19:23 +0200264 def __init__(self, fid='6f43', sfid=None, name='EF.SMSS', desc='SMS status', size=(2, 8), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100265 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100266 self._construct = Struct(
267 'last_used_tpmr'/Int8ub, 'memory_capacity_exceeded'/self.MemCapAdapter(Int8ub))
Harald Welte790b2702021-04-11 00:01:35 +0200268
269# TS 51.011 Section 10.5.8
270class EF_SMSR(LinFixedEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200271 def __init__(self, fid='6f47', sfid=None, name='EF.SMSR', desc='SMS status reports', rec_len=(30, 30), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100272 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100273 self._construct = Struct(
274 'sms_record_id'/Int8ub, 'sms_status_report'/HexAdapter(Bytes(29)))
275
Harald Welte790b2702021-04-11 00:01:35 +0200276
277class EF_EXT(LinFixedEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200278 def __init__(self, fid, sfid=None, name='EF.EXT', desc='Extension', rec_len=(13, 13), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100279 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100280 self._construct = Struct(
281 'record_type'/Int8ub, 'extension_data'/HexAdapter(Bytes(11)), 'identifier'/Int8ub)
Harald Welte790b2702021-04-11 00:01:35 +0200282
283# TS 51.011 Section 10.5.16
284class EF_CMI(LinFixedEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200285 def __init__(self, fid='6f58', sfid=None, name='EF.CMI', rec_len=(2, 21),
Harald Welte6169c722022-02-12 09:05:15 +0100286 desc='Comparison Method Information', **kwargs):
287 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100288 self._construct = Struct(
Harald Welte540adb02022-02-13 11:33:21 +0100289 'alpha_id'/GsmStringAdapter(Rpad(Bytes(this._.total_len-1))), 'comparison_method_id'/Int8ub)
Harald Weltec91085e2022-02-10 18:05:45 +0100290
Harald Welte790b2702021-04-11 00:01:35 +0200291
Harald Welteb2edd142021-01-08 23:29:35 +0100292class DF_TELECOM(CardDF):
Harald Welte6169c722022-02-12 09:05:15 +0100293 def __init__(self, fid='7f10', name='DF.TELECOM', desc=None, **kwargs):
294 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
Harald Welteb2edd142021-01-08 23:29:35 +0100295 files = [
Harald Weltec91085e2022-02-10 18:05:45 +0100296 EF_ADN(),
Harald Welte0dc6c202023-01-24 18:10:13 +0100297 EF_ADN(fid='6f3b', name='EF.FDN', desc='Fixed dialling numbers', ext=2),
Harald Weltec91085e2022-02-10 18:05:45 +0100298 EF_SMS(),
299 LinFixedEF(fid='6f3d', name='EF.CCP',
Harald Welte99e4cc02022-07-21 15:25:47 +0200300 desc='Capability Configuration Parameters', rec_len=(14, 14)),
Harald Weltec91085e2022-02-10 18:05:45 +0100301 LinFixedEF(fid='6f4f', name='EF.ECCP',
Harald Welte99e4cc02022-07-21 15:25:47 +0200302 desc='Extended Capability Configuration Parameters', rec_len=(15, 32)),
Harald Weltec91085e2022-02-10 18:05:45 +0100303 EF_MSISDN(),
304 EF_SMSP(),
305 EF_SMSS(),
Harald Weltea1bb3f72023-01-24 18:15:56 +0100306 EF_ADN('6f44', None, 'EF.LND', 'Last Number Dialled', ext=1),
Harald Welte0dc6c202023-01-24 18:10:13 +0100307 EF_ADN('6f49', None, 'EF.SDN', 'Service Dialling Numbers', ext=3),
Harald Weltec91085e2022-02-10 18:05:45 +0100308 EF_EXT('6f4a', None, 'EF.EXT1', 'Extension1 (ADN/SSC)'),
309 EF_EXT('6f4b', None, 'EF.EXT2', 'Extension2 (FDN/SSC)'),
310 EF_EXT('6f4c', None, 'EF.EXT3', 'Extension3 (SDN)'),
311 EF_ADN(fid='6f4d', name='EF.BDN', desc='Barred Dialling Numbers'),
312 EF_EXT('6f4e', None, 'EF.EXT4', 'Extension4 (BDN/SSC)'),
313 EF_SMSR(),
314 EF_CMI(),
Harald Weltede4c14c2022-07-16 11:53:59 +0200315 # not really part of 51.011 but something that TS 31.102 specifies may exist here.
316 DF_PHONEBOOK(),
Harald Weltea0452212022-07-17 21:23:21 +0200317 DF_MULTIMEDIA(),
Harald Welte650f6122022-07-17 21:42:50 +0200318 DF_MCS(),
Harald Welte228ae8e2022-07-17 22:01:04 +0200319 DF_V2X(),
Harald Weltec91085e2022-02-10 18:05:45 +0100320 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100321 self.add_files(files)
322
Harald Welteb2edd142021-01-08 23:29:35 +0100323######################################################################
324# DF.GSM
325######################################################################
326
327# TS 51.011 Section 10.3.1
328class EF_LP(TransRecEF):
Harald Welte865eea62023-01-27 19:26:12 +0100329 _test_de_encode = [
330 ( "24", "24"),
331 ]
Harald Welte13edf302022-07-21 15:19:23 +0200332 def __init__(self, fid='6f05', sfid=None, name='EF.LP', size=(1, None), rec_len=1,
Harald Welteb2edd142021-01-08 23:29:35 +0100333 desc='Language Preference'):
334 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
Harald Weltec91085e2022-02-10 18:05:45 +0100335
Harald Weltef6b37af2023-01-24 15:42:26 +0100336 def _decode_record_bin(self, in_bin, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100337 return b2h(in_bin)
Harald Weltec91085e2022-02-10 18:05:45 +0100338
Harald Weltef6b37af2023-01-24 15:42:26 +0100339 def _encode_record_bin(self, in_json, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100340 return h2b(in_json)
341
342# TS 51.011 Section 10.3.2
343class EF_IMSI(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100344 _test_de_encode = [
345 ( "082982608200002080", { "imsi": "228062800000208" } ),
346 ( "082926101160845740", { "imsi": "262011106487504" } ),
347 ]
Harald Welte13edf302022-07-21 15:19:23 +0200348 def __init__(self, fid='6f07', sfid=None, name='EF.IMSI', desc='IMSI', size=(9, 9)):
Harald Welteb2edd142021-01-08 23:29:35 +0100349 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Bjoern Riemere91405e2021-12-17 15:16:35 +0100350 # add those commands to the general commands of a TransparentEF
351 self.shell_commands += [self.AddlShellCommands(self)]
Harald Weltec91085e2022-02-10 18:05:45 +0100352
Harald Welteb2edd142021-01-08 23:29:35 +0100353 def _decode_hex(self, raw_hex):
354 return {'imsi': dec_imsi(raw_hex)}
Harald Weltec91085e2022-02-10 18:05:45 +0100355
Harald Welteb2edd142021-01-08 23:29:35 +0100356 def _encode_hex(self, abstract):
357 return enc_imsi(abstract['imsi'])
Harald Weltec91085e2022-02-10 18:05:45 +0100358
Bjoern Riemere91405e2021-12-17 15:16:35 +0100359 @with_default_category('File-Specific Commands')
360 class AddlShellCommands(CommandSet):
Harald Weltec91085e2022-02-10 18:05:45 +0100361 def __init__(self, ef: TransparentEF):
Bjoern Riemere91405e2021-12-17 15:16:35 +0100362 super().__init__()
Harald Weltec91085e2022-02-10 18:05:45 +0100363 self._ef = ef
Bjoern Riemere91405e2021-12-17 15:16:35 +0100364
365 def do_update_imsi_plmn(self, arg: str):
366 """Change the plmn part of the IMSI"""
367 plmn = arg.strip()
368 if len(plmn) == 5 or len(plmn) == 6:
Harald Weltea6c0f882022-07-17 14:23:17 +0200369 (data, sw) = self._cmd.lchan.read_binary_dec()
Bjoern Riemere91405e2021-12-17 15:16:35 +0100370 if sw == '9000' and len(data['imsi'])-len(plmn) == 10:
371 imsi = data['imsi']
372 msin = imsi[len(plmn):]
Harald Weltea6c0f882022-07-17 14:23:17 +0200373 (data, sw) = self._cmd.lchan.update_binary_dec(
Harald Weltec91085e2022-02-10 18:05:45 +0100374 {'imsi': plmn+msin})
Bjoern Riemere91405e2021-12-17 15:16:35 +0100375 if sw == '9000' and data:
Harald Weltec91085e2022-02-10 18:05:45 +0100376 self._cmd.poutput_json(
Harald Weltea6c0f882022-07-17 14:23:17 +0200377 self._cmd.lchan.selected_file.decode_hex(data))
Bjoern Riemere91405e2021-12-17 15:16:35 +0100378 else:
379 raise ValueError("PLMN length does not match IMSI length")
380 else:
381 raise ValueError("PLMN has wrong length!")
382
Harald Welteb2edd142021-01-08 23:29:35 +0100383
384# TS 51.011 Section 10.3.4
385class EF_PLMNsel(TransRecEF):
Harald Welte865eea62023-01-27 19:26:12 +0100386 _test_de_encode = [
387 ( "22F860", { "mcc": "228", "mnc": "06" } ),
388 ( "330420", { "mcc": "334", "mnc": "020" } ),
389 ]
Harald Welteb2edd142021-01-08 23:29:35 +0100390 def __init__(self, fid='6f30', sfid=None, name='EF.PLMNsel', desc='PLMN selector',
Harald Welte13edf302022-07-21 15:19:23 +0200391 size=(24, None), rec_len=3, **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100392 super().__init__(fid, name=name, sfid=sfid, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100393
Harald Weltef6b37af2023-01-24 15:42:26 +0100394 def _decode_record_hex(self, in_hex, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100395 if in_hex[:6] == "ffffff":
396 return None
397 else:
398 return dec_plmn(in_hex)
Harald Weltec91085e2022-02-10 18:05:45 +0100399
Harald Weltef6b37af2023-01-24 15:42:26 +0100400 def _encode_record_hex(self, in_json, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100401 if in_json == None:
402 return "ffffff"
403 else:
404 return enc_plmn(in_json['mcc'], in_json['mnc'])
405
Harald Welte790b2702021-04-11 00:01:35 +0200406# TS 51.011 Section 10.3.6
407class EF_ACMmax(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100408 _test_de_encode = [
409 ( "000000", { "acm_max": 0 } ),
410 ]
Harald Welte13edf302022-07-21 15:19:23 +0200411 def __init__(self, fid='6f37', sfid=None, name='EF.ACMmax', size=(3, 3),
Harald Welte6169c722022-02-12 09:05:15 +0100412 desc='ACM maximum value', **kwargs):
413 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200414 self._construct = Struct('acm_max'/Int24ub)
415
Harald Welteb2edd142021-01-08 23:29:35 +0100416# TS 51.011 Section 10.3.7
417class EF_ServiceTable(TransparentEF):
418 def __init__(self, fid, sfid, name, desc, size, table):
419 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
420 self.table = table
Harald Weltec224b3b2023-05-23 18:44:44 +0200421 self.shell_commands += [self.AddlShellCommands()]
Harald Weltec91085e2022-02-10 18:05:45 +0100422
Harald Welte7a8aa862021-10-14 20:46:19 +0200423 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +0100424 def _bit_byte_offset_for_service(service: int) -> Tuple[int, int]:
Harald Welte7a8aa862021-10-14 20:46:19 +0200425 i = service - 1
426 byte_offset = i//4
427 bit_offset = (i % 4) * 2
428 return (byte_offset, bit_offset)
Harald Weltec91085e2022-02-10 18:05:45 +0100429
Harald Welteb2edd142021-01-08 23:29:35 +0100430 def _decode_bin(self, raw_bin):
431 ret = {}
432 for i in range(0, len(raw_bin)*4):
433 service_nr = i+1
434 byte = int(raw_bin[i//4])
435 bit_offset = (i % 4) * 2
436 bits = (byte >> bit_offset) & 3
437 ret[service_nr] = {
Harald Weltec91085e2022-02-10 18:05:45 +0100438 'description': self.table[service_nr] if service_nr in self.table else None,
439 'allocated': True if bits & 1 else False,
440 'activated': True if bits & 2 else False,
441 }
Harald Welteb2edd142021-01-08 23:29:35 +0100442 return ret
Harald Weltec91085e2022-02-10 18:05:45 +0100443
Harald Welte7a8aa862021-10-14 20:46:19 +0200444 def _encode_bin(self, in_json):
445 # compute the required binary size
446 bin_len = 0
447 for srv in in_json.keys():
448 service_nr = int(srv)
Vadim Yanitskiy6b19d802023-04-22 19:55:00 +0700449 (byte_offset, bit_offset) = self._bit_byte_offset_for_service(service_nr)
Harald Welte7a8aa862021-10-14 20:46:19 +0200450 if byte_offset >= bin_len:
451 bin_len = byte_offset+1
452 # encode the actual data
453 out = bytearray(b'\x00' * bin_len)
454 for srv in in_json.keys():
455 service_nr = int(srv)
Vadim Yanitskiy6b19d802023-04-22 19:55:00 +0700456 (byte_offset, bit_offset) = self._bit_byte_offset_for_service(service_nr)
Harald Welte7a8aa862021-10-14 20:46:19 +0200457 bits = 0
458 if in_json[srv]['allocated'] == True:
459 bits |= 1
460 if in_json[srv]['activated'] == True:
461 bits |= 2
462 out[byte_offset] |= ((bits & 3) << bit_offset)
463 return out
Harald Welteb2edd142021-01-08 23:29:35 +0100464
Harald Weltec224b3b2023-05-23 18:44:44 +0200465 @with_default_category('File-Specific Commands')
466 class AddlShellCommands(CommandSet):
467 def _adjust_service(self, service_nr: int, allocate: Optional[bool] = None, activate : Optional[bool] = None):
468 (byte_offset, bit_offset) = EF_ServiceTable._bit_byte_offset_for_service(service_nr)
469 hex_data, sw = self._cmd.lchan.read_binary(length=1, offset=byte_offset)
470 data = h2b(hex_data)
471 if allocate is not None:
472 if allocate:
473 data[0] |= (1 << bit_offset)
474 else:
475 data[0] &= ~(1 << bit_offset)
476 if activate is not None:
477 if activate:
478 data[0] |= (2 << bit_offset)
479 else:
480 data[0] &= ~(2 << bit_offset)
481 total_data, sw = self._cmd.lchan.update_binary(b2h(data), offset=byte_offset)
482 return sw
483
484 def do_sst_service_allocate(self, arg):
485 """Allocate a service within EF.SST"""
486 self._adjust_service(int(arg), allocate = True)
487
488 def do_sst_service_deallocate(self, arg):
489 """Deallocate a service within EF.SST"""
490 self._adjust_service(int(arg), allocate = False)
491
492 def do_sst_service_activate(self, arg):
493 """Activate a service within EF.SST"""
494 self._adjust_service(int(arg), activate = True)
495
496 def do_sst_service_deactivate(self, arg):
497 """Deactivate a service within EF.SST"""
498 self._adjust_service(int(arg), activate = False)
499
500
Harald Welteb2edd142021-01-08 23:29:35 +0100501# TS 51.011 Section 10.3.11
502class EF_SPN(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100503 _test_de_encode = [
504 ( "0147534d2d52204348ffffffffffffffff",
505 { "rfu": 0, "hide_in_oplmn": False, "show_in_hplmn": True, "spn": "GSM-R CH" } ),
506 ]
507
Harald Welte6169c722022-02-12 09:05:15 +0100508 def __init__(self, fid='6f46', sfid=None, name='EF.SPN',
Harald Welte13edf302022-07-21 15:19:23 +0200509 desc='Service Provider Name', size=(17, 17), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100510 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Robert Falkenbergb07a3e92021-05-07 15:23:20 +0200511 self._construct = BitStruct(
512 # Byte 1
513 'rfu'/BitsRFU(6),
514 'hide_in_oplmn'/Flag,
515 'show_in_hplmn'/Flag,
516 # Bytes 2..17
517 'spn'/Bytewise(GsmString(16))
518 )
Harald Welteb2edd142021-01-08 23:29:35 +0100519
520# TS 51.011 Section 10.3.13
521class EF_CBMI(TransRecEF):
Harald Welte865eea62023-01-27 19:26:12 +0100522 # TODO: Test vectors
Harald Welte13edf302022-07-21 15:19:23 +0200523 def __init__(self, fid='6f45', sfid=None, name='EF.CBMI', size=(2, None), rec_len=2,
Harald Welte6169c722022-02-12 09:05:15 +0100524 desc='Cell Broadcast message identifier selection', **kwargs):
525 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200526 self._construct = GreedyRange(Int16ub)
Harald Welteb2edd142021-01-08 23:29:35 +0100527
528# TS 51.011 Section 10.3.15
529class EF_ACC(TransparentEF):
Vadim Yanitskiyb34f2342023-04-18 04:25:27 +0700530 # example: acc_list(15) will produce a dict with only ACC15 being True
531 # example: acc_list(2, 4) will produce a dict with ACC2 and ACC4 being True
532 # example: acc_list() will produce a dict with all ACCs being False
533 acc_list = lambda *active: {'ACC{}'.format(c) : c in active for c in range(16)}
534
Harald Welte865eea62023-01-27 19:26:12 +0100535 _test_de_encode = [
Vadim Yanitskiyb34f2342023-04-18 04:25:27 +0700536 ( "0000", acc_list() ),
537 ( "0001", acc_list(0) ),
538 ( "0002", acc_list(1) ),
539 ( "0100", acc_list(8) ),
540 ( "8000", acc_list(15) ),
541 ( "802b", acc_list(0,1,3,5,15) ),
Harald Welte865eea62023-01-27 19:26:12 +0100542 ]
Harald Welte6169c722022-02-12 09:05:15 +0100543 def __init__(self, fid='6f78', sfid=None, name='EF.ACC',
Harald Welte13edf302022-07-21 15:19:23 +0200544 desc='Access Control Class', size=(2, 2), **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100545 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Vadim Yanitskiyb34f2342023-04-18 04:25:27 +0700546 # LSB of octet 2 is ACC=0 ... MSB of octet 1 is ACC=15
547 flags = ['ACC{}'.format(c) / Flag for c in range(16)]
548 self._construct = ByteSwapped(BitsSwapped(BitStruct(*flags)))
Harald Welteb2edd142021-01-08 23:29:35 +0100549
Harald Welte790b2702021-04-11 00:01:35 +0200550# TS 51.011 Section 10.3.16
551class EF_LOCI(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100552 _test_de_encode = [
553 ( "7802570222f81009780000",
554 { "tmsi": "78025702", "lai": "22f8100978", "tmsi_time": 0, "lu_status": "updated" } ),
555 ]
Harald Welte13edf302022-07-21 15:19:23 +0200556 def __init__(self, fid='6f7e', sfid=None, name='EF.LOCI', desc='Location Information', size=(11, 11)):
Harald Welte790b2702021-04-11 00:01:35 +0200557 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200558 self._construct = Struct('tmsi'/HexAdapter(Bytes(4)), 'lai'/HexAdapter(Bytes(5)), 'tmsi_time'/Int8ub,
Harald Welte790b2702021-04-11 00:01:35 +0200559 'lu_status'/Enum(Byte, updated=0, not_updated=1, plmn_not_allowed=2,
560 location_area_not_allowed=3))
561
Harald Welteb2edd142021-01-08 23:29:35 +0100562# TS 51.011 Section 10.3.18
563class EF_AD(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100564 _test_de_encode = [
565 ( "00ffff",
566 { "ms_operation_mode": "normal", "rfu1": 255, "rfu2": 127, "ofm": True, "extensions": None } ),
567 ]
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200568 class OP_MODE(enum.IntEnum):
Harald Weltec91085e2022-02-10 18:05:45 +0100569 normal = 0x00
570 type_approval = 0x80
571 normal_and_specific_facilities = 0x01
572 type_approval_and_specific_facilities = 0x81
573 maintenance_off_line = 0x02
574 cell_test = 0x04
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200575 #OP_MODE_DICT = {int(v) : str(v) for v in EF_AD.OP_MODE}
576 #OP_MODE_DICT_REVERSED = {str(v) : int(v) for v in EF_AD.OP_MODE}
577
Harald Welte13edf302022-07-21 15:19:23 +0200578 def __init__(self, fid='6fad', sfid=None, name='EF.AD', desc='Administrative Data', size=(3, 4)):
Harald Welteb2edd142021-01-08 23:29:35 +0100579 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +0200580 self._construct = BitStruct(
581 # Byte 1
582 'ms_operation_mode'/Bytewise(Enum(Byte, EF_AD.OP_MODE)),
583 # Byte 2
584 'rfu1'/Bytewise(ByteRFU),
585 # Byte 3
586 'rfu2'/BitsRFU(7),
587 'ofm'/Flag,
588 # Byte 4 (optional),
589 'extensions'/COptional(Struct(
590 'rfu3'/BitsRFU(4),
591 'mnc_len'/BitsInteger(4),
592 # Byte 5..N-4 (optional, RFU)
593 'extensions'/Bytewise(GreedyBytesRFU)
594 ))
595 )
Harald Welteb2edd142021-01-08 23:29:35 +0100596
Harald Welte790b2702021-04-11 00:01:35 +0200597# TS 51.011 Section 10.3.20 / 10.3.22
598class EF_VGCS(TransRecEF):
Harald Welte865eea62023-01-27 19:26:12 +0100599 _test_de_encode = [
600 ( "92f9ffff", "299fffff" ),
601 ]
Harald Welte13edf302022-07-21 15:19:23 +0200602 def __init__(self, fid='6fb1', sfid=None, name='EF.VGCS', size=(4, 200), rec_len=4,
Harald Welte6169c722022-02-12 09:05:15 +0100603 desc='Voice Group Call Service', **kwargs):
604 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200605 self._construct = BcdAdapter(Bytes(4))
606
607# TS 51.011 Section 10.3.21 / 10.3.23
608class EF_VGCSS(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100609 _test_decode = [
610 ( "010000004540fc",
611 { "flags": [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
612 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ] }
613 ),
614 ]
Harald Welte13edf302022-07-21 15:19:23 +0200615 def __init__(self, fid='6fb2', sfid=None, name='EF.VGCSS', size=(7, 7),
Harald Welte6169c722022-02-12 09:05:15 +0100616 desc='Voice Group Call Service Status', **kwargs):
617 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte5b9472d2023-01-24 17:26:59 +0100618 self._construct = BitsSwapped(BitStruct(
619 'flags'/Bit[50], Padding(6, pattern=b'\xff')))
Harald Welte790b2702021-04-11 00:01:35 +0200620
621# TS 51.011 Section 10.3.24
622class EF_eMLPP(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100623 _test_de_encode = [
624 ( "7c04", { "levels": { "A": False, "B": False, "zero": True, "one": True,
625 "two": True, "three": True, "four": True },
626 "fast_call_setup_cond": { "A": False, "B": False, "zero": True, "one": False,
627 "two": False, "three": False, "four": False }
628 }),
629 ]
Harald Welte13edf302022-07-21 15:19:23 +0200630 def __init__(self, fid='6fb5', sfid=None, name='EF.eMLPP', size=(2, 2),
Harald Welte6169c722022-02-12 09:05:15 +0100631 desc='enhanced Multi Level Pre-emption and Priority', **kwargs):
632 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100633 FlagsConstruct = FlagsEnum(
634 Byte, A=1, B=2, zero=4, one=8, two=16, three=32, four=64)
635 self._construct = Struct(
636 'levels'/FlagsConstruct, 'fast_call_setup_cond'/FlagsConstruct)
Harald Welte790b2702021-04-11 00:01:35 +0200637
638# TS 51.011 Section 10.3.25
639class EF_AAeM(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100640 _test_de_encode = [
641 ( "3c", { "auto_answer_prio_levels": { "A": False, "B": False, "zero": True, "one": True,
642 "two": True, "three": True, "four": False } } ),
643 ]
Harald Welte13edf302022-07-21 15:19:23 +0200644 def __init__(self, fid='6fb6', sfid=None, name='EF.AAeM', size=(1, 1),
Harald Welte6169c722022-02-12 09:05:15 +0100645 desc='Automatic Answer for eMLPP Service', **kwargs):
646 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100647 FlagsConstruct = FlagsEnum(
648 Byte, A=1, B=2, zero=4, one=8, two=16, three=32, four=64)
Harald Welte790b2702021-04-11 00:01:35 +0200649 self._construct = Struct('auto_answer_prio_levels'/FlagsConstruct)
650
651# TS 51.011 Section 10.3.26
Harald Welteb2edd142021-01-08 23:29:35 +0100652class EF_CBMID(EF_CBMI):
Harald Welte13edf302022-07-21 15:19:23 +0200653 def __init__(self, fid='6f48', sfid=None, name='EF.CBMID', size=(2, None), rec_len=2,
Harald Welte6169c722022-02-12 09:05:15 +0100654 desc='Cell Broadcast Message Identifier for Data Download', **kwargs):
655 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200656 self._construct = GreedyRange(Int16ub)
Harald Welteb2edd142021-01-08 23:29:35 +0100657
Harald Welte89e59542021-04-02 21:33:13 +0200658# TS 51.011 Section 10.3.27
659class EF_ECC(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200660 def __init__(self, fid='6fb7', sfid=None, name='EF.ECC', size=(3, 15), rec_len=3,
Harald Welte6169c722022-02-12 09:05:15 +0100661 desc='Emergency Call Codes', **kwargs):
662 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200663 self._construct = GreedyRange(BcdAdapter(Bytes(3)))
Harald Welteb2edd142021-01-08 23:29:35 +0100664
665# TS 51.011 Section 10.3.28
666class EF_CBMIR(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200667 def __init__(self, fid='6f50', sfid=None, name='EF.CBMIR', size=(4, None), rec_len=4,
Harald Welte6169c722022-02-12 09:05:15 +0100668 desc='Cell Broadcast message identifier range selection', **kwargs):
669 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200670 self._construct = GreedyRange(Struct('lower'/Int16ub, 'upper'/Int16ub))
Harald Welteb2edd142021-01-08 23:29:35 +0100671
Harald Welte790b2702021-04-11 00:01:35 +0200672# TS 51.011 Section 10.3.29
673class EF_DCK(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200674 def __init__(self, fid='6f2c', sfid=None, name='EF.DCK', size=(16, 16),
Harald Welte6169c722022-02-12 09:05:15 +0100675 desc='Depersonalisation Control Keys', **kwargs):
676 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200677 self._construct = Struct('network'/BcdAdapter(Bytes(4)),
678 'network_subset'/BcdAdapter(Bytes(4)),
679 'service_provider'/BcdAdapter(Bytes(4)),
680 'corporate'/BcdAdapter(Bytes(4)))
681# TS 51.011 Section 10.3.30
682class EF_CNL(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200683 def __init__(self, fid='6f32', sfid=None, name='EF.CNL', size=(6, None), rec_len=6,
Harald Welte6169c722022-02-12 09:05:15 +0100684 desc='Co-operative Network List', **kwargs):
685 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100686
Harald Weltef6b37af2023-01-24 15:42:26 +0100687 def _decode_record_hex(self, in_hex, **kwargs):
Harald Welte790b2702021-04-11 00:01:35 +0200688 (in_plmn, sub, svp, corp) = unpack('!3sBBB', h2b(in_hex))
689 res = dec_plmn(b2h(in_plmn))
690 res['network_subset'] = sub
691 res['service_provider_id'] = svp
692 res['corporate_id'] = corp
693 return res
Harald Weltec91085e2022-02-10 18:05:45 +0100694
Harald Weltef6b37af2023-01-24 15:42:26 +0100695 def _encode_record_hex(self, in_json, **kwargs):
Harald Welte790b2702021-04-11 00:01:35 +0200696 plmn = enc_plmn(in_json['mcc'], in_json['mnc'])
Vadim Yanitskiy1a95d2b2021-05-02 01:42:09 +0200697 return b2h(pack('!3sBBB',
698 h2b(plmn),
699 in_json['network_subset'],
700 in_json['service_provider_id'],
701 in_json['corporate_id']))
Harald Welte790b2702021-04-11 00:01:35 +0200702
703# TS 51.011 Section 10.3.31
704class EF_NIA(LinFixedEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200705 def __init__(self, fid='6f51', sfid=None, name='EF.NIA', rec_len=(1, 32),
Harald Welte6169c722022-02-12 09:05:15 +0100706 desc='Network\'s Indication of Alerting', **kwargs):
707 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100708 self._construct = Struct(
709 'alerting_category'/Int8ub, 'category'/GreedyBytes)
Harald Welte790b2702021-04-11 00:01:35 +0200710
711# TS 51.011 Section 10.3.32
712class EF_Kc(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100713 _test_de_encode = [
714 ( "837d783609a3858f05", { "kc": "837d783609a3858f", "cksn": 5 } ),
715 ]
Harald Welte13edf302022-07-21 15:19:23 +0200716 def __init__(self, fid='6f20', sfid=None, name='EF.Kc', desc='Ciphering key Kc', size=(9, 9), **kwargs):
Harald Welted90ceb82022-07-17 22:10:58 +0200717 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200718 self._construct = Struct('kc'/HexAdapter(Bytes(8)), 'cksn'/Int8ub)
719
720# TS 51.011 Section 10.3.33
721class EF_LOCIGPRS(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100722 _test_de_encode = [
723 ( "ffffffffffffff22f8990000ff01",
724 { "ptmsi": "ffffffff", "ptmsi_sig": "ffffff", "rai": "22f8990000ff", "rau_status": "not_updated" } ),
725 ]
Harald Welte13edf302022-07-21 15:19:23 +0200726 def __init__(self, fid='6f53', sfid=None, name='EF.LOCIGPRS', desc='GPRS Location Information', size=(14, 14)):
Harald Welte790b2702021-04-11 00:01:35 +0200727 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
Harald Welted2edd412023-01-31 16:49:03 +0100728 self._construct = Struct('ptmsi'/HexAdapter(Bytes(4)), 'ptmsi_sig'/HexAdapter(Bytes(3)),
729 'rai'/HexAdapter(Bytes(6)),
Harald Welte790b2702021-04-11 00:01:35 +0200730 'rau_status'/Enum(Byte, updated=0, not_updated=1, plmn_not_allowed=2,
731 routing_area_not_allowed=3))
Harald Welteb2edd142021-01-08 23:29:35 +0100732
733# TS 51.011 Section 10.3.35..37
734class EF_xPLMNwAcT(TransRecEF):
Harald Welte865eea62023-01-27 19:26:12 +0100735 _test_de_encode = [
Harald Welte542dbf62023-12-21 20:14:48 +0100736 ( '62F2104000', { "mcc": "262", "mnc": "01", "act": [ "E-UTRAN NB-S1", "E-UTRAN WB-S1" ] } ),
Harald Welte865eea62023-01-27 19:26:12 +0100737 ( '62F2108000', { "mcc": "262", "mnc": "01", "act": [ "UTRAN" ] } ),
Harald Welte324175f2023-12-21 20:25:30 +0100738 ( '62F220488C', { "mcc": "262", "mnc": "02", "act": ['E-UTRAN NB-S1', 'E-UTRAN WB-S1', 'EC-GSM-IoT', 'GSM', 'NG-RAN'] } ),
Harald Welte865eea62023-01-27 19:26:12 +0100739 ]
740 def __init__(self, fid='1234', sfid=None, name=None, desc=None, size=(40, None), rec_len=5, **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100741 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100742
Harald Weltef6b37af2023-01-24 15:42:26 +0100743 def _decode_record_hex(self, in_hex, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100744 if in_hex[:6] == "ffffff":
745 return None
746 else:
747 return dec_xplmn_w_act(in_hex)
Harald Weltec91085e2022-02-10 18:05:45 +0100748
Harald Weltef6b37af2023-01-24 15:42:26 +0100749 def _encode_record_hex(self, in_json, **kwargs):
Harald Welteb2edd142021-01-08 23:29:35 +0100750 if in_json == None:
751 return "ffffff0000"
752 else:
753 hplmn = enc_plmn(in_json['mcc'], in_json['mnc'])
754 act = self.enc_act(in_json['act'])
755 return hplmn + act
Harald Weltec91085e2022-02-10 18:05:45 +0100756
Harald Welteb2edd142021-01-08 23:29:35 +0100757 @staticmethod
758 def enc_act(in_list):
759 u16 = 0
760 # first the simple ones
761 if 'UTRAN' in in_list:
762 u16 |= 0x8000
763 if 'NG-RAN' in in_list:
764 u16 |= 0x0800
765 if 'GSM COMPACT' in in_list:
766 u16 |= 0x0040
767 if 'cdma2000 HRPD' in in_list:
768 u16 |= 0x0020
769 if 'cdma2000 1xRTT' in in_list:
770 u16 |= 0x0010
771 # E-UTRAN
Harald Welte542dbf62023-12-21 20:14:48 +0100772 if 'E-UTRAN WB-S1' in in_list and 'E-UTRAN NB-S1' in in_list:
Philipp Maiere7d41792021-04-29 16:20:07 +0200773 u16 |= 0x4000
Harald Welte542dbf62023-12-21 20:14:48 +0100774 elif 'E-UTRAN WB-S1' in in_list:
Vadim Yanitskiy5452d642021-03-07 21:45:34 +0100775 u16 |= 0x6000
Harald Welte542dbf62023-12-21 20:14:48 +0100776 elif 'E-UTRAN NB-S1' in in_list:
Vadim Yanitskiy5452d642021-03-07 21:45:34 +0100777 u16 |= 0x5000
Harald Welteb2edd142021-01-08 23:29:35 +0100778 # GSM mess
779 if 'GSM' in in_list and 'EC-GSM-IoT' in in_list:
780 u16 |= 0x008C
781 elif 'GSM' in in_list:
782 u16 |= 0x0084
Harald Welte542dbf62023-12-21 20:14:48 +0100783 elif 'EC-GSM-IoT' in in_list:
Harald Welteb2edd142021-01-08 23:29:35 +0100784 u16 |= 0x0088
Harald Weltec91085e2022-02-10 18:05:45 +0100785 return '%04X' % (u16)
Harald Welteb2edd142021-01-08 23:29:35 +0100786
Harald Welte790b2702021-04-11 00:01:35 +0200787# TS 51.011 Section 10.3.38
788class EF_CPBCCH(TransRecEF):
Harald Welte13edf302022-07-21 15:19:23 +0200789 def __init__(self, fid='6f63', sfid=None, name='EF.CPBCCH', size=(2, 14), rec_len=2,
Harald Welted90ceb82022-07-17 22:10:58 +0200790 desc='CPBCCH Information', **kwargs):
791 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200792 self._construct = Struct('cpbcch'/Int16ub)
793
794# TS 51.011 Section 10.3.39
795class EF_InvScan(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200796 def __init__(self, fid='6f64', sfid=None, name='EF.InvScan', size=(1, 1),
Harald Welted90ceb82022-07-17 22:10:58 +0200797 desc='IOnvestigation Scan', **kwargs):
798 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Weltec91085e2022-02-10 18:05:45 +0100799 self._construct = FlagsEnum(
800 Byte, in_limited_service_mode=1, after_successful_plmn_selection=2)
Harald Welte790b2702021-04-11 00:01:35 +0200801
Harald Welte53762512023-12-21 20:16:17 +0100802# TS 51.011 Section 10.3.46
803class EF_CFIS(LinFixedEF):
804 _test_decode = [
805 ( '0100ffffffffffffffffffffffffffff',
806 {"msp_number": 1, "cfu_indicator_status": { "voice": False, "fax": False, "data": False, "rfu": 0 },
807 "len_of_bcd": 255, "ton_npi": {"ext": True,
808 "type_of_number": "reserved_for_extension",
809 "numbering_plan_id": "reserved_for_extension"},
810 "dialing_nr": "", "cap_conf_id": 255, "ext7_record_id": 255} ),
811 ]
812 def __init__(self, fid='6fcb', sfid=None, name='EF.CFIS', desc='Call Forwarding Indication Status', ext=7, **kwargs):
813 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(16, 30), **kwargs)
814 ext_name = 'ext%u_record_id' % ext
815 self._construct = Struct('msp_number'/Int8ub,
816 'cfu_indicator_status'/BitStruct('voice'/Flag, 'fax'/Flag, 'data'/Flag, 'rfu'/BitsRFU(5)),
817 'len_of_bcd'/Int8ub,
818 'ton_npi'/TonNpi,
819 'dialing_nr'/ExtendedBcdAdapter(BcdAdapter(Rpad(Bytes(10)))),
820 'cap_conf_id'/Int8ub,
821 ext_name/Int8ub)
822
Harald Welte14105dc2021-05-31 08:48:51 +0200823# TS 51.011 Section 4.2.58
824class EF_PNN(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100825 # TODO: 430a82d432bbbc7eb75de432450a82d432bbbc7eb75de432ffffffff
826 # TODO: 430a82c596b34cbfbfe5eb39ffffffffffffffffffffffffffffffffffff
Harald Welte14105dc2021-05-31 08:48:51 +0200827 class FullNameForNetwork(BER_TLV_IE, tag=0x43):
828 # TS 24.008 10.5.3.5a
Harald Welte857f1102022-07-20 07:44:25 +0200829 # TODO: proper decode
830 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100831
Harald Welte14105dc2021-05-31 08:48:51 +0200832 class ShortNameForNetwork(BER_TLV_IE, tag=0x45):
833 # TS 24.008 10.5.3.5a
Harald Welte857f1102022-07-20 07:44:25 +0200834 # TODO: proper decode
835 _construct = HexAdapter(GreedyBytes)
Harald Weltec91085e2022-02-10 18:05:45 +0100836
Harald Welte14105dc2021-05-31 08:48:51 +0200837 class NetworkNameCollection(TLV_IE_Collection, nested=[FullNameForNetwork, ShortNameForNetwork]):
838 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100839
Harald Welte6169c722022-02-12 09:05:15 +0100840 def __init__(self, fid='6fc5', sfid=None, name='EF.PNN', desc='PLMN Network Name', **kwargs):
841 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200842 self._tlv = EF_PNN.NetworkNameCollection
843
Harald Welte790b2702021-04-11 00:01:35 +0200844# TS 51.011 Section 10.3.42
845class EF_OPL(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100846 _test_de_encode = [
847 ( '62f2100000fffe01',
Harald Welte842fbdb2023-12-27 17:06:58 +0100848 { "lai": { "mcc_mnc": "262-01", "lac_min": "0000", "lac_max": "fffe" }, "pnn_record_id": 1 } ),
Harald Welte865eea62023-01-27 19:26:12 +0100849 ]
Harald Welte99e4cc02022-07-21 15:25:47 +0200850 def __init__(self, fid='6fc6', sfid=None, name='EF.OPL', rec_len=(8, 8), desc='Operator PLMN List', **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100851 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte842fbdb2023-12-27 17:06:58 +0100852 self._construct = Struct('lai'/Struct('mcc_mnc'/PlmnAdapter(Bytes(3)),
Harald Welte3c98d5e2022-07-20 07:40:05 +0200853 'lac_min'/HexAdapter(Bytes(2)), 'lac_max'/HexAdapter(Bytes(2))), 'pnn_record_id'/Int8ub)
Harald Welte790b2702021-04-11 00:01:35 +0200854
855# TS 51.011 Section 10.3.44 + TS 31.102 4.2.62
856class EF_MBI(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100857 _test_de_encode = [
858 ( '0100000000',
859 { "mbi_voicemail": 1, "mbi_fax": 0, "mbi_email": 0, "mbi_other": 0, "mbi_videocall": 0 } ),
860 ]
Harald Welte99e4cc02022-07-21 15:25:47 +0200861 def __init__(self, fid='6fc9', sfid=None, name='EF.MBI', rec_len=(4, 5), desc='Mailbox Identifier', **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100862 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200863 self._construct = Struct('mbi_voicemail'/Int8ub, 'mbi_fax'/Int8ub, 'mbi_email'/Int8ub,
864 'mbi_other'/Int8ub, 'mbi_videocall'/COptional(Int8ub))
865
866# TS 51.011 Section 10.3.45 + TS 31.102 4.2.63
867class EF_MWIS(LinFixedEF):
Harald Welte324175f2023-12-21 20:25:30 +0100868 _test_de_encode = [
869 ( '0000000000',
870 {"mwi_status": {"voicemail": False, "fax": False, "email": False, "other": False, "videomail":
871 False}, "num_waiting_voicemail": 0, "num_waiting_fax": 0, "num_waiting_email": 0,
872 "num_waiting_other": 0, "num_waiting_videomail": None} ),
873 ]
Harald Welte99e4cc02022-07-21 15:25:47 +0200874 def __init__(self, fid='6fca', sfid=None, name='EF.MWIS', rec_len=(5, 6),
Harald Welte6169c722022-02-12 09:05:15 +0100875 desc='Message Waiting Indication Status', **kwargs):
876 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200877 self._construct = Struct('mwi_status'/FlagsEnum(Byte, voicemail=1, fax=2, email=4, other=8, videomail=16),
878 'num_waiting_voicemail'/Int8ub,
879 'num_waiting_fax'/Int8ub, 'num_waiting_email'/Int8ub,
880 'num_waiting_other'/Int8ub, 'num_waiting_videomail'/COptional(Int8ub))
881
Harald Welte14105dc2021-05-31 08:48:51 +0200882# TS 51.011 Section 10.3.66
883class EF_SPDI(TransparentEF):
Harald Welte865eea62023-01-27 19:26:12 +0100884 # TODO: a305800337f800ffffffffffffffffffffffffffffffffffffffffffffff
Harald Welte14105dc2021-05-31 08:48:51 +0200885 class ServiceProviderPLMN(BER_TLV_IE, tag=0x80):
886 # flexible numbers of 3-byte PLMN records
Harald Welte842fbdb2023-12-27 17:06:58 +0100887 _construct = GreedyRange(PlmnAdapter(Bytes(3)))
Harald Weltec91085e2022-02-10 18:05:45 +0100888
Harald Welte14105dc2021-05-31 08:48:51 +0200889 class SPDI(BER_TLV_IE, tag=0xA3, nested=[ServiceProviderPLMN]):
890 pass
891 def __init__(self, fid='6fcd', sfid=None, name='EF.SPDI',
Harald Welte6169c722022-02-12 09:05:15 +0100892 desc='Service Provider Display Information', **kwargs):
893 super().__init__(fid, sfid=sfid, name=name, desc=desc, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200894 self._tlv = EF_SPDI.SPDI
895
Harald Welte790b2702021-04-11 00:01:35 +0200896# TS 51.011 Section 10.3.51
897class EF_MMSN(LinFixedEF):
Harald Welte99e4cc02022-07-21 15:25:47 +0200898 def __init__(self, fid='6fce', sfid=None, name='EF.MMSN', rec_len=(4, 20), desc='MMS Notification', **kwargs):
Harald Welte6169c722022-02-12 09:05:15 +0100899 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte3c98d5e2022-07-20 07:40:05 +0200900 self._construct = Struct('mms_status'/HexAdapter(Bytes(2)), 'mms_implementation'/HexAdapter(Bytes(1)),
901 'mms_notification'/HexAdapter(Bytes(this._.total_len-4)), 'ext_record_nr'/Byte)
Harald Welte790b2702021-04-11 00:01:35 +0200902
Harald Welte14105dc2021-05-31 08:48:51 +0200903# TS 51.011 Annex K.1
904class MMS_Implementation(BER_TLV_IE, tag=0x80):
905 _construct = FlagsEnum(Byte, WAP=1)
906
Harald Welte790b2702021-04-11 00:01:35 +0200907# TS 51.011 Section 10.3.53
908class EF_MMSICP(TransparentEF):
Harald Welte14105dc2021-05-31 08:48:51 +0200909 class MMS_Relay_Server(BER_TLV_IE, tag=0x81):
910 # 3GPP TS 23.140
911 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100912
Harald Welte14105dc2021-05-31 08:48:51 +0200913 class Interface_to_CN(BER_TLV_IE, tag=0x82):
914 # 3GPP TS 23.140
915 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100916
Harald Welte14105dc2021-05-31 08:48:51 +0200917 class Gateway(BER_TLV_IE, tag=0x83):
918 # Address, Type of address, Port, Service, AuthType, AuthId, AuthPass / 3GPP TS 23.140
919 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100920
Harald Welte14105dc2021-05-31 08:48:51 +0200921 class MMS_ConnectivityParamters(TLV_IE_Collection,
Harald Weltec91085e2022-02-10 18:05:45 +0100922 nested=[MMS_Implementation, MMS_Relay_Server, Interface_to_CN, Gateway]):
Harald Welte14105dc2021-05-31 08:48:51 +0200923 pass
Harald Welte13edf302022-07-21 15:19:23 +0200924 def __init__(self, fid='6fd0', sfid=None, name='EF.MMSICP', size=(1, None),
Harald Welte6169c722022-02-12 09:05:15 +0100925 desc='MMS Issuer Connectivity Parameters', **kwargs):
926 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte14105dc2021-05-31 08:48:51 +0200927 self._tlv = EF_MMSICP.MMS_ConnectivityParamters
Harald Welte790b2702021-04-11 00:01:35 +0200928
929# TS 51.011 Section 10.3.54
930class EF_MMSUP(LinFixedEF):
Harald Welte14105dc2021-05-31 08:48:51 +0200931 class MMS_UserPref_ProfileName(BER_TLV_IE, tag=0x81):
932 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100933
Harald Welte14105dc2021-05-31 08:48:51 +0200934 class MMS_UserPref_Info(BER_TLV_IE, tag=0x82):
935 pass
Harald Weltec91085e2022-02-10 18:05:45 +0100936
Harald Welte14105dc2021-05-31 08:48:51 +0200937 class MMS_User_Preferences(TLV_IE_Collection,
Harald Weltec91085e2022-02-10 18:05:45 +0100938 nested=[MMS_Implementation, MMS_UserPref_ProfileName, MMS_UserPref_Info]):
Harald Welte14105dc2021-05-31 08:48:51 +0200939 pass
Harald Welte99e4cc02022-07-21 15:25:47 +0200940 def __init__(self, fid='6fd1', sfid=None, name='EF.MMSUP', rec_len=(1, None),
Harald Welte6169c722022-02-12 09:05:15 +0100941 desc='MMS User Preferences', **kwargs):
942 super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=rec_len, **kwargs)
Harald Welte6113fe92022-01-21 15:51:35 +0100943 self._tlv = EF_MMSUP.MMS_User_Preferences
Harald Welte790b2702021-04-11 00:01:35 +0200944
945# TS 51.011 Section 10.3.55
946class EF_MMSUCP(TransparentEF):
Harald Welte13edf302022-07-21 15:19:23 +0200947 def __init__(self, fid='6fd2', sfid=None, name='EF.MMSUCP', size=(1, None),
Harald Welte6169c722022-02-12 09:05:15 +0100948 desc='MMS User Connectivity Parameters', **kwargs):
949 super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
Harald Welte790b2702021-04-11 00:01:35 +0200950
Harald Welteb2edd142021-01-08 23:29:35 +0100951
952class DF_GSM(CardDF):
953 def __init__(self, fid='7f20', name='DF.GSM', desc='GSM Network related files'):
954 super().__init__(fid=fid, name=name, desc=desc)
Harald Welte61ef1572023-03-09 19:42:38 +0100955 self.shell_commands += [self.AddlShellCommands()]
956
Harald Welteb2edd142021-01-08 23:29:35 +0100957 files = [
Harald Weltec91085e2022-02-10 18:05:45 +0100958 EF_LP(),
959 EF_IMSI(),
960 EF_Kc(),
961 EF_PLMNsel(),
962 TransparentEF('6f31', None, 'EF.HPPLMN',
Harald Welte509ecf82023-10-18 23:32:57 +0200963 desc='Higher Priority PLMN search period'),
Harald Weltec91085e2022-02-10 18:05:45 +0100964 EF_ACMmax(),
965 EF_ServiceTable('6f38', None, 'EF.SST',
Harald Welte509ecf82023-10-18 23:32:57 +0200966 desc='SIM service table', table=EF_SST_map, size=(2, 16)),
Harald Weltec91085e2022-02-10 18:05:45 +0100967 CyclicEF('6f39', None, 'EF.ACM',
Harald Welte509ecf82023-10-18 23:32:57 +0200968 desc='Accumulated call meter', rec_len=(3, 3)),
969 TransparentEF('6f3e', None, 'EF.GID1', desc='Group Identifier Level 1'),
970 TransparentEF('6f3f', None, 'EF.GID2', desc='Group Identifier Level 2'),
Harald Weltec91085e2022-02-10 18:05:45 +0100971 EF_SPN(),
972 TransparentEF('6f41', None, 'EF.PUCT',
Harald Welte509ecf82023-10-18 23:32:57 +0200973 desc='Price per unit and currency table', size=(5, 5)),
Harald Weltec91085e2022-02-10 18:05:45 +0100974 EF_CBMI(),
Harald Welte10a1a0a2023-05-24 15:19:28 +0200975 TransparentEF('6f74', None, 'EF.BCCH',
Harald Welte509ecf82023-10-18 23:32:57 +0200976 desc='Broadcast control channels', size=(16, 16)),
Harald Weltec91085e2022-02-10 18:05:45 +0100977 EF_ACC(),
978 EF_PLMNsel('6f7b', None, 'EF.FPLMN',
Harald Welte509ecf82023-10-18 23:32:57 +0200979 desc='Forbidden PLMNs', size=(12, 12)),
Harald Weltec91085e2022-02-10 18:05:45 +0100980 EF_LOCI(),
981 EF_AD(),
Harald Welte33eef852023-05-24 15:20:34 +0200982 TransparentEF('6fae', None, 'EF.Phase',
Harald Welte509ecf82023-10-18 23:32:57 +0200983 desc='Phase identification', size=(1, 1)),
Harald Weltec91085e2022-02-10 18:05:45 +0100984 EF_VGCS(),
985 EF_VGCSS(),
Harald Welte509ecf82023-10-18 23:32:57 +0200986 EF_VGCS('6fb3', None, 'EF.VBS', desc='Voice Broadcast Service'),
Harald Weltec91085e2022-02-10 18:05:45 +0100987 EF_VGCSS('6fb4', None, 'EF.VBSS',
Harald Welte509ecf82023-10-18 23:32:57 +0200988 desc='Voice Broadcast Service Status'),
Harald Weltec91085e2022-02-10 18:05:45 +0100989 EF_eMLPP(),
990 EF_AAeM(),
991 EF_CBMID(),
992 EF_ECC(),
993 EF_CBMIR(),
994 EF_DCK(),
995 EF_CNL(),
996 EF_NIA(),
Harald Welte509ecf82023-10-18 23:32:57 +0200997 EF_Kc('6f52', None, 'EF.KcGPRS', desc='GPRS Ciphering key KcGPRS'),
Harald Weltec91085e2022-02-10 18:05:45 +0100998 EF_LOCIGPRS(),
Harald Welte509ecf82023-10-18 23:32:57 +0200999 TransparentEF('6f54', None, 'EF.SUME', desc='SetUpMenu Elements'),
Harald Weltec91085e2022-02-10 18:05:45 +01001000 EF_xPLMNwAcT('6f60', None, 'EF.PLMNwAcT',
Harald Welte509ecf82023-10-18 23:32:57 +02001001 desc='User controlled PLMN Selector with Access Technology'),
Harald Weltec91085e2022-02-10 18:05:45 +01001002 EF_xPLMNwAcT('6f61', None, 'EF.OPLMNwAcT',
Harald Welte509ecf82023-10-18 23:32:57 +02001003 desc='Operator controlled PLMN Selector with Access Technology'),
Harald Weltec91085e2022-02-10 18:05:45 +01001004 EF_xPLMNwAcT('6f62', None, 'EF.HPLMNwAcT',
Harald Welte509ecf82023-10-18 23:32:57 +02001005 desc='HPLMN Selector with Access Technology'),
Harald Weltec91085e2022-02-10 18:05:45 +01001006 EF_CPBCCH(),
1007 EF_InvScan(),
1008 EF_PNN(),
1009 EF_OPL(),
Harald Welte509ecf82023-10-18 23:32:57 +02001010 EF_ADN('6fc7', None, 'EF.MBDN', desc='Mailbox Dialling Numbers'),
Harald Weltec91085e2022-02-10 18:05:45 +01001011 EF_MBI(),
1012 EF_MWIS(),
Harald Welte53762512023-12-21 20:16:17 +01001013 EF_CFIS(),
Harald Welte509ecf82023-10-18 23:32:57 +02001014 EF_EXT('6fc8', None, 'EF.EXT6', desc='Externsion6 (MBDN)'),
1015 EF_EXT('6fcc', None, 'EF.EXT7', desc='Externsion7 (CFIS)'),
Harald Weltec91085e2022-02-10 18:05:45 +01001016 EF_SPDI(),
1017 EF_MMSN(),
Harald Welte509ecf82023-10-18 23:32:57 +02001018 EF_EXT('6fcf', None, 'EF.EXT8', desc='Extension8 (MMSN)'),
Harald Weltec91085e2022-02-10 18:05:45 +01001019 EF_MMSICP(),
1020 EF_MMSUP(),
1021 EF_MMSUCP(),
1022 ]
Harald Welteb2edd142021-01-08 23:29:35 +01001023 self.add_files(files)
1024
Harald Welte61ef1572023-03-09 19:42:38 +01001025 @with_default_category('Application-Specific Commands')
1026 class AddlShellCommands(CommandSet):
1027 def __init__(self):
1028 super().__init__()
1029
1030 authenticate_parser = argparse.ArgumentParser()
1031 authenticate_parser.add_argument('rand', help='Random challenge')
1032
1033 @cmd2.with_argparser(authenticate_parser)
1034 def do_authenticate(self, opts):
1035 """Perform GSM Authentication."""
Harald Welte46255122023-10-21 23:40:42 +02001036 (data, sw) = self._cmd.lchan.scc.run_gsm(opts.rand)
Harald Welte61ef1572023-03-09 19:42:38 +01001037 self._cmd.poutput_json(data)
1038
Harald Weltec91085e2022-02-10 18:05:45 +01001039
Philipp Maierc8387dc2021-10-29 17:59:50 +02001040class CardProfileSIM(CardProfile):
Philipp Maiera028c7d2021-11-08 16:12:03 +01001041
Vadim Yanitskiy87dd0202023-04-22 20:45:29 +07001042 ORDER = 3
Philipp Maiera028c7d2021-11-08 16:12:03 +01001043
Philipp Maierc8387dc2021-10-29 17:59:50 +02001044 def __init__(self):
Philipp Maiera4df9422021-11-10 17:13:40 +01001045 sw = {
Harald Weltec91085e2022-02-10 18:05:45 +01001046 'Normal': {
1047 '9000': 'Normal ending of the command',
1048 '91xx': 'normal ending of the command, with extra information from the proactive SIM containing a command for the ME',
1049 '9exx': 'length XX of the response data given in case of a SIM data download error',
1050 '9fxx': 'length XX of the response data',
Philipp Maiera4df9422021-11-10 17:13:40 +01001051 },
Harald Weltec91085e2022-02-10 18:05:45 +01001052 'Postponed processing': {
1053 '9300': 'SIM Application Toolkit is busy. Command cannot be executed at present, further normal commands are allowed',
Philipp Maiera4df9422021-11-10 17:13:40 +01001054 },
Harald Weltec91085e2022-02-10 18:05:45 +01001055 'Memory management': {
1056 '920x': 'command successful but after using an internal update retry routine X times',
1057 '9240': 'memory problem',
Philipp Maiera4df9422021-11-10 17:13:40 +01001058 },
Harald Weltec91085e2022-02-10 18:05:45 +01001059 'Referencing management': {
1060 '9400': 'no EF selected',
1061 '9402': 'out of range (invalid address)',
1062 '9404': 'file ID not found or pattern not found',
1063 '9408': 'file is inconsistent with the command',
Philipp Maiera4df9422021-11-10 17:13:40 +01001064 },
Harald Weltec91085e2022-02-10 18:05:45 +01001065 'Security management': {
1066 '9802': 'no CHV initialized',
1067 '9804': 'access condition not fulfilled, unsuccessful CHV verification or authentication failed',
1068 '9808': 'in contradiction with CHV status',
1069 '9810': 'in contradiction with invalidation status',
1070 '9840': 'unsuccessful verification, CHV blocked, UNBLOCK CHV blocked',
1071 '9850': 'increase cannot be performed, Max value reached',
Philipp Maiera4df9422021-11-10 17:13:40 +01001072 },
Harald Weltec91085e2022-02-10 18:05:45 +01001073 'Application independent errors': {
1074 '67xx': 'incorrect parameter P3',
1075 '6bxx': 'incorrect parameter P1 or P2',
1076 '6dxx': 'unknown instruction code given in the command',
1077 '6exx': 'wrong instruction class given in the command',
1078 '6fxx': 'technical problem with no diagnostic given',
Philipp Maiera4df9422021-11-10 17:13:40 +01001079 },
Harald Weltec91085e2022-02-10 18:05:45 +01001080 }
Philipp Maiera4df9422021-11-10 17:13:40 +01001081
Harald Welte323a3502023-07-11 17:26:39 +02001082 addons = [
1083 AddonGSMR,
1084 ]
1085
Harald Weltec91085e2022-02-10 18:05:45 +01001086 super().__init__('SIM', desc='GSM SIM Card', cla="a0",
Harald Welte323a3502023-07-11 17:26:39 +02001087 sel_ctrl="0000", files_in_mf=[DF_TELECOM(), DF_GSM()], sw=sw, addons = addons)
Philipp Maier4ab971c2021-11-11 11:53:49 +01001088
Philipp Maier5998a3a2021-11-16 15:16:39 +01001089 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +01001090 def decode_select_response(resp_hex: str) -> object:
Harald Welte5924ec42023-01-24 19:49:24 +01001091 # we try to build something that resembles a dict resulting from the TLV decoder
1092 # of TS 102.221 (FcpTemplate), so that higher-level code only has to deal with one
1093 # format of SELECT response
Philipp Maier4ab971c2021-11-11 11:53:49 +01001094 resp_bin = h2b(resp_hex)
1095 struct_of_file_map = {
1096 0: 'transparent',
1097 1: 'linear_fixed',
1098 3: 'cyclic'
Harald Weltec91085e2022-02-10 18:05:45 +01001099 }
Philipp Maier4ab971c2021-11-11 11:53:49 +01001100 type_of_file_map = {
1101 1: 'mf',
1102 2: 'df',
1103 4: 'working_ef'
Harald Weltec91085e2022-02-10 18:05:45 +01001104 }
Philipp Maier4ab971c2021-11-11 11:53:49 +01001105 ret = {
Harald Welte747a9782022-02-13 17:52:28 +01001106 'file_descriptor': {
1107 'file_descriptor_byte': {},
1108 },
Philipp Maier4ab971c2021-11-11 11:53:49 +01001109 'proprietary_info': {},
Harald Weltec91085e2022-02-10 18:05:45 +01001110 }
Philipp Maier4ab971c2021-11-11 11:53:49 +01001111 ret['file_id'] = b2h(resp_bin[4:6])
Harald Weltec91085e2022-02-10 18:05:45 +01001112 file_type = type_of_file_map[resp_bin[6]
1113 ] if resp_bin[6] in type_of_file_map else resp_bin[6]
Harald Welte747a9782022-02-13 17:52:28 +01001114 ret['file_descriptor']['file_descriptor_byte']['file_type'] = file_type
Philipp Maier4ab971c2021-11-11 11:53:49 +01001115 if file_type in ['mf', 'df']:
Harald Welte5924ec42023-01-24 19:49:24 +01001116 ret['proprietary_info']['available_memory'] = int.from_bytes(resp_bin[2:4], 'big')
Philipp Maier4ab971c2021-11-11 11:53:49 +01001117 ret['file_characteristics'] = b2h(resp_bin[13:14])
1118 ret['num_direct_child_df'] = resp_bin[14]
1119 ret['num_direct_child_ef'] = resp_bin[15]
1120 ret['num_chv_unblock_adm_codes'] = int(resp_bin[16])
1121 # CHV / UNBLOCK CHV stats
1122 elif file_type in ['working_ef']:
Harald Welte5924ec42023-01-24 19:49:24 +01001123 ret['file_size'] = int.from_bytes(resp_bin[2:4], 'big')
Harald Weltec91085e2022-02-10 18:05:45 +01001124 file_struct = struct_of_file_map[resp_bin[13]
1125 ] if resp_bin[13] in struct_of_file_map else resp_bin[13]
Harald Welte747a9782022-02-13 17:52:28 +01001126 ret['file_descriptor']['file_descriptor_byte']['structure'] = file_struct
Harald Welte5924ec42023-01-24 19:49:24 +01001127 if file_struct != 'transparent':
1128 record_len = resp_bin[14]
1129 ret['file_descriptor']['record_len'] = record_len
1130 ret['file_descriptor']['num_of_rec'] = ret['file_size'] // record_len
Philipp Maier4ab971c2021-11-11 11:53:49 +01001131 ret['access_conditions'] = b2h(resp_bin[8:10])
1132 if resp_bin[11] & 0x01 == 0:
1133 ret['life_cycle_status_int'] = 'operational_activated'
1134 elif resp_bin[11] & 0x04:
1135 ret['life_cycle_status_int'] = 'operational_deactivated'
1136 else:
1137 ret['life_cycle_status_int'] = 'terminated'
1138 return ret
Philipp Maiera028c7d2021-11-08 16:12:03 +01001139
1140 @staticmethod
Harald Weltec91085e2022-02-10 18:05:45 +01001141 def match_with_card(scc: SimCardCommands) -> bool:
Philipp Maiera028c7d2021-11-08 16:12:03 +01001142 return match_sim(scc)
Harald Welte323a3502023-07-11 17:26:39 +02001143
1144
1145class AddonSIM(CardProfileAddon):
1146 """An add-on that can be found on a UICC in order to support classic GSM SIM."""
1147 def __init__(self):
1148 files = [
1149 DF_GSM(),
1150 DF_TELECOM(),
1151 ]
1152 super().__init__('SIM', desc='GSM SIM', files_in_mf=files)
1153
1154 def probe(self, card:'CardBase') -> bool:
1155 # we assume the add-on to be present in case DF.GSM is found on the card
1156 return card.file_exists(self.files_in_mf[0].fid)