blob: d43e7f2a714f9cc24d14963ec1153ff09537cec8 [file] [log] [blame]
Harald Welteb1a56e02020-10-27 15:44:54 +01001/* simtrace2-cardem-pcsc - main program for the host PC to provide a remote SIM
Harald Welte964cda32019-11-24 22:27:10 +01002 * using the SIMtrace 2 firmware in card emulation mode
3 *
Harald Weltec690a1f2021-04-25 21:15:20 +02004 * (C) 2016-2021 by Harald Welte <hwelte@hmw-consulting.de>
Harald Welte964cda32019-11-24 22:27:10 +01005 * (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 Welte7f7de1e2019-12-15 20:17:44 +010041#include <osmocom/usb/libusb.h>
Harald Welte964cda32019-11-24 22:27:10 +010042#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 Welte964cda32019-11-24 22:27:10 +010047#include <osmocom/core/utils.h>
48#include <osmocom/core/socket.h>
49#include <osmocom/core/msgb.h>
Harald Welte9ec3de92021-04-05 20:30:21 +020050#include <osmocom/core/select.h>
Harald Welte054d7ca2021-06-02 22:09:26 +020051#include <osmocom/core/logging.h>
52#include <osmocom/core/application.h>
Harald Welte964cda32019-11-24 22:27:10 +010053#include <osmocom/sim/class_tables.h>
54#include <osmocom/sim/sim.h>
55
Harald Welte9ec3de92021-04-05 20:30:21 +020056#define LOGCI(ci, lvl, fmt, args ...) printf(fmt, ## args)
Eric Wild0b1a3b42020-04-03 21:21:27 +020057
Harald Welte964cda32019-11-24 22:27:10 +010058static 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 Welte644c2132021-06-01 19:56:27 +020069static 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 Weltec1033c82021-06-01 20:29:40 +020079static uint32_t last_flags = 0;
80
81static 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 Welte964cda32019-11-24 22:27:10 +0100100/***********************************************************************
101 * Incoming Messages
102 ***********************************************************************/
103
104/*! \brief Process a STATUS message from the SIMtrace2 */
Harald Welte208890a2019-11-24 22:46:51 +0100105static int process_do_status(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
Harald Welte964cda32019-11-24 22:27:10 +0100106{
Harald Welte644c2132021-06-01 19:56:27 +0200107 struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
108 char fbuf[80];
Harald Welte964cda32019-11-24 22:27:10 +0100109
Harald Welte644c2132021-06-01 19:56:27 +0200110 cemu_flags2str(fbuf, sizeof(fbuf), status->flags);
111 printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u (%s)\n",
Harald Welte964cda32019-11-24 22:27:10 +0100112 status->flags, status->fi, status->di, status->wi,
Harald Welte644c2132021-06-01 19:56:27 +0200113 status->waiting_time, fbuf);
Harald Welte964cda32019-11-24 22:27:10 +0100114
Harald Weltec1033c82021-06-01 20:29:40 +0200115 update_flags(ci, status->flags);
116
Harald Welte964cda32019-11-24 22:27:10 +0100117 return 0;
118}
119
120/*! \brief Process a PTS indication message from the SIMtrace2 */
Harald Welte208890a2019-11-24 22:46:51 +0100121static int process_do_pts(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
Harald Welte964cda32019-11-24 22:27:10 +0100122{
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 Welte208890a2019-11-24 22:46:51 +0100132static int process_do_rx_da(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
Harald Welte964cda32019-11-24 22:27:10 +0100133{
Harald Welte208890a2019-11-24 22:46:51 +0100134 static struct osmo_apdu_context ac;
Harald Welte964cda32019-11-24 22:27:10 +0100135 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 Welte208890a2019-11-24 22:46:51 +0100143 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
144 data->flags & CEMU_DATA_F_TPDU_HDR);
Harald Welte964cda32019-11-24 22:27:10 +0100145
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 Welte208890a2019-11-24 22:46:51 +0100172 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 Welte964cda32019-11-24 22:27:10 +0100174 } else if (ac.lc.tot > ac.lc.cur) {
Harald Welte208890a2019-11-24 22:46:51 +0100175 osmo_st2_cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
Harald Welte964cda32019-11-24 22:27:10 +0100176 }
177 return 0;
178}
179
180/*! \brief Process an incoming message from the SIMtrace2 */
Harald Welte208890a2019-11-24 22:46:51 +0100181static int process_usb_msg(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
Harald Welte964cda32019-11-24 22:27:10 +0100182{
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 Welte9ec3de92021-04-05 20:30:21 +0200200 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
201 /* firmware confirms configuration change; ignore */
202 break;
Harald Welte964cda32019-11-24 22:27:10 +0100203 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 Welte9ec3de92021-04-05 20:30:21 +0200212/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
213static 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 Welte644c2132021-06-01 19:56:27 +0200216 char fbuf[80];
Harald Welte9ec3de92021-04-05 20:30:21 +0200217
Harald Welte644c2132021-06-01 19:56:27 +0200218 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 Welte9ec3de92021-04-05 20:30:21 +0200220 status->flags, status->fi, status->di, status->wi,
Harald Welte644c2132021-06-01 19:56:27 +0200221 status->waiting_time, fbuf);
Harald Welte9ec3de92021-04-05 20:30:21 +0200222
Harald Weltec1033c82021-06-01 20:29:40 +0200223 update_flags(ci, status->flags);
224
Harald Welte9ec3de92021-04-05 20:30:21 +0200225 return 0;
226}
227
228static 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
250static 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
276static 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
302static 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
327static 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 Welte964cda32019-11-24 22:27:10 +0100354static void print_welcome(void)
355{
Harald Welteb1a56e02020-10-27 15:44:54 +0100356 printf("simtrace2-cardem-pcsc - Using PC/SC reader as SIM\n"
357 "(C) 2010-2020, Harald Welte <laforge@gnumonks.org>\n"
Harald Welte964cda32019-11-24 22:27:10 +0100358 "(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
359}
360
361static void print_help(void)
362{
Harald Welte80b88772020-10-27 15:40:39 +0100363 printf( "\t-h\t--help\n"
Harald Welte964cda32019-11-24 22:27:10 +0100364 "\t-i\t--gsmtap-ip\tA.B.C.D\n"
365 "\t-a\t--skip-atr\n"
Leonard Hübner1372aca2020-10-27 10:16:41 +0100366 "\t-t\t--set-atr\tATR-STRING in HEX\n"
Harald Welte964cda32019-11-24 22:27:10 +0100367 "\t-k\t--keep-running\n"
Eric Wild1fad9222020-01-29 14:44:32 +0100368 "\t-n\t--pcsc-reader-num\n"
Harald Welte964cda32019-11-24 22:27:10 +0100369 "\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
380static const struct option opts[] = {
Harald Welte964cda32019-11-24 22:27:10 +0100381 { "gsmtap-ip", 1, 0, 'i' },
382 { "skip-atr", 0, 0, 'a' },
Leonard Hübner1372aca2020-10-27 10:16:41 +0100383 { "set-atr", 1, 0, 't' },
Harald Welte964cda32019-11-24 22:27:10 +0100384 { "help", 0, 0, 'h' },
385 { "keep-running", 0, 0, 'k' },
Eric Wild1fad9222020-01-29 14:44:32 +0100386 { "pcsc-reader-num", 1, 0, 'n' },
Harald Welte964cda32019-11-24 22:27:10 +0100387 { "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 Welte208890a2019-11-24 22:46:51 +0100397static void run_mainloop(struct osmo_st2_cardem_inst *ci)
Harald Welte964cda32019-11-24 22:27:10 +0100398{
Harald Welte964cda32019-11-24 22:27:10 +0100399 printf("Entering main loop\n");
Harald Welte964cda32019-11-24 22:27:10 +0100400 while (1) {
Harald Welte9ec3de92021-04-05 20:30:21 +0200401 osmo_select_main(0);
Harald Welte964cda32019-11-24 22:27:10 +0100402 }
403}
404
Harald Welte208890a2019-11-24 22:46:51 +0100405static struct osmo_st2_transport _transp;
Harald Welte964cda32019-11-24 22:27:10 +0100406
Harald Welte208890a2019-11-24 22:46:51 +0100407static struct osmo_st2_slot _slot = {
Harald Welte964cda32019-11-24 22:27:10 +0100408 .transp = &_transp,
409 .slot_nr = 0,
410};
411
Harald Welte208890a2019-11-24 22:46:51 +0100412struct osmo_st2_cardem_inst _ci = {
Harald Welte964cda32019-11-24 22:27:10 +0100413 .slot = &_slot,
414};
415
Harald Welte208890a2019-11-24 22:46:51 +0100416struct osmo_st2_cardem_inst *ci = &_ci;
Harald Welte964cda32019-11-24 22:27:10 +0100417
418static void signal_handler(int signal)
419{
420 switch (signal) {
421 case SIGINT:
Harald Welte208890a2019-11-24 22:46:51 +0100422 osmo_st2_cardem_request_card_insert(ci, false);
Harald Welte964cda32019-11-24 22:27:10 +0100423 exit(0);
424 break;
425 default:
426 break;
427 }
428}
429
Harald Welte054d7ca2021-06-02 22:09:26 +0200430static struct log_info log_info = {};
431
Harald Welte964cda32019-11-24 22:27:10 +0100432int main(int argc, char **argv)
433{
Harald Welte208890a2019-11-24 22:46:51 +0100434 struct osmo_st2_transport *transp = ci->slot->transp;
Harald Welte964cda32019-11-24 22:27:10 +0100435 char *gsmtap_host = "127.0.0.1";
436 int rc;
437 int c, ret = 1;
438 int skip_atr = 0;
Harald Weltec690a1f2021-04-25 21:15:20 +0200439 char *atr = NULL;
440 uint8_t override_atr[OSIM_MAX_ATR_LEN];
441 int override_atr_len = 0;
Harald Welte964cda32019-11-24 22:27:10 +0100442 int keep_running = 0;
Harald Welte964cda32019-11-24 22:27:10 +0100443 int if_num = 0, vendor_id = -1, product_id = -1;
444 int config_id = -1, altsetting = 0, addr = -1;
Eric Wild1fad9222020-01-29 14:44:32 +0100445 int reader_num = 0;
Harald Welte964cda32019-11-24 22:27:10 +0100446 char *path = NULL;
447 struct osim_reader_hdl *reader;
448 struct osim_card_hdl *card;
449
450 print_welcome();
451
Harald Welte9ec3de92021-04-05 20:30:21 +0200452 rc = osmo_libusb_init(NULL);
453 if (rc < 0) {
454 fprintf(stderr, "libusb initialization failed\n");
455 return rc;
456 }
457
Harald Welte054d7ca2021-06-02 22:09:26 +0200458 osmo_init_logging2(NULL, &log_info);
459
Harald Welte964cda32019-11-24 22:27:10 +0100460 while (1) {
461 int option_index = 0;
462
Harald Welte80b88772020-10-27 15:40:39 +0100463 c = getopt_long(argc, argv, "hi:V:P:C:I:S:A:H:akn:t:", opts, &option_index);
Harald Welte964cda32019-11-24 22:27:10 +0100464 if (c == -1)
465 break;
466 switch (c) {
Harald Welte964cda32019-11-24 22:27:10 +0100467 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übner1372aca2020-10-27 10:16:41 +0100477 case 't':
478 atr = optarg;
479 break;
Harald Welte964cda32019-11-24 22:27:10 +0100480 case 'k':
481 keep_running = 1;
482 break;
Eric Wild1fad9222020-01-29 14:44:32 +0100483 case 'n':
484 reader_num = atoi(optarg);
485 break;
Harald Welte964cda32019-11-24 22:27:10 +0100486 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 Weltec690a1f2021-04-25 21:15:20 +0200510 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übner1372aca2020-10-27 10:16:41 +0100517 }
518
Harald Welte80b88772020-10-27 15:40:39 +0100519 if (vendor_id < 0 || product_id < 0) {
Harald Welte964cda32019-11-24 22:27:10 +0100520 fprintf(stderr, "You have to specify the vendor and product ID\n");
521 goto do_exit;
522 }
523
Harald Welte964cda32019-11-24 22:27:10 +0100524 ci->card_prof = &osim_uicc_sim_cic_profile;
525
Harald Welte80b88772020-10-27 15:40:39 +0100526 rc = libusb_init(NULL);
527 if (rc < 0) {
528 fprintf(stderr, "libusb initialization failed\n");
529 goto do_exit;
Harald Welte964cda32019-11-24 22:27:10 +0100530 }
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 Wild1fad9222020-01-29 14:44:32 +0100538 reader = osim_reader_open(OSIM_READER_DRV_PCSC, reader_num, "", NULL);
Harald Welte964cda32019-11-24 22:27:10 +0100539 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 Welte80b88772020-10-27 15:40:39 +0100559 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 Welte9ec3de92021-04-05 20:30:21 +0200568 transp->udp_fd = -1;
569 transp->usb_async = true;
Harald Welte80b88772020-10-27 15:40:39 +0100570 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 Welte8e6ba002021-04-25 21:29:22 +0200573 goto close;
Harald Welte80b88772020-10-27 15:40:39 +0100574 }
Harald Welte964cda32019-11-24 22:27:10 +0100575
Harald Welte80b88772020-10-27 15:40:39 +0100576 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 Welte8e6ba002021-04-25 21:29:22 +0200579 goto close;
Harald Welte80b88772020-10-27 15:40:39 +0100580 }
Harald Welte964cda32019-11-24 22:27:10 +0100581
Harald Welte80b88772020-10-27 15:40:39 +0100582 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 Welte8e6ba002021-04-25 21:29:22 +0200586 goto close;
Harald Welte964cda32019-11-24 22:27:10 +0100587 }
588
Harald Welte9ec3de92021-04-05 20:30:21 +0200589 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 Welte964cda32019-11-24 22:27:10 +0100596 /* simulate card-insert to modem (owhw, not qmod) */
Harald Welte208890a2019-11-24 22:46:51 +0100597 osmo_st2_cardem_request_card_insert(ci, true);
Harald Welte964cda32019-11-24 22:27:10 +0100598
599 /* select remote (forwarded) SIM */
Harald Welte208890a2019-11-24 22:46:51 +0100600 osmo_st2_modem_sim_select_remote(ci->slot);
Harald Welte964cda32019-11-24 22:27:10 +0100601
602 if (!skip_atr) {
603 /* set the ATR */
Harald Weltec690a1f2021-04-25 21:15:20 +0200604 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 Welte964cda32019-11-24 22:27:10 +0100612 }
613
614 /* select remote (forwarded) SIM */
Harald Welte208890a2019-11-24 22:46:51 +0100615 osmo_st2_modem_reset_pulse(ci->slot, 300);
Harald Welte964cda32019-11-24 22:27:10 +0100616
617 run_mainloop(ci);
618 ret = 0;
619
Harald Welte80b88772020-10-27 15:40:39 +0100620 libusb_release_interface(transp->usb_devh, 0);
Harald Welte8e6ba002021-04-25 21:29:22 +0200621
622close:
623 if (transp->usb_devh) {
Harald Welte964cda32019-11-24 22:27:10 +0100624 libusb_close(transp->usb_devh);
Harald Welte8e6ba002021-04-25 21:29:22 +0200625 transp->usb_devh = NULL;
626 }
Harald Welte964cda32019-11-24 22:27:10 +0100627 if (keep_running)
628 sleep(1);
629 } while (keep_running);
630
Harald Welte8e6ba002021-04-25 21:29:22 +0200631close_exit:
632 if (transp->usb_devh)
633 libusb_close(transp->usb_devh);
634
Harald Welte80b88772020-10-27 15:40:39 +0100635 libusb_exit(NULL);
Harald Welte964cda32019-11-24 22:27:10 +0100636do_exit:
637 return ret;
638}