ccid.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "seader_i.h"
  2. #define TAG "SeaderCCID"
  3. bool hasSAM = false;
  4. const uint8_t SAM_ATR[] =
  5. {0x3b, 0x95, 0x96, 0x80, 0xb1, 0xfe, 0x55, 0x1f, 0xc7, 0x47, 0x72, 0x61, 0x63, 0x65, 0x13};
  6. const uint8_t SAM_ATR2[] = {0x3b, 0x90, 0x96, 0x91, 0x81, 0xb1, 0xfe, 0x55, 0x1f, 0xc7, 0xd4};
  7. uint8_t slot = 0;
  8. uint8_t sequence = 0;
  9. uint8_t retries = 3;
  10. uint8_t getSequence() {
  11. if(sequence > 254) {
  12. sequence = 0;
  13. }
  14. return sequence++;
  15. }
  16. size_t addLRC(uint8_t* data, size_t len) {
  17. uint8_t lrc = 0;
  18. for(size_t i = 0; i < len; i++) {
  19. lrc ^= data[i];
  20. }
  21. data[len] = lrc;
  22. return len + 1;
  23. }
  24. void PC_to_RDR_IccPowerOn(SeaderUartBridge* seader_uart) {
  25. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  26. seader_uart->tx_buf[0] = SYNC;
  27. seader_uart->tx_buf[1] = CTRL;
  28. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn;
  29. seader_uart->tx_buf[2 + 5] = slot;
  30. seader_uart->tx_buf[2 + 6] = getSequence();
  31. seader_uart->tx_buf[2 + 7] = 2; //power
  32. seader_uart->tx_len = addLRC(seader_uart->tx_buf, 2 + 10);
  33. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  34. }
  35. void PC_to_RDR_GetSlotStatus(SeaderUartBridge* seader_uart) {
  36. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  37. seader_uart->tx_buf[0] = SYNC;
  38. seader_uart->tx_buf[1] = CTRL;
  39. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus;
  40. seader_uart->tx_buf[2 + 5] = slot;
  41. seader_uart->tx_buf[2 + 6] = getSequence();
  42. seader_uart->tx_len = addLRC(seader_uart->tx_buf, 2 + 10);
  43. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  44. }
  45. void PC_to_RDR_SetParameters(SeaderUartBridge* seader_uart) {
  46. uint8_t T1 = 1;
  47. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  48. seader_uart->tx_buf[0] = SYNC;
  49. seader_uart->tx_buf[1] = CTRL;
  50. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters;
  51. seader_uart->tx_buf[2 + 1] = 0;
  52. seader_uart->tx_buf[2 + 5] = slot;
  53. seader_uart->tx_buf[2 + 6] = getSequence();
  54. seader_uart->tx_buf[2 + 7] = T1;
  55. seader_uart->tx_buf[2 + 8] = 0;
  56. seader_uart->tx_buf[2 + 9] = 0;
  57. seader_uart->tx_len = addLRC(seader_uart->tx_buf, 2 + 10);
  58. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  59. }
  60. void PC_to_RDR_GetParameters(SeaderUartBridge* seader_uart) {
  61. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  62. seader_uart->tx_buf[0] = SYNC;
  63. seader_uart->tx_buf[1] = CTRL;
  64. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters;
  65. seader_uart->tx_buf[2 + 1] = 0;
  66. seader_uart->tx_buf[2 + 5] = slot;
  67. seader_uart->tx_buf[2 + 6] = getSequence();
  68. seader_uart->tx_buf[2 + 7] = 0;
  69. seader_uart->tx_buf[2 + 8] = 0;
  70. seader_uart->tx_buf[2 + 9] = 0;
  71. seader_uart->tx_len = addLRC(seader_uart->tx_buf, 2 + 10);
  72. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  73. }
  74. void PC_to_RDR_XfrBlock(SeaderUartBridge* seader_uart, uint8_t* data, size_t len) {
  75. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  76. seader_uart->tx_buf[0] = SYNC;
  77. seader_uart->tx_buf[1] = CTRL;
  78. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock;
  79. seader_uart->tx_buf[2 + 1] = len;
  80. seader_uart->tx_buf[2 + 5] = slot;
  81. seader_uart->tx_buf[2 + 6] = getSequence();
  82. seader_uart->tx_buf[2 + 7] = 5;
  83. seader_uart->tx_buf[2 + 8] = 0;
  84. seader_uart->tx_buf[2 + 9] = 0;
  85. memcpy(seader_uart->tx_buf + 2 + 10, data, len);
  86. seader_uart->tx_len = addLRC(seader_uart->tx_buf, 2 + 10 + len);
  87. // FURI_LOG_I(TAG, "PC_to_RDR_XfrBlock %d bytes", seader_uart->tx_len);
  88. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  89. }
  90. size_t processCCID(SeaderWorker* seader_worker, uint8_t* cmd, size_t cmd_len) {
  91. SeaderUartBridge* seader_uart = seader_worker->uart;
  92. CCID_Message message;
  93. message.consumed = 0;
  94. char display[SEADER_UART_RX_BUF_SIZE * 2 + 1] = {0};
  95. for(uint8_t i = 0; i < cmd_len; i++) {
  96. snprintf(display + (i * 2), sizeof(display), "%02x", cmd[i]);
  97. }
  98. FURI_LOG_D(TAG, "CCID %d: %s", cmd_len, display);
  99. if(cmd_len == 2) {
  100. if(cmd[0] == CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange) {
  101. switch(cmd[1]) {
  102. case CARD_OUT:
  103. FURI_LOG_D(TAG, "Card removed");
  104. break;
  105. case CARD_IN_1:
  106. FURI_LOG_D(TAG, "Card Inserted (1)");
  107. slot = 0;
  108. sequence = 0;
  109. FURI_LOG_D(TAG, "Sending Power On");
  110. PC_to_RDR_IccPowerOn(seader_uart);
  111. break;
  112. case CARD_IN_2:
  113. FURI_LOG_D(TAG, "Card Inserted (2)");
  114. slot = 1;
  115. sequence = 0;
  116. FURI_LOG_D(TAG, "Sending Power On");
  117. PC_to_RDR_IccPowerOn(seader_uart);
  118. break;
  119. case CARD_IN_BOTH:
  120. FURI_LOG_W(TAG, "Loading 2 cards not supported");
  121. break;
  122. };
  123. return 2;
  124. }
  125. }
  126. while(cmd_len >= 3 && cmd[0] == SYNC && cmd[1] == NAK) {
  127. // 031516
  128. FURI_LOG_W(TAG, "NAK");
  129. cmd += 3;
  130. cmd_len -= 3;
  131. message.consumed += 3;
  132. }
  133. while(cmd_len > 2 && (cmd[0] != SYNC || cmd[1] != CTRL)) {
  134. FURI_LOG_W(TAG, "invalid start");
  135. cmd += 1;
  136. cmd_len -= 1;
  137. message.consumed += 1;
  138. }
  139. if(cmd_len > 12 && cmd[0] == SYNC && cmd[1] == CTRL) {
  140. uint8_t* ccid = cmd + 2;
  141. message.bMessageType = ccid[0];
  142. message.dwLength = *((uint32_t*)(ccid + 1));
  143. message.bStatus = ccid[7];
  144. message.bError = ccid[8];
  145. message.payload = ccid + 10;
  146. if(cmd_len < 2 + 10 + message.dwLength + 1) {
  147. return message.consumed;
  148. }
  149. message.consumed += 2 + 10 + message.dwLength + 1;
  150. //0306 81 00000000 0000 0200 01 87
  151. //0306 81 00000000 0000 0100 01 84
  152. if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus) {
  153. uint8_t status = (message.bStatus & BMICCSTATUS_MASK);
  154. if(status == 0 || status == 1) {
  155. FURI_LOG_D(TAG, "Sending Power On");
  156. PC_to_RDR_IccPowerOn(seader_uart);
  157. return message.consumed;
  158. } else if(status == 2) {
  159. FURI_LOG_W(TAG, "No ICC is present [retries %d]", retries);
  160. if(retries-- > 1) {
  161. furi_delay_ms(100);
  162. PC_to_RDR_GetSlotStatus(seader_uart);
  163. } else {
  164. if(seader_worker->callback) {
  165. seader_worker->callback(
  166. SeaderWorkerEventSamMissing, seader_worker->context);
  167. }
  168. }
  169. return message.consumed;
  170. }
  171. }
  172. //0306 80 00000000 0001 42fe 00 38
  173. if(message.bStatus == 0x41 && message.bError == 0xfe) {
  174. FURI_LOG_W(TAG, "card probably upside down");
  175. if(seader_worker->callback) {
  176. seader_worker->callback(SeaderWorkerEventSamMissing, seader_worker->context);
  177. }
  178. return message.consumed;
  179. }
  180. if(message.bStatus == 0x42 && message.bError == 0xfe) {
  181. FURI_LOG_W(TAG, "No card");
  182. if(seader_worker->callback) {
  183. seader_worker->callback(SeaderWorkerEventSamMissing, seader_worker->context);
  184. }
  185. return message.consumed;
  186. }
  187. if(message.bError != 0) {
  188. FURI_LOG_W(TAG, "CCID error");
  189. message.consumed = cmd_len;
  190. if(seader_worker->callback) {
  191. seader_worker->callback(SeaderWorkerEventSamMissing, seader_worker->context);
  192. }
  193. return message.consumed;
  194. }
  195. if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock) {
  196. if(hasSAM) {
  197. seader_worker_process_message(seader_worker, &message);
  198. } else {
  199. if(memcmp(SAM_ATR, message.payload, sizeof(SAM_ATR)) == 0) {
  200. FURI_LOG_I(TAG, "SAM ATR!");
  201. hasSAM = true;
  202. if(seader_worker->callback) {
  203. seader_worker->callback(
  204. SeaderWorkerEventSamPresent, seader_worker->context);
  205. }
  206. } else if(memcmp(SAM_ATR2, message.payload, sizeof(SAM_ATR2)) == 0) {
  207. FURI_LOG_I(TAG, "SAM ATR2!");
  208. hasSAM = true;
  209. if(seader_worker->callback) {
  210. seader_worker->callback(
  211. SeaderWorkerEventSamPresent, seader_worker->context);
  212. }
  213. } else {
  214. FURI_LOG_W(TAG, "Unknown ATR");
  215. }
  216. }
  217. } else {
  218. FURI_LOG_W(TAG, "Unhandled CCID message type %d", message.bMessageType);
  219. }
  220. }
  221. return message.consumed;
  222. }