gen4_poller.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include "bit_buffer.h"
  2. #include "gen4_poller_i.h"
  3. #include "protocols/gen4/gen4.h"
  4. #include "protocols/gen4/gen4_poller.h"
  5. #include <nfc/protocols/iso14443_3a/iso14443_3a.h>
  6. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  7. #include <nfc/nfc_poller.h>
  8. #include <bit_lib.h>
  9. #include <string.h>
  10. #define GEN4_POLLER_THREAD_FLAG_DETECTED (1U << 0)
  11. #define GEN4_POLLER_DEFAULT_CONFIG_SIZE (28)
  12. typedef NfcCommand (*Gen4PollerStateHandler)(Gen4Poller* instance);
  13. typedef struct {
  14. NfcPoller* poller;
  15. Gen4Password password;
  16. Gen4 gen4_data;
  17. BitBuffer* tx_buffer;
  18. BitBuffer* rx_buffer;
  19. FuriThreadId thread_id;
  20. Gen4PollerError error;
  21. } Gen4PollerDetectContext;
  22. static const Gen4Config gen4_poller_default_config = {
  23. .data_raw = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x78,
  24. 0x00, 0x91, 0x02, 0xDA, 0xBC, 0x19, 0x10, 0x10, 0x11, 0x12,
  25. 0x13, 0x14, 0x15, 0x16, 0x04, 0x00, 0x08, 0x00}};
  26. static const uint8_t gen4_poller_default_block_0[GEN4_POLLER_BLOCK_SIZE] =
  27. {0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  28. static const uint8_t gen4_poller_default_empty_block[GEN4_POLLER_BLOCK_SIZE] = {0};
  29. static const uint8_t gen4_poller_default_sector_trailer_block[GEN4_POLLER_BLOCK_SIZE] =
  30. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  31. static bool gen4_poller_is_sector_trailer(uint8_t block_num) {
  32. uint8_t sec_tr_block_num = 0;
  33. if(block_num < 128) {
  34. sec_tr_block_num = block_num | 0x03;
  35. } else {
  36. sec_tr_block_num = block_num | 0x0f;
  37. }
  38. return block_num == sec_tr_block_num;
  39. }
  40. Gen4Poller* gen4_poller_alloc(Nfc* nfc) {
  41. furi_assert(nfc);
  42. Gen4Poller* instance = malloc(sizeof(Gen4Poller));
  43. instance->poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
  44. instance->gen4_event.data = &instance->gen4_event_data;
  45. instance->tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  46. instance->rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  47. instance->gen4_data = gen4_alloc();
  48. return instance;
  49. }
  50. void gen4_poller_free(Gen4Poller* instance) {
  51. furi_assert(instance);
  52. nfc_poller_free(instance->poller);
  53. bit_buffer_free(instance->tx_buffer);
  54. bit_buffer_free(instance->rx_buffer);
  55. gen4_free(instance->gen4_data);
  56. free(instance);
  57. }
  58. void gen4_poller_set_password(Gen4Poller* instance, Gen4Password password) {
  59. furi_assert(instance);
  60. instance->password = password;
  61. }
  62. NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
  63. furi_assert(context);
  64. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  65. furi_assert(event.instance);
  66. furi_assert(event.event_data);
  67. NfcCommand command = NfcCommandStop;
  68. Gen4PollerDetectContext* gen4_poller_detect_ctx = context;
  69. Iso14443_3aPoller* iso3_poller = event.instance;
  70. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  71. gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
  72. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  73. do {
  74. // check config
  75. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
  76. bit_buffer_append_bytes(
  77. gen4_poller_detect_ctx->tx_buffer,
  78. gen4_poller_detect_ctx->password.bytes,
  79. GEN4_PASSWORD_LEN);
  80. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_CFG);
  81. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  82. iso3_poller,
  83. gen4_poller_detect_ctx->tx_buffer,
  84. gen4_poller_detect_ctx->rx_buffer,
  85. GEN4_POLLER_MAX_FWT);
  86. if(error != Iso14443_3aErrorNone) {
  87. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  88. break;
  89. }
  90. size_t rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
  91. if(rx_bytes != GEN4_CONFIG_SIZE) {
  92. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  93. break;
  94. }
  95. memcpy(
  96. gen4_poller_detect_ctx->gen4_data.config.data_raw,
  97. bit_buffer_get_data(gen4_poller_detect_ctx->rx_buffer),
  98. GEN4_CONFIG_SIZE);
  99. // check revision
  100. bit_buffer_reset(gen4_poller_detect_ctx->tx_buffer);
  101. bit_buffer_reset(gen4_poller_detect_ctx->rx_buffer);
  102. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
  103. bit_buffer_append_bytes(
  104. gen4_poller_detect_ctx->tx_buffer,
  105. gen4_poller_detect_ctx->password.bytes,
  106. GEN4_PASSWORD_LEN);
  107. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_REVISION);
  108. error = iso14443_3a_poller_send_standard_frame(
  109. iso3_poller,
  110. gen4_poller_detect_ctx->tx_buffer,
  111. gen4_poller_detect_ctx->rx_buffer,
  112. GEN4_POLLER_MAX_FWT);
  113. if(error != Iso14443_3aErrorNone) {
  114. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  115. break;
  116. }
  117. rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
  118. if(rx_bytes != GEN4_REVISION_SIZE) {
  119. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  120. break;
  121. }
  122. memcpy(
  123. gen4_poller_detect_ctx->gen4_data.revision.data,
  124. bit_buffer_get_data(gen4_poller_detect_ctx->rx_buffer),
  125. GEN4_REVISION_SIZE);
  126. gen4_poller_detect_ctx->error = Gen4PollerErrorNone;
  127. } while(false);
  128. } else if(iso3_event->type == Iso14443_3aPollerEventTypeError) {
  129. gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
  130. }
  131. furi_thread_flags_set(gen4_poller_detect_ctx->thread_id, GEN4_POLLER_THREAD_FLAG_DETECTED);
  132. return command;
  133. }
  134. Gen4PollerError gen4_poller_detect(Nfc* nfc, Gen4Password password, Gen4* gen4_data) {
  135. furi_assert(nfc);
  136. Gen4PollerDetectContext gen4_poller_detect_ctx = {};
  137. gen4_poller_detect_ctx.poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
  138. gen4_poller_detect_ctx.password = password;
  139. gen4_poller_detect_ctx.tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  140. gen4_poller_detect_ctx.rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  141. gen4_poller_detect_ctx.thread_id = furi_thread_get_current_id();
  142. gen4_poller_detect_ctx.error = Gen4PollerErrorNone;
  143. nfc_poller_start(
  144. gen4_poller_detect_ctx.poller, gen4_poller_detect_callback, &gen4_poller_detect_ctx);
  145. uint32_t flags =
  146. furi_thread_flags_wait(GEN4_POLLER_THREAD_FLAG_DETECTED, FuriFlagWaitAny, FuriWaitForever);
  147. if(flags & GEN4_POLLER_THREAD_FLAG_DETECTED) {
  148. furi_thread_flags_clear(GEN4_POLLER_THREAD_FLAG_DETECTED);
  149. }
  150. nfc_poller_stop(gen4_poller_detect_ctx.poller);
  151. nfc_poller_free(gen4_poller_detect_ctx.poller);
  152. bit_buffer_free(gen4_poller_detect_ctx.tx_buffer);
  153. bit_buffer_free(gen4_poller_detect_ctx.rx_buffer);
  154. if(gen4_poller_detect_ctx.error == Gen4PollerErrorNone)
  155. gen4_copy(gen4_data, &gen4_poller_detect_ctx.gen4_data);
  156. return gen4_poller_detect_ctx.error;
  157. }
  158. NfcCommand gen4_poller_idle_handler(Gen4Poller* instance) {
  159. NfcCommand command = NfcCommandContinue;
  160. instance->current_block = 0;
  161. //TODO: FOR WHAT?
  162. //memset(instance->config, 0, sizeof(instance->config));
  163. instance->gen4_event.type = Gen4PollerEventTypeCardDetected;
  164. command = instance->callback(instance->gen4_event, instance->context);
  165. instance->state = Gen4PollerStateRequestMode;
  166. return command;
  167. }
  168. NfcCommand gen4_poller_request_mode_handler(Gen4Poller* instance) {
  169. NfcCommand command = NfcCommandContinue;
  170. instance->gen4_event.type = Gen4PollerEventTypeRequestMode;
  171. command = instance->callback(instance->gen4_event, instance->context);
  172. if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWipe) {
  173. instance->state = Gen4PollerStateWipe;
  174. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWrite) {
  175. instance->state = Gen4PollerStateRequestWriteData;
  176. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetPassword) {
  177. instance->state = Gen4PollerStateChangePassword;
  178. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetInfo) {
  179. instance->state = Gen4PollerStateGetInfo;
  180. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDefaultCfg) {
  181. instance->state = Gen4PollerStateSetDefaultConfig;
  182. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetShadowMode) {
  183. instance->state = Gen4PollerStateSetShadowMode;
  184. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDirectWriteBlock0Mode) {
  185. instance->state = Gen4PollerStateSetDirectWriteBlock0;
  186. } else {
  187. instance->state = Gen4PollerStateFail;
  188. }
  189. return command;
  190. }
  191. NfcCommand gen4_poller_wipe_handler(Gen4Poller* instance) {
  192. NfcCommand command = NfcCommandContinue;
  193. do {
  194. Gen4PollerError error = Gen4PollerErrorNone;
  195. if(instance->current_block == 0) {
  196. error = gen4_poller_set_config(
  197. instance,
  198. instance->password,
  199. &gen4_poller_default_config,
  200. GEN4_POLLER_DEFAULT_CONFIG_SIZE,
  201. false);
  202. if(error != Gen4PollerErrorNone) {
  203. FURI_LOG_D(TAG, "Failed to set default config: %d", error);
  204. instance->state = Gen4PollerStateFail;
  205. break;
  206. }
  207. instance->password.value = 0;
  208. error = gen4_poller_write_block(
  209. instance, instance->password, instance->current_block, gen4_poller_default_block_0);
  210. if(error != Gen4PollerErrorNone) {
  211. FURI_LOG_D(TAG, "Failed to write 0 block: %d", error);
  212. instance->state = Gen4PollerStateFail;
  213. break;
  214. }
  215. } else if(instance->current_block < GEN4_POLLER_BLOCKS_TOTAL) {
  216. const uint8_t* block = gen4_poller_is_sector_trailer(instance->current_block) ?
  217. gen4_poller_default_sector_trailer_block :
  218. gen4_poller_default_empty_block;
  219. error = gen4_poller_write_block(
  220. instance, instance->password, instance->current_block, block);
  221. if(error != Gen4PollerErrorNone) {
  222. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  223. instance->state = Gen4PollerStateFail;
  224. break;
  225. }
  226. } else {
  227. instance->state = Gen4PollerStateSuccess;
  228. break;
  229. }
  230. instance->current_block++;
  231. } while(false);
  232. return command;
  233. }
  234. NfcCommand gen4_poller_request_write_data_handler(Gen4Poller* instance) {
  235. NfcCommand command = NfcCommandContinue;
  236. instance->gen4_event.type = Gen4PollerEventTypeRequestDataToWrite;
  237. command = instance->callback(instance->gen4_event, instance->context);
  238. instance->protocol = instance->gen4_event_data.request_data.protocol;
  239. instance->data = instance->gen4_event_data.request_data.data;
  240. if((instance->protocol == NfcProtocolMfClassic) ||
  241. (instance->protocol == NfcProtocolMfUltralight)) {
  242. instance->state = Gen4PollerStateWrite;
  243. } else {
  244. FURI_LOG_E(TAG, "Unsupported protocol");
  245. instance->state = Gen4PollerStateFail;
  246. }
  247. return command;
  248. }
  249. static NfcCommand gen4_poller_write_mf_classic(Gen4Poller* instance) {
  250. NfcCommand command = NfcCommandContinue;
  251. do {
  252. const MfClassicData* mfc_data = instance->data;
  253. const Iso14443_3aData* iso3_data = mfc_data->iso14443_3a_data;
  254. if(instance->current_block == 0) {
  255. instance->config.data_parsed.protocol = Gen4ProtocolMfClassic;
  256. instance->total_blocks = mf_classic_get_total_block_num(mfc_data->type);
  257. if(iso3_data->uid_len == 4) {
  258. instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
  259. } else if(iso3_data->uid_len == 7) {
  260. instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
  261. } else {
  262. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  263. instance->state = Gen4PollerStateFail;
  264. break;
  265. }
  266. instance->config.data_parsed.gtu_mode = Gen4ShadowModeDisabled;
  267. instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
  268. instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
  269. instance->config.data_parsed.sak = iso3_data->sak;
  270. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
  271. instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
  272. instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
  273. Gen4PollerError error = gen4_poller_set_config(
  274. instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
  275. if(error != Gen4PollerErrorNone) {
  276. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  277. instance->state = Gen4PollerStateFail;
  278. break;
  279. }
  280. }
  281. if(instance->current_block < instance->total_blocks) {
  282. FURI_LOG_D(TAG, "Writing block %d", instance->current_block);
  283. Gen4PollerError error = gen4_poller_write_block(
  284. instance,
  285. instance->password,
  286. instance->current_block,
  287. mfc_data->block[instance->current_block].data);
  288. if(error != Gen4PollerErrorNone) {
  289. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  290. instance->state = Gen4PollerStateFail;
  291. break;
  292. }
  293. } else {
  294. instance->state = Gen4PollerStateSuccess;
  295. break;
  296. }
  297. instance->current_block++;
  298. } while(false);
  299. return command;
  300. }
  301. static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
  302. NfcCommand command = NfcCommandContinue;
  303. do {
  304. const MfUltralightData* mfu_data = instance->data;
  305. const Iso14443_3aData* iso3_data = mfu_data->iso14443_3a_data;
  306. if(instance->current_block == 0) {
  307. instance->total_blocks = 64;
  308. instance->config.data_parsed.protocol = Gen4ProtocolMfUltralight;
  309. switch(mfu_data->type) {
  310. case MfUltralightTypeNTAG203:
  311. case MfUltralightTypeNTAG213:
  312. case MfUltralightTypeNTAG215:
  313. case MfUltralightTypeNTAG216:
  314. case MfUltralightTypeNTAGI2C1K:
  315. case MfUltralightTypeNTAGI2C2K:
  316. case MfUltralightTypeNTAGI2CPlus1K:
  317. case MfUltralightTypeNTAGI2CPlus2K:
  318. instance->config.data_parsed.mfu_mode = Gen4UltralightModeNTAG;
  319. instance->total_blocks = 64 * 2;
  320. break;
  321. case MfUltralightTypeUL11:
  322. case MfUltralightTypeUL21:
  323. // UL-C?
  324. // UL?
  325. default:
  326. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
  327. break;
  328. }
  329. if(iso3_data->uid_len == 4) {
  330. instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
  331. } else if(iso3_data->uid_len == 7) {
  332. instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
  333. } else {
  334. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  335. instance->state = Gen4PollerStateFail;
  336. break;
  337. }
  338. instance->config.data_parsed.gtu_mode = Gen4ShadowModeHighSpeedDisabled;
  339. instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
  340. instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
  341. instance->config.data_parsed.sak = iso3_data->sak;
  342. //instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
  343. instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
  344. instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
  345. Gen4PollerError error = gen4_poller_set_config(
  346. instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
  347. if(error != Gen4PollerErrorNone) {
  348. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  349. instance->state = Gen4PollerStateFail;
  350. break;
  351. }
  352. }
  353. if(instance->current_block < mfu_data->pages_read) {
  354. FURI_LOG_D(
  355. TAG, "Writing page %zu / %zu", instance->current_block, mfu_data->pages_read);
  356. Gen4PollerError error = gen4_poller_write_block(
  357. instance,
  358. instance->password,
  359. instance->current_block,
  360. mfu_data->page[instance->current_block].data);
  361. if(error != Gen4PollerErrorNone) {
  362. FURI_LOG_D(TAG, "Failed to write %d page: %d", instance->current_block, error);
  363. instance->state = Gen4PollerStateFail;
  364. break;
  365. }
  366. instance->current_block++;
  367. } else {
  368. uint8_t block[GEN4_POLLER_BLOCK_SIZE] = {};
  369. bool write_success = true;
  370. for(size_t i = 0; i < 8; i++) {
  371. memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
  372. Gen4PollerError error =
  373. gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
  374. if(error != Gen4PollerErrorNone) {
  375. write_success = false;
  376. break;
  377. }
  378. }
  379. if(!write_success) {
  380. FURI_LOG_E(TAG, "Failed to write Signature");
  381. instance->state = Gen4PollerStateFail;
  382. break;
  383. }
  384. block[0] = mfu_data->version.header;
  385. block[1] = mfu_data->version.vendor_id;
  386. block[2] = mfu_data->version.prod_type;
  387. block[3] = mfu_data->version.prod_subtype;
  388. Gen4PollerError error =
  389. gen4_poller_write_block(instance, instance->password, 0xFA, block);
  390. if(error != Gen4PollerErrorNone) {
  391. FURI_LOG_E(TAG, "Failed to write 1st part Version");
  392. instance->state = Gen4PollerStateFail;
  393. break;
  394. }
  395. block[0] = mfu_data->version.prod_ver_major;
  396. block[1] = mfu_data->version.prod_ver_minor;
  397. block[2] = mfu_data->version.storage_size;
  398. block[3] = mfu_data->version.protocol_type;
  399. error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
  400. if(error != Gen4PollerErrorNone) {
  401. FURI_LOG_E(TAG, "Failed to write 2nd part Version");
  402. instance->state = Gen4PollerStateFail;
  403. break;
  404. }
  405. // Password
  406. MfUltralightConfigPages* config_pages = NULL;
  407. mf_ultralight_get_config_page(mfu_data, &config_pages);
  408. block[0] = config_pages->password.data[0];
  409. block[1] = config_pages->password.data[1];
  410. block[2] = config_pages->password.data[2];
  411. block[3] = config_pages->password.data[3];
  412. error = gen4_poller_write_block(instance, instance->password, 0xE5, block);
  413. if(error != Gen4PollerErrorNone) {
  414. FURI_LOG_E(TAG, "Failed to write Password to sector E5");
  415. instance->state = Gen4PollerStateFail;
  416. break;
  417. }
  418. error = gen4_poller_write_block(instance, instance->password, 0xF0, block);
  419. if(error != Gen4PollerErrorNone) {
  420. FURI_LOG_E(TAG, "Failed to write Password to sector F0");
  421. instance->state = Gen4PollerStateFail;
  422. break;
  423. }
  424. // PACK
  425. block[0] = config_pages->pack.data[0];
  426. block[1] = config_pages->pack.data[1];
  427. block[2] = 0x00;
  428. block[3] = 0x00;
  429. error = gen4_poller_write_block(instance, instance->password, 0xE6, block);
  430. if(error != Gen4PollerErrorNone) {
  431. FURI_LOG_E(TAG, "Failed to write PACK to sector E6");
  432. instance->state = Gen4PollerStateFail;
  433. break;
  434. }
  435. error = gen4_poller_write_block(instance, instance->password, 0xF1, block);
  436. if(error != Gen4PollerErrorNone) {
  437. FURI_LOG_E(TAG, "Failed to write PACK to sector F1");
  438. instance->state = Gen4PollerStateFail;
  439. break;
  440. }
  441. instance->state = Gen4PollerStateSuccess;
  442. }
  443. } while(false);
  444. return command;
  445. }
  446. NfcCommand gen4_poller_write_handler(Gen4Poller* instance) {
  447. NfcCommand command = NfcCommandContinue;
  448. memcpy(
  449. instance->config.data_raw,
  450. gen4_poller_default_config.data_raw,
  451. GEN4_POLLER_DEFAULT_CONFIG_SIZE);
  452. memcpy(
  453. instance->config.data_parsed.password.bytes, instance->password.bytes, GEN4_PASSWORD_LEN);
  454. memset(&instance->config.data_raw[7], 0, 17);
  455. if(instance->protocol == NfcProtocolMfClassic) {
  456. command = gen4_poller_write_mf_classic(instance);
  457. } else if(instance->protocol == NfcProtocolMfUltralight) {
  458. command = gen4_poller_write_mf_ultralight(instance);
  459. } else {
  460. furi_crash("Unsupported protocol to write");
  461. }
  462. return command;
  463. }
  464. NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
  465. NfcCommand command = NfcCommandContinue;
  466. do {
  467. instance->gen4_event.type = Gen4PollerEventTypeRequestNewPassword;
  468. command = instance->callback(instance->gen4_event, instance->context);
  469. if(command != NfcCommandContinue) break;
  470. Gen4Password new_password = instance->gen4_event_data.request_password.password;
  471. Gen4PollerError error =
  472. gen4_poller_change_password(instance, instance->password, new_password);
  473. if(error != Gen4PollerErrorNone) {
  474. FURI_LOG_E(TAG, "Failed to change password: %d", error);
  475. instance->state = Gen4PollerStateFail;
  476. break;
  477. }
  478. instance->password = new_password;
  479. instance->state = Gen4PollerStateSuccess;
  480. } while(false);
  481. return command;
  482. }
  483. NfcCommand gen4_poller_set_default_cfg_handler(Gen4Poller* instance) {
  484. NfcCommand command = NfcCommandContinue;
  485. do {
  486. Gen4PollerError error = gen4_poller_set_config(
  487. instance,
  488. instance->password,
  489. &gen4_poller_default_config,
  490. GEN4_POLLER_DEFAULT_CONFIG_SIZE,
  491. false);
  492. if(error != Gen4PollerErrorNone) {
  493. FURI_LOG_E(TAG, "Failed to set default config: %d", error);
  494. instance->state = Gen4PollerStateFail;
  495. break;
  496. }
  497. instance->state = Gen4PollerStateSuccess;
  498. } while(false);
  499. return command;
  500. }
  501. NfcCommand gen4_poller_get_current_cfg_handler(Gen4Poller* instance) {
  502. NfcCommand command = NfcCommandContinue;
  503. do {
  504. Gen4Config config;
  505. Gen4PollerError error = gen4_poller_get_config(instance, instance->password, &config);
  506. if(error != Gen4PollerErrorNone) {
  507. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  508. instance->state = Gen4PollerStateFail;
  509. break;
  510. }
  511. // Copy config data to event data buffer
  512. memcpy(instance->gen4_data->config.data_raw, config.data_raw, sizeof(config));
  513. instance->state = Gen4PollerStateSuccess;
  514. } while(false);
  515. return command;
  516. }
  517. NfcCommand gen4_poller_get_revision_handler(Gen4Poller* instance) {
  518. NfcCommand command = NfcCommandContinue;
  519. do {
  520. Gen4Revision revision;
  521. Gen4PollerError error = gen4_poller_get_revision(instance, instance->password, &revision);
  522. if(error != Gen4PollerErrorNone) {
  523. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  524. instance->state = Gen4PollerStateFail;
  525. break;
  526. }
  527. // Copy revision data to event data buffer
  528. memcpy(instance->gen4_data->revision.data, revision.data, sizeof(revision));
  529. instance->state = Gen4PollerStateSuccess;
  530. } while(false);
  531. return command;
  532. }
  533. NfcCommand gen4_poller_get_info_handler(Gen4Poller* instance) {
  534. NfcCommand command = NfcCommandContinue;
  535. do {
  536. Gen4 gen4_data;
  537. Gen4PollerError error =
  538. gen4_poller_get_revision(instance, instance->password, &gen4_data.revision);
  539. if(error != Gen4PollerErrorNone) {
  540. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  541. instance->state = Gen4PollerStateFail;
  542. break;
  543. }
  544. error = gen4_poller_get_config(instance, instance->password, &gen4_data.config);
  545. if(error != Gen4PollerErrorNone) {
  546. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  547. instance->state = Gen4PollerStateFail;
  548. break;
  549. }
  550. // Copy config&&revision data to event data buffer
  551. gen4_copy(instance->gen4_data, &gen4_data);
  552. instance->state = Gen4PollerStateSuccess;
  553. } while(false);
  554. return command;
  555. }
  556. NfcCommand gen4_poller_set_shadow_mode_handler(Gen4Poller* instance) {
  557. NfcCommand command = NfcCommandContinue;
  558. do {
  559. Gen4PollerError error =
  560. gen4_poller_set_shadow_mode(instance, instance->password, instance->shadow_mode);
  561. if(error != Gen4PollerErrorNone) {
  562. FURI_LOG_E(TAG, "Failed to set shadow mode: %d", error);
  563. instance->state = Gen4PollerStateFail;
  564. break;
  565. }
  566. instance->state = Gen4PollerStateSuccess;
  567. } while(false);
  568. return command;
  569. }
  570. NfcCommand gen4_poller_set_direct_write_block_0_mode_handler(Gen4Poller* instance) {
  571. NfcCommand command = NfcCommandContinue;
  572. do {
  573. Gen4PollerError error = gen4_poller_set_direct_write_block_0_mode(
  574. instance, instance->password, instance->direct_write_block_0_mode);
  575. if(error != Gen4PollerErrorNone) {
  576. FURI_LOG_E(TAG, "Failed to set direct write to block 0 mode: %d", error);
  577. instance->state = Gen4PollerStateFail;
  578. break;
  579. }
  580. instance->state = Gen4PollerStateSuccess;
  581. } while(false);
  582. return command;
  583. }
  584. NfcCommand gen4_poller_success_handler(Gen4Poller* instance) {
  585. NfcCommand command = NfcCommandContinue;
  586. instance->gen4_event.type = Gen4PollerEventTypeSuccess;
  587. command = instance->callback(instance->gen4_event, instance->context);
  588. if(command != NfcCommandStop) {
  589. furi_delay_ms(100);
  590. }
  591. return command;
  592. }
  593. NfcCommand gen4_poller_fail_handler(Gen4Poller* instance) {
  594. NfcCommand command = NfcCommandContinue;
  595. instance->gen4_event.type = Gen4PollerEventTypeFail;
  596. command = instance->callback(instance->gen4_event, instance->context);
  597. if(command != NfcCommandStop) {
  598. furi_delay_ms(100);
  599. }
  600. return command;
  601. }
  602. static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNum] = {
  603. [Gen4PollerStateIdle] = gen4_poller_idle_handler,
  604. [Gen4PollerStateRequestMode] = gen4_poller_request_mode_handler,
  605. [Gen4PollerStateRequestWriteData] = gen4_poller_request_write_data_handler,
  606. [Gen4PollerStateWrite] = gen4_poller_write_handler,
  607. [Gen4PollerStateWipe] = gen4_poller_wipe_handler,
  608. [Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
  609. [Gen4PollerStateGetInfo] = gen4_poller_get_info_handler,
  610. [Gen4PollerStateSetDefaultConfig] = gen4_poller_set_default_cfg_handler,
  611. [Gen4PollerStateSetShadowMode] = gen4_poller_set_shadow_mode_handler,
  612. [Gen4PollerStateSetDirectWriteBlock0] = gen4_poller_set_direct_write_block_0_mode_handler,
  613. [Gen4PollerStateSuccess] = gen4_poller_success_handler,
  614. [Gen4PollerStateFail] = gen4_poller_fail_handler,
  615. };
  616. static NfcCommand gen4_poller_callback(NfcGenericEvent event, void* context) {
  617. furi_assert(context);
  618. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  619. furi_assert(event.event_data);
  620. furi_assert(event.instance);
  621. NfcCommand command = NfcCommandContinue;
  622. Gen4Poller* instance = context;
  623. instance->iso3_poller = event.instance;
  624. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  625. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  626. command = gen4_poller_state_handlers[instance->state](instance);
  627. }
  628. return command;
  629. }
  630. void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context) {
  631. furi_assert(instance);
  632. furi_assert(callback);
  633. instance->callback = callback;
  634. instance->context = context;
  635. nfc_poller_start(instance->poller, gen4_poller_callback, instance);
  636. }
  637. void gen4_poller_stop(Gen4Poller* instance) {
  638. furi_assert(instance);
  639. nfc_poller_stop(instance->poller);
  640. }