Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* ISO7816-3 state machine for the card side |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 2 | * |
Harald Welte | e92cb50 | 2019-12-14 21:48:45 +0100 | [diff] [blame] | 3 | * (C) 2010-2019 by Harald Welte <laforge@gnumonks.org> |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 4 | * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 5 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 10 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 15 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 19 | */ |
Harald Welte | c430ac1 | 2017-02-28 01:25:12 +0100 | [diff] [blame] | 20 | #include <stdio.h> |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 21 | #include <assert.h> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <string.h> |
| 24 | #include <stdint.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include "utils.h" |
| 28 | #include "trace.h" |
| 29 | #include "iso7816_fidi.h" |
| 30 | #include "tc_etu.h" |
| 31 | #include "card_emu.h" |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 32 | #include "simtrace_prot.h" |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 33 | #include "usb_buf.h" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 34 | #include <osmocom/core/linuxlist.h> |
| 35 | #include <osmocom/core/msgb.h> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 36 | |
| 37 | |
| 38 | #define NUM_SLOTS 2 |
| 39 | |
| 40 | #define ISO7816_3_INIT_WTIME 9600 |
| 41 | #define ISO7816_3_DEFAULT_WI 10 |
| 42 | #define ISO7816_3_ATR_LEN_MAX (1+32) /* TS plus 32 chars */ |
| 43 | |
| 44 | #define ISO7816_3_PB_NULL 0x60 |
| 45 | |
| 46 | enum iso7816_3_card_state { |
| 47 | ISO_S_WAIT_POWER, /* waiting for power being applied */ |
| 48 | ISO_S_WAIT_CLK, /* waiting for clock being applied */ |
| 49 | ISO_S_WAIT_RST, /* waiting for reset being released */ |
| 50 | ISO_S_WAIT_ATR, /* waiting for start of ATR */ |
| 51 | ISO_S_IN_ATR, /* transmitting ATR to reader */ |
| 52 | ISO_S_IN_PTS, /* transmitting ATR to reader */ |
| 53 | ISO_S_WAIT_TPDU, /* waiting for data from reader */ |
| 54 | ISO_S_IN_TPDU, /* inside a TPDU */ |
| 55 | }; |
| 56 | |
Kévin Redon | 8a4fba5 | 2018-08-02 17:44:16 +0200 | [diff] [blame] | 57 | const struct value_string iso7816_3_card_state_names[] = { |
Kévin Redon | 2fdcf3b | 2018-10-15 19:43:15 +0200 | [diff] [blame] | 58 | { |
| 59 | .value = ISO_S_WAIT_POWER, |
| 60 | .str = "WAIT_POWER", |
| 61 | }, |
| 62 | { |
| 63 | .value = ISO_S_WAIT_CLK, |
| 64 | .str = "WAIT_CLK", |
| 65 | }, |
| 66 | { |
| 67 | .value = ISO_S_WAIT_RST, |
| 68 | .str = "WAIT_RST", |
| 69 | }, |
| 70 | { |
| 71 | .value = ISO_S_WAIT_ATR, |
| 72 | .str = "WAIT_ATR", |
| 73 | }, |
| 74 | { |
| 75 | .value = ISO_S_IN_ATR, |
| 76 | .str = "IN_ATR", |
| 77 | }, |
| 78 | { |
| 79 | .value = ISO_S_IN_PTS, |
| 80 | .str = "IN_PTS", |
| 81 | }, |
| 82 | { |
| 83 | .value = ISO_S_WAIT_TPDU, |
| 84 | .str = "WAIT_TPDU", |
| 85 | }, |
| 86 | { |
| 87 | .value = ISO_S_IN_TPDU, |
| 88 | .str = "IN_TPDU", |
| 89 | }, |
Kévin Redon | 8a4fba5 | 2018-08-02 17:44:16 +0200 | [diff] [blame] | 90 | { |
| 91 | .value = 0, |
| 92 | .str = NULL, |
| 93 | }, |
| 94 | }; |
| 95 | |
| 96 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 97 | /* detailed sub-states of ISO_S_IN_PTS */ |
| 98 | enum pts_state { |
| 99 | PTS_S_WAIT_REQ_PTSS, |
| 100 | PTS_S_WAIT_REQ_PTS0, |
| 101 | PTS_S_WAIT_REQ_PTS1, |
| 102 | PTS_S_WAIT_REQ_PTS2, |
| 103 | PTS_S_WAIT_REQ_PTS3, |
| 104 | PTS_S_WAIT_REQ_PCK, |
| 105 | PTS_S_WAIT_RESP_PTSS = PTS_S_WAIT_REQ_PTSS | 0x10, |
| 106 | PTS_S_WAIT_RESP_PTS0 = PTS_S_WAIT_REQ_PTS0 | 0x10, |
| 107 | PTS_S_WAIT_RESP_PTS1 = PTS_S_WAIT_REQ_PTS1 | 0x10, |
| 108 | PTS_S_WAIT_RESP_PTS2 = PTS_S_WAIT_REQ_PTS2 | 0x10, |
| 109 | PTS_S_WAIT_RESP_PTS3 = PTS_S_WAIT_REQ_PTS3 | 0x10, |
| 110 | PTS_S_WAIT_RESP_PCK = PTS_S_WAIT_REQ_PCK | 0x10, |
| 111 | }; |
| 112 | |
Kévin Redon | 2fdcf3b | 2018-10-15 19:43:15 +0200 | [diff] [blame] | 113 | /* PTS field byte index */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 114 | #define _PTSS 0 |
| 115 | #define _PTS0 1 |
| 116 | #define _PTS1 2 |
| 117 | #define _PTS2 3 |
| 118 | #define _PTS3 4 |
| 119 | #define _PCK 5 |
| 120 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 121 | /* T-PDU state machine states */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 122 | enum tpdu_state { |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 123 | TPDU_S_WAIT_CLA, /* waiting for CLA byte from reader */ |
| 124 | TPDU_S_WAIT_INS, /* waiting for INS byte from reader */ |
| 125 | TPDU_S_WAIT_P1, /* waiting for P1 byte from reader */ |
| 126 | TPDU_S_WAIT_P2, /* waiting for P2 byte from reader */ |
| 127 | TPDU_S_WAIT_P3, /* waiting for P3 byte from reader */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 128 | TPDU_S_WAIT_PB, /* waiting for Tx of procedure byte */ |
Kévin Redon | 0f4abf5 | 2018-08-02 17:42:48 +0200 | [diff] [blame] | 129 | TPDU_S_WAIT_RX, /* waiting for more data from reader */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 130 | TPDU_S_WAIT_TX, /* waiting for more data to reader */ |
| 131 | }; |
| 132 | |
Kévin Redon | 2fdcf3b | 2018-10-15 19:43:15 +0200 | [diff] [blame] | 133 | const struct value_string tpdu_state_names[] = { |
| 134 | { |
| 135 | .value = TPDU_S_WAIT_CLA, |
| 136 | .str = "WAIT_CLA", |
| 137 | }, |
| 138 | { |
| 139 | .value = TPDU_S_WAIT_INS, |
| 140 | .str = "WAIT_INS", |
| 141 | }, |
| 142 | { |
| 143 | .value = TPDU_S_WAIT_P1, |
| 144 | .str = "WAIT_P1", |
| 145 | }, |
| 146 | { |
| 147 | .value = TPDU_S_WAIT_P2, |
| 148 | .str = "WAIT_P2", |
| 149 | }, |
| 150 | { |
| 151 | .value = TPDU_S_WAIT_P3, |
| 152 | .str = "WAIT_P3", |
| 153 | }, |
| 154 | { |
| 155 | .value = TPDU_S_WAIT_PB, |
| 156 | .str = "WAIT_PB", |
| 157 | }, |
| 158 | { |
| 159 | .value = TPDU_S_WAIT_RX, |
| 160 | .str = "WAIT_RX", |
| 161 | }, |
| 162 | { |
| 163 | .value = TPDU_S_WAIT_TX, |
| 164 | .str = "WAIT_TX", |
| 165 | }, |
| 166 | { |
| 167 | .value = 0, |
| 168 | .str = NULL, |
| 169 | }, |
| 170 | }; |
| 171 | |
| 172 | /* TPDU field byte index */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 173 | #define _CLA 0 |
| 174 | #define _INS 1 |
| 175 | #define _P1 2 |
| 176 | #define _P2 3 |
| 177 | #define _P3 4 |
| 178 | |
| 179 | struct card_handle { |
Harald Welte | c394109 | 2018-08-26 09:53:13 +0200 | [diff] [blame] | 180 | unsigned int num; |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 181 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 182 | enum iso7816_3_card_state state; |
| 183 | |
| 184 | /* signal levels */ |
Kévin Redon | 7233cf8 | 2019-11-14 19:37:32 +0100 | [diff] [blame] | 185 | bool vcc_active; /*< if VCC is active (true = active/ON) */ |
| 186 | bool in_reset; /*< if card is in reset (true = RST low/asserted, false = RST high/ released) */ |
| 187 | bool clocked; /*< if clock is active ( true = active, false = inactive) */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 188 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 189 | /* timing parameters, from PTS */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 190 | uint8_t fi; |
| 191 | uint8_t di; |
| 192 | uint8_t wi; |
| 193 | |
| 194 | uint8_t tc_chan; /* TC channel number */ |
| 195 | uint8_t uart_chan; /* UART channel */ |
| 196 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 197 | uint8_t in_ep; /* USB IN EP */ |
| 198 | uint8_t irq_ep; /* USB IN EP */ |
| 199 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 200 | uint32_t waiting_time; /* in clocks */ |
| 201 | |
| 202 | /* ATR state machine */ |
| 203 | struct { |
| 204 | uint8_t idx; |
| 205 | uint8_t len; |
| 206 | //uint8_t hist_len; |
| 207 | //uint8_t last_td; |
| 208 | uint8_t atr[ISO7816_3_ATR_LEN_MAX]; |
| 209 | } atr; |
| 210 | |
| 211 | /* PPS / PTS support */ |
| 212 | struct { |
| 213 | enum pts_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 214 | uint8_t req[6]; /* request bytes */ |
| 215 | uint8_t resp[6]; /* response bytes */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 216 | } pts; |
| 217 | |
| 218 | /* TPDU */ |
| 219 | struct { |
| 220 | enum tpdu_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 221 | uint8_t hdr[5]; /* CLA INS P1 P2 P3 */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 222 | } tpdu; |
| 223 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 224 | struct msgb *uart_rx_msg; /* UART RX -> USB TX */ |
| 225 | struct msgb *uart_tx_msg; /* USB RX -> UART TX */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 226 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 227 | struct llist_head uart_tx_queue; |
| 228 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 229 | struct { |
| 230 | uint32_t tx_bytes; |
| 231 | uint32_t rx_bytes; |
| 232 | uint32_t pps; |
| 233 | } stats; |
| 234 | }; |
| 235 | |
Harald Welte | e92cb50 | 2019-12-14 21:48:45 +0100 | [diff] [blame] | 236 | /* reset all the 'dynamic' state of the card handle to the initial/default values */ |
| 237 | static void card_handle_reset(struct card_handle *ch) |
| 238 | { |
Harald Welte | 81f4ef7 | 2019-12-14 22:20:00 +0100 | [diff] [blame] | 239 | struct msgb *msg; |
| 240 | |
| 241 | tc_etu_disable(ch->tc_chan); |
| 242 | |
Harald Welte | e92cb50 | 2019-12-14 21:48:45 +0100 | [diff] [blame] | 243 | ch->tpdu.state = TPDU_S_WAIT_CLA; |
Harald Welte | 81f4ef7 | 2019-12-14 22:20:00 +0100 | [diff] [blame] | 244 | |
| 245 | /* release any buffers we may still own */ |
| 246 | if (ch->uart_tx_msg) { |
| 247 | usb_buf_free(ch->uart_tx_msg); |
| 248 | ch->uart_tx_msg = NULL; |
| 249 | } |
| 250 | if (ch->uart_rx_msg) { |
| 251 | usb_buf_free(ch->uart_rx_msg); |
| 252 | ch->uart_rx_msg = NULL; |
| 253 | } |
| 254 | while ((msg = msgb_dequeue(&ch->uart_tx_queue))) { |
| 255 | usb_buf_free(msg); |
| 256 | } |
Harald Welte | e92cb50 | 2019-12-14 21:48:45 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 259 | struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch) |
| 260 | { |
| 261 | return &ch->uart_tx_queue; |
| 262 | } |
| 263 | |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 264 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts); |
Harald Welte | e7194ab | 2015-11-14 21:03:25 +0100 | [diff] [blame] | 265 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 266 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 267 | /* update simtrace header msg_len and submit USB buffer */ |
| 268 | void usb_buf_upd_len_and_submit(struct msgb *msg) |
| 269 | { |
Harald Welte | b91f6ad | 2017-05-10 22:51:30 +0200 | [diff] [blame] | 270 | struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 271 | |
| 272 | sh->msg_len = msgb_length(msg); |
| 273 | |
| 274 | usb_buf_submit(msg); |
| 275 | } |
| 276 | |
| 277 | /* Allocate USB buffer and push + initialize simtrace_msg_hdr */ |
| 278 | struct msgb *usb_buf_alloc_st(uint8_t ep, uint8_t msg_class, uint8_t msg_type) |
| 279 | { |
Kévin Redon | c90de69 | 2019-11-18 22:32:58 +0100 | [diff] [blame] | 280 | struct msgb *msg = NULL; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 281 | struct simtrace_msg_hdr *sh; |
| 282 | |
Kévin Redon | c90de69 | 2019-11-18 22:32:58 +0100 | [diff] [blame] | 283 | while (!msg) { |
| 284 | msg = usb_buf_alloc(ep); // try to allocate some memory |
| 285 | if (!msg) { // allocation failed, we might be out of memory |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 286 | struct usb_buffered_ep *bep = usb_get_buf_ep(ep); |
| 287 | if (!bep) { |
Kévin Redon | c90de69 | 2019-11-18 22:32:58 +0100 | [diff] [blame] | 288 | TRACE_ERROR("ep %u: %s queue does not exist\n\r", |
| 289 | ep, __func__); |
| 290 | return NULL; |
| 291 | } |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 292 | if (llist_empty(&bep->queue)) { |
Kévin Redon | c90de69 | 2019-11-18 22:32:58 +0100 | [diff] [blame] | 293 | TRACE_ERROR("ep %u: %s EOMEM (queue already empty)\n\r", |
| 294 | ep, __func__); |
| 295 | return NULL; |
| 296 | } |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 297 | msg = msgb_dequeue_count(&bep->queue, &bep->queue_len); |
Kévin Redon | c90de69 | 2019-11-18 22:32:58 +0100 | [diff] [blame] | 298 | if (!msg) { |
| 299 | TRACE_ERROR("ep %u: %s no msg in non-empty queue\n\r", |
| 300 | ep, __func__); |
| 301 | return NULL; |
| 302 | } |
| 303 | usb_buf_free(msg); |
| 304 | msg = NULL; |
| 305 | TRACE_DEBUG("ep %u: %s queue msg dropped\n\r", |
| 306 | ep, __func__); |
| 307 | } |
| 308 | } |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 309 | |
| 310 | msg->l1h = msgb_put(msg, sizeof(*sh)); |
| 311 | sh = (struct simtrace_msg_hdr *) msg->l1h; |
| 312 | memset(sh, 0, sizeof(*sh)); |
| 313 | sh->msg_class = msg_class; |
| 314 | sh->msg_type = msg_type; |
| 315 | msg->l2h = msg->l1h + sizeof(*sh); |
| 316 | |
| 317 | return msg; |
| 318 | } |
| 319 | |
Kévin Redon | 680bdab | 2018-07-18 14:00:57 +0200 | [diff] [blame] | 320 | /* Update cardemu_usb_msg_rx_data length + submit buffer */ |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 321 | static void flush_rx_buffer(struct card_handle *ch) |
| 322 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 323 | struct msgb *msg; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 324 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 325 | uint32_t data_len; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 326 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 327 | msg = ch->uart_rx_msg; |
| 328 | if (!msg) |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 329 | return; |
| 330 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 331 | ch->uart_rx_msg = NULL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 332 | |
Kévin Redon | 680bdab | 2018-07-18 14:00:57 +0200 | [diff] [blame] | 333 | /* store length of data payload field in header */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 334 | rd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
| 335 | rd->data_len = msgb_l2len(msg) - sizeof(*rd); |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 336 | |
Kévin Redon | 910e683 | 2018-08-02 17:47:57 +0200 | [diff] [blame] | 337 | TRACE_INFO("%u: %s (%u)\n\r", |
| 338 | ch->num, __func__, rd->data_len); |
| 339 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 340 | usb_buf_upd_len_and_submit(msg); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Kévin Redon | 680bdab | 2018-07-18 14:00:57 +0200 | [diff] [blame] | 343 | /* convert a non-contiguous PTS request/response into a contiguous |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 344 | * buffer, returning the number of bytes used in the buffer */ |
| 345 | static int serialize_pts(uint8_t *out, const uint8_t *in) |
| 346 | { |
| 347 | int i = 0; |
| 348 | |
| 349 | out[i++] = in[_PTSS]; |
| 350 | out[i++] = in[_PTS0]; |
| 351 | if (in[_PTS0] & (1 << 4)) |
| 352 | out[i++] = in[_PTS1]; |
| 353 | if (in[_PTS0] & (1 << 5)) |
| 354 | out[i++] = in[_PTS2]; |
| 355 | if (in[_PTS0] & (1 << 6)) |
| 356 | out[i++] = in[_PTS3]; |
| 357 | out[i++] = in[_PCK]; |
| 358 | |
| 359 | return i; |
| 360 | } |
| 361 | |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 362 | static uint8_t csum_pts(const uint8_t *in) |
| 363 | { |
| 364 | uint8_t out[6]; |
| 365 | int len = serialize_pts(out, in); |
| 366 | uint8_t csum = 0; |
| 367 | int i; |
| 368 | |
| 369 | /* we don't include the PCK byte in the checksumming process */ |
| 370 | len -= 1; |
| 371 | |
| 372 | for (i = 0; i < len; i++) |
| 373 | csum = csum ^ out[i]; |
| 374 | |
| 375 | return csum; |
| 376 | } |
| 377 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 378 | static void flush_pts(struct card_handle *ch) |
| 379 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 380 | struct msgb *msg; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 381 | struct cardemu_usb_msg_pts_info *ptsi; |
| 382 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 383 | msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, SIMTRACE_MSGT_DO_CEMU_PTS); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 384 | if (!msg) |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 385 | return; |
| 386 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 387 | ptsi = (struct cardemu_usb_msg_pts_info *) msgb_put(msg, sizeof(*ptsi)); |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 388 | ptsi->pts_len = serialize_pts(ptsi->req, ch->pts.req); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 389 | serialize_pts(ptsi->resp, ch->pts.resp); |
| 390 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 391 | usb_buf_upd_len_and_submit(msg); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 394 | static void emu_update_fidi(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 395 | { |
| 396 | int rc; |
| 397 | |
| 398 | rc = compute_fidi_ratio(ch->fi, ch->di); |
| 399 | if (rc > 0 && rc < 0x400) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 400 | TRACE_INFO("%u: computed Fi(%u) Di(%u) ratio: %d\r\n", |
| 401 | ch->num, ch->fi, ch->di, rc); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 402 | /* make sure UART uses new F/D ratio */ |
| 403 | card_emu_uart_update_fidi(ch->uart_chan, rc); |
| 404 | /* notify ETU timer about this */ |
| 405 | tc_etu_set_etu(ch->tc_chan, rc); |
| 406 | } else |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 407 | TRACE_INFO("%u: computed FiDi ration %d unsupported\r\n", |
| 408 | ch->num, rc); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Update the ISO 7816-3 TPDU receiver state */ |
| 412 | static void card_set_state(struct card_handle *ch, |
| 413 | enum iso7816_3_card_state new_state) |
| 414 | { |
Harald Welte | 903d63a | 2016-03-20 13:38:39 +0100 | [diff] [blame] | 415 | if (ch->state == new_state) |
| 416 | return; |
| 417 | |
Kévin Redon | 2fdcf3b | 2018-10-15 19:43:15 +0200 | [diff] [blame] | 418 | TRACE_DEBUG("%u: 7816 card state %s -> %s\r\n", ch->num, |
| 419 | get_value_string(iso7816_3_card_state_names, ch->state), |
| 420 | get_value_string(iso7816_3_card_state_names, new_state)); |
Harald Welte | 903d63a | 2016-03-20 13:38:39 +0100 | [diff] [blame] | 421 | ch->state = new_state; |
| 422 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 423 | switch (new_state) { |
| 424 | case ISO_S_WAIT_POWER: |
| 425 | case ISO_S_WAIT_CLK: |
| 426 | case ISO_S_WAIT_RST: |
| 427 | /* disable Rx and Tx of UART */ |
| 428 | card_emu_uart_enable(ch->uart_chan, 0); |
Kévin Redon | ac7e73a | 2019-11-14 20:11:42 +0100 | [diff] [blame] | 429 | /* check end activation state (only necessary if the reader to not respect the activation sequence) */ |
| 430 | if (ch->vcc_active && ch->clocked && !ch->in_reset) { |
| 431 | /* enable the TC/ETU counter once reset has been released */ |
| 432 | tc_etu_enable(ch->tc_chan); |
| 433 | /* prepare to send the ATR */ |
| 434 | card_set_state(ch, ISO_S_WAIT_ATR); |
| 435 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 436 | break; |
| 437 | case ISO_S_WAIT_ATR: |
| 438 | /* Reset to initial Fi / Di ratio */ |
| 439 | ch->fi = 1; |
| 440 | ch->di = 1; |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 441 | emu_update_fidi(ch); |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 442 | /* the ATR should only be sent 400 to 40k clock cycles after the RESET. |
| 443 | * we use the tc_etu mechanism to wait this time. |
| 444 | * since the initial ETU is Fd=372/Dd=1 clock cycles long, we have to wait 2-107 ETU. |
| 445 | */ |
| 446 | tc_etu_set_wtime(ch->tc_chan, 2); |
| 447 | /* ensure the TC_ETU timer is enabled */ |
| 448 | tc_etu_enable(ch->tc_chan); |
| 449 | break; |
| 450 | case ISO_S_IN_ATR: |
Kévin Redon | 680bdab | 2018-07-18 14:00:57 +0200 | [diff] [blame] | 451 | /* initialize to default WI, this will be overwritten if we |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 452 | * send TC2, and it will be programmed into hardware after |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 453 | * ATR is finished */ |
| 454 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 455 | /* update waiting time to initial waiting time */ |
| 456 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 457 | /* set initial waiting time */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 458 | tc_etu_set_wtime(ch->tc_chan, ch->waiting_time); |
| 459 | /* Set ATR sub-state to initial state */ |
| 460 | ch->atr.idx = 0; |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 461 | /* enable USART transmission to reader */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 462 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 463 | /* trigger USART TX IRQ to sent first ATR byte TS */ |
| 464 | card_emu_uart_interrupt(ch->uart_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 465 | break; |
| 466 | case ISO_S_WAIT_TPDU: |
| 467 | /* enable the receiver, disable transmitter */ |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 468 | set_tpdu_state(ch, TPDU_S_WAIT_CLA); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 469 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 470 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 471 | case ISO_S_IN_PTS: |
| 472 | case ISO_S_IN_TPDU: |
| 473 | /* do nothing */ |
| 474 | break; |
| 475 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 478 | /********************************************************************** |
| 479 | * ATR handling |
| 480 | **********************************************************************/ |
| 481 | |
| 482 | /*! Transmit ATR data to reader |
| 483 | * @param[in] ch card interface connected to reader |
| 484 | * @return numbers of bytes transmitted |
| 485 | */ |
| 486 | static int tx_byte_atr(struct card_handle *ch) |
| 487 | { |
| 488 | if (NULL == ch) { |
| 489 | TRACE_ERROR("ATR TX: no card handle provided\n\r"); |
| 490 | return 0; |
| 491 | } |
| 492 | if (ISO_S_IN_ATR != ch->state) { |
| 493 | TRACE_ERROR("%u: ATR TX: no in ATR state\n\r", ch->num); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* Transmit ATR */ |
| 498 | if (ch->atr.idx < ch->atr.len) { |
| 499 | uint8_t byte = ch->atr.atr[ch->atr.idx++]; |
| 500 | card_emu_uart_tx(ch->uart_chan, byte); |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 501 | return 1; |
| 502 | } else { /* The ATR has been completely transmitted */ |
Kévin Redon | 8b8e58b | 2018-07-30 18:19:11 +0200 | [diff] [blame] | 503 | /* search for TC2 to updated WI */ |
| 504 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 505 | if (ch->atr.len >= 2 && ch->atr.atr[1] & 0xf0) { /* Y1 has some data */ |
| 506 | uint8_t atr_td1 = 2; |
| 507 | if (ch->atr.atr[1] & 0x10) { /* TA1 is present */ |
| 508 | atr_td1++; |
| 509 | } |
| 510 | if (ch->atr.atr[1] & 0x20) { /* TB1 is present */ |
| 511 | atr_td1++; |
| 512 | } |
| 513 | if (ch->atr.atr[1] & 0x40) { /* TC1 is present */ |
| 514 | atr_td1++; |
| 515 | } |
| 516 | if (ch->atr.atr[1] & 0x80) { /* TD1 is present */ |
| 517 | if (ch->atr.len > atr_td1 && ch->atr.atr[atr_td1] & 0xf0) { /* Y2 has some data */ |
| 518 | uint8_t atr_tc2 = atr_td1+1; |
| 519 | if (ch->atr.atr[atr_td1] & 0x10) { /* TA2 is present */ |
| 520 | atr_tc2++; |
| 521 | } |
| 522 | if (ch->atr.atr[atr_td1] & 0x20) { /* TB2 is present */ |
| 523 | atr_tc2++; |
| 524 | } |
| 525 | if (ch->atr.atr[atr_td1] & 0x40) { /* TC2 is present */ |
| 526 | if (ch->atr.len > atr_tc2 && ch->atr.atr[atr_tc2]) { /* TC2 encodes WI */ |
| 527 | ch->wi = ch->atr.atr[atr_tc2]; /* set WI */ |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | /* update waiting time (see ISO 7816-3 10.2) */ |
| 534 | ch->waiting_time = ch->wi * 960 * ch->fi; |
| 535 | tc_etu_set_wtime(ch->tc_chan, ch->waiting_time); |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 536 | /* go to next state */ |
| 537 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | /* return number of bytes transmitted */ |
| 542 | return 1; |
| 543 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 544 | |
| 545 | /********************************************************************** |
| 546 | * PTS / PPS handling |
| 547 | **********************************************************************/ |
| 548 | |
Kévin Redon | 680bdab | 2018-07-18 14:00:57 +0200 | [diff] [blame] | 549 | /* Update the PTS sub-state */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 550 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss) |
| 551 | { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 552 | TRACE_DEBUG("%u: 7816 PTS state %u -> %u\r\n", |
| 553 | ch->num, ch->pts.state, new_ptss); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 554 | ch->pts.state = new_ptss; |
| 555 | } |
| 556 | |
| 557 | /* Determine the next PTS state */ |
| 558 | static enum pts_state next_pts_state(struct card_handle *ch) |
| 559 | { |
| 560 | uint8_t is_resp = ch->pts.state & 0x10; |
| 561 | uint8_t sstate = ch->pts.state & 0x0f; |
| 562 | uint8_t *pts_ptr; |
| 563 | |
| 564 | if (!is_resp) |
| 565 | pts_ptr = ch->pts.req; |
| 566 | else |
| 567 | pts_ptr = ch->pts.resp; |
| 568 | |
| 569 | switch (sstate) { |
| 570 | case PTS_S_WAIT_REQ_PTSS: |
| 571 | goto from_ptss; |
| 572 | case PTS_S_WAIT_REQ_PTS0: |
| 573 | goto from_pts0; |
| 574 | case PTS_S_WAIT_REQ_PTS1: |
| 575 | goto from_pts1; |
| 576 | case PTS_S_WAIT_REQ_PTS2: |
| 577 | goto from_pts2; |
| 578 | case PTS_S_WAIT_REQ_PTS3: |
| 579 | goto from_pts3; |
| 580 | } |
| 581 | |
| 582 | if (ch->pts.state == PTS_S_WAIT_REQ_PCK) |
| 583 | return PTS_S_WAIT_RESP_PTSS; |
| 584 | |
| 585 | from_ptss: |
| 586 | return PTS_S_WAIT_REQ_PTS0 | is_resp; |
| 587 | from_pts0: |
| 588 | if (pts_ptr[_PTS0] & (1 << 4)) |
| 589 | return PTS_S_WAIT_REQ_PTS1 | is_resp; |
| 590 | from_pts1: |
| 591 | if (pts_ptr[_PTS0] & (1 << 5)) |
| 592 | return PTS_S_WAIT_REQ_PTS2 | is_resp; |
| 593 | from_pts2: |
| 594 | if (pts_ptr[_PTS0] & (1 << 6)) |
| 595 | return PTS_S_WAIT_REQ_PTS3 | is_resp; |
| 596 | from_pts3: |
| 597 | return PTS_S_WAIT_REQ_PCK | is_resp; |
| 598 | } |
| 599 | |
| 600 | |
Harald Welte | ccb8a22 | 2016-03-20 13:37:11 +0100 | [diff] [blame] | 601 | static int |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 602 | process_byte_pts(struct card_handle *ch, uint8_t byte) |
| 603 | { |
| 604 | switch (ch->pts.state) { |
| 605 | case PTS_S_WAIT_REQ_PTSS: |
| 606 | ch->pts.req[_PTSS] = byte; |
| 607 | break; |
| 608 | case PTS_S_WAIT_REQ_PTS0: |
| 609 | ch->pts.req[_PTS0] = byte; |
| 610 | break; |
| 611 | case PTS_S_WAIT_REQ_PTS1: |
| 612 | ch->pts.req[_PTS1] = byte; |
| 613 | break; |
| 614 | case PTS_S_WAIT_REQ_PTS2: |
| 615 | ch->pts.req[_PTS2] = byte; |
| 616 | break; |
| 617 | case PTS_S_WAIT_REQ_PTS3: |
| 618 | ch->pts.req[_PTS3] = byte; |
| 619 | break; |
| 620 | case PTS_S_WAIT_REQ_PCK: |
| 621 | ch->pts.req[_PCK] = byte; |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 622 | if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 623 | TRACE_ERROR("%u: Error in PTS Checksum!\r\n", |
| 624 | ch->num); |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 625 | /* Wait for the next TPDU */ |
| 626 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 627 | return ISO_S_WAIT_TPDU; |
| 628 | } |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 629 | /* FIXME: check if proposal matches capabilities in ATR */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 630 | memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp)); |
| 631 | break; |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 632 | default: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 633 | TRACE_ERROR("%u: process_byte_pts() in invalid state %u\r\n", |
| 634 | ch->num, ch->pts.state); |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 635 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 636 | } |
| 637 | /* calculate the next state and set it */ |
| 638 | set_pts_state(ch, next_pts_state(ch)); |
| 639 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 640 | if (ch->pts.state == PTS_S_WAIT_RESP_PTSS) { |
| 641 | flush_pts(ch); |
| 642 | /* activate UART TX to transmit PTS response */ |
| 643 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 644 | /* don't fall-through to the 'return ISO_S_IN_PTS' |
| 645 | * below, rather keep ISO7816 state as-is, it will be |
| 646 | * further updated by the tx-completion handler */ |
Harald Welte | ccb8a22 | 2016-03-20 13:37:11 +0100 | [diff] [blame] | 647 | return -1; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 650 | return ISO_S_IN_PTS; |
| 651 | } |
| 652 | |
| 653 | /* return a single byte to be transmitted to the reader */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 654 | static int tx_byte_pts(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 655 | { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 656 | uint8_t byte; |
| 657 | |
| 658 | /* 1: Determine the next transmit byte */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 659 | switch (ch->pts.state) { |
| 660 | case PTS_S_WAIT_RESP_PTSS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 661 | byte = ch->pts.resp[_PTSS]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 662 | break; |
| 663 | case PTS_S_WAIT_RESP_PTS0: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 664 | byte = ch->pts.resp[_PTS0]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 665 | break; |
| 666 | case PTS_S_WAIT_RESP_PTS1: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 667 | byte = ch->pts.resp[_PTS1]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 668 | /* This must be TA1 */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 669 | ch->fi = byte >> 4; |
| 670 | ch->di = byte & 0xf; |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 671 | TRACE_DEBUG("%u: found Fi=%u Di=%u\r\n", ch->num, |
| 672 | ch->fi, ch->di); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 673 | break; |
| 674 | case PTS_S_WAIT_RESP_PTS2: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 675 | byte = ch->pts.resp[_PTS2]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 676 | break; |
| 677 | case PTS_S_WAIT_RESP_PTS3: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 678 | byte = ch->pts.resp[_PTS3]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 679 | break; |
| 680 | case PTS_S_WAIT_RESP_PCK: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 681 | byte = ch->pts.resp[_PCK]; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 682 | break; |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 683 | default: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 684 | TRACE_ERROR("%u: get_byte_pts() in invalid state %u\r\n", |
| 685 | ch->num, ch->pts.state); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | /* 2: Transmit the byte */ |
| 690 | card_emu_uart_tx(ch->uart_chan, byte); |
| 691 | |
| 692 | /* 3: Update the state */ |
| 693 | |
| 694 | switch (ch->pts.state) { |
| 695 | case PTS_S_WAIT_RESP_PCK: |
Harald Welte | c58bba0 | 2016-03-20 14:57:53 +0100 | [diff] [blame] | 696 | card_emu_uart_wait_tx_idle(ch->uart_chan); |
Harald Welte | 52d5546 | 2016-03-20 13:38:05 +0100 | [diff] [blame] | 697 | /* update baud rate generator with Fi/Di */ |
| 698 | emu_update_fidi(ch); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 699 | /* Wait for the next TPDU */ |
| 700 | card_set_state(ch, ISO_S_WAIT_TPDU); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 701 | break; |
| 702 | default: |
| 703 | /* calculate the next state and set it */ |
| 704 | set_pts_state(ch, next_pts_state(ch)); |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 705 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 706 | } |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 707 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 708 | /* return number of bytes transmitted */ |
| 709 | return 1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
| 713 | /********************************************************************** |
| 714 | * TPDU handling |
| 715 | **********************************************************************/ |
| 716 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 717 | |
| 718 | /* compute number of data bytes according to Chapter 10.3.2 of 7816-3 */ |
| 719 | static unsigned int t0_num_data_bytes(uint8_t p3, int reader_to_card) |
| 720 | { |
| 721 | if (reader_to_card) { |
| 722 | return p3; |
| 723 | } else { |
| 724 | if (p3 == 0) |
| 725 | return 256; |
| 726 | else |
| 727 | return p3; |
| 728 | } |
| 729 | } |
| 730 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 731 | /* add a just-received TPDU byte (from reader) to USB buffer */ |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 732 | static void add_tpdu_byte(struct card_handle *ch, uint8_t byte) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 733 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 734 | struct msgb *msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 735 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 736 | unsigned int num_data_bytes = t0_num_data_bytes(ch->tpdu.hdr[_P3], 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 737 | |
| 738 | /* ensure we have a buffer */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 739 | if (!ch->uart_rx_msg) { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 740 | msg = ch->uart_rx_msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, |
| 741 | SIMTRACE_MSGT_DO_CEMU_RX_DATA); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 742 | if (!ch->uart_rx_msg) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 743 | TRACE_ERROR("%u: Received UART byte but ENOMEM\r\n", |
| 744 | ch->num); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 745 | return; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 746 | } |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 747 | msgb_put(msg, sizeof(*rd)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 748 | } else |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 749 | msg = ch->uart_rx_msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 750 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 751 | rd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 752 | msgb_put_u8(msg, byte); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 753 | |
| 754 | /* check if the buffer is full. If so, send it */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 755 | if (msgb_l2len(msg) >= sizeof(*rd) + num_data_bytes) { |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 756 | rd->flags |= CEMU_DATA_F_FINAL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 757 | flush_rx_buffer(ch); |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 758 | /* We need to transmit the SW now, */ |
| 759 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 760 | } else if (msgb_tailroom(msg) <= 0) |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 761 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts) |
| 765 | { |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 766 | if (ch->tpdu.state == new_ts) |
| 767 | return; |
| 768 | |
Kévin Redon | 2fdcf3b | 2018-10-15 19:43:15 +0200 | [diff] [blame] | 769 | TRACE_DEBUG("%u: 7816 TPDU state %s -> %s\r\n", ch->num, |
| 770 | get_value_string(tpdu_state_names, ch->tpdu.state), |
| 771 | get_value_string(tpdu_state_names, new_ts)); |
Harald Welte | 903d63a | 2016-03-20 13:38:39 +0100 | [diff] [blame] | 772 | ch->tpdu.state = new_ts; |
| 773 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 774 | switch (new_ts) { |
| 775 | case TPDU_S_WAIT_CLA: |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 776 | case TPDU_S_WAIT_RX: |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 777 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 778 | break; |
| 779 | case TPDU_S_WAIT_PB: |
| 780 | /* we just completed the TPDU header from reader to card |
| 781 | * and now need to disable the receiver, enable the |
| 782 | * transmitter and transmit the procedure byte */ |
| 783 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 784 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 785 | default: |
| 786 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 787 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static enum tpdu_state next_tpdu_state(struct card_handle *ch) |
| 791 | { |
| 792 | switch (ch->tpdu.state) { |
| 793 | case TPDU_S_WAIT_CLA: |
| 794 | return TPDU_S_WAIT_INS; |
| 795 | case TPDU_S_WAIT_INS: |
| 796 | return TPDU_S_WAIT_P1; |
| 797 | case TPDU_S_WAIT_P1: |
| 798 | return TPDU_S_WAIT_P2; |
| 799 | case TPDU_S_WAIT_P2: |
| 800 | return TPDU_S_WAIT_P3; |
| 801 | case TPDU_S_WAIT_P3: |
| 802 | return TPDU_S_WAIT_PB; |
| 803 | /* simply stay in Rx or Tx by default */ |
| 804 | case TPDU_S_WAIT_PB: |
| 805 | return TPDU_S_WAIT_PB; |
| 806 | case TPDU_S_WAIT_RX: |
| 807 | return TPDU_S_WAIT_RX; |
| 808 | case TPDU_S_WAIT_TX: |
| 809 | return TPDU_S_WAIT_TX; |
| 810 | } |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 811 | /* we should never reach here */ |
| 812 | assert(0); |
| 813 | return -1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void send_tpdu_header(struct card_handle *ch) |
| 817 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 818 | struct msgb *msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 819 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 820 | uint8_t *cur; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 821 | |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 822 | TRACE_INFO("%u: %s: %02x %02x %02x %02x %02x\r\n", |
| 823 | ch->num, __func__, |
Harald Welte | 43f7949 | 2016-02-29 10:06:54 +0100 | [diff] [blame] | 824 | ch->tpdu.hdr[0], ch->tpdu.hdr[1], |
| 825 | ch->tpdu.hdr[2], ch->tpdu.hdr[3], |
| 826 | ch->tpdu.hdr[4]); |
| 827 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 828 | /* if we already/still have a context, send it off */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 829 | if (ch->uart_rx_msg) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 830 | TRACE_DEBUG("%u: have old buffer\r\n", ch->num); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 831 | if (msgb_l2len(ch->uart_rx_msg)) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 832 | TRACE_DEBUG("%u: flushing old buffer\r\n", ch->num); |
Harald Welte | f1697e2 | 2016-03-02 10:28:54 +0100 | [diff] [blame] | 833 | flush_rx_buffer(ch); |
| 834 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 835 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 836 | TRACE_DEBUG("%u: allocating new buffer\r\n", ch->num); |
| 837 | /* ensure we have a new buffer */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 838 | ch->uart_rx_msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, |
| 839 | SIMTRACE_MSGT_DO_CEMU_RX_DATA); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 840 | if (!ch->uart_rx_msg) { |
| 841 | TRACE_ERROR("%u: %s: ENOMEM\r\n", ch->num, __func__); |
| 842 | return; |
| 843 | } |
| 844 | msg = ch->uart_rx_msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 845 | rd = (struct cardemu_usb_msg_rx_data *) msgb_put(msg, sizeof(*rd)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 846 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 847 | /* initialize header */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 848 | rd->flags = CEMU_DATA_F_TPDU_HDR; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 849 | |
| 850 | /* copy TPDU header to data field */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 851 | cur = msgb_put(msg, sizeof(ch->tpdu.hdr)); |
| 852 | memcpy(cur, ch->tpdu.hdr, sizeof(ch->tpdu.hdr)); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 853 | /* rd->data_len is set in flush_rx_buffer() */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 854 | |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 855 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static enum iso7816_3_card_state |
| 859 | process_byte_tpdu(struct card_handle *ch, uint8_t byte) |
| 860 | { |
| 861 | switch (ch->tpdu.state) { |
| 862 | case TPDU_S_WAIT_CLA: |
| 863 | ch->tpdu.hdr[_CLA] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 864 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 865 | break; |
| 866 | case TPDU_S_WAIT_INS: |
| 867 | ch->tpdu.hdr[_INS] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 868 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 869 | break; |
| 870 | case TPDU_S_WAIT_P1: |
| 871 | ch->tpdu.hdr[_P1] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 872 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 873 | break; |
| 874 | case TPDU_S_WAIT_P2: |
| 875 | ch->tpdu.hdr[_P2] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 876 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 877 | break; |
| 878 | case TPDU_S_WAIT_P3: |
| 879 | ch->tpdu.hdr[_P3] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 880 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 881 | /* FIXME: start timer to transmit further 0x60 */ |
| 882 | /* send the TPDU header as part of a procedure byte |
| 883 | * request to the USB host */ |
| 884 | send_tpdu_header(ch); |
| 885 | break; |
| 886 | case TPDU_S_WAIT_RX: |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 887 | add_tpdu_byte(ch, byte); |
| 888 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 889 | default: |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 890 | TRACE_ERROR("%u: process_byte_tpdu() in invalid state %u\r\n", |
| 891 | ch->num, ch->tpdu.state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 892 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 893 | |
| 894 | /* ensure we stay in TPDU ISO state */ |
| 895 | return ISO_S_IN_TPDU; |
| 896 | } |
| 897 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 898 | /* tx a single byte to be transmitted to the reader */ |
| 899 | static int tx_byte_tpdu(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 900 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 901 | struct msgb *msg; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 902 | struct cardemu_usb_msg_tx_data *td; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 903 | uint8_t byte; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 904 | |
| 905 | /* ensure we are aware of any data that might be pending for |
| 906 | * transmit */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 907 | if (!ch->uart_tx_msg) { |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 908 | /* uart_tx_queue is filled from main loop, so no need |
| 909 | * for irq-safe operations */ |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 910 | if (llist_empty(&ch->uart_tx_queue)) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 911 | return 0; |
| 912 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 913 | /* dequeue first at head */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 914 | ch->uart_tx_msg = msgb_dequeue(&ch->uart_tx_queue); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 915 | ch->uart_tx_msg->l1h = ch->uart_tx_msg->head; |
| 916 | ch->uart_tx_msg->l2h = ch->uart_tx_msg->l1h + sizeof(struct simtrace_msg_hdr); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 917 | msg = ch->uart_tx_msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 918 | /* remove the header */ |
| 919 | msgb_pull(msg, sizeof(struct simtrace_msg_hdr) + sizeof(*td)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 920 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 921 | msg = ch->uart_tx_msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 922 | td = (struct cardemu_usb_msg_tx_data *) msg->l2h; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 923 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 924 | /* take the next pending byte out of the msgb */ |
| 925 | byte = msgb_pull_u8(msg); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 926 | |
| 927 | card_emu_uart_tx(ch->uart_chan, byte); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 928 | |
Kévin Redon | 0f4abf5 | 2018-08-02 17:42:48 +0200 | [diff] [blame] | 929 | /* this must happen _after_ the byte has been transmitted */ |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 930 | switch (ch->tpdu.state) { |
| 931 | case TPDU_S_WAIT_PB: |
| 932 | /* if we just transmitted the procedure byte, we need to decide |
| 933 | * if we want to continue to receive or transmit */ |
| 934 | if (td->flags & CEMU_DATA_F_PB_AND_TX) |
| 935 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
| 936 | else if (td->flags & CEMU_DATA_F_PB_AND_RX) |
| 937 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
| 938 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 939 | default: |
| 940 | break; |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 943 | /* check if the buffer has now been fully transmitted */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 944 | if (msgb_length(msg) == 0) { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 945 | if (td->flags & CEMU_DATA_F_PB_AND_RX) { |
| 946 | /* we have just sent the procedure byte and now |
| 947 | * need to continue receiving */ |
| 948 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 949 | } else { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 950 | /* we have transmitted all bytes */ |
| 951 | if (td->flags & CEMU_DATA_F_FINAL) { |
| 952 | /* this was the final part of the APDU, go |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 953 | * back to state one */ |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 954 | card_set_state(ch, ISO_S_WAIT_TPDU); |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 955 | } |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 956 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 957 | usb_buf_free(msg); |
| 958 | ch->uart_tx_msg = NULL; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | return 1; |
| 962 | } |
| 963 | |
| 964 | /********************************************************************** |
| 965 | * Public API |
| 966 | **********************************************************************/ |
| 967 | |
| 968 | /* process a single byte received from the reader */ |
| 969 | void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte) |
| 970 | { |
| 971 | int new_state = -1; |
| 972 | |
| 973 | ch->stats.rx_bytes++; |
| 974 | |
| 975 | switch (ch->state) { |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 976 | case ISO_S_WAIT_TPDU: |
| 977 | if (byte == 0xff) { |
Harald Welte | 02d0d73 | 2019-12-15 13:19:24 +0100 | [diff] [blame^] | 978 | /* reset PTS to initial state */ |
| 979 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 980 | new_state = process_byte_pts(ch, byte); |
| 981 | ch->stats.pps++; |
| 982 | goto out_silent; |
| 983 | } |
| 984 | /* fall-through */ |
| 985 | case ISO_S_IN_TPDU: |
| 986 | new_state = process_byte_tpdu(ch, byte); |
| 987 | break; |
| 988 | case ISO_S_IN_PTS: |
| 989 | new_state = process_byte_pts(ch, byte); |
| 990 | goto out_silent; |
Kévin Redon | 5f6b871 | 2018-07-11 09:49:06 +0200 | [diff] [blame] | 991 | default: |
| 992 | TRACE_ERROR("%u: Received UART char in invalid 7816 state " |
| 993 | "%u\r\n", ch->num, ch->state); |
| 994 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | out_silent: |
| 998 | if (new_state != -1) |
| 999 | card_set_state(ch, new_state); |
| 1000 | } |
| 1001 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 1002 | /* transmit a single byte to the reader */ |
| 1003 | int card_emu_tx_byte(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1004 | { |
| 1005 | int rc = 0; |
| 1006 | |
| 1007 | switch (ch->state) { |
| 1008 | case ISO_S_IN_ATR: |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 1009 | rc = tx_byte_atr(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1010 | break; |
| 1011 | case ISO_S_IN_PTS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 1012 | rc = tx_byte_pts(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1013 | break; |
| 1014 | case ISO_S_IN_TPDU: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 1015 | rc = tx_byte_tpdu(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1016 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 1017 | default: |
| 1018 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | if (rc) |
| 1022 | ch->stats.tx_bytes++; |
| 1023 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 1024 | /* if we return 0 here, the UART needs to disable transmit-ready |
| 1025 | * interrupts */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1026 | return rc; |
| 1027 | } |
| 1028 | |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 1029 | void card_emu_have_new_uart_tx(struct card_handle *ch) |
| 1030 | { |
| 1031 | switch (ch->state) { |
| 1032 | case ISO_S_IN_TPDU: |
| 1033 | switch (ch->tpdu.state) { |
| 1034 | case TPDU_S_WAIT_TX: |
| 1035 | case TPDU_S_WAIT_PB: |
| 1036 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 1037 | break; |
| 1038 | default: |
| 1039 | break; |
| 1040 | } |
| 1041 | default: |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 1046 | void card_emu_report_status(struct card_handle *ch) |
| 1047 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 1048 | struct msgb *msg; |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 1049 | struct cardemu_usb_msg_status *sts; |
| 1050 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 1051 | msg = usb_buf_alloc_st(ch->in_ep, SIMTRACE_MSGC_CARDEM, |
| 1052 | SIMTRACE_MSGT_BD_CEMU_STATUS); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 1053 | if (!msg) |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 1054 | return; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 1055 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 1056 | sts = (struct cardemu_usb_msg_status *) msgb_put(msg, sizeof(*sts)); |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 1057 | sts->flags = 0; |
| 1058 | if (ch->vcc_active) |
| 1059 | sts->flags |= CEMU_STATUS_F_VCC_PRESENT; |
| 1060 | if (ch->clocked) |
| 1061 | sts->flags |= CEMU_STATUS_F_CLK_ACTIVE; |
| 1062 | if (ch->in_reset) |
| 1063 | sts->flags |= CEMU_STATUS_F_RESET_ACTIVE; |
| 1064 | /* FIXME: voltage + card insert */ |
| 1065 | sts->fi = ch->fi; |
| 1066 | sts->di = ch->di; |
| 1067 | sts->wi = ch->wi; |
| 1068 | sts->waiting_time = ch->waiting_time; |
| 1069 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 1070 | usb_buf_upd_len_and_submit(msg); |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 1071 | } |
| 1072 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1073 | /* hardware driver informs us that a card I/O signal has changed */ |
| 1074 | void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active) |
| 1075 | { |
| 1076 | switch (io) { |
| 1077 | case CARD_IO_VCC: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1078 | if (active == 0 && ch->vcc_active == 1) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1079 | TRACE_INFO("%u: VCC deactivated\r\n", ch->num); |
Harald Welte | 81f4ef7 | 2019-12-14 22:20:00 +0100 | [diff] [blame] | 1080 | card_handle_reset(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1081 | card_set_state(ch, ISO_S_WAIT_POWER); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1082 | } else if (active == 1 && ch->vcc_active == 0) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1083 | TRACE_INFO("%u: VCC activated\r\n", ch->num); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1084 | card_set_state(ch, ISO_S_WAIT_CLK); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1085 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1086 | ch->vcc_active = active; |
| 1087 | break; |
| 1088 | case CARD_IO_CLK: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1089 | if (active == 1 && ch->clocked == 0) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1090 | TRACE_INFO("%u: CLK activated\r\n", ch->num); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1091 | if (ch->state == ISO_S_WAIT_CLK) |
| 1092 | card_set_state(ch, ISO_S_WAIT_RST); |
| 1093 | } else if (active == 0 && ch->clocked == 1) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1094 | TRACE_INFO("%u: CLK deactivated\r\n", ch->num); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1095 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1096 | ch->clocked = active; |
| 1097 | break; |
| 1098 | case CARD_IO_RST: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1099 | if (active == 0 && ch->in_reset) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1100 | TRACE_INFO("%u: RST released\r\n", ch->num); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1101 | if (ch->vcc_active && ch->clocked) { |
| 1102 | /* enable the TC/ETU counter once reset has been released */ |
| 1103 | tc_etu_enable(ch->tc_chan); |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 1104 | /* prepare to send the ATR */ |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1105 | card_set_state(ch, ISO_S_WAIT_ATR); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 1106 | } |
| 1107 | } else if (active && !ch->in_reset) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1108 | TRACE_INFO("%u: RST asserted\r\n", ch->num); |
Harald Welte | 81f4ef7 | 2019-12-14 22:20:00 +0100 | [diff] [blame] | 1109 | card_handle_reset(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1110 | } |
| 1111 | ch->in_reset = active; |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /* User sets a new ATR to be returned during next card reset */ |
| 1117 | int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len) |
| 1118 | { |
| 1119 | if (len > sizeof(ch->atr.atr)) |
| 1120 | return -1; |
| 1121 | |
| 1122 | memcpy(ch->atr.atr, atr, len); |
| 1123 | ch->atr.len = len; |
| 1124 | ch->atr.idx = 0; |
| 1125 | |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 1126 | #if TRACE_LEVEL >= TRACE_LEVEL_INFO |
| 1127 | uint8_t i; |
| 1128 | TRACE_INFO("%u: ATR set: ", ch->num); |
| 1129 | for (i = 0; i < ch->atr.len; i++) { |
| 1130 | TRACE_INFO_WP("%02x ", atr[i]); |
| 1131 | } |
| 1132 | TRACE_INFO_WP("\n\r"); |
| 1133 | #endif |
| 1134 | /* FIXME: race condition with transmitting ATR to reader? */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | /* hardware driver informs us that one (more) ETU has expired */ |
| 1140 | void tc_etu_wtime_half_expired(void *handle) |
| 1141 | { |
| 1142 | struct card_handle *ch = handle; |
| 1143 | /* transmit NULL procedure byte well before waiting time expires */ |
Harald Welte | dda7355 | 2016-03-02 10:29:55 +0100 | [diff] [blame] | 1144 | switch (ch->state) { |
| 1145 | case ISO_S_IN_TPDU: |
| 1146 | switch (ch->tpdu.state) { |
| 1147 | case TPDU_S_WAIT_PB: |
| 1148 | case TPDU_S_WAIT_TX: |
| 1149 | putchar('N'); |
| 1150 | card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL); |
| 1151 | break; |
| 1152 | default: |
| 1153 | break; |
| 1154 | } |
| 1155 | break; |
| 1156 | default: |
| 1157 | break; |
| 1158 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* hardware driver informs us that one (more) ETU has expired */ |
| 1162 | void tc_etu_wtime_expired(void *handle) |
| 1163 | { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1164 | struct card_handle *ch = handle; |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 1165 | switch (ch->state) { |
| 1166 | case ISO_S_WAIT_ATR: |
| 1167 | /* ISO 7816-3 6.2.1 time tc has passed, we can now send the ATR */ |
| 1168 | card_set_state(ch, ISO_S_IN_ATR); |
| 1169 | break; |
| 1170 | default: |
| 1171 | TRACE_ERROR("%u: wtime_exp\r\n", ch->num); |
| 1172 | break; |
| 1173 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Kévin Redon | 503e184 | 2019-05-15 00:23:21 +0200 | [diff] [blame] | 1176 | /* shortest ATR possible (uses default speed and no options) */ |
| 1177 | static const uint8_t default_atr[] = { 0x3B, 0x00 }; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1178 | |
| 1179 | static struct card_handle card_handles[NUM_SLOTS]; |
| 1180 | |
Kévin Redon | 7233cf8 | 2019-11-14 19:37:32 +0100 | [diff] [blame] | 1181 | struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan, uint8_t in_ep, uint8_t irq_ep, bool vcc_active, bool in_reset, bool clocked) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1182 | { |
| 1183 | struct card_handle *ch; |
| 1184 | |
| 1185 | if (slot_num >= ARRAY_SIZE(card_handles)) |
| 1186 | return NULL; |
| 1187 | |
| 1188 | ch = &card_handles[slot_num]; |
| 1189 | |
| 1190 | memset(ch, 0, sizeof(*ch)); |
| 1191 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 1192 | INIT_LLIST_HEAD(&ch->uart_tx_queue); |
| 1193 | |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 1194 | ch->num = slot_num; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 1195 | ch->irq_ep = irq_ep; |
| 1196 | ch->in_ep = in_ep; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1197 | ch->state = ISO_S_WAIT_POWER; |
Kévin Redon | 7233cf8 | 2019-11-14 19:37:32 +0100 | [diff] [blame] | 1198 | ch->vcc_active = vcc_active; |
| 1199 | ch->in_reset = in_reset; |
| 1200 | ch->clocked = clocked; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1201 | |
| 1202 | ch->fi = 0; |
| 1203 | ch->di = 1; |
| 1204 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 1205 | |
| 1206 | ch->tc_chan = tc_chan; |
| 1207 | ch->uart_chan = uart_chan; |
| 1208 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 1209 | |
| 1210 | ch->atr.idx = 0; |
| 1211 | ch->atr.len = sizeof(default_atr); |
| 1212 | memcpy(ch->atr.atr, default_atr, ch->atr.len); |
| 1213 | |
Harald Welte | e92cb50 | 2019-12-14 21:48:45 +0100 | [diff] [blame] | 1214 | card_handle_reset(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1215 | |
| 1216 | tc_etu_init(ch->tc_chan, ch); |
| 1217 | |
| 1218 | return ch; |
| 1219 | } |