Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include "card_emu.h" |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 8 | #include "simtrace_prot.h" |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 9 | #include "tc_etu.h" |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 10 | #include "usb_buf.h" |
| 11 | |
| 12 | #define PHONE_DATAIN 1 |
| 13 | #define PHONE_INT 2 |
| 14 | #define PHONE_DATAOUT 3 |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 15 | |
Harald Welte | d401b12 | 2019-10-11 10:35:33 +0200 | [diff] [blame] | 16 | /*********************************************************************** |
| 17 | * stub functions required by card_emu.c |
| 18 | ***********************************************************************/ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 19 | |
Harald Welte | a1cd0f3 | 2017-02-27 14:29:07 +0100 | [diff] [blame] | 20 | void card_emu_uart_wait_tx_idle(uint8_t uart_chan) |
| 21 | { |
| 22 | } |
| 23 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 24 | int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi) |
| 25 | { |
| 26 | printf("uart_update_fidi(uart_chan=%u, fidi=%u)\n", uart_chan, fidi); |
| 27 | return 0; |
| 28 | } |
| 29 | |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 30 | /* a buffer in which we store those bytes send by the UART towards the card |
| 31 | * reader, so we can verify in test cases what was actually written */ |
| 32 | static uint8_t tx_debug_buf[1024]; |
| 33 | static unsigned int tx_debug_buf_idx; |
| 34 | |
Harald Welte | d401b12 | 2019-10-11 10:35:33 +0200 | [diff] [blame] | 35 | /* the card emulator wants to send some data to the host [reader] */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 36 | int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte) |
| 37 | { |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 38 | printf("UART_TX(%02x)\n", byte); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 39 | tx_debug_buf[tx_debug_buf_idx++] = byte; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx) |
| 44 | { |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 45 | char *rts; |
| 46 | switch (rxtx) { |
| 47 | case 0: |
| 48 | rts = "OFF"; |
| 49 | break; |
| 50 | case ENABLE_TX: |
| 51 | rts = "TX"; |
| 52 | break; |
| 53 | case ENABLE_RX: |
| 54 | rts = "RX"; |
| 55 | break; |
| 56 | default: |
| 57 | rts = "unknown"; |
| 58 | break; |
| 59 | }; |
| 60 | |
| 61 | printf("uart_enable(uart_chan=%u, %s)\n", uart_chan, rts); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 64 | void card_emu_uart_interrupt(uint8_t uart_chan) |
| 65 | { |
| 66 | printf("uart_interrupt(uart_chan=%u)\n", uart_chan); |
| 67 | } |
| 68 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 69 | void tc_etu_set_wtime(uint8_t tc_chan, uint16_t wtime) |
| 70 | { |
| 71 | printf("tc_etu_set_wtime(tc_chan=%u, wtime=%u)\n", tc_chan, wtime); |
| 72 | } |
| 73 | |
| 74 | void tc_etu_set_etu(uint8_t tc_chan, uint16_t etu) |
| 75 | { |
| 76 | printf("tc_etu_set_etu(tc_chan=%u, etu=%u)\n", tc_chan, etu); |
| 77 | } |
| 78 | |
| 79 | void tc_etu_init(uint8_t chan_nr, void *handle) |
| 80 | { |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 81 | printf("tc_etu_init(tc_chan=%u)\n", chan_nr); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 84 | void tc_etu_enable(uint8_t chan_nr) |
| 85 | { |
| 86 | printf("tc_etu_enable(tc_chan=%u)\n", chan_nr); |
| 87 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 88 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 89 | void tc_etu_disable(uint8_t chan_nr) |
| 90 | { |
| 91 | printf("tc_etu_disable(tc_chan=%u)\n", chan_nr); |
| 92 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 93 | |
Harald Welte | d401b12 | 2019-10-11 10:35:33 +0200 | [diff] [blame] | 94 | |
| 95 | |
| 96 | /*********************************************************************** |
| 97 | * test helper functions |
| 98 | ***********************************************************************/ |
| 99 | |
| 100 | |
| 101 | static void reader_check_and_clear(const uint8_t *data, unsigned int len) |
| 102 | { |
| 103 | assert(len == tx_debug_buf_idx); |
| 104 | assert(!memcmp(tx_debug_buf, data, len)); |
| 105 | tx_debug_buf_idx = 0; |
| 106 | } |
| 107 | |
| 108 | static const uint8_t atr[] = { 0x3b, 0x02, 0x14, 0x50 }; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 109 | |
| 110 | static int verify_atr(struct card_handle *ch) |
| 111 | { |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 112 | unsigned int i; |
| 113 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 114 | printf("receiving + verifying ATR:\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 115 | for (i = 0; i < sizeof(atr); i++) { |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 116 | assert(card_emu_tx_byte(ch) == 1); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 117 | } |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 118 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 119 | reader_check_and_clear(atr, sizeof(atr)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 120 | |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | static void io_start_card(struct card_handle *ch) |
| 125 | { |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 126 | card_emu_set_atr(ch, atr, sizeof(atr)); |
| 127 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 128 | /* bring the card up from the dead */ |
| 129 | card_emu_io_statechg(ch, CARD_IO_VCC, 1); |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 130 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 131 | card_emu_io_statechg(ch, CARD_IO_CLK, 1); |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 132 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 133 | card_emu_io_statechg(ch, CARD_IO_RST, 1); |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 134 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 135 | |
| 136 | /* release from reset and verify th ATR */ |
| 137 | card_emu_io_statechg(ch, CARD_IO_RST, 0); |
Kévin Redon | ebe672e | 2018-07-29 00:18:12 +0200 | [diff] [blame] | 138 | /* simulate waiting time before ATR expired */ |
| 139 | tc_etu_wtime_expired(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 140 | verify_atr(ch); |
| 141 | } |
| 142 | |
Harald Welte | d401b12 | 2019-10-11 10:35:33 +0200 | [diff] [blame] | 143 | /* emulate the host/reader sending some bytes to the [emulated] card */ |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 144 | static void reader_send_bytes(struct card_handle *ch, const uint8_t *bytes, unsigned int len) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 145 | { |
| 146 | unsigned int i; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 147 | for (i = 0; i < len; i++) { |
| 148 | printf("UART_RX(%02x)\n", bytes[i]); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 149 | card_emu_process_rx_byte(ch, bytes[i]); |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 150 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 153 | static void dump_rctx(struct msgb *msg) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 154 | { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 155 | struct simtrace_msg_hdr *mh = (struct simtrace_msg_hdr *) msg->l1h; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 156 | struct cardemu_usb_msg_rx_data *rxd; |
| 157 | int i; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 158 | #if 0 |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 159 | |
| 160 | printf("req_ctx(%p): state=%u, size=%u, tot_len=%u, idx=%u, data=%p\n", |
| 161 | rctx, rctx->state, rctx->size, rctx->tot_len, rctx->idx, rctx->data); |
Harald Welte | a1cd0f3 | 2017-02-27 14:29:07 +0100 | [diff] [blame] | 162 | printf(" msg_type=%u, seq_nr=%u, msg_len=%u\n", |
| 163 | mh->msg_type, mh->seq_nr, mh->msg_len); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 164 | #endif |
| 165 | printf("%s\n", msgb_hexdump(msg)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 166 | |
| 167 | switch (mh->msg_type) { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 168 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
| 169 | rxd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 170 | printf(" flags=%x, data=", rxd->flags); |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 171 | for (i = 0; i < rxd->data_len; i++) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 172 | printf(" %02x", rxd->data[i]); |
| 173 | printf("\n"); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 178 | static void get_and_verify_rctx(uint8_t ep, const uint8_t *data, unsigned int len) |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 179 | { |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 180 | struct usb_buffered_ep *bep = usb_get_buf_ep(ep); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 181 | struct msgb *msg; |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 182 | struct cardemu_usb_msg_tx_data *td; |
| 183 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 184 | struct simtrace_msg_hdr *mh; |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 185 | |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 186 | assert(bep); |
| 187 | msg = msgb_dequeue_count(&bep->queue, &bep->queue_len); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 188 | assert(msg); |
| 189 | dump_rctx(msg); |
| 190 | assert(msg->l1h); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 191 | mh = (struct simtrace_msg_hdr *) msg->l1h; |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 192 | |
| 193 | /* verify the contents of the rctx */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 194 | switch (mh->msg_type) { |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 195 | case SIMTRACE_MSGT_DO_CEMU_RX_DATA: |
| 196 | rd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 197 | assert(rd->data_len == len); |
| 198 | assert(!memcmp(rd->data, data, len)); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 199 | break; |
| 200 | #if 0 |
| 201 | case RCTX_S_UART_RX_PENDING: |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 202 | rd = (struct cardemu_usb_msg_rx_data *) msg->l2h; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 203 | assert(rd->data_len == len); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 204 | assert(!memcmp(rd->data, data, len)); |
| 205 | break; |
| 206 | #endif |
| 207 | default: |
| 208 | assert(0); |
| 209 | } |
| 210 | |
| 211 | /* free the req_ctx, indicating it has fully arrived on the host */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 212 | usb_buf_free(msg); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 215 | static void get_and_verify_rctx_pps(const uint8_t *data, unsigned int len) |
| 216 | { |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 217 | struct usb_buffered_ep *bep = usb_get_buf_ep(PHONE_DATAIN); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 218 | struct msgb *msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 219 | struct simtrace_msg_hdr *mh; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 220 | struct cardemu_usb_msg_pts_info *ptsi; |
| 221 | |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 222 | assert(bep); |
| 223 | msg = msgb_dequeue_count(&bep->queue, &bep->queue_len); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 224 | assert(msg); |
| 225 | dump_rctx(msg); |
| 226 | assert(msg->l1h); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 227 | mh = (struct simtrace_msg_hdr *) msg->l1h; |
| 228 | ptsi = (struct cardemu_usb_msg_pts_info *) msg->l2h; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 229 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 230 | /* FIXME: verify */ |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 231 | assert(mh->msg_type == SIMTRACE_MSGT_DO_CEMU_PTS); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 232 | assert(!memcmp(ptsi->req, data, len)); |
| 233 | assert(!memcmp(ptsi->resp, data, len)); |
| 234 | |
| 235 | /* free the req_ctx, indicating it has fully arrived on the host */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 236 | usb_buf_free(msg); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 239 | /* emulate a TPDU header being sent by the reader/phone */ |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 240 | static void rdr_send_tpdu_hdr(struct card_handle *ch, const uint8_t *tpdu_hdr) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 241 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 242 | struct llist_head *queue = usb_get_queue(PHONE_DATAIN); |
| 243 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 244 | /* we don't want a receive context to become available during |
| 245 | * the first four bytes */ |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 246 | reader_send_bytes(ch, tpdu_hdr, 4); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 247 | assert(llist_empty(queue)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 248 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 249 | reader_send_bytes(ch, tpdu_hdr+4, 1); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 250 | /* but then after the final byte of the TPDU header, we want a |
| 251 | * receive context to be available for USB transmission */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 252 | get_and_verify_rctx(PHONE_DATAIN, tpdu_hdr, 5); |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 255 | /* emulate a SIMTRACE_MSGT_DT_CEMU_TX_DATA received from USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 256 | static void host_to_device_data(struct card_handle *ch, const uint8_t *data, uint16_t len, |
| 257 | unsigned int flags) |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 258 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 259 | struct msgb *msg; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 260 | struct simtrace_msg_hdr *mh; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 261 | struct cardemu_usb_msg_tx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 262 | struct llist_head *queue; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 263 | |
| 264 | /* allocate a free req_ctx */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 265 | msg = usb_buf_alloc(PHONE_DATAOUT); |
| 266 | assert(msg); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 267 | /* initialize the common header */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 268 | msg->l1h = msg->head; |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 269 | mh = (struct simtrace_msg_hdr *) msgb_put(msg, sizeof(*mh)); |
| 270 | mh->msg_class = SIMTRACE_MSGC_CARDEM; |
| 271 | mh->msg_type = SIMTRACE_MSGT_DT_CEMU_TX_DATA; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 272 | |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 273 | /* initialize the tx_data message */ |
| 274 | msg->l2h = msgb_put(msg, sizeof(*rd) + len); |
| 275 | rd = (struct cardemu_usb_msg_tx_data *) msg->l2h; |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 276 | rd->flags = flags; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 277 | /* copy data and set length */ |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 278 | rd->data_len = len; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 279 | memcpy(rd->data, data, len); |
Harald Welte | 25a9a80 | 2017-05-08 13:30:09 +0200 | [diff] [blame] | 280 | |
| 281 | mh->msg_len = sizeof(*mh) + sizeof(*rd) + len; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 282 | |
| 283 | /* hand the req_ctx to the UART transmit code */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 284 | queue = card_emu_get_uart_tx_queue(ch); |
| 285 | assert(queue); |
| 286 | msgb_enqueue(queue, msg); |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 289 | /* card-transmit any pending characters */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 290 | static int card_tx_verify_chars(struct card_handle *ch, const uint8_t *data, unsigned int data_len) |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 291 | { |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 292 | int count = 0; |
| 293 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 294 | while (card_emu_tx_byte(ch)) { |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 295 | count++; |
| 296 | } |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 297 | |
| 298 | assert(count == data_len); |
| 299 | reader_check_and_clear(data, data_len); |
| 300 | |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 301 | return count; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 304 | const uint8_t tpdu_hdr_sel_mf[] = { 0xA0, 0xA4, 0x00, 0x00, 0x00 }; |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 305 | const uint8_t tpdu_pb_sw[] = { 0x90, 0x00 }; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 306 | |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 307 | static void |
| 308 | test_tpdu_reader2card(struct card_handle *ch, const uint8_t *hdr, const uint8_t *body, uint8_t body_len) |
| 309 | { |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 310 | printf("\n==> transmitting APDU (HDR + PB + card-RX)\n"); |
| 311 | |
| 312 | /* emulate the reader sending a TPDU header */ |
| 313 | rdr_send_tpdu_hdr(ch, hdr); |
| 314 | /* we shouldn't have any pending card-TX yet */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 315 | card_tx_verify_chars(ch, NULL, 0); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 316 | |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 317 | /* card emulator PC sends a singly byte PB response via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 318 | host_to_device_data(ch, hdr+1, 1, CEMU_DATA_F_FINAL | CEMU_DATA_F_PB_AND_RX); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 319 | /* card actually sends that single PB */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 320 | card_tx_verify_chars(ch, hdr+1, 1); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 321 | |
| 322 | /* emulate more characters from reader to card */ |
| 323 | reader_send_bytes(ch, body, body_len); |
| 324 | |
| 325 | /* check if we have received them on the USB side */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 326 | get_and_verify_rctx(PHONE_DATAIN, body, body_len); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 327 | |
| 328 | /* ensure there is no extra data received on usb */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 329 | assert(llist_empty(usb_get_queue(PHONE_DATAOUT))); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 330 | |
| 331 | /* card emulator sends SW via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 332 | host_to_device_data(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw), |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 333 | CEMU_DATA_F_FINAL | CEMU_DATA_F_PB_AND_TX); |
| 334 | /* obtain any pending tx chars */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 335 | card_tx_verify_chars(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw)); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 336 | |
| 337 | /* simulate some clock stop */ |
| 338 | card_emu_io_statechg(ch, CARD_IO_CLK, 0); |
| 339 | card_emu_io_statechg(ch, CARD_IO_CLK, 1); |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | test_tpdu_card2reader(struct card_handle *ch, const uint8_t *hdr, const uint8_t *body, uint8_t body_len) |
| 344 | { |
| 345 | printf("\n==> transmitting APDU (HDR + PB + card-TX)\n"); |
| 346 | |
| 347 | /* emulate the reader sending a TPDU header */ |
| 348 | rdr_send_tpdu_hdr(ch, hdr); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 349 | card_tx_verify_chars(ch, NULL, 0); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 350 | |
| 351 | /* card emulator PC sends a response PB via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 352 | host_to_device_data(ch, hdr+1, 1, CEMU_DATA_F_PB_AND_TX); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 353 | |
| 354 | /* card actually sends that PB */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 355 | card_tx_verify_chars(ch, hdr+1, 1); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 356 | |
| 357 | /* emulate more characters from card to reader */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 358 | host_to_device_data(ch, body, body_len, 0); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 359 | /* obtain those bytes as they arrvive on the card */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 360 | card_tx_verify_chars(ch, body, body_len); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 361 | |
| 362 | /* ensure there is no extra data received on usb */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 363 | assert(llist_empty(usb_get_queue(PHONE_DATAOUT))); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 364 | |
| 365 | /* card emulator sends SW via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 366 | host_to_device_data(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw), CEMU_DATA_F_FINAL); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 367 | |
| 368 | /* obtain any pending tx chars */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 369 | card_tx_verify_chars(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw)); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 370 | |
| 371 | /* simulate some clock stop */ |
| 372 | card_emu_io_statechg(ch, CARD_IO_CLK, 0); |
| 373 | card_emu_io_statechg(ch, CARD_IO_CLK, 1); |
| 374 | } |
| 375 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 376 | const uint8_t pps[] = { |
| 377 | /* PPSS identifies the PPS request or response and is set to |
| 378 | * 'FF'. */ |
| 379 | 0xFF, // PPSS |
| 380 | /* In PPS0, each bit 5, 6 or 7 set to 1 indicates the presence |
| 381 | * of an optional byte PPS 1 , PPS 2 , PPS 3 , |
| 382 | * respectively. Bits 4 to 1 encode a type T to propose a |
| 383 | * transmission protocol. Bit 8 is reserved for future |
| 384 | * use and shall be set to 0. */ |
| 385 | 0b00010000, // PPS0: PPS1 present |
| 386 | 0x00, // PPS1 proposed Fi/Di value |
| 387 | 0xFF ^ 0b00010000// PCK |
| 388 | }; |
| 389 | |
| 390 | static void |
| 391 | test_ppss(struct card_handle *ch) |
| 392 | { |
| 393 | reader_send_bytes(ch, pps, sizeof(pps)); |
| 394 | get_and_verify_rctx_pps(pps, sizeof(pps)); |
| 395 | card_tx_verify_chars(ch, pps, sizeof(pps)); |
| 396 | } |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 397 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 398 | /* READ RECORD (offset 0, 10 bytes) */ |
| 399 | const uint8_t tpdu_hdr_read_rec[] = { 0xA0, 0xB2, 0x00, 0x00, 0x0A }; |
| 400 | const uint8_t tpdu_body_read_rec[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; |
| 401 | |
| 402 | /* WRITE RECORD */ |
| 403 | const uint8_t tpdu_hdr_write_rec[] = { 0xA0, 0xD2, 0x00, 0x00, 0x07 }; |
| 404 | const uint8_t tpdu_body_write_rec[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 405 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 406 | int main(int argc, char **argv) |
| 407 | { |
| 408 | struct card_handle *ch; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 409 | unsigned int i; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 410 | |
Kévin Redon | 7233cf8 | 2019-11-14 19:37:32 +0100 | [diff] [blame] | 411 | ch = card_emu_init(0, 23, 42, PHONE_DATAIN, PHONE_INT, false, true, false); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 412 | assert(ch); |
| 413 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 414 | usb_buf_init(); |
| 415 | |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 416 | /* start up the card (VCC/RST, ATR) */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 417 | io_start_card(ch); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 418 | card_tx_verify_chars(ch, NULL, 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 419 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 420 | test_ppss(ch); |
| 421 | |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 422 | for (i = 0; i < 2; i++) { |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 423 | test_tpdu_reader2card(ch, tpdu_hdr_write_rec, tpdu_body_write_rec, sizeof(tpdu_body_write_rec)); |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 424 | |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 425 | test_tpdu_card2reader(ch, tpdu_hdr_read_rec, tpdu_body_read_rec, sizeof(tpdu_body_read_rec)); |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 426 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 427 | |
| 428 | exit(0); |
| 429 | } |