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