Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: Card programmation logic |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 9 | # Copyright (C) 2011 Harald Welte <laforge@gnumonks.org> |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 10 | # Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com> |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 11 | # |
| 12 | # This program is free software: you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License as published by |
| 14 | # the Free Software Foundation, either version 2 of the License, or |
| 15 | # (at your option) any later version. |
| 16 | # |
| 17 | # This program is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | # GNU General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU General Public License |
| 23 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | # |
| 25 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 26 | from pySim.ts_51_011 import EF, DF |
| 27 | from pySim.utils import * |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 28 | from smartcard.util import toBytes |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 29 | |
| 30 | class Card(object): |
| 31 | |
| 32 | def __init__(self, scc): |
| 33 | self._scc = scc |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 34 | self._adm_chv_num = 4 |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 35 | self._aids = [] |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 36 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 37 | def reset(self): |
| 38 | self._scc.reset_card() |
| 39 | |
Philipp Maier | d58c632 | 2020-05-12 16:47:45 +0200 | [diff] [blame] | 40 | def erase(self): |
| 41 | print("warning: erasing is not supported for specified card type!") |
| 42 | return |
| 43 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 44 | def verify_adm(self, key): |
| 45 | ''' |
| 46 | Authenticate with ADM key |
| 47 | ''' |
| 48 | (res, sw) = self._scc.verify_chv(self._adm_chv_num, key) |
| 49 | return sw |
| 50 | |
| 51 | def read_iccid(self): |
| 52 | (res, sw) = self._scc.read_binary(EF['ICCID']) |
| 53 | if sw == '9000': |
| 54 | return (dec_iccid(res), sw) |
| 55 | else: |
| 56 | return (None, sw) |
| 57 | |
| 58 | def read_imsi(self): |
| 59 | (res, sw) = self._scc.read_binary(EF['IMSI']) |
| 60 | if sw == '9000': |
| 61 | return (dec_imsi(res), sw) |
| 62 | else: |
| 63 | return (None, sw) |
| 64 | |
| 65 | def update_imsi(self, imsi): |
| 66 | data, sw = self._scc.update_binary(EF['IMSI'], enc_imsi(imsi)) |
| 67 | return sw |
| 68 | |
| 69 | def update_acc(self, acc): |
| 70 | data, sw = self._scc.update_binary(EF['ACC'], lpad(acc, 4)) |
| 71 | return sw |
| 72 | |
Supreeth Herle | a850a47 | 2020-03-19 12:44:11 +0100 | [diff] [blame] | 73 | def read_hplmn_act(self): |
| 74 | (res, sw) = self._scc.read_binary(EF['HPLMNAcT']) |
| 75 | if sw == '9000': |
| 76 | return (format_xplmn_w_act(res), sw) |
| 77 | else: |
| 78 | return (None, sw) |
| 79 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 80 | def update_hplmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 81 | """ |
| 82 | Update Home PLMN with access technology bit-field |
| 83 | |
| 84 | See Section "10.3.37 EFHPLMNwAcT (HPLMN Selector with Access Technology)" |
| 85 | in ETSI TS 151 011 for the details of the access_tech field coding. |
| 86 | Some common values: |
| 87 | access_tech = '0080' # Only GSM is selected |
| 88 | access_tech = 'FFFF' # All technologues selected, even Reserved for Future Use ones |
| 89 | """ |
| 90 | # get size and write EF.HPLMNwAcT |
Supreeth Herle | 2d78597 | 2019-11-30 11:00:10 +0100 | [diff] [blame] | 91 | data = self._scc.read_binary(EF['HPLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 92 | size = len(data[0]) // 2 |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 93 | hplmn = enc_plmn(mcc, mnc) |
| 94 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 95 | data, sw = self._scc.update_binary(EF['HPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1)) |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 96 | return sw |
| 97 | |
Supreeth Herle | 1757b26 | 2020-03-19 12:43:11 +0100 | [diff] [blame] | 98 | def read_oplmn_act(self): |
| 99 | (res, sw) = self._scc.read_binary(EF['OPLMNwAcT']) |
| 100 | if sw == '9000': |
| 101 | return (format_xplmn_w_act(res), sw) |
| 102 | else: |
| 103 | return (None, sw) |
| 104 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 105 | def update_oplmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 106 | """ |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 107 | See note in update_hplmn_act() |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 108 | """ |
| 109 | # get size and write EF.OPLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 110 | data = self._scc.read_binary(EF['OPLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 111 | size = len(data[0]) // 2 |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 112 | hplmn = enc_plmn(mcc, mnc) |
| 113 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 114 | data, sw = self._scc.update_binary(EF['OPLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1)) |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 115 | return sw |
| 116 | |
Supreeth Herle | 1408440 | 2020-03-19 12:42:10 +0100 | [diff] [blame] | 117 | def read_plmn_act(self): |
| 118 | (res, sw) = self._scc.read_binary(EF['PLMNwAcT']) |
| 119 | if sw == '9000': |
| 120 | return (format_xplmn_w_act(res), sw) |
| 121 | else: |
| 122 | return (None, sw) |
| 123 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 124 | def update_plmn_act(self, mcc, mnc, access_tech='FFFF'): |
| 125 | """ |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 126 | See note in update_hplmn_act() |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 127 | """ |
| 128 | # get size and write EF.PLMNwAcT |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 129 | data = self._scc.read_binary(EF['PLMNwAcT'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 130 | size = len(data[0]) // 2 |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 131 | hplmn = enc_plmn(mcc, mnc) |
| 132 | content = hplmn + access_tech |
Vadim Yanitskiy | 9664b2e | 2020-02-27 01:49:51 +0700 | [diff] [blame] | 133 | data, sw = self._scc.update_binary(EF['PLMNwAcT'], content + 'ffffff0000' * (size // 5 - 1)) |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 134 | return sw |
| 135 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 136 | def update_plmnsel(self, mcc, mnc): |
| 137 | data = self._scc.read_binary(EF['PLMNsel'], length=None, offset=0) |
Vadim Yanitskiy | 99affe1 | 2020-02-15 05:03:09 +0700 | [diff] [blame] | 138 | size = len(data[0]) // 2 |
Philipp Maier | 5bf4260 | 2018-07-11 23:23:40 +0200 | [diff] [blame] | 139 | hplmn = enc_plmn(mcc, mnc) |
Philipp Maier | af9ae8b | 2018-07-13 11:15:49 +0200 | [diff] [blame] | 140 | data, sw = self._scc.update_binary(EF['PLMNsel'], hplmn + 'ff' * (size-3)) |
| 141 | return sw |
Philipp Maier | 5bf4260 | 2018-07-11 23:23:40 +0200 | [diff] [blame] | 142 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 143 | def update_smsp(self, smsp): |
| 144 | data, sw = self._scc.update_record(EF['SMSP'], 1, rpad(smsp, 84)) |
| 145 | return sw |
| 146 | |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 147 | def update_ad(self, mnc): |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 148 | #See also: 3GPP TS 31.102, chapter 4.2.18 |
| 149 | mnclen = len(str(mnc)) |
| 150 | if mnclen == 1: |
| 151 | mnclen = 2 |
| 152 | if mnclen > 3: |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 153 | raise RuntimeError('unable to calculate proper mnclen') |
| 154 | |
Philipp Maier | 7f9f64a | 2020-05-11 21:28:52 +0200 | [diff] [blame] | 155 | data, sw = self._scc.read_binary(EF['AD'], length=None, offset=0) |
| 156 | |
| 157 | # Reset contents to EF.AD in case the file is uninintalized |
| 158 | if data.lower() == "ffffffff": |
| 159 | data = "00000000" |
| 160 | |
| 161 | content = data[0:6] + "%02X" % mnclen |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 162 | data, sw = self._scc.update_binary(EF['AD'], content) |
| 163 | return sw |
| 164 | |
Alexander Chemeris | eb6807d | 2017-07-18 17:04:38 +0300 | [diff] [blame] | 165 | def read_spn(self): |
| 166 | (spn, sw) = self._scc.read_binary(EF['SPN']) |
| 167 | if sw == '9000': |
| 168 | return (dec_spn(spn), sw) |
| 169 | else: |
| 170 | return (None, sw) |
| 171 | |
| 172 | def update_spn(self, name, hplmn_disp=False, oplmn_disp=False): |
| 173 | content = enc_spn(name, hplmn_disp, oplmn_disp) |
| 174 | data, sw = self._scc.update_binary(EF['SPN'], rpad(content, 32)) |
| 175 | return sw |
| 176 | |
Supreeth Herle | d21349a | 2020-04-01 08:37:47 +0200 | [diff] [blame] | 177 | def read_binary(self, ef, length=None, offset=0): |
| 178 | ef_path = ef in EF and EF[ef] or ef |
| 179 | return self._scc.read_binary(ef_path, length, offset) |
| 180 | |
Supreeth Herle | ad10d66 | 2020-04-01 08:43:08 +0200 | [diff] [blame] | 181 | def read_record(self, ef, rec_no): |
| 182 | ef_path = ef in EF and EF[ef] or ef |
| 183 | return self._scc.read_record(ef_path, rec_no) |
| 184 | |
Supreeth Herle | 98a6927 | 2020-03-18 12:14:48 +0100 | [diff] [blame] | 185 | def read_gid1(self): |
| 186 | (res, sw) = self._scc.read_binary(EF['GID1']) |
| 187 | if sw == '9000': |
| 188 | return (res, sw) |
| 189 | else: |
| 190 | return (None, sw) |
| 191 | |
Supreeth Herle | 6d66af6 | 2020-03-19 12:49:16 +0100 | [diff] [blame] | 192 | def read_msisdn(self): |
| 193 | (res, sw) = self._scc.read_record(EF['MSISDN'], 1) |
| 194 | if sw == '9000': |
| 195 | return (dec_msisdn(res), sw) |
| 196 | else: |
| 197 | return (None, sw) |
| 198 | |
Supreeth Herle | e26331e | 2020-03-20 18:50:39 +0100 | [diff] [blame] | 199 | # Read the (full) AID for either ISIM or USIM or ISIM application |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 200 | def read_aid(self, isim = False): |
| 201 | |
| 202 | # First (known) halves of the AID |
| 203 | aid_usim = "a0000000871002" |
| 204 | aid_isim = "a0000000871004" |
| 205 | |
| 206 | # Select which one to look for |
| 207 | if isim: |
| 208 | aid = aid_isim |
| 209 | else: |
| 210 | aid = aid_usim |
| 211 | |
| 212 | # Find out how many records the EF.DIR has, then go through |
| 213 | # all records and try to find the AID we are looking for |
| 214 | aid_record_count = self._scc.record_count(['2F00']) |
| 215 | for i in range(0, aid_record_count): |
| 216 | record = self._scc.read_record(['2F00'], i + 1) |
| 217 | if aid in record[0]: |
| 218 | aid_len = int(record[0][6:8], 16) |
| 219 | return record[0][8:8 + aid_len * 2] |
| 220 | |
| 221 | return None |
| 222 | |
Supreeth Herle | e4e9831 | 2020-03-18 11:33:14 +0100 | [diff] [blame] | 223 | # Fetch all the AIDs present on UICC |
| 224 | def read_aids(self): |
| 225 | try: |
| 226 | # Find out how many records the EF.DIR has |
| 227 | # and store all the AIDs in the UICC |
| 228 | rec_cnt = self._scc.record_count(['3f00', '2f00']) |
| 229 | for i in range(0, rec_cnt): |
| 230 | rec = self._scc.read_record(['3f00', '2f00'], i + 1) |
| 231 | if (rec[0][0:2], rec[0][4:6]) == ('61', '4f') and len(rec[0]) > 12 \ |
| 232 | and rec[0][8:8 + int(rec[0][6:8], 16) * 2] not in self._aids: |
| 233 | self._aids.append(rec[0][8:8 + int(rec[0][6:8], 16) * 2]) |
| 234 | except Exception as e: |
| 235 | print("Can't read AIDs from SIM -- %s" % (str(e),)) |
| 236 | |
Supreeth Herle | f9f3e5e | 2020-03-22 08:04:59 +0100 | [diff] [blame] | 237 | # Select ADF.U/ISIM in the Card using its full AID |
| 238 | def select_adf_by_aid(self, adf="usim"): |
| 239 | # Check for valid ADF name |
| 240 | if adf not in ["usim", "isim"]: |
| 241 | return None |
| 242 | |
| 243 | # First (known) halves of the U/ISIM AID |
| 244 | aid_map = {} |
| 245 | aid_map["usim"] = "a0000000871002" |
| 246 | aid_map["isim"] = "a0000000871004" |
| 247 | |
| 248 | for aid in self._aids: |
| 249 | if aid_map[adf] in aid: |
| 250 | (res, sw) = self._scc.select_adf(aid) |
| 251 | return sw |
| 252 | |
| 253 | return None |
| 254 | |
Philipp Maier | 5c2cc66 | 2020-05-12 16:27:12 +0200 | [diff] [blame] | 255 | # Erase the contents of a file |
| 256 | def erase_binary(self, ef): |
| 257 | len = self._scc.binary_size(ef) |
| 258 | self._scc.update_binary(ef, "ff" * len, offset=0, verify=True) |
| 259 | |
| 260 | # Erase the contents of a single record |
| 261 | def erase_record(self, ef, rec_no): |
| 262 | len = self._scc.record_size(ef) |
| 263 | self._scc.update_record(ef, rec_no, "ff" * len, force_len=False, verify=True) |
| 264 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 265 | |
| 266 | class _MagicSimBase(Card): |
| 267 | """ |
| 268 | Theses cards uses several record based EFs to store the provider infos, |
| 269 | each possible provider uses a specific record number in each EF. The |
| 270 | indexes used are ( where N is the number of providers supported ) : |
| 271 | - [2 .. N+1] for the operator name |
Supreeth Herle | 9ca41c1 | 2020-01-21 12:50:30 +0100 | [diff] [blame] | 272 | - [1 .. N] for the programable EFs |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 273 | |
| 274 | * 3f00/7f4d/8f0c : Operator Name |
| 275 | |
| 276 | bytes 0-15 : provider name, padded with 0xff |
| 277 | byte 16 : length of the provider name |
| 278 | byte 17 : 01 for valid records, 00 otherwise |
| 279 | |
| 280 | * 3f00/7f4d/8f0d : Programmable Binary EFs |
| 281 | |
| 282 | * 3f00/7f4d/8f0e : Programmable Record EFs |
| 283 | |
| 284 | """ |
| 285 | |
| 286 | @classmethod |
| 287 | def autodetect(kls, scc): |
| 288 | try: |
| 289 | for p, l, t in kls._files.values(): |
| 290 | if not t: |
| 291 | continue |
| 292 | if scc.record_size(['3f00', '7f4d', p]) != l: |
| 293 | return None |
| 294 | except: |
| 295 | return None |
| 296 | |
| 297 | return kls(scc) |
| 298 | |
| 299 | def _get_count(self): |
| 300 | """ |
| 301 | Selects the file and returns the total number of entries |
| 302 | and entry size |
| 303 | """ |
| 304 | f = self._files['name'] |
| 305 | |
| 306 | r = self._scc.select_file(['3f00', '7f4d', f[0]]) |
| 307 | rec_len = int(r[-1][28:30], 16) |
| 308 | tlen = int(r[-1][4:8],16) |
| 309 | rec_cnt = (tlen / rec_len) - 1; |
| 310 | |
| 311 | if (rec_cnt < 1) or (rec_len != f[1]): |
| 312 | raise RuntimeError('Bad card type') |
| 313 | |
| 314 | return rec_cnt |
| 315 | |
| 316 | def program(self, p): |
| 317 | # Go to dir |
| 318 | self._scc.select_file(['3f00', '7f4d']) |
| 319 | |
| 320 | # Home PLMN in PLMN_Sel format |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 321 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 322 | |
| 323 | # Operator name ( 3f00/7f4d/8f0c ) |
| 324 | self._scc.update_record(self._files['name'][0], 2, |
| 325 | rpad(b2h(p['name']), 32) + ('%02x' % len(p['name'])) + '01' |
| 326 | ) |
| 327 | |
| 328 | # ICCID/IMSI/Ki/HPLMN ( 3f00/7f4d/8f0d ) |
| 329 | v = '' |
| 330 | |
| 331 | # inline Ki |
| 332 | if self._ki_file is None: |
| 333 | v += p['ki'] |
| 334 | |
| 335 | # ICCID |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 336 | v += '3f00' + '2fe2' + '0a' + enc_iccid(p['iccid']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 337 | |
| 338 | # IMSI |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 339 | v += '7f20' + '6f07' + '09' + enc_imsi(p['imsi']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 340 | |
| 341 | # Ki |
| 342 | if self._ki_file: |
| 343 | v += self._ki_file + '10' + p['ki'] |
| 344 | |
| 345 | # PLMN_Sel |
| 346 | v+= '6f30' + '18' + rpad(hplmn, 36) |
| 347 | |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 348 | # ACC |
| 349 | # This doesn't work with "fake" SuperSIM cards, |
| 350 | # but will hopefully work with real SuperSIMs. |
| 351 | if p.get('acc') is not None: |
| 352 | v+= '6f78' + '02' + lpad(p['acc'], 4) |
| 353 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 354 | self._scc.update_record(self._files['b_ef'][0], 1, |
| 355 | rpad(v, self._files['b_ef'][1]*2) |
| 356 | ) |
| 357 | |
| 358 | # SMSP ( 3f00/7f4d/8f0e ) |
| 359 | # FIXME |
| 360 | |
| 361 | # Write PLMN_Sel forcefully as well |
| 362 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 363 | tl = int(r[-1][4:8], 16) |
| 364 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 365 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 366 | self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3)) |
| 367 | |
| 368 | def erase(self): |
| 369 | # Dummy |
| 370 | df = {} |
| 371 | for k, v in self._files.iteritems(): |
| 372 | ofs = 1 |
| 373 | fv = v[1] * 'ff' |
| 374 | if k == 'name': |
| 375 | ofs = 2 |
| 376 | fv = fv[0:-4] + '0000' |
| 377 | df[v[0]] = (fv, ofs) |
| 378 | |
| 379 | # Write |
| 380 | for n in range(0,self._get_count()): |
| 381 | for k, (msg, ofs) in df.iteritems(): |
| 382 | self._scc.update_record(['3f00', '7f4d', k], n + ofs, msg) |
| 383 | |
| 384 | |
| 385 | class SuperSim(_MagicSimBase): |
| 386 | |
| 387 | name = 'supersim' |
| 388 | |
| 389 | _files = { |
| 390 | 'name' : ('8f0c', 18, True), |
| 391 | 'b_ef' : ('8f0d', 74, True), |
| 392 | 'r_ef' : ('8f0e', 50, True), |
| 393 | } |
| 394 | |
| 395 | _ki_file = None |
| 396 | |
| 397 | |
| 398 | class MagicSim(_MagicSimBase): |
| 399 | |
| 400 | name = 'magicsim' |
| 401 | |
| 402 | _files = { |
| 403 | 'name' : ('8f0c', 18, True), |
| 404 | 'b_ef' : ('8f0d', 130, True), |
| 405 | 'r_ef' : ('8f0e', 102, False), |
| 406 | } |
| 407 | |
| 408 | _ki_file = '6f1b' |
| 409 | |
| 410 | |
| 411 | class FakeMagicSim(Card): |
| 412 | """ |
| 413 | Theses cards have a record based EF 3f00/000c that contains the provider |
| 414 | informations. See the program method for its format. The records go from |
| 415 | 1 to N. |
| 416 | """ |
| 417 | |
| 418 | name = 'fakemagicsim' |
| 419 | |
| 420 | @classmethod |
| 421 | def autodetect(kls, scc): |
| 422 | try: |
| 423 | if scc.record_size(['3f00', '000c']) != 0x5a: |
| 424 | return None |
| 425 | except: |
| 426 | return None |
| 427 | |
| 428 | return kls(scc) |
| 429 | |
| 430 | def _get_infos(self): |
| 431 | """ |
| 432 | Selects the file and returns the total number of entries |
| 433 | and entry size |
| 434 | """ |
| 435 | |
| 436 | r = self._scc.select_file(['3f00', '000c']) |
| 437 | rec_len = int(r[-1][28:30], 16) |
| 438 | tlen = int(r[-1][4:8],16) |
| 439 | rec_cnt = (tlen / rec_len) - 1; |
| 440 | |
| 441 | if (rec_cnt < 1) or (rec_len != 0x5a): |
| 442 | raise RuntimeError('Bad card type') |
| 443 | |
| 444 | return rec_cnt, rec_len |
| 445 | |
| 446 | def program(self, p): |
| 447 | # Home PLMN |
| 448 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 449 | tl = int(r[-1][4:8], 16) |
| 450 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 451 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 452 | self._scc.update_binary('6f30', hplmn + 'ff' * (tl-3)) |
| 453 | |
| 454 | # Get total number of entries and entry size |
| 455 | rec_cnt, rec_len = self._get_infos() |
| 456 | |
| 457 | # Set first entry |
| 458 | entry = ( |
Philipp Maier | 45daa92 | 2019-04-01 15:49:45 +0200 | [diff] [blame] | 459 | '81' + # 1b Status: Valid & Active |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 460 | rpad(b2h(p['name'][0:14]), 28) + # 14b Entry Name |
Philipp Maier | 45daa92 | 2019-04-01 15:49:45 +0200 | [diff] [blame] | 461 | enc_iccid(p['iccid']) + # 10b ICCID |
| 462 | enc_imsi(p['imsi']) + # 9b IMSI_len + id_type(9) + IMSI |
| 463 | p['ki'] + # 16b Ki |
| 464 | lpad(p['smsp'], 80) # 40b SMSP (padded with ff if needed) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 465 | ) |
| 466 | self._scc.update_record('000c', 1, entry) |
| 467 | |
| 468 | def erase(self): |
| 469 | # Get total number of entries and entry size |
| 470 | rec_cnt, rec_len = self._get_infos() |
| 471 | |
| 472 | # Erase all entries |
| 473 | entry = 'ff' * rec_len |
| 474 | for i in range(0, rec_cnt): |
| 475 | self._scc.update_record('000c', 1+i, entry) |
| 476 | |
Sylvain Munaut | 5da8d4e | 2013-07-02 15:13:24 +0200 | [diff] [blame] | 477 | |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 478 | class GrcardSim(Card): |
| 479 | """ |
| 480 | Greencard (grcard.cn) HZCOS GSM SIM |
| 481 | These cards have a much more regular ISO 7816-4 / TS 11.11 structure, |
| 482 | and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki. |
| 483 | """ |
| 484 | |
| 485 | name = 'grcardsim' |
| 486 | |
| 487 | @classmethod |
| 488 | def autodetect(kls, scc): |
| 489 | return None |
| 490 | |
| 491 | def program(self, p): |
| 492 | # We don't really know yet what ADM PIN 4 is about |
| 493 | #self._scc.verify_chv(4, h2b("4444444444444444")) |
| 494 | |
| 495 | # Authenticate using ADM PIN 5 |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 496 | if p['pin_adm']: |
Philipp Maier | a3de5a3 | 2018-08-23 10:27:04 +0200 | [diff] [blame] | 497 | pin = h2b(p['pin_adm']) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 498 | else: |
| 499 | pin = h2b("4444444444444444") |
| 500 | self._scc.verify_chv(5, pin) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 501 | |
| 502 | # EF.ICCID |
| 503 | r = self._scc.select_file(['3f00', '2fe2']) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 504 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 505 | |
| 506 | # EF.IMSI |
| 507 | r = self._scc.select_file(['3f00', '7f20', '6f07']) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 508 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 509 | |
| 510 | # EF.ACC |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 511 | if p.get('acc') is not None: |
| 512 | data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4)) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 513 | |
| 514 | # EF.SMSP |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 515 | if p.get('smsp'): |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 516 | r = self._scc.select_file(['3f00', '7f10', '6f42']) |
| 517 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80)) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 518 | |
| 519 | # Set the Ki using proprietary command |
| 520 | pdu = '80d4020010' + p['ki'] |
| 521 | data, sw = self._scc._tp.send_apdu(pdu) |
| 522 | |
| 523 | # EF.HPLMN |
| 524 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 525 | size = int(r[-1][4:8], 16) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 526 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
Harald Welte | 3156d90 | 2011-03-22 21:48:19 +0100 | [diff] [blame] | 527 | self._scc.update_binary('6f30', hplmn + 'ff' * (size-3)) |
| 528 | |
| 529 | # EF.SPN (Service Provider Name) |
| 530 | r = self._scc.select_file(['3f00', '7f20', '6f30']) |
| 531 | size = int(r[-1][4:8], 16) |
| 532 | # FIXME |
| 533 | |
| 534 | # FIXME: EF.MSISDN |
| 535 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 536 | |
Harald Welte | e10394b | 2011-12-07 12:34:14 +0100 | [diff] [blame] | 537 | class SysmoSIMgr1(GrcardSim): |
| 538 | """ |
| 539 | sysmocom sysmoSIM-GR1 |
| 540 | These cards have a much more regular ISO 7816-4 / TS 11.11 structure, |
| 541 | and use standard UPDATE RECORD / UPDATE BINARY commands except for Ki. |
| 542 | """ |
| 543 | name = 'sysmosim-gr1' |
| 544 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 545 | @classmethod |
Philipp Maier | 087feff | 2018-08-23 09:41:36 +0200 | [diff] [blame] | 546 | def autodetect(kls, scc): |
| 547 | try: |
| 548 | # Look for ATR |
| 549 | if scc.get_atr() == toBytes("3B 99 18 00 11 88 22 33 44 55 66 77 60"): |
| 550 | return kls(scc) |
| 551 | except: |
| 552 | return None |
| 553 | return None |
Sylvain Munaut | 5da8d4e | 2013-07-02 15:13:24 +0200 | [diff] [blame] | 554 | |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 555 | class SysmoUSIMgr1(Card): |
| 556 | """ |
| 557 | sysmocom sysmoUSIM-GR1 |
| 558 | """ |
| 559 | name = 'sysmoUSIM-GR1' |
| 560 | |
| 561 | @classmethod |
| 562 | def autodetect(kls, scc): |
| 563 | # TODO: Access the ATR |
| 564 | return None |
| 565 | |
| 566 | def program(self, p): |
| 567 | # TODO: check if verify_chv could be used or what it needs |
| 568 | # self._scc.verify_chv(0x0A, [0x33,0x32,0x32,0x31,0x33,0x32,0x33,0x32]) |
| 569 | # Unlock the card.. |
| 570 | data, sw = self._scc._tp.send_apdu_checksw("0020000A083332323133323332") |
| 571 | |
| 572 | # TODO: move into SimCardCommands |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 573 | par = ( p['ki'] + # 16b K |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 574 | p['opc'] + # 32b OPC |
| 575 | enc_iccid(p['iccid']) + # 10b ICCID |
| 576 | enc_imsi(p['imsi']) # 9b IMSI_len + id_type(9) + IMSI |
Holger Hans Peter Freyther | 4d91bf4 | 2012-03-22 14:28:38 +0100 | [diff] [blame] | 577 | ) |
| 578 | data, sw = self._scc._tp.send_apdu_checksw("0099000033" + par) |
| 579 | |
Sylvain Munaut | 053c895 | 2013-07-02 15:12:32 +0200 | [diff] [blame] | 580 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 581 | class SysmoSIMgr2(Card): |
| 582 | """ |
| 583 | sysmocom sysmoSIM-GR2 |
| 584 | """ |
| 585 | |
| 586 | name = 'sysmoSIM-GR2' |
| 587 | |
| 588 | @classmethod |
| 589 | def autodetect(kls, scc): |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 590 | try: |
| 591 | # Look for ATR |
| 592 | if scc.get_atr() == toBytes("3B 7D 94 00 00 55 55 53 0A 74 86 93 0B 24 7C 4D 54 68"): |
| 593 | return kls(scc) |
| 594 | except: |
| 595 | return None |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 596 | return None |
| 597 | |
| 598 | def program(self, p): |
| 599 | |
| 600 | # select MF |
| 601 | r = self._scc.select_file(['3f00']) |
| 602 | |
| 603 | # authenticate as SUPER ADM using default key |
| 604 | self._scc.verify_chv(0x0b, h2b("3838383838383838")) |
| 605 | |
| 606 | # set ADM pin using proprietary command |
| 607 | # INS: D4 |
| 608 | # P1: 3A for PIN, 3B for PUK |
| 609 | # P2: CHV number, as in VERIFY CHV for PIN, and as in UNBLOCK CHV for PUK |
| 610 | # P3: 08, CHV length (curiously the PUK is also 08 length, instead of 10) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 611 | if p['pin_adm']: |
Daniel Willmann | 7d38d74 | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 612 | pin = h2b(p['pin_adm']) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 613 | else: |
| 614 | pin = h2b("4444444444444444") |
| 615 | |
| 616 | pdu = 'A0D43A0508' + b2h(pin) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 617 | data, sw = self._scc._tp.send_apdu(pdu) |
| 618 | |
| 619 | # authenticate as ADM (enough to write file, and can set PINs) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 620 | |
| 621 | self._scc.verify_chv(0x05, pin) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 622 | |
| 623 | # write EF.ICCID |
| 624 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 625 | |
| 626 | # select DF_GSM |
| 627 | r = self._scc.select_file(['7f20']) |
| 628 | |
| 629 | # write EF.IMSI |
| 630 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 631 | |
| 632 | # write EF.ACC |
| 633 | if p.get('acc') is not None: |
| 634 | data, sw = self._scc.update_binary('6f78', lpad(p['acc'], 4)) |
| 635 | |
| 636 | # get size and write EF.HPLMN |
| 637 | r = self._scc.select_file(['6f30']) |
| 638 | size = int(r[-1][4:8], 16) |
| 639 | hplmn = enc_plmn(p['mcc'], p['mnc']) |
| 640 | self._scc.update_binary('6f30', hplmn + 'ff' * (size-3)) |
| 641 | |
| 642 | # set COMP128 version 0 in proprietary file |
| 643 | data, sw = self._scc.update_binary('0001', '001000') |
| 644 | |
| 645 | # set Ki in proprietary file |
| 646 | data, sw = self._scc.update_binary('0001', p['ki'], 3) |
| 647 | |
| 648 | # select DF_TELECOM |
| 649 | r = self._scc.select_file(['3f00', '7f10']) |
| 650 | |
| 651 | # write EF.SMSP |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 652 | if p.get('smsp'): |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 653 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 80)) |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 654 | |
Sylvain Munaut | 2fc205c | 2013-12-23 17:22:56 +0100 | [diff] [blame] | 655 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 656 | class SysmoUSIMSJS1(Card): |
| 657 | """ |
| 658 | sysmocom sysmoUSIM-SJS1 |
| 659 | """ |
| 660 | |
| 661 | name = 'sysmoUSIM-SJS1' |
| 662 | |
| 663 | def __init__(self, ssc): |
| 664 | super(SysmoUSIMSJS1, self).__init__(ssc) |
| 665 | self._scc.cla_byte = "00" |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 666 | self._scc.sel_ctrl = "0004" #request an FCP |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 667 | |
| 668 | @classmethod |
| 669 | def autodetect(kls, scc): |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 670 | try: |
| 671 | # Look for ATR |
| 672 | if scc.get_atr() == toBytes("3B 9F 96 80 1F C7 80 31 A0 73 BE 21 13 67 43 20 07 18 00 00 01 A5"): |
| 673 | return kls(scc) |
| 674 | except: |
| 675 | return None |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 676 | return None |
| 677 | |
| 678 | def program(self, p): |
| 679 | |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 680 | # authenticate as ADM using default key (written on the card..) |
| 681 | if not p['pin_adm']: |
| 682 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 683 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 684 | |
| 685 | # select MF |
| 686 | r = self._scc.select_file(['3f00']) |
| 687 | |
Philipp Maier | e960488 | 2017-03-21 17:24:31 +0100 | [diff] [blame] | 688 | # write EF.ICCID |
| 689 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 690 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 691 | # select DF_GSM |
| 692 | r = self._scc.select_file(['7f20']) |
| 693 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 694 | # set Ki in proprietary file |
| 695 | data, sw = self._scc.update_binary('00FF', p['ki']) |
| 696 | |
Philipp Maier | 1be35bf | 2018-07-13 11:29:03 +0200 | [diff] [blame] | 697 | # set OPc in proprietary file |
Daniel Willmann | 67acdbc | 2018-06-15 07:42:48 +0200 | [diff] [blame] | 698 | if 'opc' in p: |
| 699 | content = "01" + p['opc'] |
| 700 | data, sw = self._scc.update_binary('00F7', content) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 701 | |
Supreeth Herle | 7947d92 | 2019-06-08 07:50:53 +0200 | [diff] [blame] | 702 | # set Service Provider Name |
Supreeth Herle | 840a9e2 | 2020-01-21 13:32:46 +0100 | [diff] [blame] | 703 | if p.get('name') is not None: |
| 704 | content = enc_spn(p['name'], True, True) |
| 705 | data, sw = self._scc.update_binary('6F46', rpad(content, 32)) |
Supreeth Herle | 7947d92 | 2019-06-08 07:50:53 +0200 | [diff] [blame] | 706 | |
Supreeth Herle | c8796a3 | 2019-12-23 12:23:42 +0100 | [diff] [blame] | 707 | if p.get('acc') is not None: |
| 708 | self.update_acc(p['acc']) |
| 709 | |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 710 | # write EF.IMSI |
| 711 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 712 | |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 713 | # EF.PLMNsel |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 714 | if p.get('mcc') and p.get('mnc'): |
| 715 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 716 | if sw != '9000': |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 717 | print("Programming PLMNsel failed with code %s"%sw) |
| 718 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 719 | # EF.PLMNwAcT |
| 720 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 721 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 722 | if sw != '9000': |
| 723 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 724 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 725 | # EF.OPLMNwAcT |
| 726 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 727 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 728 | if sw != '9000': |
| 729 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 730 | |
Supreeth Herle | f442fb4 | 2020-01-21 12:47:32 +0100 | [diff] [blame] | 731 | # EF.HPLMNwAcT |
| 732 | if p.get('mcc') and p.get('mnc'): |
| 733 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 734 | if sw != '9000': |
| 735 | print("Programming HPLMNwAcT failed with code %s"%sw) |
| 736 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 737 | # EF.AD |
| 738 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | ee908ae | 2019-03-21 16:21:12 +0100 | [diff] [blame] | 739 | sw = self.update_ad(p['mnc']) |
| 740 | if sw != '9000': |
| 741 | print("Programming AD failed with code %s"%sw) |
Philipp Maier | 2d15ea0 | 2019-03-20 12:40:36 +0100 | [diff] [blame] | 742 | |
Daniel Willmann | 1d087ef | 2017-08-31 10:08:45 +0200 | [diff] [blame] | 743 | # EF.SMSP |
Harald Welte | 23888da | 2019-08-28 23:19:11 +0200 | [diff] [blame] | 744 | if p.get('smsp'): |
| 745 | r = self._scc.select_file(['3f00', '7f10']) |
| 746 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 747 | |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 748 | # EF.MSISDN |
| 749 | # TODO: Alpha Identifier (currently 'ff'O * 20) |
| 750 | # TODO: Capability/Configuration1 Record Identifier |
| 751 | # TODO: Extension1 Record Identifier |
| 752 | if p.get('msisdn') is not None: |
| 753 | msisdn = enc_msisdn(p['msisdn']) |
| 754 | data = 'ff' * 20 + msisdn + 'ff' * 2 |
| 755 | |
| 756 | r = self._scc.select_file(['3f00', '7f10']) |
| 757 | data, sw = self._scc.update_record('6F40', 1, data, force_len=True) |
| 758 | |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 759 | |
| 760 | class FairwavesSIM(Card): |
| 761 | """ |
| 762 | FairwavesSIM |
| 763 | |
| 764 | The SIM card is operating according to the standard. |
| 765 | For Ki/OP/OPC programming the following files are additionally open for writing: |
| 766 | 3F00/7F20/FF01 – OP/OPC: |
| 767 | byte 1 = 0x01, bytes 2-17: OPC; |
| 768 | byte 1 = 0x00, bytes 2-17: OP; |
| 769 | 3F00/7F20/FF02: Ki |
| 770 | """ |
| 771 | |
Philipp Maier | 5a87631 | 2019-11-11 11:01:46 +0100 | [diff] [blame] | 772 | name = 'Fairwaves-SIM' |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 773 | # Propriatary files |
| 774 | _EF_num = { |
| 775 | 'Ki': 'FF02', |
| 776 | 'OP/OPC': 'FF01', |
| 777 | } |
| 778 | _EF = { |
| 779 | 'Ki': DF['GSM']+[_EF_num['Ki']], |
| 780 | 'OP/OPC': DF['GSM']+[_EF_num['OP/OPC']], |
| 781 | } |
| 782 | |
| 783 | def __init__(self, ssc): |
| 784 | super(FairwavesSIM, self).__init__(ssc) |
| 785 | self._adm_chv_num = 0x11 |
| 786 | self._adm2_chv_num = 0x12 |
| 787 | |
| 788 | |
| 789 | @classmethod |
| 790 | def autodetect(kls, scc): |
| 791 | try: |
| 792 | # Look for ATR |
| 793 | if scc.get_atr() == toBytes("3B 9F 96 80 1F C7 80 31 A0 73 BE 21 13 67 44 22 06 10 00 00 01 A9"): |
| 794 | return kls(scc) |
| 795 | except: |
| 796 | return None |
| 797 | return None |
| 798 | |
| 799 | |
| 800 | def verify_adm2(self, key): |
| 801 | ''' |
| 802 | Authenticate with ADM2 key. |
| 803 | |
| 804 | Fairwaves SIM cards support hierarchical key structure and ADM2 key |
| 805 | is a key which has access to proprietary files (Ki and OP/OPC). |
| 806 | That said, ADM key inherits permissions of ADM2 key and thus we rarely |
| 807 | need ADM2 key per se. |
| 808 | ''' |
| 809 | (res, sw) = self._scc.verify_chv(self._adm2_chv_num, key) |
| 810 | return sw |
| 811 | |
| 812 | |
| 813 | def read_ki(self): |
| 814 | """ |
| 815 | Read Ki in proprietary file. |
| 816 | |
| 817 | Requires ADM1 access level |
| 818 | """ |
| 819 | return self._scc.read_binary(self._EF['Ki']) |
| 820 | |
| 821 | |
| 822 | def update_ki(self, ki): |
| 823 | """ |
| 824 | Set Ki in proprietary file. |
| 825 | |
| 826 | Requires ADM1 access level |
| 827 | """ |
| 828 | data, sw = self._scc.update_binary(self._EF['Ki'], ki) |
| 829 | return sw |
| 830 | |
| 831 | |
| 832 | def read_op_opc(self): |
| 833 | """ |
| 834 | Read Ki in proprietary file. |
| 835 | |
| 836 | Requires ADM1 access level |
| 837 | """ |
| 838 | (ef, sw) = self._scc.read_binary(self._EF['OP/OPC']) |
| 839 | type = 'OP' if ef[0:2] == '00' else 'OPC' |
| 840 | return ((type, ef[2:]), sw) |
| 841 | |
| 842 | |
| 843 | def update_op(self, op): |
| 844 | """ |
| 845 | Set OP in proprietary file. |
| 846 | |
| 847 | Requires ADM1 access level |
| 848 | """ |
| 849 | content = '00' + op |
| 850 | data, sw = self._scc.update_binary(self._EF['OP/OPC'], content) |
| 851 | return sw |
| 852 | |
| 853 | |
| 854 | def update_opc(self, opc): |
| 855 | """ |
| 856 | Set OPC in proprietary file. |
| 857 | |
| 858 | Requires ADM1 access level |
| 859 | """ |
| 860 | content = '01' + opc |
| 861 | data, sw = self._scc.update_binary(self._EF['OP/OPC'], content) |
| 862 | return sw |
| 863 | |
| 864 | |
| 865 | def program(self, p): |
| 866 | # authenticate as ADM1 |
| 867 | if not p['pin_adm']: |
| 868 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 869 | sw = self.verify_adm(h2b(p['pin_adm'])) |
| 870 | if sw != '9000': |
| 871 | raise RuntimeError('Failed to authenticate with ADM key %s'%(p['pin_adm'],)) |
| 872 | |
| 873 | # TODO: Set operator name |
| 874 | if p.get('smsp') is not None: |
| 875 | sw = self.update_smsp(p['smsp']) |
| 876 | if sw != '9000': |
| 877 | print("Programming SMSP failed with code %s"%sw) |
| 878 | # This SIM doesn't support changing ICCID |
| 879 | if p.get('mcc') is not None and p.get('mnc') is not None: |
| 880 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 881 | if sw != '9000': |
| 882 | print("Programming MCC/MNC failed with code %s"%sw) |
| 883 | if p.get('imsi') is not None: |
| 884 | sw = self.update_imsi(p['imsi']) |
| 885 | if sw != '9000': |
| 886 | print("Programming IMSI failed with code %s"%sw) |
| 887 | if p.get('ki') is not None: |
| 888 | sw = self.update_ki(p['ki']) |
| 889 | if sw != '9000': |
| 890 | print("Programming Ki failed with code %s"%sw) |
| 891 | if p.get('opc') is not None: |
| 892 | sw = self.update_opc(p['opc']) |
| 893 | if sw != '9000': |
| 894 | print("Programming OPC failed with code %s"%sw) |
| 895 | if p.get('acc') is not None: |
| 896 | sw = self.update_acc(p['acc']) |
| 897 | if sw != '9000': |
| 898 | print("Programming ACC failed with code %s"%sw) |
Jan Balke | 3e84067 | 2015-01-26 15:36:27 +0100 | [diff] [blame] | 899 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 900 | class OpenCellsSim(Card): |
| 901 | """ |
| 902 | OpenCellsSim |
| 903 | |
| 904 | """ |
| 905 | |
Philipp Maier | 5a87631 | 2019-11-11 11:01:46 +0100 | [diff] [blame] | 906 | name = 'OpenCells-SIM' |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 907 | |
| 908 | def __init__(self, ssc): |
| 909 | super(OpenCellsSim, self).__init__(ssc) |
| 910 | self._adm_chv_num = 0x0A |
| 911 | |
| 912 | |
| 913 | @classmethod |
| 914 | def autodetect(kls, scc): |
| 915 | try: |
| 916 | # Look for ATR |
| 917 | if scc.get_atr() == toBytes("3B 9F 95 80 1F C3 80 31 E0 73 FE 21 13 57 86 81 02 86 98 44 18 A8"): |
| 918 | return kls(scc) |
| 919 | except: |
| 920 | return None |
| 921 | return None |
| 922 | |
| 923 | |
| 924 | def program(self, p): |
| 925 | if not p['pin_adm']: |
| 926 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 927 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
| 928 | |
| 929 | # select MF |
| 930 | r = self._scc.select_file(['3f00']) |
| 931 | |
| 932 | # write EF.ICCID |
| 933 | data, sw = self._scc.update_binary('2fe2', enc_iccid(p['iccid'])) |
| 934 | |
| 935 | r = self._scc.select_file(['7ff0']) |
| 936 | |
| 937 | # set Ki in proprietary file |
| 938 | data, sw = self._scc.update_binary('FF02', p['ki']) |
| 939 | |
| 940 | # set OPC in proprietary file |
| 941 | data, sw = self._scc.update_binary('FF01', p['opc']) |
| 942 | |
| 943 | # select DF_GSM |
| 944 | r = self._scc.select_file(['7f20']) |
| 945 | |
| 946 | # write EF.IMSI |
| 947 | data, sw = self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 948 | |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 949 | class WavemobileSim(Card): |
| 950 | """ |
| 951 | WavemobileSim |
| 952 | |
| 953 | """ |
| 954 | |
| 955 | name = 'Wavemobile-SIM' |
| 956 | |
| 957 | def __init__(self, ssc): |
| 958 | super(WavemobileSim, self).__init__(ssc) |
| 959 | self._adm_chv_num = 0x0A |
| 960 | self._scc.cla_byte = "00" |
| 961 | self._scc.sel_ctrl = "0004" #request an FCP |
| 962 | |
| 963 | @classmethod |
| 964 | def autodetect(kls, scc): |
| 965 | try: |
| 966 | # Look for ATR |
| 967 | if scc.get_atr() == toBytes("3B 9F 95 80 1F C7 80 31 E0 73 F6 21 13 67 4D 45 16 00 43 01 00 8F"): |
| 968 | return kls(scc) |
| 969 | except: |
| 970 | return None |
| 971 | return None |
| 972 | |
| 973 | def program(self, p): |
| 974 | if not p['pin_adm']: |
| 975 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 976 | sw = self.verify_adm(h2b(p['pin_adm'])) |
| 977 | if sw != '9000': |
| 978 | raise RuntimeError('Failed to authenticate with ADM key %s'%(p['pin_adm'],)) |
| 979 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 980 | # EF.ICCID |
| 981 | # TODO: Add programming of the ICCID |
| 982 | if p.get('iccid'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 983 | print("Warning: Programming of the ICCID is not implemented for this type of card.") |
| 984 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 985 | # KI (Presumably a propritary file) |
| 986 | # TODO: Add programming of KI |
| 987 | if p.get('ki'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 988 | print("Warning: Programming of the KI is not implemented for this type of card.") |
| 989 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 990 | # OPc (Presumably a propritary file) |
| 991 | # TODO: Add programming of OPc |
| 992 | if p.get('opc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 993 | print("Warning: Programming of the OPc is not implemented for this type of card.") |
| 994 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 995 | # EF.SMSP |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 996 | if p.get('smsp'): |
| 997 | sw = self.update_smsp(p['smsp']) |
| 998 | if sw != '9000': |
| 999 | print("Programming SMSP failed with code %s"%sw) |
| 1000 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1001 | # EF.IMSI |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1002 | if p.get('imsi'): |
| 1003 | sw = self.update_imsi(p['imsi']) |
| 1004 | if sw != '9000': |
| 1005 | print("Programming IMSI failed with code %s"%sw) |
| 1006 | |
| 1007 | # EF.ACC |
| 1008 | if p.get('acc'): |
| 1009 | sw = self.update_acc(p['acc']) |
| 1010 | if sw != '9000': |
| 1011 | print("Programming ACC failed with code %s"%sw) |
| 1012 | |
| 1013 | # EF.PLMNsel |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1014 | if p.get('mcc') and p.get('mnc'): |
| 1015 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 1016 | if sw != '9000': |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1017 | print("Programming PLMNsel failed with code %s"%sw) |
| 1018 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1019 | # EF.PLMNwAcT |
| 1020 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1021 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 1022 | if sw != '9000': |
| 1023 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 1024 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1025 | # EF.OPLMNwAcT |
| 1026 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1027 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 1028 | if sw != '9000': |
| 1029 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 1030 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1031 | # EF.AD |
| 1032 | if p.get('mcc') and p.get('mnc'): |
Philipp Maier | 6e507a7 | 2019-04-01 16:33:48 +0200 | [diff] [blame] | 1033 | sw = self.update_ad(p['mnc']) |
| 1034 | if sw != '9000': |
| 1035 | print("Programming AD failed with code %s"%sw) |
| 1036 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 1037 | return None |
Philipp Maier | c8ce82a | 2018-07-04 17:57:20 +0200 | [diff] [blame] | 1038 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1039 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1040 | class SysmoISIMSJA2(Card): |
| 1041 | """ |
| 1042 | sysmocom sysmoISIM-SJA2 |
| 1043 | """ |
| 1044 | |
| 1045 | name = 'sysmoISIM-SJA2' |
| 1046 | |
| 1047 | def __init__(self, ssc): |
| 1048 | super(SysmoISIMSJA2, self).__init__(ssc) |
| 1049 | self._scc.cla_byte = "00" |
| 1050 | self._scc.sel_ctrl = "0004" #request an FCP |
| 1051 | |
| 1052 | @classmethod |
| 1053 | def autodetect(kls, scc): |
| 1054 | try: |
| 1055 | # Try card model #1 |
| 1056 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9" |
| 1057 | if scc.get_atr() == toBytes(atr): |
| 1058 | return kls(scc) |
| 1059 | |
| 1060 | # Try card model #2 |
| 1061 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2" |
| 1062 | if scc.get_atr() == toBytes(atr): |
| 1063 | return kls(scc) |
Philipp Maier | b3e11ea | 2020-03-11 12:32:44 +0100 | [diff] [blame] | 1064 | |
| 1065 | # Try card model #3 |
| 1066 | atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5" |
| 1067 | if scc.get_atr() == toBytes(atr): |
| 1068 | return kls(scc) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1069 | except: |
| 1070 | return None |
| 1071 | return None |
| 1072 | |
| 1073 | def program(self, p): |
| 1074 | # authenticate as ADM using default key (written on the card..) |
| 1075 | if not p['pin_adm']: |
| 1076 | raise ValueError("Please provide a PIN-ADM as there is no default one") |
| 1077 | self._scc.verify_chv(0x0A, h2b(p['pin_adm'])) |
| 1078 | |
| 1079 | # This type of card does not allow to reprogram the ICCID. |
| 1080 | # Reprogramming the ICCID would mess up the card os software |
| 1081 | # license management, so the ICCID must be kept at its factory |
| 1082 | # setting! |
| 1083 | if p.get('iccid'): |
| 1084 | print("Warning: Programming of the ICCID is not implemented for this type of card.") |
| 1085 | |
| 1086 | # select DF_GSM |
| 1087 | self._scc.select_file(['7f20']) |
| 1088 | |
| 1089 | # write EF.IMSI |
| 1090 | if p.get('imsi'): |
| 1091 | self._scc.update_binary('6f07', enc_imsi(p['imsi'])) |
| 1092 | |
| 1093 | # EF.PLMNsel |
| 1094 | if p.get('mcc') and p.get('mnc'): |
| 1095 | sw = self.update_plmnsel(p['mcc'], p['mnc']) |
| 1096 | if sw != '9000': |
| 1097 | print("Programming PLMNsel failed with code %s"%sw) |
| 1098 | |
| 1099 | # EF.PLMNwAcT |
| 1100 | if p.get('mcc') and p.get('mnc'): |
| 1101 | sw = self.update_plmn_act(p['mcc'], p['mnc']) |
| 1102 | if sw != '9000': |
| 1103 | print("Programming PLMNwAcT failed with code %s"%sw) |
| 1104 | |
| 1105 | # EF.OPLMNwAcT |
| 1106 | if p.get('mcc') and p.get('mnc'): |
| 1107 | sw = self.update_oplmn_act(p['mcc'], p['mnc']) |
| 1108 | if sw != '9000': |
| 1109 | print("Programming OPLMNwAcT failed with code %s"%sw) |
| 1110 | |
Harald Welte | 32f0d41 | 2020-05-05 17:35:57 +0200 | [diff] [blame] | 1111 | # EF.HPLMNwAcT |
| 1112 | if p.get('mcc') and p.get('mnc'): |
| 1113 | sw = self.update_hplmn_act(p['mcc'], p['mnc']) |
| 1114 | if sw != '9000': |
| 1115 | print("Programming HPLMNwAcT failed with code %s"%sw) |
| 1116 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1117 | # EF.AD |
| 1118 | if p.get('mcc') and p.get('mnc'): |
| 1119 | sw = self.update_ad(p['mnc']) |
| 1120 | if sw != '9000': |
| 1121 | print("Programming AD failed with code %s"%sw) |
| 1122 | |
| 1123 | # EF.SMSP |
| 1124 | if p.get('smsp'): |
| 1125 | r = self._scc.select_file(['3f00', '7f10']) |
| 1126 | data, sw = self._scc.update_record('6f42', 1, lpad(p['smsp'], 104), force_len=True) |
| 1127 | |
| 1128 | # update EF-SIM_AUTH_KEY (and EF-USIM_AUTH_KEY_2G, which is |
| 1129 | # hard linked to EF-USIM_AUTH_KEY) |
| 1130 | self._scc.select_file(['3f00']) |
| 1131 | self._scc.select_file(['a515']) |
| 1132 | if p.get('ki'): |
| 1133 | self._scc.update_binary('6f20', p['ki'], 1) |
| 1134 | if p.get('opc'): |
| 1135 | self._scc.update_binary('6f20', p['opc'], 17) |
| 1136 | |
| 1137 | # update EF-USIM_AUTH_KEY in ADF.ISIM |
| 1138 | self._scc.select_file(['3f00']) |
| 1139 | aid = self.read_aid(isim = True) |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1140 | if (aid): |
| 1141 | self._scc.select_adf(aid) |
| 1142 | if p.get('ki'): |
| 1143 | self._scc.update_binary('af20', p['ki'], 1) |
| 1144 | if p.get('opc'): |
| 1145 | self._scc.update_binary('af20', p['opc'], 17) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1146 | |
| 1147 | # update EF-USIM_AUTH_KEY in ADF.USIM |
| 1148 | self._scc.select_file(['3f00']) |
| 1149 | aid = self.read_aid() |
Philipp Maier | d950786 | 2020-03-11 12:18:29 +0100 | [diff] [blame] | 1150 | if (aid): |
| 1151 | self._scc.select_adf(aid) |
| 1152 | if p.get('ki'): |
| 1153 | self._scc.update_binary('af20', p['ki'], 1) |
| 1154 | if p.get('opc'): |
| 1155 | self._scc.update_binary('af20', p['opc'], 17) |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1156 | |
| 1157 | return |
| 1158 | |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1159 | |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 1160 | # In order for autodetection ... |
Harald Welte | e10394b | 2011-12-07 12:34:14 +0100 | [diff] [blame] | 1161 | _cards_classes = [ FakeMagicSim, SuperSim, MagicSim, GrcardSim, |
Alexander Chemeris | e0d9d88 | 2018-01-10 14:18:32 +0900 | [diff] [blame] | 1162 | SysmoSIMgr1, SysmoSIMgr2, SysmoUSIMgr1, SysmoUSIMSJS1, |
Philipp Maier | 0ad5bcf | 2019-12-31 17:55:47 +0100 | [diff] [blame] | 1163 | FairwavesSIM, OpenCellsSim, WavemobileSim, SysmoISIMSJA2 ] |
Alexander Chemeris | 8ad124a | 2018-01-10 14:17:55 +0900 | [diff] [blame] | 1164 | |
| 1165 | def card_autodetect(scc): |
| 1166 | for kls in _cards_classes: |
| 1167 | card = kls.autodetect(scc) |
| 1168 | if card is not None: |
| 1169 | card.reset() |
| 1170 | return card |
| 1171 | return None |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 1172 | |
| 1173 | def card_detect(ctype, scc): |
| 1174 | # Detect type if needed |
| 1175 | card = None |
| 1176 | ctypes = dict([(kls.name, kls) for kls in _cards_classes]) |
| 1177 | |
| 1178 | if ctype in ("auto", "auto_once"): |
| 1179 | for kls in _cards_classes: |
| 1180 | card = kls.autodetect(scc) |
| 1181 | if card: |
| 1182 | print("Autodetected card type: %s" % card.name) |
| 1183 | card.reset() |
| 1184 | break |
| 1185 | |
| 1186 | if card is None: |
| 1187 | print("Autodetection failed") |
| 1188 | return None |
| 1189 | |
| 1190 | if ctype == "auto_once": |
| 1191 | ctype = card.name |
| 1192 | |
| 1193 | elif ctype in ctypes: |
| 1194 | card = ctypes[ctype](scc) |
| 1195 | |
| 1196 | else: |
| 1197 | raise ValueError("Unknown card type: %s" % ctype) |
| 1198 | |
| 1199 | return card |