blob: 747fef383be671c186c94c69f418786895d45e84 [file] [log] [blame]
Harald Welte727d6752019-09-30 21:46:44 +02001/* Code providing a ccid_slot_ops implementation based on iso7716_fsm,
2 * (which in turn sits on top of card_uart) */
3
4#include <unistd.h>
5#include <errno.h>
Harald Welte6def1cf2019-10-10 15:40:02 +02006#include <string.h>
Harald Welte727d6752019-09-30 21:46:44 +02007
8#include <osmocom/core/msgb.h>
9#include <osmocom/core/timer.h>
10#include <osmocom/core/logging.h>
11#include <osmocom/core/fsm.h>
12
13#include "ccid_device.h"
14#include "cuart.h"
15#include "iso7816_fsm.h"
Eric Wildad1edce2019-11-27 16:51:08 +010016#include "iso7816_3.h"
Harald Welte727d6752019-09-30 21:46:44 +020017
18struct iso_fsm_slot {
19 /* CCID slot above us */
20 struct ccid_slot *cs;
21 /* main ISO7816-3 FSM instance beneath us */
22 struct osmo_fsm_inst *fi;
23 /* UART beneath the ISO7816-3 FSM */
24 struct card_uart *cuart;
25 /* bSeq of the operation currently in progress */
26 uint8_t seq;
27};
28
29struct iso_fsm_slot_instance {
30 struct iso_fsm_slot slot[NR_SLOTS];
31};
32
33static struct iso_fsm_slot_instance g_si;
34
Harald Welte03d6ebb2019-09-28 23:19:31 +020035static struct iso_fsm_slot *ccid_slot2iso_fsm_slot(struct ccid_slot *cs)
Harald Welte727d6752019-09-30 21:46:44 +020036{
37 OSMO_ASSERT(cs->slot_nr < ARRAY_SIZE(g_si.slot));
38 return &g_si.slot[cs->slot_nr];
39}
40
Harald Welte03d6ebb2019-09-28 23:19:31 +020041struct card_uart *cuart4slot_nr(uint8_t slot_nr)
42{
43 OSMO_ASSERT(slot_nr < ARRAY_SIZE(g_si.slot));
44 return g_si.slot[slot_nr].cuart;
45}
46
Harald Welte727d6752019-09-30 21:46:44 +020047static const uint8_t sysmousim_sjs1_atr[] = {
48 0x3B, 0x9F, 0x96, 0x80, 0x1F, 0xC7, 0x80, 0x31,
49 0xA0, 0x73, 0xBE, 0x21, 0x13, 0x67, 0x43, 0x20,
50 0x07, 0x18, 0x00, 0x00, 0x01, 0xA5 };
51
52static const struct ccid_pars_decoded iso_fsm_def_pars = {
53 .fi = 372,
54 .di = 1,
55 .clock_stop = CCID_CLOCK_STOP_NOTALLOWED,
56 .inverse_convention = false,
57 .t0 = {
58 .guard_time_etu = 0,
59 .waiting_integer = 0,
60 },
61 /* FIXME: T=1 */
62};
63
64static void iso_fsm_slot_pre_proc_cb(struct ccid_slot *cs, struct msgb *msg)
65{
66 /* do nothing; real hardware would update the slot related state here */
67}
68
Eric Wild9e622dc2019-11-27 14:43:16 +010069static void iso_fsm_slot_icc_set_insertion_status(struct ccid_slot *cs, bool present) {
70 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
71
72 if(present == cs->icc_present)
73 return;
74
75 cs->icc_present = present;
76
77 if (!present) {
78 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_CARD_REMOVAL, NULL);
79 card_uart_ctrl(ss->cuart, CUART_CTL_RST, true);
80 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, false);
81 cs->icc_powered = false;
82 cs->cmd_busy = false;
83 }
84}
85
Harald Welte727d6752019-09-30 21:46:44 +020086static void iso_fsm_slot_icc_power_on_async(struct ccid_slot *cs, struct msgb *msg,
87 const struct ccid_pc_to_rdr_icc_power_on *ipo)
88{
89 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
90
91 ss->seq = ipo->hdr.bSeq;
92 LOGPCS(cs, LOGL_DEBUG, "scheduling power-up\n");
93
94 /* FIXME: do this via a FSM? */
95 card_uart_ctrl(ss->cuart, CUART_CTL_RST, true);
Harald Weltef54a6b22019-10-10 13:30:24 +020096 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_ACT_IND, NULL);
Harald Welte727d6752019-09-30 21:46:44 +020097 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, true);
98 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_POWER_UP_IND, NULL);
99 cs->icc_powered = true;
100 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true);
Eric Wilde84a5712019-11-28 17:30:30 +0100101#ifdef OCTSIMFWBUILD
Harald Welte03d6ebb2019-09-28 23:19:31 +0200102 delay_us(10000);
Eric Wilde84a5712019-11-28 17:30:30 +0100103#else
104 usleep(10000);
105#endif
Harald Welte03d6ebb2019-09-28 23:19:31 +0200106
Harald Welte727d6752019-09-30 21:46:44 +0200107 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_RESET_REL_IND, NULL);
Harald Welte03d6ebb2019-09-28 23:19:31 +0200108 card_uart_ctrl(ss->cuart, CUART_CTL_RST, false);
Harald Welte727d6752019-09-30 21:46:44 +0200109
110 msgb_free(msg);
111 /* continues in iso_fsm_clot_user_cb once ATR is received */
112}
113static void iso_fsm_clot_user_cb(struct osmo_fsm_inst *fi, int event, int cause, void *data)
114{
115 struct iso_fsm_slot *ss = iso7816_fsm_get_user_priv(fi);
116 struct ccid_slot *cs = ss->cs;
Eric Wild759a6462019-11-11 14:22:52 +0100117
118 switch (event) {
119 case ISO7816_E_ATR_DONE_IND:
120 case ISO7816_E_ATR_ERR_IND:
121 case ISO7816_E_TPDU_DONE_IND:
122 case ISO7816_E_TPDU_FAILED_IND:
123 case ISO7816_E_PPS_DONE_IND:
124 case ISO7816_E_PPS_FAILED_IND:
125 cs->event_data = data;
Eric Wilde84a5712019-11-28 17:30:30 +0100126#ifdef OCTSIMFWBUILD
Eric Wild759a6462019-11-11 14:22:52 +0100127 asm volatile("dmb st": : :"memory");
Eric Wilde84a5712019-11-28 17:30:30 +0100128#endif
Eric Wild759a6462019-11-11 14:22:52 +0100129 cs->event = event;
130 break;
131 default:
132 LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, cause=%d, data=%p) unhandled\n",
133 __func__, event, cause, data);
134 break;
135 }
136}
137
138static int iso_handle_fsm_events(struct ccid_slot *cs, bool enable){
139 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
Harald Welte727d6752019-09-30 21:46:44 +0200140 struct msgb *tpdu, *resp;
Eric Wild759a6462019-11-11 14:22:52 +0100141 volatile uint32_t event = cs->event;
142 volatile void * volatile data = cs->event_data;
143
144 if(!event)
145 return 0;
146 if(event && !data)
147 return 0;
Harald Welte727d6752019-09-30 21:46:44 +0200148
Harald Welte727d6752019-09-30 21:46:44 +0200149 switch (event) {
150 case ISO7816_E_ATR_DONE_IND:
151 tpdu = data;
Eric Wild759a6462019-11-11 14:22:52 +0100152 LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
Harald Welte22dd1ff2019-10-10 15:40:53 +0200153 msgb_hexdump(tpdu));
Harald Welte727d6752019-09-30 21:46:44 +0200154 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0,
155 msgb_data(tpdu), msgb_length(tpdu));
156 ccid_slot_send_unbusy(cs, resp);
Harald Weltebbb50092019-10-10 14:55:25 +0200157 /* Don't free "TPDU" here, as the ATR should survive */
Eric Wild759a6462019-11-11 14:22:52 +0100158 cs->event = 0;
159 break;
160 case ISO7816_E_ATR_ERR_IND:
161 tpdu = data;
162 LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
163 msgb_hexdump(tpdu));
164 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_FAILED, CCID_ERR_ICC_MUTE,
165 msgb_data(tpdu), msgb_length(tpdu));
166 ccid_slot_send_unbusy(cs, resp);
167 /* Don't free "TPDU" here, as the ATR should survive */
168 cs->event = 0;
169 break;
Harald Welte727d6752019-09-30 21:46:44 +0200170 break;
171 case ISO7816_E_TPDU_DONE_IND:
172 tpdu = data;
Eric Wild759a6462019-11-11 14:22:52 +0100173 LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
Harald Welte22dd1ff2019-10-10 15:40:53 +0200174 msgb_hexdump(tpdu));
Harald Welte727d6752019-09-30 21:46:44 +0200175 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0, msgb_l2(tpdu), msgb_l2len(tpdu));
176 ccid_slot_send_unbusy(cs, resp);
177 msgb_free(tpdu);
Eric Wild759a6462019-11-11 14:22:52 +0100178 cs->event = 0;
Harald Welte727d6752019-09-30 21:46:44 +0200179 break;
Eric Wild9e622dc2019-11-27 14:43:16 +0100180 case ISO7816_E_TPDU_FAILED_IND:
181 tpdu = data;
Eric Wild759a6462019-11-11 14:22:52 +0100182 LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
Eric Wild9e622dc2019-11-27 14:43:16 +0100183 msgb_hexdump(tpdu));
184 /* FIXME: other error causes than card removal?*/
185 resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_FAILED, CCID_ERR_ICC_MUTE, msgb_l2(tpdu), 0);
186 ccid_slot_send_unbusy(cs, resp);
187 msgb_free(tpdu);
Eric Wild759a6462019-11-11 14:22:52 +0100188 cs->event = 0;
Eric Wild9e622dc2019-11-27 14:43:16 +0100189 break;
Eric Wildad1edce2019-11-27 16:51:08 +0100190 case ISO7816_E_PPS_DONE_IND:
191 tpdu = data;
192 /* pps was successful, so we know these values are fine */
193 uint16_t F = iso7816_3_fi_table[cs->proposed_pars.fi];
194 uint8_t D = iso7816_3_di_table[cs->proposed_pars.di];
195 uint32_t fmax = iso7816_3_fmax_table[cs->proposed_pars.fi];
196
Eric Wilda0574572019-11-21 15:28:59 +0100197 /* 7816-3 5.2.3
198 * No information shall be exchanged when switching the
199 * frequency value. Two different times are recommended
200 * for switching the frequency value, either
201 * - after ATR while card is idle
202 * - after PPS while card is idle
203 */
Eric Wild587d4fb2019-11-27 18:59:43 +0100204 card_uart_ctrl(ss->cuart, CUART_CTL_SET_CLOCK_FREQ, fmax);
205 card_uart_ctrl(ss->cuart, CUART_CTL_SET_FD, F/D);
Eric Wildad1edce2019-11-27 16:51:08 +0100206 card_uart_ctrl(ss->cuart, CUART_CTL_WTIME, cs->proposed_pars.t0.waiting_integer);
207
208 cs->pars = cs->proposed_pars;
209 resp = ccid_gen_parameters_t0(cs, ss->seq, CCID_CMD_STATUS_OK, 0);
210
211 ccid_slot_send_unbusy(cs, resp);
212
213 /* this frees the pps req from the host, pps resp buffer stays with the pps fsm */
214 msgb_free(tpdu);
Eric Wild759a6462019-11-11 14:22:52 +0100215 cs->event = 0;
Eric Wildad1edce2019-11-27 16:51:08 +0100216 break;
217 case ISO7816_E_PPS_FAILED_IND:
218 tpdu = data;
219 /* failed fi/di */
220 resp = ccid_gen_parameters_t0(cs, ss->seq, CCID_CMD_STATUS_FAILED, 10);
221 ccid_slot_send_unbusy(cs, resp);
222 /* this frees the pps req from the host, pps resp buffer stays with the pps fsm */
223 msgb_free(tpdu);
Eric Wild759a6462019-11-11 14:22:52 +0100224 cs->event = 0;
225 break;
226 case 0:
Eric Wildad1edce2019-11-27 16:51:08 +0100227 break;
Harald Welte22dd1ff2019-10-10 15:40:53 +0200228 default:
Eric Wild759a6462019-11-11 14:22:52 +0100229 LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, data=%p) unhandled\n",
230 __func__, event, data);
Harald Welte22dd1ff2019-10-10 15:40:53 +0200231 break;
Harald Welte727d6752019-09-30 21:46:44 +0200232 }
233}
234
Eric Wild9e622dc2019-11-27 14:43:16 +0100235static int iso_fsm_slot_xfr_block_async(struct ccid_slot *cs, struct msgb *msg,
Harald Welte727d6752019-09-30 21:46:44 +0200236 const struct ccid_pc_to_rdr_xfr_block *xfb)
237{
238 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
Eric Wild759a6462019-11-11 14:22:52 +0100239
Harald Welte727d6752019-09-30 21:46:44 +0200240
Harald Welte727d6752019-09-30 21:46:44 +0200241 ss->seq = xfb->hdr.bSeq;
Harald Welte6def1cf2019-10-10 15:40:02 +0200242
243 /* must be '0' for TPDU level exchanges or for short APDU */
244 OSMO_ASSERT(xfb->wLevelParameter == 0x0000);
245 OSMO_ASSERT(msgb_length(msg) > xfb->hdr.dwLength);
246
Eric Wild759a6462019-11-11 14:22:52 +0100247 msgb_pull(msg, 10);
Harald Welte6def1cf2019-10-10 15:40:02 +0200248
Eric Wild759a6462019-11-11 14:22:52 +0100249 LOGPCS(cs, LOGL_DEBUG, "scheduling TPDU transfer: %s\n", msgb_hexdump(msg));
250 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_TPDU_CMD, msg);
Harald Welte727d6752019-09-30 21:46:44 +0200251 /* continues in iso_fsm_clot_user_cb once response/error/timeout is received */
Eric Wild9e622dc2019-11-27 14:43:16 +0100252 return 0;
Harald Welte727d6752019-09-30 21:46:44 +0200253}
254
255
256static void iso_fsm_slot_set_power(struct ccid_slot *cs, bool enable)
257{
258 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
259
260 if (enable) {
261 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, true);
Harald Welte03d6ebb2019-09-28 23:19:31 +0200262 cs->icc_powered = true;
Harald Welte727d6752019-09-30 21:46:44 +0200263 } else {
264 card_uart_ctrl(ss->cuart, CUART_CTL_POWER, false);
Harald Welte03d6ebb2019-09-28 23:19:31 +0200265 cs->icc_powered = false;
Harald Welte727d6752019-09-30 21:46:44 +0200266 }
267}
268
269static void iso_fsm_slot_set_clock(struct ccid_slot *cs, enum ccid_clock_command cmd)
270{
271 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
272
273 switch (cmd) {
274 case CCID_CLOCK_CMD_STOP:
275 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, false);
276 break;
277 case CCID_CLOCK_CMD_RESTART:
278 card_uart_ctrl(ss->cuart, CUART_CTL_CLOCK, true);
279 break;
280 default:
281 OSMO_ASSERT(0);
282 }
283}
284
Eric Wildad1edce2019-11-27 16:51:08 +0100285static int iso_fsm_slot_set_params(struct ccid_slot *cs, uint8_t seq, enum ccid_protocol_num proto,
Harald Welte727d6752019-09-30 21:46:44 +0200286 const struct ccid_pars_decoded *pars_dec)
287{
Eric Wildad1edce2019-11-27 16:51:08 +0100288 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
289 struct msgb *tpdu;
290
291 /* see 6.1.7 for error offsets */
292 if(proto != CCID_PROTOCOL_NUM_T0)
293 return -7;
294
295 if(pars_dec->t0.guard_time_etu != 0)
296 return -12;
297
298 if(pars_dec->clock_stop != CCID_CLOCK_STOP_NOTALLOWED)
299 return -14;
300
301 ss->seq = seq;
302
Eric Wild45e930d2019-11-21 14:38:16 +0100303 /* FIXME:
304 When using D=64, the interface device shall ensure a delay
305 of at least 16 etu between the leading edge of the last
306 received character and the leading edge of the character transmitted
307 for initiating a command.
308 -> we can't really do 4 stop bits?!
309 */
310
Eric Wildad1edce2019-11-27 16:51:08 +0100311 /* Hardware does not support SPU, so no PPS2, and PPS3 is reserved anyway */
312 tpdu = msgb_alloc(6, "PPSRQ");
313 OSMO_ASSERT(tpdu);
314 msgb_put_u8(tpdu, 0xff);
315 msgb_put_u8(tpdu, (1 << 4)); /* only PPS1, T=0 */
316 msgb_put_u8(tpdu, (pars_dec->fi << 4 | pars_dec->di));
317 msgb_put_u8(tpdu, 0xff ^ (1 << 4) ^ (pars_dec->fi << 4 | pars_dec->di));
318
319
320 LOGPCS(cs, LOGL_DEBUG, "scheduling PPS transfer: %s\n", msgb_hexdump(tpdu));
321 osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_PPS_CMD, tpdu);
322 /* continues in iso_fsm_clot_user_cb once response/error/timeout is received */
Harald Welte727d6752019-09-30 21:46:44 +0200323 return 0;
324}
325
Eric Wild56a50552020-01-28 16:07:23 +0100326static int iso_fsm_slot_set_rate_and_clock(struct ccid_slot *cs, uint32_t* freq_hz, uint32_t* rate_bps)
Harald Welte727d6752019-09-30 21:46:44 +0200327{
Eric Wild56a50552020-01-28 16:07:23 +0100328 /* we return the currently used values, since we support automatic features */
329 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
330
331 *rate_bps = card_uart_ctrl(ss->cuart, CUART_CTL_GET_BAUDRATE, false);
332 *freq_hz = card_uart_ctrl(ss->cuart, CUART_CTL_GET_CLOCK_FREQ, false)/1000;
333
Harald Welte727d6752019-09-30 21:46:44 +0200334 return 0;
335}
336
Harald Welte03d6ebb2019-09-28 23:19:31 +0200337extern void *g_tall_ctx;
Harald Welte727d6752019-09-30 21:46:44 +0200338static int iso_fsm_slot_init(struct ccid_slot *cs)
339{
Harald Welte03d6ebb2019-09-28 23:19:31 +0200340 void *ctx = g_tall_ctx; /* FIXME */
Harald Welte727d6752019-09-30 21:46:44 +0200341 struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
342 struct card_uart *cuart = talloc_zero(ctx, struct card_uart);
Eric Wilde84a5712019-11-28 17:30:30 +0100343 char id_buf[3+3+1];
344 char devname[2+1];
345 char *devnamep = 0;
346 char *drivername = "asf4";
Harald Welte727d6752019-09-30 21:46:44 +0200347 int rc;
348
349 LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
350
Eric Wilde84a5712019-11-28 17:30:30 +0100351 snprintf(id_buf, sizeof(id_buf), "SIM%d", cs->slot_nr);
352#ifdef OCTSIMFWBUILD
353 snprintf(devname, sizeof(devname), "%d", cs->slot_nr);
354 devnamep = devname;
355#else
356 if (cs->slot_nr == 0) {
357 cs->icc_present = true;
358 devnamep = "/dev/ttyUSB5";
359 }
360 drivername = "tty";
361#endif
Harald Welte727d6752019-09-30 21:46:44 +0200362
363 if (!cuart)
364 return -ENOMEM;
365
Eric Wilde84a5712019-11-28 17:30:30 +0100366 if (devnamep) {
367 rc = card_uart_open(cuart, drivername, devnamep);
Harald Welte515d5b22019-10-10 13:46:13 +0200368 if (rc < 0) {
369 LOGPCS(cs, LOGL_ERROR, "Cannot open UART %s: %d\n", devname, rc);
370 talloc_free(cuart);
371 return rc;
372 }
Harald Welte727d6752019-09-30 21:46:44 +0200373 }
Eric Wilde84a5712019-11-28 17:30:30 +0100374
Harald Welte727d6752019-09-30 21:46:44 +0200375 ss->fi = iso7816_fsm_alloc(ctx, LOGL_DEBUG, id_buf, cuart, iso_fsm_clot_user_cb, ss);
376 if (!ss->fi) {
Harald Welte515d5b22019-10-10 13:46:13 +0200377 LOGPCS(cs, LOGL_ERROR, "Cannot allocate ISO FSM\n");
Harald Welte727d6752019-09-30 21:46:44 +0200378 talloc_free(cuart);
379 return -1;
380 }
381
382 cs->default_pars = &iso_fsm_def_pars;
383 ss->cuart = cuart;
384 ss->cs = cs;
385
386
387 return 0;
388}
389
390const struct ccid_slot_ops iso_fsm_slot_ops = {
391 .init = iso_fsm_slot_init,
392 .pre_proc_cb = iso_fsm_slot_pre_proc_cb,
393 .icc_power_on_async = iso_fsm_slot_icc_power_on_async,
Eric Wild9e622dc2019-11-27 14:43:16 +0100394 .icc_set_insertion_status = iso_fsm_slot_icc_set_insertion_status,
Harald Welte727d6752019-09-30 21:46:44 +0200395 .xfr_block_async = iso_fsm_slot_xfr_block_async,
396 .set_power = iso_fsm_slot_set_power,
397 .set_clock = iso_fsm_slot_set_clock,
398 .set_params = iso_fsm_slot_set_params,
399 .set_rate_and_clock = iso_fsm_slot_set_rate_and_clock,
Eric Wild759a6462019-11-11 14:22:52 +0100400 .handle_fsm_events = iso_handle_fsm_events,
Harald Welte727d6752019-09-30 21:46:44 +0200401};