gen4_poller.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. #include "bit_buffer.h"
  2. #include "core/check.h"
  3. #include "gen4_poller_i.h"
  4. #include "magic/protocols/gen4/gen4.h"
  5. #include "magic/protocols/gen4/gen4_poller.h"
  6. #include <nfc/protocols/iso14443_3a/iso14443_3a.h>
  7. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  8. #include <nfc/nfc_poller.h>
  9. #include <bit_lib.h>
  10. #include <string.h>
  11. #define GEN4_POLLER_THREAD_FLAG_DETECTED (1U << 0)
  12. #define GEN4_POLLER_DEFAULT_CONFIG_SIZE (28)
  13. typedef NfcCommand (*Gen4PollerStateHandler)(Gen4Poller* instance);
  14. typedef struct {
  15. NfcPoller* poller;
  16. Gen4Password password;
  17. Gen4 gen4_data;
  18. BitBuffer* tx_buffer;
  19. BitBuffer* rx_buffer;
  20. FuriThreadId thread_id;
  21. Gen4PollerError error;
  22. } Gen4PollerDetectContext;
  23. static const Gen4Config gen4_poller_default_config = {
  24. .data_raw = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x78,
  25. 0x00, 0x91, 0x02, 0xDA, 0xBC, 0x19, 0x10, 0x10, 0x11, 0x12,
  26. 0x13, 0x14, 0x15, 0x16, 0x04, 0x00, 0x08, 0x00}};
  27. static const uint8_t gen4_poller_default_block_0[GEN4_POLLER_BLOCK_SIZE] =
  28. {0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  29. static const uint8_t gen4_poller_default_empty_block[GEN4_POLLER_BLOCK_SIZE] = {0};
  30. static const uint8_t gen4_poller_default_sector_trailer_block[GEN4_POLLER_BLOCK_SIZE] =
  31. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  32. static bool gen4_poller_is_sector_trailer(uint8_t block_num) {
  33. uint8_t sec_tr_block_num = 0;
  34. if(block_num < 128) {
  35. sec_tr_block_num = block_num | 0x03;
  36. } else {
  37. sec_tr_block_num = block_num | 0x0f;
  38. }
  39. return block_num == sec_tr_block_num;
  40. }
  41. Gen4Poller* gen4_poller_alloc(Nfc* nfc) {
  42. furi_assert(nfc);
  43. Gen4Poller* instance = malloc(sizeof(Gen4Poller));
  44. instance->poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
  45. instance->gen4_event.data = &instance->gen4_event_data;
  46. instance->tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  47. instance->rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  48. instance->gen4_data = gen4_alloc();
  49. return instance;
  50. }
  51. void gen4_poller_free(Gen4Poller* instance) {
  52. furi_assert(instance);
  53. nfc_poller_free(instance->poller);
  54. bit_buffer_free(instance->tx_buffer);
  55. bit_buffer_free(instance->rx_buffer);
  56. gen4_free(instance->gen4_data);
  57. free(instance);
  58. }
  59. void gen4_poller_set_password(Gen4Poller* instance, Gen4Password password) {
  60. furi_assert(instance);
  61. instance->password = password;
  62. }
  63. NfcCommand gen4_poller_detect_callback(NfcGenericEvent event, void* context) {
  64. furi_assert(context);
  65. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  66. furi_assert(event.instance);
  67. furi_assert(event.event_data);
  68. NfcCommand command = NfcCommandStop;
  69. Gen4PollerDetectContext* gen4_poller_detect_ctx = context;
  70. Iso14443_3aPoller* iso3_poller = event.instance;
  71. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  72. gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
  73. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  74. do {
  75. // check config
  76. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
  77. bit_buffer_append_bytes(
  78. gen4_poller_detect_ctx->tx_buffer,
  79. gen4_poller_detect_ctx->password.bytes,
  80. GEN4_PASSWORD_LEN);
  81. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_CFG);
  82. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  83. iso3_poller,
  84. gen4_poller_detect_ctx->tx_buffer,
  85. gen4_poller_detect_ctx->rx_buffer,
  86. GEN4_POLLER_MAX_FWT);
  87. if(error != Iso14443_3aErrorNone) {
  88. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  89. break;
  90. }
  91. size_t rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
  92. if(rx_bytes != GEN4_CONFIG_SIZE) {
  93. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  94. break;
  95. }
  96. memcpy(
  97. gen4_poller_detect_ctx->gen4_data.config.data_raw,
  98. bit_buffer_get_data(gen4_poller_detect_ctx->rx_buffer),
  99. GEN4_CONFIG_SIZE);
  100. // check revision
  101. bit_buffer_reset(gen4_poller_detect_ctx->tx_buffer);
  102. bit_buffer_reset(gen4_poller_detect_ctx->rx_buffer);
  103. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_PREFIX);
  104. bit_buffer_append_bytes(
  105. gen4_poller_detect_ctx->tx_buffer,
  106. gen4_poller_detect_ctx->password.bytes,
  107. GEN4_PASSWORD_LEN);
  108. bit_buffer_append_byte(gen4_poller_detect_ctx->tx_buffer, GEN4_CMD_GET_REVISION);
  109. error = iso14443_3a_poller_send_standard_frame(
  110. iso3_poller,
  111. gen4_poller_detect_ctx->tx_buffer,
  112. gen4_poller_detect_ctx->rx_buffer,
  113. GEN4_POLLER_MAX_FWT);
  114. if(error != Iso14443_3aErrorNone) {
  115. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  116. break;
  117. }
  118. rx_bytes = bit_buffer_get_size_bytes(gen4_poller_detect_ctx->rx_buffer);
  119. if(rx_bytes != GEN4_REVISION_SIZE) {
  120. gen4_poller_detect_ctx->error = Gen4PollerErrorProtocol;
  121. break;
  122. }
  123. memcpy(
  124. gen4_poller_detect_ctx->gen4_data.revision.data,
  125. bit_buffer_get_data(gen4_poller_detect_ctx->rx_buffer),
  126. GEN4_REVISION_SIZE);
  127. gen4_poller_detect_ctx->error = Gen4PollerErrorNone;
  128. } while(false);
  129. } else if(iso3_event->type == Iso14443_3aPollerEventTypeError) {
  130. gen4_poller_detect_ctx->error = Gen4PollerErrorTimeout;
  131. }
  132. furi_thread_flags_set(gen4_poller_detect_ctx->thread_id, GEN4_POLLER_THREAD_FLAG_DETECTED);
  133. return command;
  134. }
  135. Gen4PollerError gen4_poller_detect(Nfc* nfc, Gen4Password password, Gen4* gen4_data) {
  136. furi_assert(nfc);
  137. Gen4PollerDetectContext gen4_poller_detect_ctx = {};
  138. gen4_poller_detect_ctx.poller = nfc_poller_alloc(nfc, NfcProtocolIso14443_3a);
  139. gen4_poller_detect_ctx.password = password;
  140. gen4_poller_detect_ctx.tx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  141. gen4_poller_detect_ctx.rx_buffer = bit_buffer_alloc(GEN4_POLLER_MAX_BUFFER_SIZE);
  142. gen4_poller_detect_ctx.thread_id = furi_thread_get_current_id();
  143. gen4_poller_detect_ctx.error = Gen4PollerErrorNone;
  144. nfc_poller_start(
  145. gen4_poller_detect_ctx.poller, gen4_poller_detect_callback, &gen4_poller_detect_ctx);
  146. uint32_t flags =
  147. furi_thread_flags_wait(GEN4_POLLER_THREAD_FLAG_DETECTED, FuriFlagWaitAny, FuriWaitForever);
  148. if(flags & GEN4_POLLER_THREAD_FLAG_DETECTED) {
  149. furi_thread_flags_clear(GEN4_POLLER_THREAD_FLAG_DETECTED);
  150. }
  151. nfc_poller_stop(gen4_poller_detect_ctx.poller);
  152. nfc_poller_free(gen4_poller_detect_ctx.poller);
  153. bit_buffer_free(gen4_poller_detect_ctx.tx_buffer);
  154. bit_buffer_free(gen4_poller_detect_ctx.rx_buffer);
  155. if(gen4_poller_detect_ctx.error == Gen4PollerErrorNone) {
  156. gen4_copy(gen4_data, &gen4_poller_detect_ctx.gen4_data);
  157. }
  158. return gen4_poller_detect_ctx.error;
  159. }
  160. NfcCommand gen4_poller_idle_handler(Gen4Poller* instance) {
  161. NfcCommand command = NfcCommandContinue;
  162. instance->current_block = 0;
  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. gen4_password_reset(&instance->password);
  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. FURI_LOG_D(TAG, "NTAG type");
  319. instance->config.data_parsed.mfu_mode = Gen4UltralightModeNTAG;
  320. instance->total_blocks = 64 * 2;
  321. break;
  322. case MfUltralightTypeUnknown:
  323. FURI_LOG_D(TAG, "Ultralight type");
  324. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL;
  325. break;
  326. case MfUltralightTypeMfulC:
  327. FURI_LOG_D(TAG, "MfulC type");
  328. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_C;
  329. break;
  330. case MfUltralightTypeUL11:
  331. case MfUltralightTypeUL21:
  332. default:
  333. FURI_LOG_D(TAG, "EV1 type");
  334. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
  335. break;
  336. }
  337. if(iso3_data->uid_len == 4) {
  338. instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
  339. } else if(iso3_data->uid_len == 7) {
  340. instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
  341. } else {
  342. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  343. instance->state = Gen4PollerStateFail;
  344. break;
  345. }
  346. instance->config.data_parsed.gtu_mode = Gen4ShadowModeHighSpeedDisabled;
  347. instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
  348. instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
  349. instance->config.data_parsed.sak = iso3_data->sak;
  350. instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
  351. instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
  352. Gen4PollerError error = gen4_poller_set_config(
  353. instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
  354. if(error != Gen4PollerErrorNone) {
  355. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  356. instance->state = Gen4PollerStateFail;
  357. break;
  358. }
  359. }
  360. if(instance->current_block < mfu_data->pages_read) {
  361. FURI_LOG_D(
  362. TAG, "Writing page %zu / %zu", instance->current_block, mfu_data->pages_read);
  363. Gen4PollerError error = gen4_poller_write_block(
  364. instance,
  365. instance->password,
  366. instance->current_block,
  367. mfu_data->page[instance->current_block].data);
  368. if(error != Gen4PollerErrorNone) {
  369. FURI_LOG_D(TAG, "Failed to write %d page: %d", instance->current_block, error);
  370. instance->state = Gen4PollerStateFail;
  371. break;
  372. }
  373. instance->current_block++;
  374. } else {
  375. uint8_t block[GEN4_POLLER_BLOCK_SIZE] = {};
  376. bool write_success = true;
  377. if(mf_ultralight_support_feature(
  378. mf_ultralight_get_feature_support_set(mfu_data->type),
  379. MfUltralightFeatureSupportReadSignature)) {
  380. FURI_LOG_D(TAG, "Writing Signature");
  381. for(size_t i = 0; i < 8; i++) {
  382. memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
  383. Gen4PollerError error =
  384. gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
  385. if(error != Gen4PollerErrorNone) {
  386. write_success = false;
  387. break;
  388. }
  389. }
  390. if(!write_success) {
  391. FURI_LOG_E(TAG, "Failed to write Signature");
  392. instance->state = Gen4PollerStateFail;
  393. break;
  394. }
  395. } else {
  396. FURI_LOG_D(TAG, "Signature is not supported, skipping");
  397. }
  398. if(mf_ultralight_support_feature(
  399. mf_ultralight_get_feature_support_set(mfu_data->type),
  400. MfUltralightFeatureSupportReadVersion)) {
  401. FURI_LOG_D(TAG, "Writing Version part 1");
  402. block[0] = mfu_data->version.header;
  403. block[1] = mfu_data->version.vendor_id;
  404. block[2] = mfu_data->version.prod_type;
  405. block[3] = mfu_data->version.prod_subtype;
  406. Gen4PollerError error =
  407. gen4_poller_write_block(instance, instance->password, 0xFA, block);
  408. if(error != Gen4PollerErrorNone) {
  409. FURI_LOG_E(TAG, "Failed to write 1st part Version");
  410. instance->state = Gen4PollerStateFail;
  411. break;
  412. }
  413. FURI_LOG_D(TAG, "Writing Version part 2");
  414. block[0] = mfu_data->version.prod_ver_major;
  415. block[1] = mfu_data->version.prod_ver_minor;
  416. block[2] = mfu_data->version.storage_size;
  417. block[3] = mfu_data->version.protocol_type;
  418. error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
  419. if(error != Gen4PollerErrorNone) {
  420. FURI_LOG_E(TAG, "Failed to write 2nd part Version");
  421. instance->state = Gen4PollerStateFail;
  422. break;
  423. }
  424. } else {
  425. FURI_LOG_D(TAG, "Version is not supported, skipping");
  426. }
  427. if(mf_ultralight_support_feature(
  428. mf_ultralight_get_feature_support_set(mfu_data->type),
  429. MfUltralightFeatureSupportPasswordAuth)) {
  430. FURI_LOG_D(TAG, "Writing Password");
  431. MfUltralightConfigPages* config_pages = NULL;
  432. if(mf_ultralight_get_config_page(mfu_data, &config_pages)) {
  433. block[0] = config_pages->password.data[0];
  434. block[1] = config_pages->password.data[1];
  435. block[2] = config_pages->password.data[2];
  436. block[3] = config_pages->password.data[3];
  437. Gen4PollerError error =
  438. gen4_poller_write_block(instance, instance->password, 0xE5, block);
  439. if(error != Gen4PollerErrorNone) {
  440. FURI_LOG_E(TAG, "Failed to write Password to sector E5");
  441. instance->state = Gen4PollerStateFail;
  442. break;
  443. }
  444. error = gen4_poller_write_block(instance, instance->password, 0xF0, block);
  445. if(error != Gen4PollerErrorNone) {
  446. FURI_LOG_E(TAG, "Failed to write Password to sector F0");
  447. instance->state = Gen4PollerStateFail;
  448. break;
  449. }
  450. FURI_LOG_D(TAG, "Writing PACK");
  451. block[0] = config_pages->pack.data[0];
  452. block[1] = config_pages->pack.data[1];
  453. block[2] = 0x00;
  454. block[3] = 0x00;
  455. error = gen4_poller_write_block(instance, instance->password, 0xE6, block);
  456. if(error != Gen4PollerErrorNone) {
  457. FURI_LOG_E(TAG, "Failed to write PACK to sector E6");
  458. instance->state = Gen4PollerStateFail;
  459. break;
  460. }
  461. error = gen4_poller_write_block(instance, instance->password, 0xF1, block);
  462. if(error != Gen4PollerErrorNone) {
  463. FURI_LOG_E(TAG, "Failed to write PACK to sector F1");
  464. instance->state = Gen4PollerStateFail;
  465. break;
  466. }
  467. }
  468. } else {
  469. FURI_LOG_D(TAG, "Password is not supported, skipping");
  470. }
  471. instance->state = Gen4PollerStateSuccess;
  472. }
  473. } while(false);
  474. return command;
  475. }
  476. NfcCommand gen4_poller_write_handler(Gen4Poller* instance) {
  477. NfcCommand command = NfcCommandContinue;
  478. memcpy(
  479. instance->config.data_raw,
  480. gen4_poller_default_config.data_raw,
  481. GEN4_POLLER_DEFAULT_CONFIG_SIZE);
  482. memcpy(
  483. instance->config.data_parsed.password.bytes, instance->password.bytes, GEN4_PASSWORD_LEN);
  484. memset(&instance->config.data_raw[7], 0, 17);
  485. if(instance->protocol == NfcProtocolMfClassic) {
  486. command = gen4_poller_write_mf_classic(instance);
  487. } else if(instance->protocol == NfcProtocolMfUltralight) {
  488. command = gen4_poller_write_mf_ultralight(instance);
  489. } else {
  490. furi_crash("Unsupported protocol to write");
  491. }
  492. return command;
  493. }
  494. NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
  495. NfcCommand command = NfcCommandContinue;
  496. do {
  497. instance->gen4_event.type = Gen4PollerEventTypeRequestNewPassword;
  498. command = instance->callback(instance->gen4_event, instance->context);
  499. if(command != NfcCommandContinue) break;
  500. Gen4Password new_password = instance->gen4_event_data.request_password.password;
  501. Gen4PollerError error =
  502. gen4_poller_change_password(instance, instance->password, new_password);
  503. if(error != Gen4PollerErrorNone) {
  504. FURI_LOG_E(TAG, "Failed to change password: %d", error);
  505. instance->state = Gen4PollerStateFail;
  506. break;
  507. }
  508. instance->password = new_password;
  509. instance->state = Gen4PollerStateSuccess;
  510. } while(false);
  511. return command;
  512. }
  513. NfcCommand gen4_poller_set_default_cfg_handler(Gen4Poller* instance) {
  514. NfcCommand command = NfcCommandContinue;
  515. do {
  516. Gen4PollerError error = gen4_poller_set_config(
  517. instance,
  518. instance->password,
  519. &gen4_poller_default_config,
  520. GEN4_POLLER_DEFAULT_CONFIG_SIZE,
  521. false);
  522. if(error != Gen4PollerErrorNone) {
  523. FURI_LOG_E(TAG, "Failed to set default config: %d", error);
  524. instance->state = Gen4PollerStateFail;
  525. break;
  526. }
  527. instance->state = Gen4PollerStateSuccess;
  528. } while(false);
  529. return command;
  530. }
  531. NfcCommand gen4_poller_get_current_cfg_handler(Gen4Poller* instance) {
  532. NfcCommand command = NfcCommandContinue;
  533. do {
  534. Gen4Config config = {};
  535. Gen4PollerError error = gen4_poller_get_config(instance, instance->password, &config);
  536. if(error != Gen4PollerErrorNone) {
  537. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  538. instance->state = Gen4PollerStateFail;
  539. break;
  540. }
  541. // Copy config data to event data buffer
  542. memcpy(instance->gen4_data->config.data_raw, config.data_raw, sizeof(config));
  543. instance->state = Gen4PollerStateSuccess;
  544. } while(false);
  545. return command;
  546. }
  547. NfcCommand gen4_poller_get_revision_handler(Gen4Poller* instance) {
  548. NfcCommand command = NfcCommandContinue;
  549. do {
  550. Gen4Revision revision = {};
  551. Gen4PollerError error = gen4_poller_get_revision(instance, instance->password, &revision);
  552. if(error != Gen4PollerErrorNone) {
  553. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  554. instance->state = Gen4PollerStateFail;
  555. break;
  556. }
  557. // Copy revision data to event data buffer
  558. memcpy(instance->gen4_data->revision.data, revision.data, sizeof(revision));
  559. instance->state = Gen4PollerStateSuccess;
  560. } while(false);
  561. return command;
  562. }
  563. NfcCommand gen4_poller_get_info_handler(Gen4Poller* instance) {
  564. NfcCommand command = NfcCommandContinue;
  565. do {
  566. Gen4 gen4_data;
  567. Gen4PollerError error =
  568. gen4_poller_get_revision(instance, instance->password, &gen4_data.revision);
  569. if(error != Gen4PollerErrorNone) {
  570. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  571. instance->state = Gen4PollerStateFail;
  572. break;
  573. }
  574. error = gen4_poller_get_config(instance, instance->password, &gen4_data.config);
  575. if(error != Gen4PollerErrorNone) {
  576. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  577. instance->state = Gen4PollerStateFail;
  578. break;
  579. }
  580. // Copy config&&revision data to event data buffer
  581. gen4_copy(instance->gen4_data, &gen4_data);
  582. instance->state = Gen4PollerStateSuccess;
  583. } while(false);
  584. return command;
  585. }
  586. NfcCommand gen4_poller_set_shadow_mode_handler(Gen4Poller* instance) {
  587. NfcCommand command = NfcCommandContinue;
  588. do {
  589. Gen4PollerError error =
  590. gen4_poller_set_shadow_mode(instance, instance->password, instance->shadow_mode);
  591. if(error != Gen4PollerErrorNone) {
  592. FURI_LOG_E(TAG, "Failed to set shadow mode: %d", error);
  593. instance->state = Gen4PollerStateFail;
  594. break;
  595. }
  596. instance->state = Gen4PollerStateSuccess;
  597. } while(false);
  598. return command;
  599. }
  600. NfcCommand gen4_poller_set_direct_write_block_0_mode_handler(Gen4Poller* instance) {
  601. NfcCommand command = NfcCommandContinue;
  602. do {
  603. Gen4PollerError error = gen4_poller_set_direct_write_block_0_mode(
  604. instance, instance->password, instance->direct_write_block_0_mode);
  605. if(error != Gen4PollerErrorNone) {
  606. FURI_LOG_E(TAG, "Failed to set direct write to block 0 mode: %d", error);
  607. instance->state = Gen4PollerStateFail;
  608. break;
  609. }
  610. instance->state = Gen4PollerStateSuccess;
  611. } while(false);
  612. return command;
  613. }
  614. NfcCommand gen4_poller_success_handler(Gen4Poller* instance) {
  615. NfcCommand command = NfcCommandContinue;
  616. instance->gen4_event.type = Gen4PollerEventTypeSuccess;
  617. command = instance->callback(instance->gen4_event, instance->context);
  618. if(command != NfcCommandStop) {
  619. furi_delay_ms(100);
  620. }
  621. return command;
  622. }
  623. NfcCommand gen4_poller_fail_handler(Gen4Poller* instance) {
  624. NfcCommand command = NfcCommandContinue;
  625. instance->gen4_event.type = Gen4PollerEventTypeFail;
  626. command = instance->callback(instance->gen4_event, instance->context);
  627. if(command != NfcCommandStop) {
  628. furi_delay_ms(100);
  629. }
  630. return command;
  631. }
  632. static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNum] = {
  633. [Gen4PollerStateIdle] = gen4_poller_idle_handler,
  634. [Gen4PollerStateRequestMode] = gen4_poller_request_mode_handler,
  635. [Gen4PollerStateRequestWriteData] = gen4_poller_request_write_data_handler,
  636. [Gen4PollerStateWrite] = gen4_poller_write_handler,
  637. [Gen4PollerStateWipe] = gen4_poller_wipe_handler,
  638. [Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
  639. [Gen4PollerStateGetInfo] = gen4_poller_get_info_handler,
  640. [Gen4PollerStateSetDefaultConfig] = gen4_poller_set_default_cfg_handler,
  641. [Gen4PollerStateSetShadowMode] = gen4_poller_set_shadow_mode_handler,
  642. [Gen4PollerStateSetDirectWriteBlock0] = gen4_poller_set_direct_write_block_0_mode_handler,
  643. [Gen4PollerStateSuccess] = gen4_poller_success_handler,
  644. [Gen4PollerStateFail] = gen4_poller_fail_handler,
  645. };
  646. static NfcCommand gen4_poller_callback(NfcGenericEvent event, void* context) {
  647. furi_assert(context);
  648. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  649. furi_assert(event.event_data);
  650. furi_assert(event.instance);
  651. NfcCommand command = NfcCommandContinue;
  652. Gen4Poller* instance = context;
  653. instance->iso3_poller = event.instance;
  654. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  655. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  656. command = gen4_poller_state_handlers[instance->state](instance);
  657. }
  658. return command;
  659. }
  660. void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context) {
  661. furi_assert(instance);
  662. furi_assert(callback);
  663. instance->callback = callback;
  664. instance->context = context;
  665. nfc_poller_start(instance->poller, gen4_poller_callback, instance);
  666. }
  667. void gen4_poller_stop(Gen4Poller* instance) {
  668. furi_assert(instance);
  669. nfc_poller_stop(instance->poller);
  670. }
  671. const Gen4* gen4_poller_get_gen4_data(const Gen4Poller* instance) {
  672. furi_assert(instance);
  673. return instance->gen4_data;
  674. }
  675. void gen4_poller_struct_set_direct_write_block_0_mode(
  676. Gen4Poller* instance,
  677. Gen4DirectWriteBlock0Mode mode) {
  678. furi_assert(instance);
  679. instance->direct_write_block_0_mode = mode;
  680. }
  681. void gen4_poller_struct_set_shadow_mode(Gen4Poller* instance, Gen4ShadowMode mode) {
  682. furi_assert(instance);
  683. instance->shadow_mode = mode;
  684. }