gen4_poller_i.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #define GEN4_CMD_PREFIX (0xCF)
  7. #define GEN4_CMD_GET_CFG (0xC6)
  8. #define GEN4_CMD_GET_REVISION (0xCC)
  9. #define GEN4_CMD_WRITE (0xCD)
  10. #define GEN4_CMD_READ (0xCE)
  11. #define GEN4_CMD_SET_CFG (0xF0)
  12. #define GEN4_CMD_FUSE_CFG (0xF1)
  13. #define GEN4_CMD_SET_PWD (0xFE)
  14. #define CONFIG_SIZE (32)
  15. #define REVISION_SIZE (5)
  16. static Gen4PollerError gen4_poller_process_error(Iso14443_3aError error) {
  17. Gen4PollerError ret = Gen4PollerErrorNone;
  18. if(error == Iso14443_3aErrorNone) {
  19. ret = Gen4PollerErrorNone;
  20. } else {
  21. ret = Gen4PollerErrorTimeout;
  22. }
  23. return ret;
  24. }
  25. Gen4PollerError
  26. gen4_poller_get_config(Gen4Poller* instance, uint32_t password, uint8_t* config_result) {
  27. Gen4PollerError ret = Gen4PollerErrorNone;
  28. bit_buffer_reset(instance->tx_buffer);
  29. do {
  30. uint8_t password_arr[4] = {};
  31. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  32. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  33. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  34. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_GET_CFG);
  35. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  36. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  37. if(error != Iso14443_3aErrorNone) {
  38. ret = gen4_poller_process_error(error);
  39. break;
  40. }
  41. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  42. if(rx_bytes != CONFIG_SIZE) {
  43. ret = Gen4PollerErrorProtocol;
  44. break;
  45. }
  46. bit_buffer_write_bytes(instance->rx_buffer, config_result, CONFIG_SIZE);
  47. } while(false);
  48. return ret;
  49. }
  50. Gen4PollerError
  51. gen4_poller_get_revision(Gen4Poller* instance, uint32_t password, uint8_t* revision_result) {
  52. Gen4PollerError ret = Gen4PollerErrorNone;
  53. bit_buffer_reset(instance->tx_buffer);
  54. do {
  55. uint8_t password_arr[4] = {};
  56. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  57. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  58. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  59. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_GET_REVISION);
  60. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  61. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  62. if(error != Iso14443_3aErrorNone) {
  63. ret = gen4_poller_process_error(error);
  64. break;
  65. }
  66. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  67. if(rx_bytes != 5) {
  68. ret = Gen4PollerErrorProtocol;
  69. break;
  70. }
  71. bit_buffer_write_bytes(instance->rx_buffer, revision_result, REVISION_SIZE);
  72. } while(false);
  73. return ret;
  74. }
  75. Gen4PollerError gen4_poller_set_config(
  76. Gen4Poller* instance,
  77. uint32_t password,
  78. const uint8_t* config,
  79. size_t config_size,
  80. bool fuse) {
  81. Gen4PollerError ret = Gen4PollerErrorNone;
  82. bit_buffer_reset(instance->tx_buffer);
  83. do {
  84. uint8_t password_arr[4] = {};
  85. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  86. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  87. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  88. uint8_t fuse_config = fuse ? GEN4_CMD_FUSE_CFG : GEN4_CMD_SET_CFG;
  89. bit_buffer_append_byte(instance->tx_buffer, fuse_config);
  90. bit_buffer_append_bytes(instance->tx_buffer, config, config_size);
  91. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  92. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  93. if(error != Iso14443_3aErrorNone) {
  94. ret = gen4_poller_process_error(error);
  95. break;
  96. }
  97. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  98. if(rx_bytes != 2) {
  99. ret = Gen4PollerErrorProtocol;
  100. break;
  101. }
  102. } while(false);
  103. return ret;
  104. }
  105. Gen4PollerError gen4_poller_write_block(
  106. Gen4Poller* instance,
  107. uint32_t password,
  108. uint8_t block_num,
  109. const uint8_t* data) {
  110. Gen4PollerError ret = Gen4PollerErrorNone;
  111. bit_buffer_reset(instance->tx_buffer);
  112. do {
  113. uint8_t password_arr[4] = {};
  114. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  115. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  116. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  117. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_WRITE);
  118. bit_buffer_append_byte(instance->tx_buffer, block_num);
  119. bit_buffer_append_bytes(instance->tx_buffer, data, GEN4_POLLER_BLOCK_SIZE);
  120. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  121. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  122. if(error != Iso14443_3aErrorNone) {
  123. ret = gen4_poller_process_error(error);
  124. break;
  125. }
  126. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  127. if(rx_bytes != 2) {
  128. ret = Gen4PollerErrorProtocol;
  129. break;
  130. }
  131. } while(false);
  132. return ret;
  133. }
  134. Gen4PollerError
  135. gen4_poller_change_password(Gen4Poller* instance, uint32_t pwd_current, uint32_t pwd_new) {
  136. Gen4PollerError ret = Gen4PollerErrorNone;
  137. bit_buffer_reset(instance->tx_buffer);
  138. do {
  139. uint8_t password_arr[4] = {};
  140. nfc_util_num2bytes(pwd_current, COUNT_OF(password_arr), password_arr);
  141. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  142. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  143. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_SET_PWD);
  144. nfc_util_num2bytes(pwd_new, COUNT_OF(password_arr), password_arr);
  145. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  146. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  147. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  148. if(error != Iso14443_3aErrorNone) {
  149. ret = gen4_poller_process_error(error);
  150. break;
  151. }
  152. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  153. if(rx_bytes != 2) {
  154. ret = Gen4PollerErrorProtocol;
  155. break;
  156. }
  157. } while(false);
  158. return ret;
  159. }