gen4_poller_i.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "gen4_poller_i.h"
  2. #include "bit_buffer.h"
  3. #include "core/log.h"
  4. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  5. #include <nfc/helpers/nfc_util.h>
  6. #include <stdint.h>
  7. #define GEN4_CMD_PREFIX (0xCF)
  8. #define GEN4_CMD_SET_SHD_MODE (0x32)
  9. #define GEN4_CMD_GET_CFG (0xC6)
  10. #define GEN4_CMD_GET_REVISION (0xCC)
  11. #define GEN4_CMD_WRITE (0xCD)
  12. #define GEN4_CMD_READ (0xCE)
  13. #define GEN4_CMD_SET_CFG (0xF0)
  14. #define GEN4_CMD_FUSE_CFG (0xF1)
  15. #define GEN4_CMD_SET_PWD (0xFE)
  16. #define CONFIG_SIZE (32)
  17. #define REVISION_SIZE (5)
  18. #define SHD_MODE_RESPONSE_SIZE (2)
  19. static Gen4PollerError gen4_poller_process_error(Iso14443_3aError error) {
  20. Gen4PollerError ret = Gen4PollerErrorNone;
  21. if(error == Iso14443_3aErrorNone) {
  22. ret = Gen4PollerErrorNone;
  23. } else {
  24. ret = Gen4PollerErrorTimeout;
  25. }
  26. return ret;
  27. }
  28. Gen4PollerError
  29. gen4_poller_set_shadow_mode(Gen4Poller* instance, uint32_t password, uint8_t mode) {
  30. Gen4PollerError ret = Gen4PollerErrorNone;
  31. bit_buffer_reset(instance->tx_buffer);
  32. do {
  33. uint8_t password_arr[4] = {};
  34. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  35. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  36. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  37. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_SET_SHD_MODE);
  38. bit_buffer_append_byte(instance->tx_buffer, mode);
  39. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  40. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  41. if(error != Iso14443_3aErrorNone) {
  42. ret = gen4_poller_process_error(error);
  43. break;
  44. }
  45. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  46. if(rx_bytes != SHD_MODE_RESPONSE_SIZE) {
  47. ret = Gen4PollerErrorProtocol;
  48. break;
  49. }
  50. uint16_t response = bit_buffer_get_size_bytes(instance->rx_buffer);
  51. FURI_LOG_D(TAG, "Card response: %X, Shadow mode set: %u", response, mode);
  52. } while(false);
  53. return ret;
  54. }
  55. Gen4PollerError
  56. gen4_poller_get_config(Gen4Poller* instance, uint32_t password, uint8_t* config_result) {
  57. Gen4PollerError ret = Gen4PollerErrorNone;
  58. bit_buffer_reset(instance->tx_buffer);
  59. do {
  60. uint8_t password_arr[4] = {};
  61. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  62. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  63. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  64. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_GET_CFG);
  65. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  66. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  67. if(error != Iso14443_3aErrorNone) {
  68. ret = gen4_poller_process_error(error);
  69. break;
  70. }
  71. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  72. if(rx_bytes != CONFIG_SIZE) {
  73. ret = Gen4PollerErrorProtocol;
  74. break;
  75. }
  76. bit_buffer_write_bytes(instance->rx_buffer, config_result, CONFIG_SIZE);
  77. } while(false);
  78. return ret;
  79. }
  80. Gen4PollerError
  81. gen4_poller_get_revision(Gen4Poller* instance, uint32_t password, uint8_t* revision_result) {
  82. Gen4PollerError ret = Gen4PollerErrorNone;
  83. bit_buffer_reset(instance->tx_buffer);
  84. do {
  85. uint8_t password_arr[4] = {};
  86. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  87. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  88. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  89. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_GET_REVISION);
  90. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  91. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  92. if(error != Iso14443_3aErrorNone) {
  93. ret = gen4_poller_process_error(error);
  94. break;
  95. }
  96. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  97. if(rx_bytes != REVISION_SIZE) {
  98. ret = Gen4PollerErrorProtocol;
  99. break;
  100. }
  101. bit_buffer_write_bytes(instance->rx_buffer, revision_result, REVISION_SIZE);
  102. } while(false);
  103. return ret;
  104. }
  105. Gen4PollerError gen4_poller_set_config(
  106. Gen4Poller* instance,
  107. uint32_t password,
  108. const uint8_t* config,
  109. size_t config_size,
  110. bool fuse) {
  111. Gen4PollerError ret = Gen4PollerErrorNone;
  112. bit_buffer_reset(instance->tx_buffer);
  113. do {
  114. uint8_t password_arr[4] = {};
  115. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  116. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  117. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  118. uint8_t fuse_config = fuse ? GEN4_CMD_FUSE_CFG : GEN4_CMD_SET_CFG;
  119. bit_buffer_append_byte(instance->tx_buffer, fuse_config);
  120. bit_buffer_append_bytes(instance->tx_buffer, config, config_size);
  121. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  122. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  123. if(error != Iso14443_3aErrorNone) {
  124. ret = gen4_poller_process_error(error);
  125. break;
  126. }
  127. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  128. if(rx_bytes != 2) {
  129. ret = Gen4PollerErrorProtocol;
  130. break;
  131. }
  132. } while(false);
  133. return ret;
  134. }
  135. Gen4PollerError gen4_poller_write_block(
  136. Gen4Poller* instance,
  137. uint32_t password,
  138. uint8_t block_num,
  139. const uint8_t* data) {
  140. Gen4PollerError ret = Gen4PollerErrorNone;
  141. bit_buffer_reset(instance->tx_buffer);
  142. do {
  143. uint8_t password_arr[4] = {};
  144. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  145. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  146. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  147. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_WRITE);
  148. bit_buffer_append_byte(instance->tx_buffer, block_num);
  149. bit_buffer_append_bytes(instance->tx_buffer, data, GEN4_POLLER_BLOCK_SIZE);
  150. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  151. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  152. if(error != Iso14443_3aErrorNone) {
  153. ret = gen4_poller_process_error(error);
  154. break;
  155. }
  156. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  157. if(rx_bytes != 2) {
  158. ret = Gen4PollerErrorProtocol;
  159. break;
  160. }
  161. } while(false);
  162. return ret;
  163. }
  164. Gen4PollerError
  165. gen4_poller_change_password(Gen4Poller* instance, uint32_t pwd_current, uint32_t pwd_new) {
  166. Gen4PollerError ret = Gen4PollerErrorNone;
  167. bit_buffer_reset(instance->tx_buffer);
  168. do {
  169. uint8_t password_arr[4] = {};
  170. nfc_util_num2bytes(pwd_current, COUNT_OF(password_arr), password_arr);
  171. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  172. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  173. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_SET_PWD);
  174. nfc_util_num2bytes(pwd_new, COUNT_OF(password_arr), password_arr);
  175. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  176. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  177. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  178. if(error != Iso14443_3aErrorNone) {
  179. ret = gen4_poller_process_error(error);
  180. break;
  181. }
  182. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  183. if(rx_bytes != 2) {
  184. ret = Gen4PollerErrorProtocol;
  185. break;
  186. }
  187. } while(false);
  188. return ret;
  189. }