ccid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. bool powered[2] = {false, false};
  8. uint8_t sam_slot = 0;
  9. uint8_t sequence[2] = {0, 0};
  10. uint8_t retries = 3;
  11. uint8_t getSequence(uint8_t slot) {
  12. if(sequence[slot] > 254) {
  13. sequence[slot] = 0;
  14. }
  15. return sequence[slot]++;
  16. }
  17. uint8_t seader_ccid_calc_lrc(uint8_t* data, size_t len) {
  18. uint8_t lrc = 0;
  19. for(size_t i = 0; i < len; i++) {
  20. lrc ^= data[i];
  21. }
  22. return lrc;
  23. }
  24. bool seader_ccid_validate_lrc(uint8_t* data, size_t len) {
  25. uint8_t lrc = seader_ccid_calc_lrc(data, len - 1);
  26. return lrc == data[len - 1];
  27. }
  28. size_t seader_ccid_add_lrc(uint8_t* data, size_t len) {
  29. data[len] = seader_ccid_calc_lrc(data, len);
  30. return len + 1;
  31. }
  32. void seader_ccid_IccPowerOn(SeaderUartBridge* seader_uart, uint8_t slot) {
  33. if(powered[slot]) {
  34. return;
  35. }
  36. powered[slot] = true;
  37. FURI_LOG_D(TAG, "Sending Power On (%d)", slot);
  38. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  39. seader_uart->tx_buf[0] = SYNC;
  40. seader_uart->tx_buf[1] = CTRL;
  41. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn;
  42. seader_uart->tx_buf[2 + 5] = slot;
  43. seader_uart->tx_buf[2 + 6] = getSequence(slot);
  44. seader_uart->tx_buf[2 + 7] = 2; //power
  45. seader_uart->tx_len = seader_ccid_add_lrc(seader_uart->tx_buf, 2 + 10);
  46. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  47. }
  48. void seader_ccid_check_for_sam(SeaderUartBridge* seader_uart) {
  49. hasSAM = false; // If someone is calling this, reset sam state
  50. powered[0] = false;
  51. powered[1] = false;
  52. retries = 3;
  53. seader_ccid_GetSlotStatus(seader_uart, 0);
  54. }
  55. void seader_ccid_GetSlotStatus(SeaderUartBridge* seader_uart, uint8_t slot) {
  56. FURI_LOG_D(TAG, "seader_ccid_GetSlotStatus(%d)", slot);
  57. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  58. seader_uart->tx_buf[0] = SYNC;
  59. seader_uart->tx_buf[1] = CTRL;
  60. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus;
  61. seader_uart->tx_buf[2 + 5] = slot;
  62. seader_uart->tx_buf[2 + 6] = getSequence(slot);
  63. seader_uart->tx_len = seader_ccid_add_lrc(seader_uart->tx_buf, 2 + 10);
  64. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  65. }
  66. void seader_ccid_SetParameters(SeaderUartBridge* seader_uart) {
  67. uint8_t T1 = 1;
  68. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  69. seader_uart->tx_buf[0] = SYNC;
  70. seader_uart->tx_buf[1] = CTRL;
  71. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters;
  72. seader_uart->tx_buf[2 + 1] = 0;
  73. seader_uart->tx_buf[2 + 5] = sam_slot;
  74. seader_uart->tx_buf[2 + 6] = getSequence(sam_slot);
  75. seader_uart->tx_buf[2 + 7] = T1;
  76. seader_uart->tx_buf[2 + 8] = 0;
  77. seader_uart->tx_buf[2 + 9] = 0;
  78. seader_uart->tx_len = seader_ccid_add_lrc(seader_uart->tx_buf, 2 + 10);
  79. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  80. }
  81. void seader_ccid_GetParameters(SeaderUartBridge* seader_uart) {
  82. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  83. seader_uart->tx_buf[0] = SYNC;
  84. seader_uart->tx_buf[1] = CTRL;
  85. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters;
  86. seader_uart->tx_buf[2 + 1] = 0;
  87. seader_uart->tx_buf[2 + 5] = sam_slot;
  88. seader_uart->tx_buf[2 + 6] = getSequence(sam_slot);
  89. seader_uart->tx_buf[2 + 7] = 0;
  90. seader_uart->tx_buf[2 + 8] = 0;
  91. seader_uart->tx_buf[2 + 9] = 0;
  92. seader_uart->tx_len = seader_ccid_add_lrc(seader_uart->tx_buf, 2 + 10);
  93. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  94. }
  95. void seader_ccid_XfrBlock(SeaderUartBridge* seader_uart, uint8_t* data, size_t len) {
  96. seader_ccid_XfrBlockToSlot(seader_uart, sam_slot, data, len);
  97. }
  98. void seader_ccid_XfrBlockToSlot(
  99. SeaderUartBridge* seader_uart,
  100. uint8_t slot,
  101. uint8_t* data,
  102. size_t len) {
  103. memset(seader_uart->tx_buf, 0, SEADER_UART_RX_BUF_SIZE);
  104. seader_uart->tx_buf[0] = SYNC;
  105. seader_uart->tx_buf[1] = CTRL;
  106. seader_uart->tx_buf[2 + 0] = CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock;
  107. seader_uart->tx_buf[2 + 1] = len;
  108. seader_uart->tx_buf[2 + 5] = slot;
  109. seader_uart->tx_buf[2 + 6] = getSequence(slot);
  110. seader_uart->tx_buf[2 + 7] = 5;
  111. seader_uart->tx_buf[2 + 8] = 0;
  112. seader_uart->tx_buf[2 + 9] = 0;
  113. memcpy(seader_uart->tx_buf + 2 + 10, data, len);
  114. seader_uart->tx_len = seader_ccid_add_lrc(seader_uart->tx_buf, 2 + 10 + len);
  115. // FURI_LOG_I(TAG, "seader_ccid_XfrBlock %d bytes", seader_uart->tx_len);
  116. furi_thread_flags_set(furi_thread_get_id(seader_uart->tx_thread), WorkerEvtSamRx);
  117. }
  118. size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len) {
  119. SeaderWorker* seader_worker = seader->worker;
  120. SeaderUartBridge* seader_uart = seader_worker->uart;
  121. CCID_Message message;
  122. message.consumed = 0;
  123. char display[SEADER_UART_RX_BUF_SIZE * 2 + 1] = {0};
  124. for(uint8_t i = 0; i < cmd_len; i++) {
  125. snprintf(display + (i * 2), sizeof(display), "%02x", cmd[i]);
  126. }
  127. FURI_LOG_D(TAG, "UART %d: %s", cmd_len, display);
  128. if(cmd_len == 2) {
  129. if(cmd[0] == CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange) {
  130. switch(cmd[1] & SLOT_0_MASK) {
  131. case 0:
  132. case 1:
  133. // No change, no-op
  134. break;
  135. case CARD_IN_1:
  136. FURI_LOG_D(TAG, "Card Inserted (0)");
  137. if(hasSAM && sam_slot == 0) {
  138. break;
  139. }
  140. sequence[0] = 0;
  141. seader_ccid_IccPowerOn(seader_uart, 0);
  142. break;
  143. case CARD_OUT_1:
  144. FURI_LOG_D(TAG, "Card Removed (0)");
  145. if(hasSAM && sam_slot == 0) {
  146. powered[0] = false;
  147. hasSAM = false;
  148. retries = 3;
  149. if(seader_worker->callback) {
  150. seader_worker->callback(
  151. SeaderWorkerEventSamMissing, seader_worker->context);
  152. }
  153. }
  154. break;
  155. };
  156. switch(cmd[1] & SLOT_1_MASK) {
  157. case 0:
  158. case 1:
  159. // No change, no-op
  160. break;
  161. case CARD_IN_2:
  162. FURI_LOG_D(TAG, "Card Inserted (1)");
  163. if(hasSAM && sam_slot == 1) {
  164. break;
  165. }
  166. sequence[1] = 0;
  167. seader_ccid_IccPowerOn(seader_uart, 1);
  168. break;
  169. case CARD_OUT_2:
  170. FURI_LOG_D(TAG, "Card Removed (1)");
  171. if(hasSAM && sam_slot == 1) {
  172. powered[1] = false;
  173. hasSAM = false;
  174. retries = 3;
  175. if(seader_worker->callback) {
  176. seader_worker->callback(
  177. SeaderWorkerEventSamMissing, seader_worker->context);
  178. }
  179. }
  180. break;
  181. };
  182. return 2;
  183. }
  184. }
  185. while(cmd_len >= 3 && cmd[0] == SYNC && cmd[1] == NAK) {
  186. // 031516
  187. FURI_LOG_W(TAG, "NAK");
  188. cmd += 3;
  189. cmd_len -= 3;
  190. message.consumed += 3;
  191. }
  192. while(cmd_len > 2 && (cmd[0] != SYNC || cmd[1] != CTRL)) {
  193. FURI_LOG_W(TAG, "invalid start: %02x", cmd[0]);
  194. cmd += 1;
  195. cmd_len -= 1;
  196. message.consumed += 1;
  197. }
  198. if(cmd_len > 12 && cmd[0] == SYNC && cmd[1] == CTRL) {
  199. uint8_t* ccid = cmd + 2;
  200. message.bMessageType = ccid[0];
  201. message.dwLength = *((uint32_t*)(ccid + 1));
  202. message.bSlot = ccid[5];
  203. message.bSeq = ccid[6];
  204. message.bStatus = ccid[7];
  205. message.bError = ccid[8];
  206. message.payload = ccid + 10;
  207. memset(display, 0, sizeof(display));
  208. for(uint8_t i = 0; i < message.dwLength; i++) {
  209. snprintf(display + (i * 2), sizeof(display), "%02x", message.payload[i]);
  210. }
  211. if(cmd_len < 2 + 10 + message.dwLength + 1) {
  212. // Incomplete
  213. return message.consumed;
  214. }
  215. message.consumed += 2 + 10 + message.dwLength + 1;
  216. if(seader_ccid_validate_lrc(cmd, 2 + 10 + message.dwLength + 1) == false) {
  217. FURI_LOG_W(
  218. TAG,
  219. "Invalid LRC. Recv: %02x vs Calc: %02x",
  220. cmd[2 + 10 + message.dwLength + 1],
  221. seader_ccid_calc_lrc(cmd, 2 + 10 + message.dwLength));
  222. // TODO: Should I respond with an error?
  223. return message.consumed;
  224. }
  225. /*
  226. if(message.dwLength == 0) {
  227. FURI_LOG_D(
  228. TAG,
  229. "CCID [%d|%d] type: %02x, status: %02x, error: %02x",
  230. message.bSlot,
  231. message.bSeq,
  232. message.bMessageType,
  233. message.bStatus,
  234. message.bError);
  235. } else {
  236. FURI_LOG_D(
  237. TAG,
  238. "CCID [%d|%d] %ld: %s",
  239. message.bSlot,
  240. message.bSeq,
  241. message.dwLength,
  242. display);
  243. }
  244. */
  245. //0306 81 00000000 0000 0200 01 87
  246. //0306 81 00000000 0000 0100 01 84
  247. if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus) {
  248. uint8_t status = (message.bStatus & BMICCSTATUS_MASK);
  249. if(status == 0 || status == 1) {
  250. seader_ccid_IccPowerOn(seader_uart, message.bSlot);
  251. return message.consumed;
  252. } else if(status == 2) {
  253. FURI_LOG_W(TAG, "No ICC is present [retries %d]", retries);
  254. if(retries-- > 1 && hasSAM == false) {
  255. furi_delay_ms(100);
  256. seader_ccid_GetSlotStatus(seader_uart, retries % 2);
  257. } else {
  258. if(seader_worker->callback) {
  259. seader_worker->callback(
  260. SeaderWorkerEventSamMissing, seader_worker->context);
  261. }
  262. }
  263. return message.consumed;
  264. }
  265. }
  266. //0306 80 00000000 0001 42fe 00 38
  267. if(message.bStatus == 0x41 && message.bError == 0xfe) {
  268. FURI_LOG_W(TAG, "card probably upside down");
  269. hasSAM = false;
  270. if(seader_worker->callback) {
  271. seader_worker->callback(SeaderWorkerEventSamMissing, seader_worker->context);
  272. }
  273. return message.consumed;
  274. }
  275. if(message.bStatus == 0x42 && message.bError == 0xfe) {
  276. FURI_LOG_W(TAG, "No card");
  277. if(seader_worker->callback) {
  278. seader_worker->callback(SeaderWorkerEventSamMissing, seader_worker->context);
  279. }
  280. return message.consumed;
  281. }
  282. if(message.bError != 0) {
  283. FURI_LOG_W(TAG, "CCID error %02x", message.bError);
  284. message.consumed = cmd_len;
  285. if(seader_worker->callback) {
  286. seader_worker->callback(SeaderWorkerEventSamMissing, seader_worker->context);
  287. }
  288. return message.consumed;
  289. }
  290. if(message.bMessageType == CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock) {
  291. if(hasSAM) {
  292. if(message.bSlot == sam_slot) {
  293. seader_worker_process_sam_message(seader, &message);
  294. } else {
  295. FURI_LOG_D(TAG, "Discarding message on non-sam slot");
  296. }
  297. } else {
  298. if(memcmp(SAM_ATR, message.payload, sizeof(SAM_ATR)) == 0) {
  299. FURI_LOG_I(TAG, "SAM ATR!");
  300. hasSAM = true;
  301. sam_slot = message.bSlot;
  302. seader_worker_send_version(seader);
  303. if(seader_worker->callback) {
  304. seader_worker->callback(
  305. SeaderWorkerEventSamPresent, seader_worker->context);
  306. }
  307. } else if(memcmp(SAM_ATR2, message.payload, sizeof(SAM_ATR2)) == 0) {
  308. FURI_LOG_I(TAG, "SAM ATR2!");
  309. hasSAM = true;
  310. sam_slot = message.bSlot;
  311. seader_worker_send_version(seader);
  312. if(seader_worker->callback) {
  313. seader_worker->callback(
  314. SeaderWorkerEventSamPresent, seader_worker->context);
  315. }
  316. } else {
  317. FURI_LOG_W(TAG, "Unknown ATR");
  318. if(seader_worker->callback) {
  319. seader_worker->callback(SeaderWorkerEventSamWrong, seader_worker->context);
  320. }
  321. }
  322. }
  323. } else {
  324. FURI_LOG_W(TAG, "Unhandled CCID message type %d", message.bMessageType);
  325. }
  326. }
  327. return message.consumed;
  328. }