Harald Welte | b1a56e0 | 2020-10-27 15:44:54 +0100 | [diff] [blame] | 1 | /* simtrace2-cardem-pcsc - main program for the host PC to provide a remote SIM |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 2 | * using the SIMtrace 2 firmware in card emulation mode |
| 3 | * |
Harald Welte | c690a1f | 2021-04-25 21:15:20 +0200 | [diff] [blame] | 4 | * (C) 2016-2021 by Harald Welte <hwelte@hmw-consulting.de> |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 5 | * (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <stdint.h> |
| 28 | #include <signal.h> |
| 29 | #include <time.h> |
| 30 | #define _GNU_SOURCE |
| 31 | #include <getopt.h> |
| 32 | |
| 33 | #include <sys/time.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <arpa/inet.h> |
| 38 | |
| 39 | #include <libusb.h> |
| 40 | |
Harald Welte | 7f7de1e | 2019-12-15 20:17:44 +0100 | [diff] [blame] | 41 | #include <osmocom/usb/libusb.h> |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 42 | #include <osmocom/simtrace2/simtrace2_api.h> |
| 43 | #include <osmocom/simtrace2/simtrace_prot.h> |
| 44 | #include <osmocom/simtrace2/apdu_dispatch.h> |
| 45 | #include <osmocom/simtrace2/gsmtap.h> |
| 46 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 47 | #include <osmocom/core/utils.h> |
| 48 | #include <osmocom/core/socket.h> |
| 49 | #include <osmocom/core/msgb.h> |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 50 | #include <osmocom/core/select.h> |
Harald Welte | 054d7ca | 2021-06-02 22:09:26 +0200 | [diff] [blame] | 51 | #include <osmocom/core/logging.h> |
| 52 | #include <osmocom/core/application.h> |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 53 | #include <osmocom/sim/class_tables.h> |
| 54 | #include <osmocom/sim/sim.h> |
| 55 | |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 56 | #define LOGCI(ci, lvl, fmt, args ...) printf(fmt, ## args) |
Eric Wild | 0b1a3b4 | 2020-04-03 21:21:27 +0200 | [diff] [blame] | 57 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 58 | static void atr_update_csum(uint8_t *atr, unsigned int atr_len) |
| 59 | { |
| 60 | uint8_t csum = 0; |
| 61 | int i; |
| 62 | |
| 63 | for (i = 1; i < atr_len - 1; i++) |
| 64 | csum = csum ^ atr[i]; |
| 65 | |
| 66 | atr[atr_len-1] = csum; |
| 67 | } |
| 68 | |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 69 | static void cemu_flags2str(char *out, unsigned int out_len, uint32_t flags) |
| 70 | { |
| 71 | snprintf(out, out_len, "%s%s%s%s%s", |
| 72 | flags & CEMU_STATUS_F_RESET_ACTIVE ? "RESET " : "", |
| 73 | flags & CEMU_STATUS_F_VCC_PRESENT ? "VCC " : "", |
| 74 | flags & CEMU_STATUS_F_CLK_ACTIVE ? "CLK " : "", |
| 75 | flags & CEMU_STATUS_F_CARD_INSERT ? "CARD_PRES " : "", |
| 76 | flags & CEMU_STATUS_F_RCEMU_ACTIVE ? "RCEMU " : ""); |
| 77 | } |
| 78 | |
Harald Welte | c1033c8 | 2021-06-01 20:29:40 +0200 | [diff] [blame] | 79 | static uint32_t last_flags = 0; |
| 80 | |
| 81 | static void update_flags(struct osmo_st2_cardem_inst *ci, uint32_t flags) |
| 82 | { |
| 83 | struct osim_card_hdl *card = ci->chan->card; |
| 84 | |
| 85 | if ((flags & CEMU_STATUS_F_VCC_PRESENT) && (flags & CEMU_STATUS_F_CLK_ACTIVE) && |
| 86 | !(flags & CEMU_STATUS_F_RESET_ACTIVE)) { |
| 87 | if (last_flags & CEMU_STATUS_F_RESET_ACTIVE) { |
| 88 | /* a reset has just ended, forward it to the real card */ |
| 89 | bool cold_reset = true; |
| 90 | if (last_flags & CEMU_STATUS_F_VCC_PRESENT) |
| 91 | cold_reset = false; |
| 92 | LOGCI(ci, LOGL_NOTICE, "%s Resetting card in reader...\n", |
| 93 | cold_reset ? "Cold" : "Warm"); |
| 94 | osim_card_reset(card, cold_reset); |
| 95 | } |
| 96 | } |
| 97 | last_flags = flags; |
| 98 | } |
| 99 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 100 | /*********************************************************************** |
| 101 | * Incoming Messages |
| 102 | ***********************************************************************/ |
| 103 | |
| 104 | /*! \brief Process a STATUS message from the SIMtrace2 */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 105 | static int process_do_status(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 106 | { |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 107 | struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf; |
| 108 | char fbuf[80]; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 109 | |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 110 | cemu_flags2str(fbuf, sizeof(fbuf), status->flags); |
| 111 | printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u (%s)\n", |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 112 | status->flags, status->fi, status->di, status->wi, |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 113 | status->waiting_time, fbuf); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 114 | |
Harald Welte | c1033c8 | 2021-06-01 20:29:40 +0200 | [diff] [blame] | 115 | update_flags(ci, status->flags); |
| 116 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /*! \brief Process a PTS indication message from the SIMtrace2 */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 121 | static int process_do_pts(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 122 | { |
| 123 | struct cardemu_usb_msg_pts_info *pts; |
| 124 | pts = (struct cardemu_usb_msg_pts_info *) buf; |
| 125 | |
| 126 | printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req))); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /*! \brief Process a RX-DATA indication message from the SIMtrace2 */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 132 | static int process_do_rx_da(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 133 | { |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 134 | static struct osmo_apdu_context ac; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 135 | struct cardemu_usb_msg_rx_data *data; |
| 136 | int rc; |
| 137 | |
| 138 | data = (struct cardemu_usb_msg_rx_data *) buf; |
| 139 | |
| 140 | printf("=> DATA: flags=%x, %s: ", data->flags, |
| 141 | osmo_hexdump(data->data, data->data_len)); |
| 142 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 143 | rc = osmo_apdu_segment_in(&ac, data->data, data->data_len, |
| 144 | data->flags & CEMU_DATA_F_TPDU_HDR); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 145 | |
| 146 | if (rc & APDU_ACT_TX_CAPDU_TO_CARD) { |
| 147 | struct msgb *tmsg = msgb_alloc(1024, "TPDU"); |
| 148 | struct osim_reader_hdl *rh = ci->chan->card->reader; |
| 149 | uint8_t *cur; |
| 150 | |
| 151 | /* Copy TPDU header */ |
| 152 | cur = msgb_put(tmsg, sizeof(ac.hdr)); |
| 153 | memcpy(cur, &ac.hdr, sizeof(ac.hdr)); |
| 154 | /* Copy D(c), if any */ |
| 155 | if (ac.lc.tot) { |
| 156 | cur = msgb_put(tmsg, ac.lc.tot); |
| 157 | memcpy(cur, ac.dc, ac.lc.tot); |
| 158 | } |
| 159 | /* send to actual card */ |
| 160 | tmsg->l3h = tmsg->tail; |
| 161 | rc = rh->ops->transceive(rh, tmsg); |
| 162 | if (rc < 0) { |
| 163 | fprintf(stderr, "error during transceive: %d\n", rc); |
| 164 | msgb_free(tmsg); |
| 165 | return rc; |
| 166 | } |
| 167 | msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg); |
| 168 | ac.sw[0] = msgb_apdu_sw(tmsg) >> 8; |
| 169 | ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff; |
| 170 | printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg)); |
| 171 | if (msgb_l3len(tmsg)) |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 172 | osmo_st2_cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg)); |
| 173 | osmo_st2_cardem_request_sw_tx(ci, ac.sw); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 174 | } else if (ac.lc.tot > ac.lc.cur) { |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 175 | osmo_st2_cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /*! \brief Process an incoming message from the SIMtrace2 */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 181 | static int process_usb_msg(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 182 | { |
| 183 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
| 184 | int rc; |
| 185 | |
| 186 | printf("-> %s\n", osmo_hexdump(buf, len)); |
| 187 | |
| 188 | buf += sizeof(*sh); |
| 189 | |
| 190 | switch (sh->msg_type) { |
| 191 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
| 192 | rc = process_do_status(ci, buf, len); |
| 193 | break; |
| 194 | case SIMTRACE_MSGT_DO_CEMU_PTS: |
| 195 | rc = process_do_pts(ci, buf, len); |
| 196 | break; |
| 197 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
| 198 | rc = process_do_rx_da(ci, buf, len); |
| 199 | break; |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 200 | case SIMTRACE_MSGT_BD_CEMU_CONFIG: |
| 201 | /* firmware confirms configuration change; ignore */ |
| 202 | break; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 203 | default: |
| 204 | printf("unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 205 | rc = -1; |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | return rc; |
| 210 | } |
| 211 | |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 212 | /*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */ |
| 213 | static int process_irq_status(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, int len) |
| 214 | { |
| 215 | const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf; |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 216 | char fbuf[80]; |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 217 | |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 218 | cemu_flags2str(fbuf, sizeof(fbuf), status->flags); |
| 219 | LOGCI(ci, LOGL_INFO, "SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u (%s)\n", |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 220 | status->flags, status->fi, status->di, status->wi, |
Harald Welte | 644c213 | 2021-06-01 19:56:27 +0200 | [diff] [blame] | 221 | status->waiting_time, fbuf); |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 222 | |
Harald Welte | c1033c8 | 2021-06-01 20:29:40 +0200 | [diff] [blame] | 223 | update_flags(ci, status->flags); |
| 224 | |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int process_usb_msg_irq(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, unsigned int len) |
| 229 | { |
| 230 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf; |
| 231 | int rc; |
| 232 | |
| 233 | LOGCI(ci, LOGL_INFO, "SIMtrace IRQ %s\n", osmo_hexdump(buf, len)); |
| 234 | |
| 235 | buf += sizeof(*sh); |
| 236 | |
| 237 | switch (sh->msg_type) { |
| 238 | case SIMTRACE_MSGT_BD_CEMU_STATUS: |
| 239 | rc = process_irq_status(ci, buf, len); |
| 240 | break; |
| 241 | default: |
| 242 | LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", sh->msg_type); |
| 243 | rc = -1; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | return rc; |
| 248 | } |
| 249 | |
| 250 | static void usb_in_xfer_cb(struct libusb_transfer *xfer) |
| 251 | { |
| 252 | struct osmo_st2_cardem_inst *ci = xfer->user_data; |
| 253 | int rc; |
| 254 | |
| 255 | switch (xfer->status) { |
| 256 | case LIBUSB_TRANSFER_COMPLETED: |
| 257 | /* hand the message up the stack */ |
| 258 | process_usb_msg(ci, xfer->buffer, xfer->actual_length); |
| 259 | break; |
| 260 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 261 | LOGCI(ci, LOGL_FATAL, "USB device disappeared\n"); |
| 262 | exit(1); |
| 263 | break; |
| 264 | default: |
| 265 | LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status); |
| 266 | exit(1); |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | /* re-submit the IN transfer */ |
| 271 | rc = libusb_submit_transfer(xfer); |
| 272 | OSMO_ASSERT(rc == 0); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | static void allocate_and_submit_in(struct osmo_st2_cardem_inst *ci) |
| 277 | { |
| 278 | struct osmo_st2_transport *transp = ci->slot->transp; |
| 279 | struct libusb_transfer *xfer; |
| 280 | int rc; |
| 281 | |
| 282 | xfer = libusb_alloc_transfer(0); |
| 283 | OSMO_ASSERT(xfer); |
| 284 | xfer->dev_handle = transp->usb_devh; |
| 285 | xfer->flags = 0; |
| 286 | xfer->type = LIBUSB_TRANSFER_TYPE_BULK; |
| 287 | xfer->endpoint = transp->usb_ep.in; |
| 288 | xfer->timeout = 0; |
| 289 | xfer->user_data = ci; |
| 290 | xfer->length = 16*256; |
| 291 | |
| 292 | xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length); |
| 293 | OSMO_ASSERT(xfer->buffer); |
| 294 | xfer->callback = usb_in_xfer_cb; |
| 295 | |
| 296 | /* submit the IN transfer */ |
| 297 | rc = libusb_submit_transfer(xfer); |
| 298 | OSMO_ASSERT(rc == 0); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | static void usb_irq_xfer_cb(struct libusb_transfer *xfer) |
| 303 | { |
| 304 | struct osmo_st2_cardem_inst *ci = xfer->user_data; |
| 305 | int rc; |
| 306 | |
| 307 | switch (xfer->status) { |
| 308 | case LIBUSB_TRANSFER_COMPLETED: |
| 309 | process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length); |
| 310 | break; |
| 311 | case LIBUSB_TRANSFER_NO_DEVICE: |
| 312 | LOGCI(ci, LOGL_FATAL, "USB device disappeared\n"); |
| 313 | exit(1); |
| 314 | break; |
| 315 | default: |
| 316 | LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status); |
| 317 | exit(1); |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | /* re-submit the IN transfer */ |
| 322 | rc = libusb_submit_transfer(xfer); |
| 323 | OSMO_ASSERT(rc == 0); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static void allocate_and_submit_irq(struct osmo_st2_cardem_inst *ci) |
| 328 | { |
| 329 | struct osmo_st2_transport *transp = ci->slot->transp; |
| 330 | struct libusb_transfer *xfer; |
| 331 | int rc; |
| 332 | |
| 333 | xfer = libusb_alloc_transfer(0); |
| 334 | OSMO_ASSERT(xfer); |
| 335 | xfer->dev_handle = transp->usb_devh; |
| 336 | xfer->flags = 0; |
| 337 | xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; |
| 338 | xfer->endpoint = transp->usb_ep.irq_in; |
| 339 | xfer->timeout = 0; |
| 340 | xfer->user_data = ci; |
| 341 | xfer->length = 64; |
| 342 | |
| 343 | xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length); |
| 344 | OSMO_ASSERT(xfer->buffer); |
| 345 | xfer->callback = usb_irq_xfer_cb; |
| 346 | |
| 347 | /* submit the IN transfer */ |
| 348 | rc = libusb_submit_transfer(xfer); |
| 349 | OSMO_ASSERT(rc == 0); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 354 | static void print_welcome(void) |
| 355 | { |
Harald Welte | b1a56e0 | 2020-10-27 15:44:54 +0100 | [diff] [blame] | 356 | printf("simtrace2-cardem-pcsc - Using PC/SC reader as SIM\n" |
| 357 | "(C) 2010-2020, Harald Welte <laforge@gnumonks.org>\n" |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 358 | "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n"); |
| 359 | } |
| 360 | |
| 361 | static void print_help(void) |
| 362 | { |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 363 | printf( "\t-h\t--help\n" |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 364 | "\t-i\t--gsmtap-ip\tA.B.C.D\n" |
| 365 | "\t-a\t--skip-atr\n" |
Leonard Hübner | 1372aca | 2020-10-27 10:16:41 +0100 | [diff] [blame] | 366 | "\t-t\t--set-atr\tATR-STRING in HEX\n" |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 367 | "\t-k\t--keep-running\n" |
Eric Wild | 1fad922 | 2020-01-29 14:44:32 +0100 | [diff] [blame] | 368 | "\t-n\t--pcsc-reader-num\n" |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 369 | "\t-V\t--usb-vendor\tVENDOR_ID\n" |
| 370 | "\t-P\t--usb-product\tPRODUCT_ID\n" |
| 371 | "\t-C\t--usb-config\tCONFIG_ID\n" |
| 372 | "\t-I\t--usb-interface\tINTERFACE_ID\n" |
| 373 | "\t-S\t--usb-altsetting ALTSETTING_ID\n" |
| 374 | "\t-A\t--usb-address\tADDRESS\n" |
| 375 | "\t-H\t--usb-path\tPATH\n" |
| 376 | "\n" |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | static const struct option opts[] = { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 381 | { "gsmtap-ip", 1, 0, 'i' }, |
| 382 | { "skip-atr", 0, 0, 'a' }, |
Leonard Hübner | 1372aca | 2020-10-27 10:16:41 +0100 | [diff] [blame] | 383 | { "set-atr", 1, 0, 't' }, |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 384 | { "help", 0, 0, 'h' }, |
| 385 | { "keep-running", 0, 0, 'k' }, |
Eric Wild | 1fad922 | 2020-01-29 14:44:32 +0100 | [diff] [blame] | 386 | { "pcsc-reader-num", 1, 0, 'n' }, |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 387 | { "usb-vendor", 1, 0, 'V' }, |
| 388 | { "usb-product", 1, 0, 'P' }, |
| 389 | { "usb-config", 1, 0, 'C' }, |
| 390 | { "usb-interface", 1, 0, 'I' }, |
| 391 | { "usb-altsetting", 1, 0, 'S' }, |
| 392 | { "usb-address", 1, 0, 'A' }, |
| 393 | { "usb-path", 1, 0, 'H' }, |
| 394 | { NULL, 0, 0, 0 } |
| 395 | }; |
| 396 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 397 | static void run_mainloop(struct osmo_st2_cardem_inst *ci) |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 398 | { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 399 | printf("Entering main loop\n"); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 400 | while (1) { |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 401 | osmo_select_main(0); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 405 | static struct osmo_st2_transport _transp; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 406 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 407 | static struct osmo_st2_slot _slot = { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 408 | .transp = &_transp, |
| 409 | .slot_nr = 0, |
| 410 | }; |
| 411 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 412 | struct osmo_st2_cardem_inst _ci = { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 413 | .slot = &_slot, |
| 414 | }; |
| 415 | |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 416 | struct osmo_st2_cardem_inst *ci = &_ci; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 417 | |
| 418 | static void signal_handler(int signal) |
| 419 | { |
| 420 | switch (signal) { |
| 421 | case SIGINT: |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 422 | osmo_st2_cardem_request_card_insert(ci, false); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 423 | exit(0); |
| 424 | break; |
| 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
Harald Welte | 054d7ca | 2021-06-02 22:09:26 +0200 | [diff] [blame] | 430 | static struct log_info log_info = {}; |
| 431 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 432 | int main(int argc, char **argv) |
| 433 | { |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 434 | struct osmo_st2_transport *transp = ci->slot->transp; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 435 | char *gsmtap_host = "127.0.0.1"; |
| 436 | int rc; |
| 437 | int c, ret = 1; |
| 438 | int skip_atr = 0; |
Harald Welte | c690a1f | 2021-04-25 21:15:20 +0200 | [diff] [blame] | 439 | char *atr = NULL; |
| 440 | uint8_t override_atr[OSIM_MAX_ATR_LEN]; |
| 441 | int override_atr_len = 0; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 442 | int keep_running = 0; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 443 | int if_num = 0, vendor_id = -1, product_id = -1; |
| 444 | int config_id = -1, altsetting = 0, addr = -1; |
Eric Wild | 1fad922 | 2020-01-29 14:44:32 +0100 | [diff] [blame] | 445 | int reader_num = 0; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 446 | char *path = NULL; |
| 447 | struct osim_reader_hdl *reader; |
| 448 | struct osim_card_hdl *card; |
| 449 | |
| 450 | print_welcome(); |
| 451 | |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 452 | rc = osmo_libusb_init(NULL); |
| 453 | if (rc < 0) { |
| 454 | fprintf(stderr, "libusb initialization failed\n"); |
| 455 | return rc; |
| 456 | } |
| 457 | |
Harald Welte | 054d7ca | 2021-06-02 22:09:26 +0200 | [diff] [blame] | 458 | osmo_init_logging2(NULL, &log_info); |
| 459 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 460 | while (1) { |
| 461 | int option_index = 0; |
| 462 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 463 | c = getopt_long(argc, argv, "hi:V:P:C:I:S:A:H:akn:t:", opts, &option_index); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 464 | if (c == -1) |
| 465 | break; |
| 466 | switch (c) { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 467 | case 'h': |
| 468 | print_help(); |
| 469 | exit(0); |
| 470 | break; |
| 471 | case 'i': |
| 472 | gsmtap_host = optarg; |
| 473 | break; |
| 474 | case 'a': |
| 475 | skip_atr = 1; |
| 476 | break; |
Leonard Hübner | 1372aca | 2020-10-27 10:16:41 +0100 | [diff] [blame] | 477 | case 't': |
| 478 | atr = optarg; |
| 479 | break; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 480 | case 'k': |
| 481 | keep_running = 1; |
| 482 | break; |
Eric Wild | 1fad922 | 2020-01-29 14:44:32 +0100 | [diff] [blame] | 483 | case 'n': |
| 484 | reader_num = atoi(optarg); |
| 485 | break; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 486 | case 'V': |
| 487 | vendor_id = strtol(optarg, NULL, 16); |
| 488 | break; |
| 489 | case 'P': |
| 490 | product_id = strtol(optarg, NULL, 16); |
| 491 | break; |
| 492 | case 'C': |
| 493 | config_id = atoi(optarg); |
| 494 | break; |
| 495 | case 'I': |
| 496 | if_num = atoi(optarg); |
| 497 | break; |
| 498 | case 'S': |
| 499 | altsetting = atoi(optarg); |
| 500 | break; |
| 501 | case 'A': |
| 502 | addr = atoi(optarg); |
| 503 | break; |
| 504 | case 'H': |
| 505 | path = optarg; |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
Harald Welte | c690a1f | 2021-04-25 21:15:20 +0200 | [diff] [blame] | 510 | if (atr) { |
| 511 | override_atr_len = osmo_hexparse(atr, override_atr, sizeof(override_atr)); |
| 512 | if (override_atr_len < 2) { |
| 513 | fprintf(stderr, "Invalid ATR - please omit a leading 0x and only use valid hex " |
| 514 | "digits and whitespace. ATRs need to be between 2 and 33 bytes long.\n"); |
| 515 | goto do_exit; |
| 516 | } |
Leonard Hübner | 1372aca | 2020-10-27 10:16:41 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 519 | if (vendor_id < 0 || product_id < 0) { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 520 | fprintf(stderr, "You have to specify the vendor and product ID\n"); |
| 521 | goto do_exit; |
| 522 | } |
| 523 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 524 | ci->card_prof = &osim_uicc_sim_cic_profile; |
| 525 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 526 | rc = libusb_init(NULL); |
| 527 | if (rc < 0) { |
| 528 | fprintf(stderr, "libusb initialization failed\n"); |
| 529 | goto do_exit; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | rc = osmo_st2_gsmtap_init(gsmtap_host); |
| 533 | if (rc < 0) { |
| 534 | perror("unable to open GSMTAP"); |
| 535 | goto close_exit; |
| 536 | } |
| 537 | |
Eric Wild | 1fad922 | 2020-01-29 14:44:32 +0100 | [diff] [blame] | 538 | reader = osim_reader_open(OSIM_READER_DRV_PCSC, reader_num, "", NULL); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 539 | if (!reader) { |
| 540 | perror("unable to open PC/SC reader"); |
| 541 | goto close_exit; |
| 542 | } |
| 543 | |
| 544 | card = osim_card_open(reader, OSIM_PROTO_T0); |
| 545 | if (!card) { |
| 546 | perror("unable to open SIM card"); |
| 547 | goto close_exit; |
| 548 | } |
| 549 | |
| 550 | ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list); |
| 551 | if (!ci->chan) { |
| 552 | perror("SIM card has no channel?!?"); |
| 553 | goto close_exit; |
| 554 | } |
| 555 | |
| 556 | signal(SIGINT, &signal_handler); |
| 557 | |
| 558 | do { |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 559 | struct usb_interface_match _ifm, *ifm = &_ifm; |
| 560 | ifm->vendor = vendor_id; |
| 561 | ifm->product = product_id; |
| 562 | ifm->configuration = config_id; |
| 563 | ifm->interface = if_num; |
| 564 | ifm->altsetting = altsetting; |
| 565 | ifm->addr = addr; |
| 566 | if (path) |
| 567 | osmo_strlcpy(ifm->path, path, sizeof(ifm->path)); |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 568 | transp->udp_fd = -1; |
| 569 | transp->usb_async = true; |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 570 | transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm); |
| 571 | if (!transp->usb_devh) { |
| 572 | fprintf(stderr, "can't open USB device\n"); |
Harald Welte | 8e6ba00 | 2021-04-25 21:29:22 +0200 | [diff] [blame] | 573 | goto close; |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 574 | } |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 575 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 576 | rc = libusb_claim_interface(transp->usb_devh, if_num); |
| 577 | if (rc < 0) { |
| 578 | fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc); |
Harald Welte | 8e6ba00 | 2021-04-25 21:29:22 +0200 | [diff] [blame] | 579 | goto close; |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 580 | } |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 581 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 582 | rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out, |
| 583 | &transp->usb_ep.in, &transp->usb_ep.irq_in); |
| 584 | if (rc < 0) { |
| 585 | fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc); |
Harald Welte | 8e6ba00 | 2021-04-25 21:29:22 +0200 | [diff] [blame] | 586 | goto close; |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Harald Welte | 9ec3de9 | 2021-04-05 20:30:21 +0200 | [diff] [blame] | 589 | allocate_and_submit_irq(ci); |
| 590 | for (int i = 0; i < 4; i++) |
| 591 | allocate_and_submit_in(ci); |
| 592 | |
| 593 | /* request firmware to generate STATUS on IRQ endpoint */ |
| 594 | osmo_st2_cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ); |
| 595 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 596 | /* simulate card-insert to modem (owhw, not qmod) */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 597 | osmo_st2_cardem_request_card_insert(ci, true); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 598 | |
| 599 | /* select remote (forwarded) SIM */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 600 | osmo_st2_modem_sim_select_remote(ci->slot); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 601 | |
| 602 | if (!skip_atr) { |
| 603 | /* set the ATR */ |
Harald Welte | c690a1f | 2021-04-25 21:15:20 +0200 | [diff] [blame] | 604 | if (override_atr_len) { |
| 605 | /* user has specified an override-ATR */ |
| 606 | atr_update_csum(override_atr, override_atr_len); |
| 607 | osmo_st2_cardem_request_set_atr(ci, override_atr, override_atr_len); |
| 608 | } else { |
| 609 | /* use the real ATR of the card */ |
| 610 | osmo_st2_cardem_request_set_atr(ci, card->atr, card->atr_len); |
| 611 | } |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* select remote (forwarded) SIM */ |
Harald Welte | 208890a | 2019-11-24 22:46:51 +0100 | [diff] [blame] | 615 | osmo_st2_modem_reset_pulse(ci->slot, 300); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 616 | |
| 617 | run_mainloop(ci); |
| 618 | ret = 0; |
| 619 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 620 | libusb_release_interface(transp->usb_devh, 0); |
Harald Welte | 8e6ba00 | 2021-04-25 21:29:22 +0200 | [diff] [blame] | 621 | |
| 622 | close: |
| 623 | if (transp->usb_devh) { |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 624 | libusb_close(transp->usb_devh); |
Harald Welte | 8e6ba00 | 2021-04-25 21:29:22 +0200 | [diff] [blame] | 625 | transp->usb_devh = NULL; |
| 626 | } |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 627 | if (keep_running) |
| 628 | sleep(1); |
| 629 | } while (keep_running); |
| 630 | |
Harald Welte | 8e6ba00 | 2021-04-25 21:29:22 +0200 | [diff] [blame] | 631 | close_exit: |
| 632 | if (transp->usb_devh) |
| 633 | libusb_close(transp->usb_devh); |
| 634 | |
Harald Welte | 80b8877 | 2020-10-27 15:40:39 +0100 | [diff] [blame] | 635 | libusb_exit(NULL); |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 636 | do_exit: |
| 637 | return ret; |
| 638 | } |