blob: 5ce034d07f5990f64a4a55d1dbbb377d7ccbfe21 [file] [log] [blame]
Harald Welte0e968cc2020-02-22 18:16:16 +01001/* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdio.h>
24#include <errno.h>
25
26#include <libusb.h>
27
28#include <osmocom/core/fsm.h>
29#include <osmocom/core/utils.h>
30
31#include <osmocom/usb/libusb.h>
32
33#include <osmocom/simtrace2/apdu_dispatch.h>
34#include <osmocom/simtrace2/simtrace2_api.h>
35#include <osmocom/simtrace2/simtrace_prot.h>
36
37#include "client.h"
38#include "debug.h"
39
Harald Welteb97e2fb2020-03-03 21:01:11 +010040#define LOGCI(ci, lvl, fmt, args ...) \
41 LOGP(DST2, lvl, fmt, ## args)
Harald Welte0e968cc2020-02-22 18:16:16 +010042
43/***********************************************************************
44 * Incoming Messages from cardem firmware
45 ***********************************************************************/
46
47/*! \brief Process a STATUS message from the SIMtrace2 */
48static int process_do_status(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
49{
50 struct cardemu_usb_msg_status *status;
51 status = (struct cardemu_usb_msg_status *) buf;
52
Harald Welteb97e2fb2020-03-03 21:01:11 +010053 LOGCI(ci, LOGL_INFO, "SIMtrace => STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
54 status->flags, status->fi, status->di, status->wi, status->waiting_time);
Harald Welte0e968cc2020-02-22 18:16:16 +010055
56 return 0;
57}
58
59/*! \brief Process a PTS indication message from the SIMtrace2 */
60static int process_do_pts(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
61{
62 struct cardemu_usb_msg_pts_info *pts = (struct cardemu_usb_msg_pts_info *) buf;
63 struct bankd_client *bc = ci->priv;
64 struct frontend_pts fpts = {
65 .buf = pts->req,
66 .len = sizeof(pts->req),
67 };
68
Harald Welteb97e2fb2020-03-03 21:01:11 +010069 LOGCI(ci, LOGL_INFO, "SIMtrace => PTS req: %s\n", osmo_hexdump(pts->req, sizeof(pts->req)));
Harald Welte0e968cc2020-02-22 18:16:16 +010070
71 osmo_fsm_inst_dispatch(bc->main_fi, MF_E_MDM_PTS_IND, &fpts);
72
73 return 0;
74}
75
76/*! \brief Process a ERROR indication message from the SIMtrace2 */
77__attribute__((unused)) static int process_do_error(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
78{
79 struct cardemu_usb_msg_error *err;
80 err = (struct cardemu_usb_msg_error *) buf;
81
Harald Welteb97e2fb2020-03-03 21:01:11 +010082 LOGCI(ci, LOGL_ERROR, "SIMtrace => ERROR: %u/%u/%u: %s\n",
83 err->severity, err->subsystem, err->code, err->msg_len ? (char *)err->msg : "");
Harald Welte0e968cc2020-02-22 18:16:16 +010084
85 return 0;
86}
87
88static struct osmo_apdu_context ac; // this will hold the complete APDU (across calls)
89
90/*! \brief Process a RX-DATA indication message from the SIMtrace2 */
91static int process_do_rx_da(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
92{
93 struct cardemu_usb_msg_rx_data *data = (struct cardemu_usb_msg_rx_data *) buf;
94 struct bankd_client *bc = ci->priv;
95 struct frontend_tpdu ftpdu;
96 int rc;
97
Harald Welte06fff2a2020-05-25 18:14:49 +020098 LOGCI(ci, LOGL_DEBUG, "SIMtrace => DATA: flags=%x, %s\n", data->flags,
Harald Welte0e968cc2020-02-22 18:16:16 +010099 osmo_hexdump(data->data, data->data_len));
100
101 /* parse the APDU data in the USB message */
102 rc = osmo_apdu_segment_in(&ac, data->data, data->data_len,
103 data->flags & CEMU_DATA_F_TPDU_HDR);
104
105 if (rc & APDU_ACT_TX_CAPDU_TO_CARD) {
106 /* there is no pending data coming from the modem */
107 uint8_t apdu_command[sizeof(ac.hdr) + ac.lc.tot];
108 memcpy(apdu_command, &ac.hdr, sizeof(ac.hdr));
109 if (ac.lc.tot)
110 memcpy(apdu_command + sizeof(ac.hdr), ac.dc, ac.lc.tot);
111 /* send APDU to card */
112 ftpdu.buf = apdu_command;
113 ftpdu.len = sizeof(ac.hdr) + ac.lc.tot;
Harald Welte1eec53d2020-05-25 22:48:27 +0200114 osmo_fsm_inst_dispatch(bc->main_fi, MF_E_MDM_TPDU, &ftpdu);
Harald Welte0e968cc2020-02-22 18:16:16 +0100115 } else if (ac.lc.tot > ac.lc.cur) {
116 /* there is pending data from the modem: send procedure byte to get remaining data */
117 osmo_st2_cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur);
118 }
119 return 0;
120}
121
122#if 0
123 case SIMTRACE_CMD_DO_ERROR
124 rc = process_do_error(ci, buf, len);
125 break;
126#endif
127
128/*! \brief Process an incoming message from the SIMtrace2 */
129static int process_usb_msg(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int len)
130{
131 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
132 int rc;
133
Harald Welteb97e2fb2020-03-03 21:01:11 +0100134 LOGCI(ci, LOGL_DEBUG, "SIMtrace -> %s\n", osmo_hexdump(buf, len));
Harald Welte0e968cc2020-02-22 18:16:16 +0100135
136 buf += sizeof(*sh);
137
138 switch (sh->msg_type) {
139 case SIMTRACE_MSGT_BD_CEMU_STATUS:
140 rc = process_do_status(ci, buf, len);
141 break;
142 case SIMTRACE_MSGT_DO_CEMU_PTS:
143 rc = process_do_pts(ci, buf, len);
144 break;
145 case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
146 rc = process_do_rx_da(ci, buf, len);
147 break;
148 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
149 /* firmware confirms configuration change; ignore */
150 break;
151 default:
Harald Welteb97e2fb2020-03-03 21:01:11 +0100152 LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", sh->msg_type);
Harald Welte0e968cc2020-02-22 18:16:16 +0100153 rc = -1;
154 break;
155 }
156
157 return rc;
158}
159
160
161/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
162static int process_irq_status(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, int len)
163{
164 const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
165 struct bankd_client *bc = ci->priv;
166 struct frontend_phys_status pstatus = {
167 .flags = {
168 .reset_active = status->flags & CEMU_STATUS_F_RESET_ACTIVE,
169 .vcc_present = status->flags & CEMU_STATUS_F_VCC_PRESENT,
170 .clk_active = status->flags & CEMU_STATUS_F_CLK_ACTIVE,
171 .card_present = -1 /* FIXME: make this dependent on board */,
172 },
173 .voltage_mv = status->voltage_mv,
174 .fi = status->fi,
175 .di = status->di,
176 .wi = status->wi,
177 .waiting_time = status->waiting_time,
178 };
179
Harald Welteb97e2fb2020-03-03 21:01:11 +0100180 LOGCI(ci, LOGL_INFO, "SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
Harald Welte0e968cc2020-02-22 18:16:16 +0100181 status->flags, status->fi, status->di, status->wi,
182 status->waiting_time);
183
184 osmo_fsm_inst_dispatch(bc->main_fi, MF_E_MDM_STATUS_IND, &pstatus);
185 return 0;
186}
187
188static int process_usb_msg_irq(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, unsigned int len)
189{
190 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
191 int rc;
192
Harald Welteb97e2fb2020-03-03 21:01:11 +0100193 LOGCI(ci, LOGL_INFO, "SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
Harald Welte0e968cc2020-02-22 18:16:16 +0100194
195 buf += sizeof(*sh);
196
197 switch (sh->msg_type) {
198 case SIMTRACE_MSGT_BD_CEMU_STATUS:
199 rc = process_irq_status(ci, buf, len);
200 break;
201 default:
Harald Welteb97e2fb2020-03-03 21:01:11 +0100202 LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", sh->msg_type);
Harald Welte0e968cc2020-02-22 18:16:16 +0100203 rc = -1;
204 break;
205 }
206
207 return rc;
208}
209
210static void usb_in_xfer_cb(struct libusb_transfer *xfer)
211{
212 struct osmo_st2_cardem_inst *ci = xfer->user_data;
213 int rc;
214
215 switch (xfer->status) {
216 case LIBUSB_TRANSFER_COMPLETED:
217 /* hand the message up the stack */
218 process_usb_msg(ci, xfer->buffer, xfer->actual_length);
219 break;
220 case LIBUSB_TRANSFER_NO_DEVICE:
Harald Welteb97e2fb2020-03-03 21:01:11 +0100221 LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
Harald Welte0e968cc2020-02-22 18:16:16 +0100222 exit(1);
223 break;
224 default:
Harald Welteb97e2fb2020-03-03 21:01:11 +0100225 LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status);
Harald Welte0e968cc2020-02-22 18:16:16 +0100226 exit(1);
227 break;
228 }
229
230 /* re-submit the IN transfer */
231 rc = libusb_submit_transfer(xfer);
232 OSMO_ASSERT(rc == 0);
233}
234
235
236static void allocate_and_submit_in(struct osmo_st2_cardem_inst *ci)
237{
238 struct osmo_st2_transport *transp = ci->slot->transp;
239 struct libusb_transfer *xfer;
240 int rc;
241
242 xfer = libusb_alloc_transfer(0);
243 OSMO_ASSERT(xfer);
244 xfer->dev_handle = transp->usb_devh;
245 xfer->flags = 0;
246 xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
247 xfer->endpoint = transp->usb_ep.in;
248 xfer->timeout = 0;
249 xfer->user_data = ci;
250 xfer->length = 16*256;
251
252 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
253 OSMO_ASSERT(xfer->buffer);
254 xfer->callback = usb_in_xfer_cb;
255
256 /* submit the IN transfer */
257 rc = libusb_submit_transfer(xfer);
258 OSMO_ASSERT(rc == 0);
259}
260
261
262static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
263{
264 struct osmo_st2_cardem_inst *ci = xfer->user_data;
265 int rc;
266
267 switch (xfer->status) {
268 case LIBUSB_TRANSFER_COMPLETED:
269 process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
270 break;
271 case LIBUSB_TRANSFER_NO_DEVICE:
Harald Welteb97e2fb2020-03-03 21:01:11 +0100272 LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
Harald Welte0e968cc2020-02-22 18:16:16 +0100273 exit(1);
274 break;
275 default:
Harald Welteb97e2fb2020-03-03 21:01:11 +0100276 LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status);
Harald Welte0e968cc2020-02-22 18:16:16 +0100277 exit(1);
278 break;
279 }
280
281 /* re-submit the IN transfer */
282 rc = libusb_submit_transfer(xfer);
283 OSMO_ASSERT(rc == 0);
284}
285
286
287static void allocate_and_submit_irq(struct osmo_st2_cardem_inst *ci)
288{
289 struct osmo_st2_transport *transp = ci->slot->transp;
290 struct libusb_transfer *xfer;
291 int rc;
292
293 xfer = libusb_alloc_transfer(0);
294 OSMO_ASSERT(xfer);
295 xfer->dev_handle = transp->usb_devh;
296 xfer->flags = 0;
297 xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
298 xfer->endpoint = transp->usb_ep.irq_in;
299 xfer->timeout = 0;
300 xfer->user_data = ci;
301 xfer->length = 64;
302
303 xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
304 OSMO_ASSERT(xfer->buffer);
305 xfer->callback = usb_irq_xfer_cb;
306
307 /* submit the IN transfer */
308 rc = libusb_submit_transfer(xfer);
309 OSMO_ASSERT(rc == 0);
310}
311
312
313
314
315/***********************************************************************
316 * simtrace2 frontend code to remsim-client
317 ***********************************************************************/
318
319int frontend_request_card_insert(struct bankd_client *bc)
320{
321 struct osmo_st2_cardem_inst *ci = bc->cardem;
322 return osmo_st2_cardem_request_card_insert(ci, true);
323}
324
325int frontend_request_sim_remote(struct bankd_client *bc)
326{
327 struct osmo_st2_cardem_inst *ci = bc->cardem;
328 return osmo_st2_modem_sim_select_remote(ci->slot);
329}
330
331int frontend_request_modem_reset(struct bankd_client *bc)
332{
333 struct osmo_st2_cardem_inst *ci = bc->cardem;
334 return osmo_st2_modem_reset_pulse(ci->slot, 300);
335}
336
337int frontend_handle_card2modem(struct bankd_client *bc, const uint8_t *data, size_t len)
338{
339 struct osmo_st2_cardem_inst *ci = bc->cardem;
340 // save SW to our current APDU context
341 ac.sw[0] = data[len-2];
342 ac.sw[1] = data[len=1];
343
Harald Welteb97e2fb2020-03-03 21:01:11 +0100344 LOGCI(ci, LOGL_DEBUG, "SIMtrace <= SW=0x%02x%02x, len_rx=%zu\n", ac.sw[0], ac.sw[1], len-2);
Harald Welte0e968cc2020-02-22 18:16:16 +0100345 if (len > 2) { // send PB and data to modem
346 osmo_st2_cardem_request_pb_and_tx(ci, ac.hdr.ins, data, len-2);
347 }
348 osmo_st2_cardem_request_sw_tx(ci, ac.sw); // send SW to modem
349 return 0;
350}
351
352int frontend_handle_set_atr(struct bankd_client *bc, const uint8_t *data, size_t len)
353{
354 struct osmo_st2_cardem_inst *ci = bc->cardem;
355 return osmo_st2_cardem_request_set_atr(ci, data, len);
356}
357
358int frontend_handle_slot_status(struct bankd_client *bc, const SlotPhysStatus_t *sts)
359{
360 /* we currently don't propagate bankd status to cardem */
361 return 0;
362}
363
Harald Weltee580c932020-05-24 16:03:56 +0200364int frontend_append_script_env(struct bankd_client *bc, char **env, int i, size_t max_env)
Harald Welte0e968cc2020-02-22 18:16:16 +0100365{
366 struct osmo_st2_cardem_inst *ci = bc->cardem;
Harald Welte0e968cc2020-02-22 18:16:16 +0100367
368 if (max_env < 4)
369 return -ENOSPC;
370
371 env[i++] = talloc_asprintf(env, "REMSIM_USB_PATH=%s", ci->usb_path);
372 /* TODO: Configuration; Altsetting */
373 env[i++] = talloc_asprintf(env, "REMSIM_USB_INTERFACE=%u", bc->cfg->usb.if_num);
374
375 return i;
376}
377
378/* FIXME: This must be cleaned up */
379static struct osmo_st2_transport _transp;
380static struct osmo_st2_slot _slot = {
381 .transp = &_transp,
382 .slot_nr = 0,
383};
384
385int client_user_main(struct bankd_client *bc)
386{
387 struct usb_interface_match _ifm, *ifm = &_ifm;
388 struct osmo_st2_transport *transp;
389 struct osmo_st2_cardem_inst *ci;
390 struct client_config *cfg = bc->cfg;
391 int rc, i;
392
393 rc = osmo_libusb_init(NULL);
394 if (rc < 0) {
395 fprintf(stderr, "libusb initialization failed\n");
396 return rc;
397 }
398
399 ci = talloc_zero(bc, struct osmo_st2_cardem_inst);
400 OSMO_ASSERT(ci);
401 ci->slot = &_slot;
402 transp = ci->slot->transp;
403 ci->priv = bc;
404 bc->cardem = ci;
405
406 ifm->vendor = cfg->usb.vendor_id;
407 ifm->product = cfg->usb.product_id;
408 ifm->configuration = cfg->usb.config_id;
409 ifm->interface = cfg->usb.if_num;
410 ifm->altsetting = cfg->usb.altsetting;
411 ifm->addr = cfg->usb.addr;
412 if (cfg->usb.path)
413 osmo_strlcpy(ifm->path, cfg->usb.path, sizeof(ifm->path));
Harald Weltee0438262020-05-25 18:15:15 +0200414 transp->udp_fd = -1;
Harald Weltedd337252020-05-25 22:38:24 +0200415 transp->usb_async = true;
Harald Welte0e968cc2020-02-22 18:16:16 +0100416 transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
417 if (!transp->usb_devh) {
418 fprintf(stderr, "can't open USB device\n");
419 return -1;
420 }
421
422 /* (re)determine the USB path of the opened device */
423 talloc_free(ci->usb_path);
424 ci->usb_path = osmo_libusb_dev_get_path_c(ci, libusb_get_device(transp->usb_devh));
425
426 rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
427 if (rc < 0) {
428 fprintf(stderr, "can't claim interface %d; rc=%d\n", cfg->usb.if_num, rc);
429 goto close_exit;
430 }
431
432 rc = osmo_libusb_get_ep_addrs(transp->usb_devh, cfg->usb.if_num, &transp->usb_ep.out,
433 &transp->usb_ep.in, &transp->usb_ep.irq_in);
434 if (rc < 0) {
435 fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
436 goto close_exit;
437 }
438
439 allocate_and_submit_irq(ci);
440 /* submit multiple IN URB in order to work around OS#4409 */
441 for (i = 0; i < 4; i++)
442 allocate_and_submit_in(ci);
443
444 /* request firmware to generate STATUS on IRQ endpoint */
445 osmo_st2_cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
446
447 while (1) {
448 osmo_select_main(0);
449 }
450
451 return 0;
452
453close_exit:
454 if (transp->usb_devh)
455 libusb_close(transp->usb_devh);
456 osmo_libusb_exit(NULL);
457
458 return -1;
459}