Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1 | /* ISO7816-3 state machine for the card side */ |
| 2 | /* (C) 2010-2015 by Harald Welte <hwelte@hmw-consulting.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | */ |
| 19 | |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 20 | #define TRACE_LEVEL 6 |
| 21 | |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 22 | #include <assert.h> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <string.h> |
| 25 | #include <stdint.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include "utils.h" |
| 29 | #include "trace.h" |
| 30 | #include "iso7816_fidi.h" |
| 31 | #include "tc_etu.h" |
| 32 | #include "card_emu.h" |
| 33 | #include "req_ctx.h" |
| 34 | #include "cardemu_prot.h" |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 35 | #include "linuxlist.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 | |
| 57 | /* detailed sub-states of ISO_S_IN_PTS */ |
| 58 | enum pts_state { |
| 59 | PTS_S_WAIT_REQ_PTSS, |
| 60 | PTS_S_WAIT_REQ_PTS0, |
| 61 | PTS_S_WAIT_REQ_PTS1, |
| 62 | PTS_S_WAIT_REQ_PTS2, |
| 63 | PTS_S_WAIT_REQ_PTS3, |
| 64 | PTS_S_WAIT_REQ_PCK, |
| 65 | PTS_S_WAIT_RESP_PTSS = PTS_S_WAIT_REQ_PTSS | 0x10, |
| 66 | PTS_S_WAIT_RESP_PTS0 = PTS_S_WAIT_REQ_PTS0 | 0x10, |
| 67 | PTS_S_WAIT_RESP_PTS1 = PTS_S_WAIT_REQ_PTS1 | 0x10, |
| 68 | PTS_S_WAIT_RESP_PTS2 = PTS_S_WAIT_REQ_PTS2 | 0x10, |
| 69 | PTS_S_WAIT_RESP_PTS3 = PTS_S_WAIT_REQ_PTS3 | 0x10, |
| 70 | PTS_S_WAIT_RESP_PCK = PTS_S_WAIT_REQ_PCK | 0x10, |
| 71 | }; |
| 72 | |
| 73 | #define _PTSS 0 |
| 74 | #define _PTS0 1 |
| 75 | #define _PTS1 2 |
| 76 | #define _PTS2 3 |
| 77 | #define _PTS3 4 |
| 78 | #define _PCK 5 |
| 79 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 80 | /* T-PDU state machine states */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 81 | enum tpdu_state { |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 82 | TPDU_S_WAIT_CLA, /* waiting for CLA byte from reader */ |
| 83 | TPDU_S_WAIT_INS, /* waiting for INS byte from reader */ |
| 84 | TPDU_S_WAIT_P1, /* waiting for P1 byte from reader */ |
| 85 | TPDU_S_WAIT_P2, /* waiting for P2 byte from reader */ |
| 86 | TPDU_S_WAIT_P3, /* waiting for P3 byte from reader */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 87 | TPDU_S_WAIT_PB, /* waiting for Tx of procedure byte */ |
| 88 | TPDU_S_WAIT_RX, /* waiitng for more data from reader */ |
| 89 | TPDU_S_WAIT_TX, /* waiting for more data to reader */ |
| 90 | }; |
| 91 | |
| 92 | #define _CLA 0 |
| 93 | #define _INS 1 |
| 94 | #define _P1 2 |
| 95 | #define _P2 3 |
| 96 | #define _P3 4 |
| 97 | |
| 98 | struct card_handle { |
| 99 | enum iso7816_3_card_state state; |
| 100 | |
| 101 | /* signal levels */ |
| 102 | uint8_t vcc_active; /* 1 = on, 0 = off */ |
| 103 | uint8_t in_reset; /* 1 = RST low, 0 = RST high */ |
| 104 | uint8_t clocked; /* 1 = active, 0 = inactive */ |
| 105 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 106 | /* timing parameters, from PTS */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 107 | uint8_t fi; |
| 108 | uint8_t di; |
| 109 | uint8_t wi; |
| 110 | |
| 111 | uint8_t tc_chan; /* TC channel number */ |
| 112 | uint8_t uart_chan; /* UART channel */ |
| 113 | |
| 114 | uint32_t waiting_time; /* in clocks */ |
| 115 | |
| 116 | /* ATR state machine */ |
| 117 | struct { |
| 118 | uint8_t idx; |
| 119 | uint8_t len; |
| 120 | //uint8_t hist_len; |
| 121 | //uint8_t last_td; |
| 122 | uint8_t atr[ISO7816_3_ATR_LEN_MAX]; |
| 123 | } atr; |
| 124 | |
| 125 | /* PPS / PTS support */ |
| 126 | struct { |
| 127 | enum pts_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 128 | uint8_t req[6]; /* request bytes */ |
| 129 | uint8_t resp[6]; /* response bytes */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 130 | } pts; |
| 131 | |
| 132 | /* TPDU */ |
| 133 | struct { |
| 134 | enum tpdu_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 135 | uint8_t hdr[5]; /* CLA INS P1 P2 P3 */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 136 | } tpdu; |
| 137 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 138 | struct req_ctx *uart_rx_ctx; /* UART RX -> USB TX */ |
| 139 | struct req_ctx *uart_tx_ctx; /* USB RX -> UART TX */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 140 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 141 | struct llist_head usb_tx_queue; |
| 142 | struct llist_head uart_tx_queue; |
| 143 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 144 | struct { |
| 145 | uint32_t tx_bytes; |
| 146 | uint32_t rx_bytes; |
| 147 | uint32_t pps; |
| 148 | } stats; |
| 149 | }; |
| 150 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 151 | struct llist_head *card_emu_get_usb_tx_queue(struct card_handle *ch) |
| 152 | { |
| 153 | return &ch->usb_tx_queue; |
| 154 | } |
| 155 | |
| 156 | struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch) |
| 157 | { |
| 158 | return &ch->uart_tx_queue; |
| 159 | } |
| 160 | |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 161 | 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] | 162 | 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] | 163 | |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 164 | static void flush_rx_buffer(struct card_handle *ch) |
| 165 | { |
| 166 | struct req_ctx *rctx; |
| 167 | struct cardemu_usb_msg_rx_data *rd; |
| 168 | |
| 169 | rctx = ch->uart_rx_ctx; |
| 170 | if (!rctx) |
| 171 | return; |
| 172 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 173 | ch->uart_rx_ctx = NULL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 174 | |
| 175 | /* store length of data payload fild in header */ |
Harald Welte | fcdd660 | 2016-03-02 10:33:58 +0100 | [diff] [blame] | 176 | rd = (struct cardemu_usb_msg_rx_data *) rctx->data; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame^] | 177 | rd->data_len = rctx->idx; |
| 178 | rd->hdr.msg_len = sizeof(*rd) + rd->data_len; |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 179 | |
| 180 | llist_add_tail(&rctx->list, &ch->usb_tx_queue); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 181 | req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 182 | |
| 183 | /* FIXME: call into USB code to see if this buffer can |
| 184 | * be transmitted now */ |
| 185 | } |
| 186 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 187 | /* convert a non-contiguous PTS request/responsei into a contiguous |
| 188 | * buffer, returning the number of bytes used in the buffer */ |
| 189 | static int serialize_pts(uint8_t *out, const uint8_t *in) |
| 190 | { |
| 191 | int i = 0; |
| 192 | |
| 193 | out[i++] = in[_PTSS]; |
| 194 | out[i++] = in[_PTS0]; |
| 195 | if (in[_PTS0] & (1 << 4)) |
| 196 | out[i++] = in[_PTS1]; |
| 197 | if (in[_PTS0] & (1 << 5)) |
| 198 | out[i++] = in[_PTS2]; |
| 199 | if (in[_PTS0] & (1 << 6)) |
| 200 | out[i++] = in[_PTS3]; |
| 201 | out[i++] = in[_PCK]; |
| 202 | |
| 203 | return i; |
| 204 | } |
| 205 | |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 206 | static uint8_t csum_pts(const uint8_t *in) |
| 207 | { |
| 208 | uint8_t out[6]; |
| 209 | int len = serialize_pts(out, in); |
| 210 | uint8_t csum = 0; |
| 211 | int i; |
| 212 | |
| 213 | /* we don't include the PCK byte in the checksumming process */ |
| 214 | len -= 1; |
| 215 | |
| 216 | for (i = 0; i < len; i++) |
| 217 | csum = csum ^ out[i]; |
| 218 | |
| 219 | return csum; |
| 220 | } |
| 221 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 222 | static void flush_pts(struct card_handle *ch) |
| 223 | { |
| 224 | struct req_ctx *rctx; |
| 225 | struct cardemu_usb_msg_pts_info *ptsi; |
| 226 | |
| 227 | rctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY); |
| 228 | if (!rctx) |
| 229 | return; |
| 230 | |
| 231 | ptsi = (struct cardemu_usb_msg_pts_info *) rctx->data; |
| 232 | ptsi->hdr.msg_type = CEMU_USB_MSGT_DO_PTS; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame^] | 233 | ptsi->hdr.msg_len = sizeof(*ptsi); |
| 234 | ptsi->pts_len = serialize_pts(ptsi->req, ch->pts.req); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 235 | serialize_pts(ptsi->resp, ch->pts.resp); |
| 236 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 237 | llist_add_tail(&rctx->list, &ch->usb_tx_queue); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 238 | req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); |
| 239 | |
| 240 | /* FIXME: call into USB code to see if this buffer can |
| 241 | * be transmitted now */ |
| 242 | } |
| 243 | |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 244 | static void emu_update_fidi(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 245 | { |
| 246 | int rc; |
| 247 | |
| 248 | rc = compute_fidi_ratio(ch->fi, ch->di); |
| 249 | if (rc > 0 && rc < 0x400) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 250 | TRACE_DEBUG("computed Fi(%u) Di(%u) ratio: %d\r\n", |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 251 | ch->fi, ch->di, rc); |
| 252 | /* make sure UART uses new F/D ratio */ |
| 253 | card_emu_uart_update_fidi(ch->uart_chan, rc); |
| 254 | /* notify ETU timer about this */ |
| 255 | tc_etu_set_etu(ch->tc_chan, rc); |
| 256 | } else |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 257 | TRACE_DEBUG("computed FiDi ration %d unsupported\r\n", rc); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* Update the ISO 7816-3 TPDU receiver state */ |
| 261 | static void card_set_state(struct card_handle *ch, |
| 262 | enum iso7816_3_card_state new_state) |
| 263 | { |
| 264 | switch (new_state) { |
| 265 | case ISO_S_WAIT_POWER: |
| 266 | case ISO_S_WAIT_CLK: |
| 267 | case ISO_S_WAIT_RST: |
| 268 | /* disable Rx and Tx of UART */ |
| 269 | card_emu_uart_enable(ch->uart_chan, 0); |
| 270 | break; |
| 271 | case ISO_S_WAIT_ATR: |
Harald Welte | e7194ab | 2015-11-14 21:03:25 +0100 | [diff] [blame] | 272 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 273 | /* Reset to initial Fi / Di ratio */ |
| 274 | ch->fi = 1; |
| 275 | ch->di = 1; |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 276 | emu_update_fidi(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 277 | /* initialize todefault WI, this will be overwritten if we |
| 278 | * receive TC2, and it will be programmed into hardware after |
| 279 | * ATR is finished */ |
| 280 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 281 | /* update waiting time to initial waiting time */ |
| 282 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 283 | tc_etu_set_wtime(ch->tc_chan, ch->waiting_time); |
| 284 | /* Set ATR sub-state to initial state */ |
| 285 | ch->atr.idx = 0; |
| 286 | //set_atr_state(ch, ATR_S_WAIT_TS); |
| 287 | /* Notice that we are just coming out of reset */ |
| 288 | //ch->sh.flags |= SIMTRACE_FLAG_ATR; |
| 289 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 290 | break; |
| 291 | break; |
| 292 | case ISO_S_WAIT_TPDU: |
| 293 | /* enable the receiver, disable transmitter */ |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 294 | set_tpdu_state(ch, TPDU_S_WAIT_CLA); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 295 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 296 | break; |
| 297 | case ISO_S_IN_ATR: |
| 298 | case ISO_S_IN_PTS: |
| 299 | case ISO_S_IN_TPDU: |
| 300 | /* do nothing */ |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | if (ch->state == new_state) |
| 305 | return; |
| 306 | |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 307 | TRACE_DEBUG("7816 card state %u -> %u\r\n", ch->state, new_state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 308 | ch->state = new_state; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /********************************************************************** |
| 313 | * PTS / PPS handling |
| 314 | **********************************************************************/ |
| 315 | |
| 316 | /* Update the ATR sub-state */ |
| 317 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss) |
| 318 | { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 319 | TRACE_DEBUG("7816 PTS state %u -> %u\r\n", ch->pts.state, new_ptss); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 320 | ch->pts.state = new_ptss; |
| 321 | } |
| 322 | |
| 323 | /* Determine the next PTS state */ |
| 324 | static enum pts_state next_pts_state(struct card_handle *ch) |
| 325 | { |
| 326 | uint8_t is_resp = ch->pts.state & 0x10; |
| 327 | uint8_t sstate = ch->pts.state & 0x0f; |
| 328 | uint8_t *pts_ptr; |
| 329 | |
| 330 | if (!is_resp) |
| 331 | pts_ptr = ch->pts.req; |
| 332 | else |
| 333 | pts_ptr = ch->pts.resp; |
| 334 | |
| 335 | switch (sstate) { |
| 336 | case PTS_S_WAIT_REQ_PTSS: |
| 337 | goto from_ptss; |
| 338 | case PTS_S_WAIT_REQ_PTS0: |
| 339 | goto from_pts0; |
| 340 | case PTS_S_WAIT_REQ_PTS1: |
| 341 | goto from_pts1; |
| 342 | case PTS_S_WAIT_REQ_PTS2: |
| 343 | goto from_pts2; |
| 344 | case PTS_S_WAIT_REQ_PTS3: |
| 345 | goto from_pts3; |
| 346 | } |
| 347 | |
| 348 | if (ch->pts.state == PTS_S_WAIT_REQ_PCK) |
| 349 | return PTS_S_WAIT_RESP_PTSS; |
| 350 | |
| 351 | from_ptss: |
| 352 | return PTS_S_WAIT_REQ_PTS0 | is_resp; |
| 353 | from_pts0: |
| 354 | if (pts_ptr[_PTS0] & (1 << 4)) |
| 355 | return PTS_S_WAIT_REQ_PTS1 | is_resp; |
| 356 | from_pts1: |
| 357 | if (pts_ptr[_PTS0] & (1 << 5)) |
| 358 | return PTS_S_WAIT_REQ_PTS2 | is_resp; |
| 359 | from_pts2: |
| 360 | if (pts_ptr[_PTS0] & (1 << 6)) |
| 361 | return PTS_S_WAIT_REQ_PTS3 | is_resp; |
| 362 | from_pts3: |
| 363 | return PTS_S_WAIT_REQ_PCK | is_resp; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | static enum iso7816_3_card_state |
| 368 | process_byte_pts(struct card_handle *ch, uint8_t byte) |
| 369 | { |
| 370 | switch (ch->pts.state) { |
| 371 | case PTS_S_WAIT_REQ_PTSS: |
| 372 | ch->pts.req[_PTSS] = byte; |
| 373 | break; |
| 374 | case PTS_S_WAIT_REQ_PTS0: |
| 375 | ch->pts.req[_PTS0] = byte; |
| 376 | break; |
| 377 | case PTS_S_WAIT_REQ_PTS1: |
| 378 | ch->pts.req[_PTS1] = byte; |
| 379 | break; |
| 380 | case PTS_S_WAIT_REQ_PTS2: |
| 381 | ch->pts.req[_PTS2] = byte; |
| 382 | break; |
| 383 | case PTS_S_WAIT_REQ_PTS3: |
| 384 | ch->pts.req[_PTS3] = byte; |
| 385 | break; |
| 386 | case PTS_S_WAIT_REQ_PCK: |
| 387 | ch->pts.req[_PCK] = byte; |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 388 | if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 389 | TRACE_DEBUG("Error in PTS Checksum!\r\n"); |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 390 | /* Wait for the next TPDU */ |
| 391 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 392 | return ISO_S_WAIT_TPDU; |
| 393 | } |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 394 | /* FIXME: check if proposal matches capabilities in ATR */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 395 | memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp)); |
| 396 | break; |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 397 | default: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 398 | TRACE_DEBUG("process_byte_pts() in invalid state %u\r\n", |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 399 | ch->pts.state); |
| 400 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 401 | } |
| 402 | /* calculate the next state and set it */ |
| 403 | set_pts_state(ch, next_pts_state(ch)); |
| 404 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 405 | if (ch->pts.state == PTS_S_WAIT_RESP_PTSS) { |
| 406 | flush_pts(ch); |
| 407 | /* activate UART TX to transmit PTS response */ |
| 408 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 409 | } |
| 410 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 411 | return ISO_S_IN_PTS; |
| 412 | } |
| 413 | |
| 414 | /* return a single byte to be transmitted to the reader */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 415 | static int tx_byte_pts(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 416 | { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 417 | uint8_t byte; |
| 418 | |
| 419 | /* 1: Determine the next transmit byte */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 420 | switch (ch->pts.state) { |
| 421 | case PTS_S_WAIT_RESP_PTSS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 422 | byte = ch->pts.resp[_PTSS]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 423 | break; |
| 424 | case PTS_S_WAIT_RESP_PTS0: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 425 | byte = ch->pts.resp[_PTS0]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 426 | break; |
| 427 | case PTS_S_WAIT_RESP_PTS1: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 428 | byte = ch->pts.resp[_PTS1]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 429 | /* This must be TA1 */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 430 | ch->fi = byte >> 4; |
| 431 | ch->di = byte & 0xf; |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 432 | TRACE_DEBUG("found Fi=%u Di=%u\r\n", ch->fi, ch->di); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 433 | break; |
| 434 | case PTS_S_WAIT_RESP_PTS2: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 435 | byte = ch->pts.resp[_PTS2]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 436 | break; |
| 437 | case PTS_S_WAIT_RESP_PTS3: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 438 | byte = ch->pts.resp[_PTS3]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 439 | break; |
| 440 | case PTS_S_WAIT_RESP_PCK: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 441 | byte = ch->pts.resp[_PCK]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 442 | /* update baud rate generator with Fi/Di */ |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 443 | emu_update_fidi(ch); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 444 | break; |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 445 | default: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 446 | TRACE_DEBUG("get_byte_pts() in invalid state %u\r\n", |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 447 | ch->pts.state); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* 2: Transmit the byte */ |
| 452 | card_emu_uart_tx(ch->uart_chan, byte); |
| 453 | |
| 454 | /* 3: Update the state */ |
| 455 | |
| 456 | switch (ch->pts.state) { |
| 457 | case PTS_S_WAIT_RESP_PCK: |
| 458 | /* Wait for the next TPDU */ |
| 459 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 460 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 461 | break; |
| 462 | default: |
| 463 | /* calculate the next state and set it */ |
| 464 | set_pts_state(ch, next_pts_state(ch)); |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 465 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 466 | } |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 467 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 468 | /* return number of bytes transmitted */ |
| 469 | return 1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | /********************************************************************** |
| 474 | * TPDU handling |
| 475 | **********************************************************************/ |
| 476 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 477 | |
| 478 | /* compute number of data bytes according to Chapter 10.3.2 of 7816-3 */ |
| 479 | static unsigned int t0_num_data_bytes(uint8_t p3, int reader_to_card) |
| 480 | { |
| 481 | if (reader_to_card) { |
| 482 | return p3; |
| 483 | } else { |
| 484 | if (p3 == 0) |
| 485 | return 256; |
| 486 | else |
| 487 | return p3; |
| 488 | } |
| 489 | } |
| 490 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 491 | /* add a just-received TPDU byte (from reader) to USB buffer */ |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 492 | static void add_tpdu_byte(struct card_handle *ch, uint8_t byte) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 493 | { |
| 494 | struct req_ctx *rctx; |
| 495 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 496 | 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] | 497 | |
| 498 | /* ensure we have a buffer */ |
| 499 | if (!ch->uart_rx_ctx) { |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 500 | rctx = ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY); |
| 501 | if (!ch->uart_rx_ctx) { |
Harald Welte | 40901a0 | 2016-03-03 10:42:45 +0100 | [diff] [blame] | 502 | TRACE_ERROR("Received UART byte but ENOMEM\r\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 503 | return; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 504 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 505 | rd = (struct cardemu_usb_msg_rx_data *) ch->uart_rx_ctx->data; |
| 506 | cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DO_RX_DATA); |
| 507 | rctx->tot_len = sizeof(*rd); |
| 508 | rctx->idx = 0; |
| 509 | } else |
| 510 | rctx = ch->uart_rx_ctx; |
| 511 | |
| 512 | rd = (struct cardemu_usb_msg_rx_data *) rctx->data; |
| 513 | |
| 514 | rd->data[rctx->idx++] = byte; |
| 515 | rctx->tot_len++; |
| 516 | |
| 517 | /* check if the buffer is full. If so, send it */ |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 518 | if (rctx->tot_len >= sizeof(*rd) + num_data_bytes) { |
| 519 | rd->flags |= CEMU_DATA_F_FINAL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 520 | flush_rx_buffer(ch); |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 521 | /* We need to transmit the SW now, */ |
| 522 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 523 | } else if (rctx->tot_len >= rctx->size) |
| 524 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts) |
| 528 | { |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 529 | if (ch->tpdu.state == new_ts) |
| 530 | return; |
| 531 | |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 532 | TRACE_DEBUG("7816 TPDU state %u -> %u\r\n", ch->tpdu.state, new_ts); |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 533 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 534 | switch (new_ts) { |
| 535 | case TPDU_S_WAIT_CLA: |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 536 | case TPDU_S_WAIT_RX: |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 537 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 538 | break; |
| 539 | case TPDU_S_WAIT_PB: |
| 540 | /* we just completed the TPDU header from reader to card |
| 541 | * and now need to disable the receiver, enable the |
| 542 | * transmitter and transmit the procedure byte */ |
| 543 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 544 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 545 | default: |
| 546 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 547 | } |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 548 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 549 | ch->tpdu.state = new_ts; |
| 550 | } |
| 551 | |
| 552 | static enum tpdu_state next_tpdu_state(struct card_handle *ch) |
| 553 | { |
| 554 | switch (ch->tpdu.state) { |
| 555 | case TPDU_S_WAIT_CLA: |
| 556 | return TPDU_S_WAIT_INS; |
| 557 | case TPDU_S_WAIT_INS: |
| 558 | return TPDU_S_WAIT_P1; |
| 559 | case TPDU_S_WAIT_P1: |
| 560 | return TPDU_S_WAIT_P2; |
| 561 | case TPDU_S_WAIT_P2: |
| 562 | return TPDU_S_WAIT_P3; |
| 563 | case TPDU_S_WAIT_P3: |
| 564 | return TPDU_S_WAIT_PB; |
| 565 | /* simply stay in Rx or Tx by default */ |
| 566 | case TPDU_S_WAIT_PB: |
| 567 | return TPDU_S_WAIT_PB; |
| 568 | case TPDU_S_WAIT_RX: |
| 569 | return TPDU_S_WAIT_RX; |
| 570 | case TPDU_S_WAIT_TX: |
| 571 | return TPDU_S_WAIT_TX; |
| 572 | } |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 573 | /* we should never reach here */ |
| 574 | assert(0); |
| 575 | return -1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void send_tpdu_header(struct card_handle *ch) |
| 579 | { |
| 580 | struct req_ctx *rctx; |
| 581 | struct cardemu_usb_msg_rx_data *rd; |
| 582 | |
Harald Welte | 43f7949 | 2016-02-29 10:06:54 +0100 | [diff] [blame] | 583 | TRACE_DEBUG("%s: %02x %02x %02x %02x %02x\r\n", __func__, |
| 584 | ch->tpdu.hdr[0], ch->tpdu.hdr[1], |
| 585 | ch->tpdu.hdr[2], ch->tpdu.hdr[3], |
| 586 | ch->tpdu.hdr[4]); |
| 587 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 588 | /* if we already/still have a context, send it off */ |
Harald Welte | f1697e2 | 2016-03-02 10:28:54 +0100 | [diff] [blame] | 589 | if (ch->uart_rx_ctx) { |
| 590 | TRACE_DEBUG("have old buffer\r\n"); |
| 591 | if (ch->uart_rx_ctx->idx) { |
| 592 | TRACE_DEBUG("flushing old buffer\r\n"); |
| 593 | flush_rx_buffer(ch); |
| 594 | } |
| 595 | } else { |
| 596 | TRACE_DEBUG("allocating new buffer\r\n"); |
| 597 | /* ensure we have a new buffer */ |
| 598 | ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY); |
| 599 | if (!ch->uart_rx_ctx) { |
| 600 | TRACE_ERROR("%s: ENOMEM\r\n", __func__); |
| 601 | return; |
| 602 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 603 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 604 | rctx = ch->uart_rx_ctx; |
| 605 | rd = (struct cardemu_usb_msg_rx_data *) rctx->data; |
| 606 | |
| 607 | /* initializ header */ |
| 608 | cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DO_RX_DATA); |
| 609 | rd->flags = CEMU_DATA_F_TPDU_HDR; |
| 610 | rctx->tot_len = sizeof(*rd) + sizeof(ch->tpdu.hdr); |
Harald Welte | 0ef96d5 | 2016-02-25 00:09:17 +0100 | [diff] [blame] | 611 | rctx->idx = sizeof(ch->tpdu.hdr); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 612 | |
| 613 | /* copy TPDU header to data field */ |
| 614 | memcpy(rd->data, ch->tpdu.hdr, sizeof(ch->tpdu.hdr)); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 615 | /* rd->data_len is set in flush_rx_buffer() */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 616 | |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 617 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static enum iso7816_3_card_state |
| 621 | process_byte_tpdu(struct card_handle *ch, uint8_t byte) |
| 622 | { |
| 623 | switch (ch->tpdu.state) { |
| 624 | case TPDU_S_WAIT_CLA: |
| 625 | ch->tpdu.hdr[_CLA] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 626 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 627 | break; |
| 628 | case TPDU_S_WAIT_INS: |
| 629 | ch->tpdu.hdr[_INS] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 630 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 631 | break; |
| 632 | case TPDU_S_WAIT_P1: |
| 633 | ch->tpdu.hdr[_P1] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 634 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 635 | break; |
| 636 | case TPDU_S_WAIT_P2: |
| 637 | ch->tpdu.hdr[_P2] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 638 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 639 | break; |
| 640 | case TPDU_S_WAIT_P3: |
| 641 | ch->tpdu.hdr[_P3] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 642 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 643 | /* FIXME: start timer to transmit further 0x60 */ |
| 644 | /* send the TPDU header as part of a procedure byte |
| 645 | * request to the USB host */ |
| 646 | send_tpdu_header(ch); |
| 647 | break; |
| 648 | case TPDU_S_WAIT_RX: |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 649 | add_tpdu_byte(ch, byte); |
| 650 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 651 | default: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 652 | TRACE_DEBUG("process_byte_tpdu() in invalid state %u\r\n", |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 653 | ch->tpdu.state); |
| 654 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 655 | |
| 656 | /* ensure we stay in TPDU ISO state */ |
| 657 | return ISO_S_IN_TPDU; |
| 658 | } |
| 659 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 660 | /* tx a single byte to be transmitted to the reader */ |
| 661 | static int tx_byte_tpdu(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 662 | { |
| 663 | struct req_ctx *rctx; |
| 664 | struct cardemu_usb_msg_tx_data *td; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 665 | uint8_t byte; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 666 | |
| 667 | /* ensure we are aware of any data that might be pending for |
| 668 | * transmit */ |
| 669 | if (!ch->uart_tx_ctx) { |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 670 | if (llist_empty(&ch->uart_tx_queue)) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 671 | return 0; |
| 672 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 673 | /* dequeue first at head */ |
| 674 | ch->uart_tx_ctx = llist_entry(ch->uart_tx_queue.next, |
| 675 | struct req_ctx, list); |
| 676 | llist_del(&ch->uart_tx_ctx->list); |
| 677 | req_ctx_set_state(ch->uart_tx_ctx, RCTX_S_UART_TX_BUSY); |
| 678 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 679 | /* start with index zero */ |
| 680 | ch->uart_tx_ctx->idx = 0; |
| 681 | |
| 682 | } |
| 683 | rctx = ch->uart_tx_ctx; |
| 684 | td = (struct cardemu_usb_msg_tx_data *) rctx->data; |
| 685 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 686 | /* take the next pending byte out of the rctx */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 687 | byte = td->data[rctx->idx++]; |
| 688 | |
| 689 | card_emu_uart_tx(ch->uart_chan, byte); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 690 | |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 691 | /* this must happen _after_ the byte has been transmittd */ |
| 692 | switch (ch->tpdu.state) { |
| 693 | case TPDU_S_WAIT_PB: |
| 694 | /* if we just transmitted the procedure byte, we need to decide |
| 695 | * if we want to continue to receive or transmit */ |
| 696 | if (td->flags & CEMU_DATA_F_PB_AND_TX) |
| 697 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
| 698 | else if (td->flags & CEMU_DATA_F_PB_AND_RX) |
| 699 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
| 700 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 701 | default: |
| 702 | break; |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 705 | /* check if the buffer has now been fully transmitted */ |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame^] | 706 | if ((rctx->idx >= td->data_len) || |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 707 | (td->data + rctx->idx >= rctx->data + rctx->tot_len)) { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 708 | if (td->flags & CEMU_DATA_F_PB_AND_RX) { |
| 709 | /* we have just sent the procedure byte and now |
| 710 | * need to continue receiving */ |
| 711 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 712 | } else { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 713 | /* we have transmitted all bytes */ |
| 714 | if (td->flags & CEMU_DATA_F_FINAL) { |
| 715 | /* this was the final part of the APDU, go |
| 716 | * back to state one*/ |
| 717 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 718 | } else { |
| 719 | /* FIXME: call into USB code to chec if we need |
| 720 | * to submit a free buffer to accept |
| 721 | * further data on bulk out endpoint */ |
| 722 | } |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 723 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 724 | req_ctx_set_state(rctx, RCTX_S_FREE); |
| 725 | ch->uart_tx_ctx = NULL; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | return 1; |
| 729 | } |
| 730 | |
| 731 | /********************************************************************** |
| 732 | * Public API |
| 733 | **********************************************************************/ |
| 734 | |
| 735 | /* process a single byte received from the reader */ |
| 736 | void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte) |
| 737 | { |
| 738 | int new_state = -1; |
| 739 | |
| 740 | ch->stats.rx_bytes++; |
| 741 | |
| 742 | switch (ch->state) { |
| 743 | case ISO_S_WAIT_POWER: |
| 744 | case ISO_S_WAIT_CLK: |
| 745 | case ISO_S_WAIT_RST: |
| 746 | case ISO_S_WAIT_ATR: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 747 | TRACE_DEBUG("Received UART char in 7816 state %u\r\n", |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 748 | ch->state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 749 | /* we shouldn't receive any data from the reader yet! */ |
| 750 | break; |
| 751 | case ISO_S_WAIT_TPDU: |
| 752 | if (byte == 0xff) { |
| 753 | new_state = process_byte_pts(ch, byte); |
| 754 | ch->stats.pps++; |
| 755 | goto out_silent; |
| 756 | } |
| 757 | /* fall-through */ |
| 758 | case ISO_S_IN_TPDU: |
| 759 | new_state = process_byte_tpdu(ch, byte); |
| 760 | break; |
| 761 | case ISO_S_IN_PTS: |
| 762 | new_state = process_byte_pts(ch, byte); |
| 763 | goto out_silent; |
| 764 | } |
| 765 | |
| 766 | out_silent: |
| 767 | if (new_state != -1) |
| 768 | card_set_state(ch, new_state); |
| 769 | } |
| 770 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 771 | /* transmit a single byte to the reader */ |
| 772 | int card_emu_tx_byte(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 773 | { |
| 774 | int rc = 0; |
| 775 | |
| 776 | switch (ch->state) { |
| 777 | case ISO_S_IN_ATR: |
| 778 | if (ch->atr.idx < ch->atr.len) { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 779 | uint8_t byte; |
| 780 | byte = ch->atr.atr[ch->atr.idx++]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 781 | rc = 1; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 782 | |
| 783 | card_emu_uart_tx(ch->uart_chan, byte); |
| 784 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 785 | /* detect end of ATR */ |
| 786 | if (ch->atr.idx >= ch->atr.len) |
| 787 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 788 | } |
| 789 | break; |
| 790 | case ISO_S_IN_PTS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 791 | rc = tx_byte_pts(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 792 | break; |
| 793 | case ISO_S_IN_TPDU: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 794 | rc = tx_byte_tpdu(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 795 | break; |
Harald Welte | ad43440 | 2016-03-02 11:18:29 +0100 | [diff] [blame] | 796 | default: |
| 797 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | if (rc) |
| 801 | ch->stats.tx_bytes++; |
| 802 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 803 | /* if we return 0 here, the UART needs to disable transmit-ready |
| 804 | * interrupts */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 805 | return rc; |
| 806 | } |
| 807 | |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 808 | void card_emu_have_new_uart_tx(struct card_handle *ch) |
| 809 | { |
| 810 | switch (ch->state) { |
| 811 | case ISO_S_IN_TPDU: |
| 812 | switch (ch->tpdu.state) { |
| 813 | case TPDU_S_WAIT_TX: |
| 814 | case TPDU_S_WAIT_PB: |
| 815 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 816 | break; |
| 817 | default: |
| 818 | break; |
| 819 | } |
| 820 | default: |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 825 | /* hardware driver informs us that a card I/O signal has changed */ |
| 826 | void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active) |
| 827 | { |
| 828 | switch (io) { |
| 829 | case CARD_IO_VCC: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 830 | if (active == 0 && ch->vcc_active == 1) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 831 | TRACE_DEBUG("VCC deactivated\r\n"); |
Harald Welte | 22cdf2a | 2016-02-24 22:18:11 +0100 | [diff] [blame] | 832 | tc_etu_disable(ch->tc_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 833 | card_set_state(ch, ISO_S_WAIT_POWER); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 834 | } else if (active == 1 && ch->vcc_active == 0) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 835 | TRACE_DEBUG("VCC activated\r\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 836 | card_set_state(ch, ISO_S_WAIT_CLK); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 837 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 838 | ch->vcc_active = active; |
| 839 | break; |
| 840 | case CARD_IO_CLK: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 841 | if (active == 1 && ch->clocked == 0) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 842 | TRACE_DEBUG("CLK activated\r\n"); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 843 | if (ch->state == ISO_S_WAIT_CLK) |
| 844 | card_set_state(ch, ISO_S_WAIT_RST); |
| 845 | } else if (active == 0 && ch->clocked == 1) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 846 | TRACE_DEBUG("CLK deactivated\r\n"); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 847 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 848 | ch->clocked = active; |
| 849 | break; |
| 850 | case CARD_IO_RST: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 851 | if (active == 0 && ch->in_reset) { |
Harald Welte | 40901a0 | 2016-03-03 10:42:45 +0100 | [diff] [blame] | 852 | TRACE_INFO("RST released\r\n"); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 853 | if (ch->vcc_active && ch->clocked) { |
| 854 | /* enable the TC/ETU counter once reset has been released */ |
| 855 | tc_etu_enable(ch->tc_chan); |
| 856 | card_set_state(ch, ISO_S_WAIT_ATR); |
| 857 | /* FIXME: wait 400 to 40k clock cycles before sending ATR */ |
| 858 | card_set_state(ch, ISO_S_IN_ATR); |
| 859 | } |
| 860 | } else if (active && !ch->in_reset) { |
Harald Welte | 40901a0 | 2016-03-03 10:42:45 +0100 | [diff] [blame] | 861 | TRACE_INFO("RST asserted\r\n"); |
Harald Welte | 22cdf2a | 2016-02-24 22:18:11 +0100 | [diff] [blame] | 862 | tc_etu_disable(ch->tc_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 863 | } |
| 864 | ch->in_reset = active; |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | /* User sets a new ATR to be returned during next card reset */ |
| 870 | int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len) |
| 871 | { |
| 872 | if (len > sizeof(ch->atr.atr)) |
| 873 | return -1; |
| 874 | |
| 875 | memcpy(ch->atr.atr, atr, len); |
| 876 | ch->atr.len = len; |
| 877 | ch->atr.idx = 0; |
| 878 | |
| 879 | /* FIXME: race condition with trasmitting ATR to reader? */ |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | /* hardware driver informs us that one (more) ETU has expired */ |
| 885 | void tc_etu_wtime_half_expired(void *handle) |
| 886 | { |
| 887 | struct card_handle *ch = handle; |
| 888 | /* transmit NULL procedure byte well before waiting time expires */ |
Harald Welte | dda7355 | 2016-03-02 10:29:55 +0100 | [diff] [blame] | 889 | switch (ch->state) { |
| 890 | case ISO_S_IN_TPDU: |
| 891 | switch (ch->tpdu.state) { |
| 892 | case TPDU_S_WAIT_PB: |
| 893 | case TPDU_S_WAIT_TX: |
| 894 | putchar('N'); |
| 895 | card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL); |
| 896 | break; |
| 897 | default: |
| 898 | break; |
| 899 | } |
| 900 | break; |
| 901 | default: |
| 902 | break; |
| 903 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /* hardware driver informs us that one (more) ETU has expired */ |
| 907 | void tc_etu_wtime_expired(void *handle) |
| 908 | { |
Harald Welte | 40901a0 | 2016-03-03 10:42:45 +0100 | [diff] [blame] | 909 | TRACE_ERROR("wtime_exp\r\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* shortest ATR found in smartcard_list.txt */ |
| 913 | static const uint8_t default_atr[] = { 0x3B, 0x02, 0x14, 0x50 }; |
| 914 | |
| 915 | static struct card_handle card_handles[NUM_SLOTS]; |
| 916 | |
| 917 | struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan) |
| 918 | { |
| 919 | struct card_handle *ch; |
| 920 | |
| 921 | if (slot_num >= ARRAY_SIZE(card_handles)) |
| 922 | return NULL; |
| 923 | |
| 924 | ch = &card_handles[slot_num]; |
| 925 | |
| 926 | memset(ch, 0, sizeof(*ch)); |
| 927 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 928 | INIT_LLIST_HEAD(&ch->usb_tx_queue); |
| 929 | INIT_LLIST_HEAD(&ch->uart_tx_queue); |
| 930 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 931 | /* initialize the card_handle with reasonabe defaults */ |
| 932 | ch->state = ISO_S_WAIT_POWER; |
| 933 | ch->vcc_active = 0; |
| 934 | ch->in_reset = 1; |
| 935 | ch->clocked = 0; |
| 936 | |
| 937 | ch->fi = 0; |
| 938 | ch->di = 1; |
| 939 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 940 | |
| 941 | ch->tc_chan = tc_chan; |
| 942 | ch->uart_chan = uart_chan; |
| 943 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 944 | |
| 945 | ch->atr.idx = 0; |
| 946 | ch->atr.len = sizeof(default_atr); |
| 947 | memcpy(ch->atr.atr, default_atr, ch->atr.len); |
| 948 | |
| 949 | ch->pts.state = PTS_S_WAIT_REQ_PTSS; |
| 950 | ch->tpdu.state = TPDU_S_WAIT_CLA; |
| 951 | |
| 952 | tc_etu_init(ch->tc_chan, ch); |
| 953 | |
| 954 | return ch; |
| 955 | } |