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