2020-10-27 14:44:54 +00:00
|
|
|
/* simtrace2-cardem-pcsc - main program for the host PC to provide a remote SIM
|
2018-07-07 15:35:15 +00:00
|
|
|
* using the SIMtrace 2 firmware in card emulation mode
|
2016-03-19 12:39:33 +00:00
|
|
|
*
|
2021-04-25 19:15:20 +00:00
|
|
|
* (C) 2016-2021 by Harald Welte <hwelte@hmw-consulting.de>
|
2018-10-02 13:56:37 +00:00
|
|
|
* (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
|
2016-03-19 12:39:33 +00:00
|
|
|
*
|
2018-07-07 15:35:15 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2016-03-19 12:39:33 +00:00
|
|
|
*
|
2018-07-07 15:35:15 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2016-03-19 12:39:33 +00:00
|
|
|
*/
|
2019-11-24 21:27:10 +00:00
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2016-03-20 14:30:46 +00:00
|
|
|
#include <signal.h>
|
2016-03-19 12:39:33 +00:00
|
|
|
#include <time.h>
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <libusb.h>
|
|
|
|
|
2019-12-15 19:17:44 +00:00
|
|
|
#include <osmocom/usb/libusb.h>
|
2019-11-24 21:27:10 +00:00
|
|
|
#include <osmocom/simtrace2/simtrace2_api.h>
|
|
|
|
#include <osmocom/simtrace2/simtrace_prot.h>
|
|
|
|
#include <osmocom/simtrace2/apdu_dispatch.h>
|
|
|
|
#include <osmocom/simtrace2/gsmtap.h>
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
#include <osmocom/core/socket.h>
|
2017-05-08 11:30:09 +00:00
|
|
|
#include <osmocom/core/msgb.h>
|
2021-04-05 18:30:21 +00:00
|
|
|
#include <osmocom/core/select.h>
|
2021-06-02 20:09:26 +00:00
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/application.h>
|
2016-03-19 12:39:33 +00:00
|
|
|
#include <osmocom/sim/class_tables.h>
|
|
|
|
#include <osmocom/sim/sim.h>
|
|
|
|
|
2021-04-05 18:30:21 +00:00
|
|
|
#define LOGCI(ci, lvl, fmt, args ...) printf(fmt, ## args)
|
2020-04-03 19:21:27 +00:00
|
|
|
|
2016-03-20 14:01:20 +00:00
|
|
|
static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
|
|
|
|
{
|
|
|
|
uint8_t csum = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < atr_len - 1; i++)
|
|
|
|
csum = csum ^ atr[i];
|
|
|
|
|
|
|
|
atr[atr_len-1] = csum;
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:56:27 +00:00
|
|
|
static void cemu_flags2str(char *out, unsigned int out_len, uint32_t flags)
|
|
|
|
{
|
|
|
|
snprintf(out, out_len, "%s%s%s%s%s",
|
|
|
|
flags & CEMU_STATUS_F_RESET_ACTIVE ? "RESET " : "",
|
|
|
|
flags & CEMU_STATUS_F_VCC_PRESENT ? "VCC " : "",
|
|
|
|
flags & CEMU_STATUS_F_CLK_ACTIVE ? "CLK " : "",
|
|
|
|
flags & CEMU_STATUS_F_CARD_INSERT ? "CARD_PRES " : "",
|
|
|
|
flags & CEMU_STATUS_F_RCEMU_ACTIVE ? "RCEMU " : "");
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:29:40 +00:00
|
|
|
static uint32_t last_flags = 0;
|
|
|
|
|
|
|
|
static void update_flags(struct osmo_st2_cardem_inst *ci, uint32_t flags)
|
|
|
|
{
|
|
|
|
struct osim_card_hdl *card = ci->chan->card;
|
|
|
|
|
|
|
|
if ((flags & CEMU_STATUS_F_VCC_PRESENT) && (flags & CEMU_STATUS_F_CLK_ACTIVE) &&
|
|
|
|
!(flags & CEMU_STATUS_F_RESET_ACTIVE)) {
|
|
|
|
if (last_flags & CEMU_STATUS_F_RESET_ACTIVE) {
|
|
|
|
/* a reset has just ended, forward it to the real card */
|
|
|
|
bool cold_reset = true;
|
|
|
|
if (last_flags & CEMU_STATUS_F_VCC_PRESENT)
|
|
|
|
cold_reset = false;
|
|
|
|
LOGCI(ci, LOGL_NOTICE, "%s Resetting card in reader...\n",
|
|
|
|
cold_reset ? "Cold" : "Warm");
|
|
|
|
osim_card_reset(card, cold_reset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_flags = flags;
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:02:52 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Incoming Messages
|
|
|
|
***********************************************************************/
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
/*! \brief Process a STATUS message from the SIMtrace2 */
|
2019-11-24 21:46:51 +00:00
|
|
|
static int process_do_status(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
|
2016-03-19 12:39:33 +00:00
|
|
|
{
|
2021-06-01 17:56:27 +00:00
|
|
|
struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
|
|
|
|
char fbuf[80];
|
2016-03-19 12:39:33 +00:00
|
|
|
|
2021-06-01 17:56:27 +00:00
|
|
|
cemu_flags2str(fbuf, sizeof(fbuf), status->flags);
|
|
|
|
printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u (%s)\n",
|
2016-03-19 12:39:33 +00:00
|
|
|
status->flags, status->fi, status->di, status->wi,
|
2021-06-01 17:56:27 +00:00
|
|
|
status->waiting_time, fbuf);
|
2016-03-19 12:39:33 +00:00
|
|
|
|
2021-06-01 18:29:40 +00:00
|
|
|
update_flags(ci, status->flags);
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Process a PTS indication message from the SIMtrace2 */
|
2019-11-24 21:46:51 +00:00
|
|
|
static int process_do_pts(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
|
2016-03-19 12:39:33 +00:00
|
|
|
{
|
|
|
|
struct cardemu_usb_msg_pts_info *pts;
|
|
|
|
pts = (struct cardemu_usb_msg_pts_info *) buf;
|
|
|
|
|
|
|
|
printf("=> PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
|
2019-11-24 21:46:51 +00:00
|
|
|
static int process_do_rx_da(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
|
2016-03-19 12:39:33 +00:00
|
|
|
{
|
2019-11-24 21:46:51 +00:00
|
|
|
static struct osmo_apdu_context ac;
|
2016-03-19 12:39:33 +00:00
|
|
|
struct cardemu_usb_msg_rx_data *data;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
data = (struct cardemu_usb_msg_rx_data *) buf;
|
|
|
|
|
|
|
|
printf("=> DATA: flags=%x, %s: ", data->flags,
|
|
|
|
osmo_hexdump(data->data, data->data_len));
|
|
|
|
|
2019-11-24 21:46:51 +00:00
|
|
|
rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
|
|
|
|
data->flags & CEMU_DATA_F_TPDU_HDR);
|
2016-03-19 12:39:33 +00:00
|
|
|
|
|
|
|
if (rc & APDU_ACT_TX_CAPDU_TO_CARD) {
|
|
|
|
struct msgb *tmsg = msgb_alloc(1024, "TPDU");
|
2017-03-06 17:51:39 +00:00
|
|
|
struct osim_reader_hdl *rh = ci->chan->card->reader;
|
2016-03-19 12:39:33 +00:00
|
|
|
uint8_t *cur;
|
|
|
|
|
|
|
|
/* Copy TPDU header */
|
|
|
|
cur = msgb_put(tmsg, sizeof(ac.hdr));
|
|
|
|
memcpy(cur, &ac.hdr, sizeof(ac.hdr));
|
|
|
|
/* Copy D(c), if any */
|
|
|
|
if (ac.lc.tot) {
|
|
|
|
cur = msgb_put(tmsg, ac.lc.tot);
|
|
|
|
memcpy(cur, ac.dc, ac.lc.tot);
|
|
|
|
}
|
|
|
|
/* send to actual card */
|
|
|
|
tmsg->l3h = tmsg->tail;
|
|
|
|
rc = rh->ops->transceive(rh, tmsg);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "error during transceive: %d\n", rc);
|
|
|
|
msgb_free(tmsg);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
msgb_apdu_sw(tmsg) = msgb_get_u16(tmsg);
|
|
|
|
ac.sw[0] = msgb_apdu_sw(tmsg) >> 8;
|
|
|
|
ac.sw[1] = msgb_apdu_sw(tmsg) & 0xff;
|
|
|
|
printf("SW=0x%04x, len_rx=%d\n", msgb_apdu_sw(tmsg), msgb_l3len(tmsg));
|
|
|
|
if (msgb_l3len(tmsg))
|
2019-11-24 21:46:51 +00:00
|
|
|
osmo_st2_cardem_request_pb_and_tx(ci, ac.hdr.ins, tmsg->l3h, msgb_l3len(tmsg));
|
|
|
|
osmo_st2_cardem_request_sw_tx(ci, ac.sw);
|
2016-03-19 12:39:33 +00:00
|
|
|
} else if (ac.lc.tot > ac.lc.cur) {
|
2019-11-24 21:46:51 +00:00
|
|
|
osmo_st2_cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
|
2016-03-19 12:39:33 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Process an incoming message from the SIMtrace2 */
|
2019-11-24 21:46:51 +00:00
|
|
|
static int process_usb_msg(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
|
2016-03-19 12:39:33 +00:00
|
|
|
{
|
2017-05-08 11:30:09 +00:00
|
|
|
struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
|
2016-03-19 12:39:33 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
printf("-> %s\n", osmo_hexdump(buf, len));
|
|
|
|
|
2017-05-08 11:30:09 +00:00
|
|
|
buf += sizeof(*sh);
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
switch (sh->msg_type) {
|
2017-05-08 11:30:09 +00:00
|
|
|
case SIMTRACE_MSGT_BD_CEMU_STATUS:
|
2017-03-06 17:51:39 +00:00
|
|
|
rc = process_do_status(ci, buf, len);
|
2016-03-19 12:39:33 +00:00
|
|
|
break;
|
2017-05-08 11:30:09 +00:00
|
|
|
case SIMTRACE_MSGT_DO_CEMU_PTS:
|
2017-03-06 17:51:39 +00:00
|
|
|
rc = process_do_pts(ci, buf, len);
|
2016-03-19 12:39:33 +00:00
|
|
|
break;
|
2017-05-08 11:30:09 +00:00
|
|
|
case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
|
2017-03-06 17:51:39 +00:00
|
|
|
rc = process_do_rx_da(ci, buf, len);
|
2016-03-19 12:39:33 +00:00
|
|
|
break;
|
2021-04-05 18:30:21 +00:00
|
|
|
case SIMTRACE_MSGT_BD_CEMU_CONFIG:
|
|
|
|
/* firmware confirms configuration change; ignore */
|
2021-10-25 06:12:08 +00:00
|
|
|
rc = 0;
|
2021-04-05 18:30:21 +00:00
|
|
|
break;
|
2016-03-19 12:39:33 +00:00
|
|
|
default:
|
|
|
|
printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:30:21 +00:00
|
|
|
/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
|
|
|
|
static int process_irq_status(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
|
2021-06-01 17:56:27 +00:00
|
|
|
char fbuf[80];
|
2021-04-05 18:30:21 +00:00
|
|
|
|
2021-06-01 17:56:27 +00:00
|
|
|
cemu_flags2str(fbuf, sizeof(fbuf), status->flags);
|
|
|
|
LOGCI(ci, LOGL_INFO, "SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u (%s)\n",
|
2021-04-05 18:30:21 +00:00
|
|
|
status->flags, status->fi, status->di, status->wi,
|
2021-06-01 17:56:27 +00:00
|
|
|
status->waiting_time, fbuf);
|
2021-04-05 18:30:21 +00:00
|
|
|
|
2021-06-01 18:29:40 +00:00
|
|
|
update_flags(ci, status->flags);
|
|
|
|
|
2021-04-05 18:30:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int process_usb_msg_irq(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
LOGCI(ci, LOGL_INFO, "SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
|
|
|
|
|
|
|
|
buf += sizeof(*sh);
|
|
|
|
|
|
|
|
switch (sh->msg_type) {
|
|
|
|
case SIMTRACE_MSGT_BD_CEMU_STATUS:
|
|
|
|
rc = process_irq_status(ci, buf, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", sh->msg_type);
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_in_xfer_cb(struct libusb_transfer *xfer)
|
|
|
|
{
|
|
|
|
struct osmo_st2_cardem_inst *ci = xfer->user_data;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (xfer->status) {
|
|
|
|
case LIBUSB_TRANSFER_COMPLETED:
|
|
|
|
/* hand the message up the stack */
|
|
|
|
process_usb_msg(ci, xfer->buffer, xfer->actual_length);
|
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_NO_DEVICE:
|
|
|
|
LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* re-submit the IN transfer */
|
|
|
|
rc = libusb_submit_transfer(xfer);
|
|
|
|
OSMO_ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void allocate_and_submit_in(struct osmo_st2_cardem_inst *ci)
|
|
|
|
{
|
|
|
|
struct osmo_st2_transport *transp = ci->slot->transp;
|
|
|
|
struct libusb_transfer *xfer;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
xfer = libusb_alloc_transfer(0);
|
|
|
|
OSMO_ASSERT(xfer);
|
|
|
|
xfer->dev_handle = transp->usb_devh;
|
|
|
|
xfer->flags = 0;
|
|
|
|
xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
|
|
|
|
xfer->endpoint = transp->usb_ep.in;
|
|
|
|
xfer->timeout = 0;
|
|
|
|
xfer->user_data = ci;
|
|
|
|
xfer->length = 16*256;
|
|
|
|
|
|
|
|
xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
|
|
|
|
OSMO_ASSERT(xfer->buffer);
|
|
|
|
xfer->callback = usb_in_xfer_cb;
|
|
|
|
|
|
|
|
/* submit the IN transfer */
|
|
|
|
rc = libusb_submit_transfer(xfer);
|
|
|
|
OSMO_ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
|
|
|
|
{
|
|
|
|
struct osmo_st2_cardem_inst *ci = xfer->user_data;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (xfer->status) {
|
|
|
|
case LIBUSB_TRANSFER_COMPLETED:
|
|
|
|
process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
|
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_NO_DEVICE:
|
|
|
|
LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* re-submit the IN transfer */
|
|
|
|
rc = libusb_submit_transfer(xfer);
|
|
|
|
OSMO_ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void allocate_and_submit_irq(struct osmo_st2_cardem_inst *ci)
|
|
|
|
{
|
|
|
|
struct osmo_st2_transport *transp = ci->slot->transp;
|
|
|
|
struct libusb_transfer *xfer;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
xfer = libusb_alloc_transfer(0);
|
|
|
|
OSMO_ASSERT(xfer);
|
|
|
|
xfer->dev_handle = transp->usb_devh;
|
|
|
|
xfer->flags = 0;
|
|
|
|
xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
|
|
|
|
xfer->endpoint = transp->usb_ep.irq_in;
|
|
|
|
xfer->timeout = 0;
|
|
|
|
xfer->user_data = ci;
|
|
|
|
xfer->length = 64;
|
|
|
|
|
|
|
|
xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
|
|
|
|
OSMO_ASSERT(xfer->buffer);
|
|
|
|
xfer->callback = usb_irq_xfer_cb;
|
|
|
|
|
|
|
|
/* submit the IN transfer */
|
|
|
|
rc = libusb_submit_transfer(xfer);
|
|
|
|
OSMO_ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
static void print_welcome(void)
|
|
|
|
{
|
2020-10-27 14:44:54 +00:00
|
|
|
printf("simtrace2-cardem-pcsc - Using PC/SC reader as SIM\n"
|
|
|
|
"(C) 2010-2020, Harald Welte <laforge@gnumonks.org>\n"
|
2018-10-02 13:56:37 +00:00
|
|
|
"(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
|
2016-03-19 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_help(void)
|
|
|
|
{
|
2020-10-27 14:40:39 +00:00
|
|
|
printf( "\t-h\t--help\n"
|
2017-03-06 19:56:14 +00:00
|
|
|
"\t-i\t--gsmtap-ip\tA.B.C.D\n"
|
|
|
|
"\t-a\t--skip-atr\n"
|
2020-10-27 09:16:41 +00:00
|
|
|
"\t-t\t--set-atr\tATR-STRING in HEX\n"
|
2016-03-19 12:39:33 +00:00
|
|
|
"\t-k\t--keep-running\n"
|
2020-01-29 13:44:32 +00:00
|
|
|
"\t-n\t--pcsc-reader-num\n"
|
2017-03-06 19:58:03 +00:00
|
|
|
"\t-V\t--usb-vendor\tVENDOR_ID\n"
|
|
|
|
"\t-P\t--usb-product\tPRODUCT_ID\n"
|
|
|
|
"\t-C\t--usb-config\tCONFIG_ID\n"
|
|
|
|
"\t-I\t--usb-interface\tINTERFACE_ID\n"
|
|
|
|
"\t-S\t--usb-altsetting ALTSETTING_ID\n"
|
|
|
|
"\t-A\t--usb-address\tADDRESS\n"
|
2018-10-02 13:55:38 +00:00
|
|
|
"\t-H\t--usb-path\tPATH\n"
|
2016-03-19 12:39:33 +00:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct option opts[] = {
|
|
|
|
{ "gsmtap-ip", 1, 0, 'i' },
|
|
|
|
{ "skip-atr", 0, 0, 'a' },
|
2020-10-27 09:16:41 +00:00
|
|
|
{ "set-atr", 1, 0, 't' },
|
2016-03-19 12:39:33 +00:00
|
|
|
{ "help", 0, 0, 'h' },
|
|
|
|
{ "keep-running", 0, 0, 'k' },
|
2020-01-29 13:44:32 +00:00
|
|
|
{ "pcsc-reader-num", 1, 0, 'n' },
|
2017-03-06 19:58:03 +00:00
|
|
|
{ "usb-vendor", 1, 0, 'V' },
|
|
|
|
{ "usb-product", 1, 0, 'P' },
|
|
|
|
{ "usb-config", 1, 0, 'C' },
|
|
|
|
{ "usb-interface", 1, 0, 'I' },
|
|
|
|
{ "usb-altsetting", 1, 0, 'S' },
|
|
|
|
{ "usb-address", 1, 0, 'A' },
|
2017-05-10 23:31:45 +00:00
|
|
|
{ "usb-path", 1, 0, 'H' },
|
2016-03-19 12:39:33 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2019-11-24 21:46:51 +00:00
|
|
|
static void run_mainloop(struct osmo_st2_cardem_inst *ci)
|
2016-03-19 12:39:33 +00:00
|
|
|
{
|
|
|
|
printf("Entering main loop\n");
|
|
|
|
while (1) {
|
2021-04-05 18:30:21 +00:00
|
|
|
osmo_select_main(0);
|
2016-03-19 12:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 21:46:51 +00:00
|
|
|
static struct osmo_st2_transport _transp;
|
2017-05-10 20:19:26 +00:00
|
|
|
|
2019-11-24 21:46:51 +00:00
|
|
|
static struct osmo_st2_slot _slot = {
|
2017-05-10 20:19:26 +00:00
|
|
|
.transp = &_transp,
|
|
|
|
.slot_nr = 0,
|
|
|
|
};
|
|
|
|
|
2019-11-24 21:46:51 +00:00
|
|
|
struct osmo_st2_cardem_inst _ci = {
|
2017-05-10 20:19:26 +00:00
|
|
|
.slot = &_slot,
|
|
|
|
};
|
|
|
|
|
2019-11-24 21:46:51 +00:00
|
|
|
struct osmo_st2_cardem_inst *ci = &_ci;
|
2017-03-06 17:51:39 +00:00
|
|
|
|
2016-03-20 14:30:46 +00:00
|
|
|
static void signal_handler(int signal)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
|
|
|
case SIGINT:
|
2019-11-24 21:46:51 +00:00
|
|
|
osmo_st2_cardem_request_card_insert(ci, false);
|
2021-07-27 15:09:03 +00:00
|
|
|
osmo_st2_modem_sim_select_local(ci->slot);
|
2016-03-20 14:30:46 +00:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 20:09:26 +00:00
|
|
|
static struct log_info log_info = {};
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-11-24 21:46:51 +00:00
|
|
|
struct osmo_st2_transport *transp = ci->slot->transp;
|
2016-03-19 12:39:33 +00:00
|
|
|
char *gsmtap_host = "127.0.0.1";
|
|
|
|
int rc;
|
|
|
|
int c, ret = 1;
|
|
|
|
int skip_atr = 0;
|
2021-04-25 19:15:20 +00:00
|
|
|
char *atr = NULL;
|
|
|
|
uint8_t override_atr[OSIM_MAX_ATR_LEN];
|
|
|
|
int override_atr_len = 0;
|
2016-03-19 12:39:33 +00:00
|
|
|
int keep_running = 0;
|
2017-03-06 19:58:03 +00:00
|
|
|
int if_num = 0, vendor_id = -1, product_id = -1;
|
|
|
|
int config_id = -1, altsetting = 0, addr = -1;
|
2020-01-29 13:44:32 +00:00
|
|
|
int reader_num = 0;
|
2017-05-10 23:31:45 +00:00
|
|
|
char *path = NULL;
|
2016-03-19 12:39:33 +00:00
|
|
|
struct osim_reader_hdl *reader;
|
|
|
|
struct osim_card_hdl *card;
|
|
|
|
|
|
|
|
print_welcome();
|
|
|
|
|
2022-03-03 14:25:43 +00:00
|
|
|
osmo_init_logging2(NULL, &log_info);
|
|
|
|
|
2021-04-05 18:30:21 +00:00
|
|
|
rc = osmo_libusb_init(NULL);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "libusb initialization failed\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:25:43 +00:00
|
|
|
log_set_print_category_hex(osmo_stderr_target, false);
|
|
|
|
log_set_print_category(osmo_stderr_target, true);
|
|
|
|
log_set_print_level(osmo_stderr_target, true);
|
|
|
|
log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
|
|
|
|
log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
|
|
|
|
log_set_category_filter(osmo_stderr_target, DLINP, 1, LOGL_DEBUG);
|
|
|
|
log_set_category_filter(osmo_stderr_target, DLGLOBAL, 1, LOGL_DEBUG);
|
2021-06-02 20:09:26 +00:00
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
|
|
|
|
2020-10-27 14:40:39 +00:00
|
|
|
c = getopt_long(argc, argv, "hi:V:P:C:I:S:A:H:akn:t:", opts, &option_index);
|
2016-03-19 12:39:33 +00:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
gsmtap_host = optarg;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
skip_atr = 1;
|
|
|
|
break;
|
2020-10-27 09:16:41 +00:00
|
|
|
case 't':
|
|
|
|
atr = optarg;
|
|
|
|
break;
|
2016-03-19 12:39:33 +00:00
|
|
|
case 'k':
|
|
|
|
keep_running = 1;
|
|
|
|
break;
|
2020-01-29 13:44:32 +00:00
|
|
|
case 'n':
|
|
|
|
reader_num = atoi(optarg);
|
|
|
|
break;
|
2017-03-06 19:58:03 +00:00
|
|
|
case 'V':
|
|
|
|
vendor_id = strtol(optarg, NULL, 16);
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
product_id = strtol(optarg, NULL, 16);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
config_id = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
if_num = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
altsetting = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
addr = atoi(optarg);
|
|
|
|
break;
|
2017-05-10 23:31:45 +00:00
|
|
|
case 'H':
|
|
|
|
path = optarg;
|
|
|
|
break;
|
2016-03-19 12:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 19:15:20 +00:00
|
|
|
if (atr) {
|
|
|
|
override_atr_len = osmo_hexparse(atr, override_atr, sizeof(override_atr));
|
|
|
|
if (override_atr_len < 2) {
|
|
|
|
fprintf(stderr, "Invalid ATR - please omit a leading 0x and only use valid hex "
|
|
|
|
"digits and whitespace. ATRs need to be between 2 and 33 bytes long.\n");
|
|
|
|
goto do_exit;
|
|
|
|
}
|
2020-10-27 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 14:40:39 +00:00
|
|
|
if (vendor_id < 0 || product_id < 0) {
|
2017-03-06 19:58:03 +00:00
|
|
|
fprintf(stderr, "You have to specify the vendor and product ID\n");
|
|
|
|
goto do_exit;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:51:39 +00:00
|
|
|
ci->card_prof = &osim_uicc_sim_cic_profile;
|
2016-03-19 12:39:33 +00:00
|
|
|
|
2019-11-24 21:27:10 +00:00
|
|
|
rc = osmo_st2_gsmtap_init(gsmtap_host);
|
|
|
|
if (rc < 0) {
|
2016-03-19 12:39:33 +00:00
|
|
|
perror("unable to open GSMTAP");
|
|
|
|
goto close_exit;
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:44:32 +00:00
|
|
|
reader = osim_reader_open(OSIM_READER_DRV_PCSC, reader_num, "", NULL);
|
2016-03-19 12:39:33 +00:00
|
|
|
if (!reader) {
|
|
|
|
perror("unable to open PC/SC reader");
|
|
|
|
goto close_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
card = osim_card_open(reader, OSIM_PROTO_T0);
|
|
|
|
if (!card) {
|
|
|
|
perror("unable to open SIM card");
|
|
|
|
goto close_exit;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:51:39 +00:00
|
|
|
ci->chan = llist_entry(card->channels.next, struct osim_chan_hdl, list);
|
|
|
|
if (!ci->chan) {
|
2016-03-19 12:39:33 +00:00
|
|
|
perror("SIM card has no channel?!?");
|
|
|
|
goto close_exit;
|
|
|
|
}
|
|
|
|
|
2016-03-20 14:30:46 +00:00
|
|
|
signal(SIGINT, &signal_handler);
|
|
|
|
|
2016-03-19 12:39:33 +00:00
|
|
|
do {
|
2020-10-27 14:40:39 +00:00
|
|
|
struct usb_interface_match _ifm, *ifm = &_ifm;
|
2022-01-25 15:40:27 +00:00
|
|
|
memset(ifm, 0, sizeof(*ifm));
|
2020-10-27 14:40:39 +00:00
|
|
|
ifm->vendor = vendor_id;
|
|
|
|
ifm->product = product_id;
|
|
|
|
ifm->configuration = config_id;
|
|
|
|
ifm->interface = if_num;
|
|
|
|
ifm->altsetting = altsetting;
|
|
|
|
ifm->addr = addr;
|
|
|
|
if (path)
|
|
|
|
osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
|
2021-04-05 18:30:21 +00:00
|
|
|
transp->udp_fd = -1;
|
|
|
|
transp->usb_async = true;
|
2020-10-27 14:40:39 +00:00
|
|
|
transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
|
|
|
|
if (!transp->usb_devh) {
|
2022-01-25 15:09:14 +00:00
|
|
|
fprintf(stderr, "can't open USB device: %s\n", strerror(errno));
|
2021-04-25 19:29:22 +00:00
|
|
|
goto close;
|
2020-10-27 14:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = libusb_claim_interface(transp->usb_devh, if_num);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
|
2021-04-25 19:29:22 +00:00
|
|
|
goto close;
|
2020-10-27 14:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
|
|
|
|
&transp->usb_ep.in, &transp->usb_ep.irq_in);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
|
2021-04-25 19:29:22 +00:00
|
|
|
goto close;
|
2016-03-19 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 18:30:21 +00:00
|
|
|
allocate_and_submit_irq(ci);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
allocate_and_submit_in(ci);
|
|
|
|
|
|
|
|
/* request firmware to generate STATUS on IRQ endpoint */
|
|
|
|
osmo_st2_cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
|
|
|
|
|
2017-05-10 23:12:50 +00:00
|
|
|
/* simulate card-insert to modem (owhw, not qmod) */
|
2019-11-24 21:46:51 +00:00
|
|
|
osmo_st2_cardem_request_card_insert(ci, true);
|
2017-05-10 23:12:50 +00:00
|
|
|
|
|
|
|
/* select remote (forwarded) SIM */
|
2019-11-24 21:46:51 +00:00
|
|
|
osmo_st2_modem_sim_select_remote(ci->slot);
|
2017-05-10 23:12:50 +00:00
|
|
|
|
2019-11-24 22:10:36 +00:00
|
|
|
if (!skip_atr) {
|
|
|
|
/* set the ATR */
|
2021-04-25 19:15:20 +00:00
|
|
|
if (override_atr_len) {
|
|
|
|
/* user has specified an override-ATR */
|
|
|
|
atr_update_csum(override_atr, override_atr_len);
|
|
|
|
osmo_st2_cardem_request_set_atr(ci, override_atr, override_atr_len);
|
|
|
|
} else {
|
|
|
|
/* use the real ATR of the card */
|
|
|
|
osmo_st2_cardem_request_set_atr(ci, card->atr, card->atr_len);
|
|
|
|
}
|
2019-11-24 22:10:36 +00:00
|
|
|
}
|
2016-03-19 12:39:33 +00:00
|
|
|
|
2017-05-10 23:12:50 +00:00
|
|
|
/* select remote (forwarded) SIM */
|
2019-11-24 21:46:51 +00:00
|
|
|
osmo_st2_modem_reset_pulse(ci->slot, 300);
|
2017-05-10 23:12:50 +00:00
|
|
|
|
2017-03-06 17:51:39 +00:00
|
|
|
run_mainloop(ci);
|
2016-03-19 12:39:33 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2020-10-27 14:40:39 +00:00
|
|
|
libusb_release_interface(transp->usb_devh, 0);
|
2021-04-25 19:29:22 +00:00
|
|
|
|
|
|
|
close:
|
|
|
|
if (transp->usb_devh) {
|
2017-05-09 13:11:53 +00:00
|
|
|
libusb_close(transp->usb_devh);
|
2021-04-25 19:29:22 +00:00
|
|
|
transp->usb_devh = NULL;
|
|
|
|
}
|
2016-03-19 12:39:33 +00:00
|
|
|
if (keep_running)
|
|
|
|
sleep(1);
|
|
|
|
} while (keep_running);
|
|
|
|
|
2021-04-25 19:29:22 +00:00
|
|
|
close_exit:
|
|
|
|
if (transp->usb_devh)
|
|
|
|
libusb_close(transp->usb_devh);
|
|
|
|
|
2021-10-06 19:02:20 +00:00
|
|
|
osmo_libusb_exit(NULL);
|
2016-03-19 12:39:33 +00:00
|
|
|
do_exit:
|
|
|
|
return ret;
|
|
|
|
}
|