Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 3 | # Copyright (C) 2018 Vadim Yanitskiy <axilirator@gmail.com> |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | # |
| 18 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 19 | import select |
| 20 | import struct |
| 21 | import socket |
| 22 | import os |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 23 | import argparse |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 24 | from typing import Optional |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 25 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 26 | from pySim.transport import LinkBase |
| 27 | from pySim.exceptions import * |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 28 | from pySim.utils import h2b, b2h, Hexstr, ResTuple |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 29 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 30 | |
Vadim Yanitskiy | 04b5d9d | 2022-07-07 03:05:30 +0700 | [diff] [blame] | 31 | class L1CTLMessage: |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 32 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 33 | # Every (encoded) L1CTL message has the following structure: |
| 34 | # - msg_length (2 bytes, net order) |
| 35 | # - l1ctl_hdr (packed structure) |
| 36 | # - msg_type |
| 37 | # - flags |
| 38 | # - padding (2 spare bytes) |
| 39 | # - ... payload ... |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 40 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 41 | def __init__(self, msg_type, flags=0x00): |
| 42 | # Init L1CTL message header |
| 43 | self.data = struct.pack("BBxx", msg_type, flags) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 44 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 45 | def gen_msg(self): |
| 46 | return struct.pack("!H", len(self.data)) + self.data |
| 47 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 48 | |
| 49 | class L1CTLMessageReset(L1CTLMessage): |
| 50 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 51 | # L1CTL message types |
| 52 | L1CTL_RESET_REQ = 0x0d |
| 53 | L1CTL_RESET_IND = 0x07 |
| 54 | L1CTL_RESET_CONF = 0x0e |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 55 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 56 | # Reset types |
| 57 | L1CTL_RES_T_BOOT = 0x00 |
| 58 | L1CTL_RES_T_FULL = 0x01 |
| 59 | L1CTL_RES_T_SCHED = 0x02 |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 60 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 61 | def __init__(self, type=L1CTL_RES_T_FULL): |
| 62 | super(L1CTLMessageReset, self).__init__(self.L1CTL_RESET_REQ) |
| 63 | self.data += struct.pack("Bxxx", type) |
| 64 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 65 | |
| 66 | class L1CTLMessageSIM(L1CTLMessage): |
| 67 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 68 | # SIM related message types |
| 69 | L1CTL_SIM_REQ = 0x16 |
| 70 | L1CTL_SIM_CONF = 0x17 |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 71 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 72 | def __init__(self, pdu): |
| 73 | super(L1CTLMessageSIM, self).__init__(self.L1CTL_SIM_REQ) |
| 74 | self.data += pdu |
| 75 | |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 76 | |
| 77 | class CalypsoSimLink(LinkBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 78 | """Transport Link for Calypso based phones.""" |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 79 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 80 | def __init__(self, sock_path: str = "/tmp/osmocom_l2", **kwargs): |
| 81 | super().__init__(**kwargs) |
Philipp Maier | 3077343 | 2023-10-23 10:18:04 +0200 | [diff] [blame] | 82 | print("Using Calypso-based (OsmocomBB) reader interface") |
| 83 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 84 | # Make sure that a given socket path exists |
| 85 | if not os.path.exists(sock_path): |
| 86 | raise ReaderError( |
| 87 | "There is no such ('%s') UNIX socket" % sock_path) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 88 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 89 | print("Connecting to osmocon at '%s'..." % sock_path) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 90 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 91 | # Establish a client connection |
| 92 | self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 93 | self.sock.connect(sock_path) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 94 | |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 95 | # Remember socket path |
| 96 | self._sock_path = sock_path |
| 97 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 98 | def __del__(self): |
| 99 | self.sock.close() |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 100 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 101 | def wait_for_rsp(self, exp_len: int = 128): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 102 | # Wait for incoming data (timeout is 3 seconds) |
| 103 | s, _, _ = select.select([self.sock], [], [], 3.0) |
| 104 | if not s: |
| 105 | raise ReaderError("Timeout waiting for card response") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 106 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 107 | # Receive expected amount of bytes from osmocon |
| 108 | rsp = self.sock.recv(exp_len) |
| 109 | return rsp |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 110 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 111 | def reset_card(self): |
| 112 | # Request FULL reset |
| 113 | req_msg = L1CTLMessageReset() |
| 114 | self.sock.send(req_msg.gen_msg()) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 115 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 116 | # Wait for confirmation |
| 117 | rsp = self.wait_for_rsp() |
| 118 | rsp_msg = struct.unpack_from("!HB", rsp) |
| 119 | if rsp_msg[1] != L1CTLMessageReset.L1CTL_RESET_CONF: |
| 120 | raise ReaderError("Failed to reset Calypso PHY") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 121 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 122 | def connect(self): |
| 123 | self.reset_card() |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 124 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 125 | def disconnect(self): |
| 126 | pass # Nothing to do really ... |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 127 | |
Harald Welte | ab6897c | 2023-07-09 16:21:23 +0200 | [diff] [blame] | 128 | def wait_for_card(self, timeout: Optional[int] = None, newcardonly: bool = False): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 129 | pass # Nothing to do really ... |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 130 | |
Harald Welte | f9f8d7a | 2023-07-09 17:06:16 +0200 | [diff] [blame] | 131 | def _send_apdu_raw(self, pdu: Hexstr) -> ResTuple: |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 132 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 133 | # Request FULL reset |
| 134 | req_msg = L1CTLMessageSIM(h2b(pdu)) |
| 135 | self.sock.send(req_msg.gen_msg()) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 136 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 137 | # Read message length first |
| 138 | rsp = self.wait_for_rsp(struct.calcsize("!H")) |
| 139 | msg_len = struct.unpack_from("!H", rsp)[0] |
| 140 | if msg_len < struct.calcsize("BBxx"): |
| 141 | raise ReaderError("Missing L1CTL header for L1CTL_SIM_CONF") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 142 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 143 | # Read the whole message then |
| 144 | rsp = self.sock.recv(msg_len) |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 145 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 146 | # Verify L1CTL header |
| 147 | hdr = struct.unpack_from("BBxx", rsp) |
| 148 | if hdr[0] != L1CTLMessageSIM.L1CTL_SIM_CONF: |
| 149 | raise ReaderError("Unexpected L1CTL message received") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 150 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 151 | # Verify the payload length |
| 152 | offset = struct.calcsize("BBxx") |
| 153 | if len(rsp) <= offset: |
| 154 | raise ProtocolError("Empty response from SIM?!?") |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 155 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 156 | # Omit L1CTL header |
| 157 | rsp = rsp[offset:] |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 158 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 159 | # Unpack data and SW |
| 160 | data = rsp[:-2] |
| 161 | sw = rsp[-2:] |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 162 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 163 | return b2h(data), b2h(sw) |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 164 | |
Philipp Maier | 58e89eb | 2023-10-10 11:59:03 +0200 | [diff] [blame] | 165 | def __str__(self) -> str: |
Philipp Maier | 6bfa8a8 | 2023-10-09 13:32:49 +0200 | [diff] [blame] | 166 | return "osmocon:%s" % (self._sock_path) |
Philipp Maier | 8c82378 | 2023-10-23 10:44:44 +0200 | [diff] [blame] | 167 | |
| 168 | @staticmethod |
| 169 | def argparse_add_reader_args(arg_parser: argparse.ArgumentParser): |
| 170 | osmobb_group = arg_parser.add_argument_group('OsmocomBB Reader') |
| 171 | osmobb_group.add_argument('--osmocon', dest='osmocon_sock', metavar='PATH', default=None, |
| 172 | help='Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)') |