gen4_poller.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. return gen4_poller_detect_ctx.error;
  158. }
  159. NfcCommand gen4_poller_idle_handler(Gen4Poller* instance) {
  160. NfcCommand command = NfcCommandContinue;
  161. instance->current_block = 0;
  162. instance->gen4_event.type = Gen4PollerEventTypeCardDetected;
  163. command = instance->callback(instance->gen4_event, instance->context);
  164. instance->state = Gen4PollerStateRequestMode;
  165. return command;
  166. }
  167. NfcCommand gen4_poller_request_mode_handler(Gen4Poller* instance) {
  168. NfcCommand command = NfcCommandContinue;
  169. instance->gen4_event.type = Gen4PollerEventTypeRequestMode;
  170. command = instance->callback(instance->gen4_event, instance->context);
  171. if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWipe) {
  172. instance->state = Gen4PollerStateWipe;
  173. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeWrite) {
  174. instance->state = Gen4PollerStateRequestWriteData;
  175. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetPassword) {
  176. instance->state = Gen4PollerStateChangePassword;
  177. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeGetInfo) {
  178. instance->state = Gen4PollerStateGetInfo;
  179. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDefaultCfg) {
  180. instance->state = Gen4PollerStateSetDefaultConfig;
  181. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetShadowMode) {
  182. instance->state = Gen4PollerStateSetShadowMode;
  183. } else if(instance->gen4_event_data.request_mode.mode == Gen4PollerModeSetDirectWriteBlock0Mode) {
  184. instance->state = Gen4PollerStateSetDirectWriteBlock0;
  185. } else {
  186. instance->state = Gen4PollerStateFail;
  187. }
  188. return command;
  189. }
  190. NfcCommand gen4_poller_wipe_handler(Gen4Poller* instance) {
  191. NfcCommand command = NfcCommandContinue;
  192. do {
  193. Gen4PollerError error = Gen4PollerErrorNone;
  194. if(instance->current_block == 0) {
  195. error = gen4_poller_set_config(
  196. instance,
  197. instance->password,
  198. &gen4_poller_default_config,
  199. GEN4_POLLER_DEFAULT_CONFIG_SIZE,
  200. false);
  201. if(error != Gen4PollerErrorNone) {
  202. FURI_LOG_D(TAG, "Failed to set default config: %d", error);
  203. instance->state = Gen4PollerStateFail;
  204. break;
  205. }
  206. gen4_password_reset(&instance->password);
  207. error = gen4_poller_write_block(
  208. instance, instance->password, instance->current_block, gen4_poller_default_block_0);
  209. if(error != Gen4PollerErrorNone) {
  210. FURI_LOG_D(TAG, "Failed to write 0 block: %d", error);
  211. instance->state = Gen4PollerStateFail;
  212. break;
  213. }
  214. } else if(instance->current_block < GEN4_POLLER_BLOCKS_TOTAL) {
  215. const uint8_t* block = gen4_poller_is_sector_trailer(instance->current_block) ?
  216. gen4_poller_default_sector_trailer_block :
  217. gen4_poller_default_empty_block;
  218. error = gen4_poller_write_block(
  219. instance, instance->password, instance->current_block, block);
  220. if(error != Gen4PollerErrorNone) {
  221. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  222. instance->state = Gen4PollerStateFail;
  223. break;
  224. }
  225. } else {
  226. instance->state = Gen4PollerStateSuccess;
  227. break;
  228. }
  229. instance->current_block++;
  230. } while(false);
  231. return command;
  232. }
  233. NfcCommand gen4_poller_request_write_data_handler(Gen4Poller* instance) {
  234. NfcCommand command = NfcCommandContinue;
  235. instance->gen4_event.type = Gen4PollerEventTypeRequestDataToWrite;
  236. command = instance->callback(instance->gen4_event, instance->context);
  237. instance->protocol = instance->gen4_event_data.request_data.protocol;
  238. instance->data = instance->gen4_event_data.request_data.data;
  239. if((instance->protocol == NfcProtocolMfClassic) ||
  240. (instance->protocol == NfcProtocolMfUltralight)) {
  241. instance->state = Gen4PollerStateWrite;
  242. } else {
  243. FURI_LOG_E(TAG, "Unsupported protocol");
  244. instance->state = Gen4PollerStateFail;
  245. }
  246. return command;
  247. }
  248. static NfcCommand gen4_poller_write_mf_classic(Gen4Poller* instance) {
  249. NfcCommand command = NfcCommandContinue;
  250. do {
  251. const MfClassicData* mfc_data = instance->data;
  252. const Iso14443_3aData* iso3_data = mfc_data->iso14443_3a_data;
  253. if(instance->current_block == 0) {
  254. instance->config.data_parsed.protocol = Gen4ProtocolMfClassic;
  255. instance->total_blocks = mf_classic_get_total_block_num(mfc_data->type);
  256. if(iso3_data->uid_len == 4) {
  257. instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
  258. } else if(iso3_data->uid_len == 7) {
  259. instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
  260. } else {
  261. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  262. instance->state = Gen4PollerStateFail;
  263. break;
  264. }
  265. instance->config.data_parsed.gtu_mode = Gen4ShadowModeDisabled;
  266. instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
  267. instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
  268. instance->config.data_parsed.sak = iso3_data->sak;
  269. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
  270. instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
  271. instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
  272. Gen4PollerError error = gen4_poller_set_config(
  273. instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
  274. if(error != Gen4PollerErrorNone) {
  275. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  276. instance->state = Gen4PollerStateFail;
  277. break;
  278. }
  279. }
  280. if(instance->current_block < instance->total_blocks) {
  281. FURI_LOG_D(TAG, "Writing block %d", instance->current_block);
  282. Gen4PollerError error = gen4_poller_write_block(
  283. instance,
  284. instance->password,
  285. instance->current_block,
  286. mfc_data->block[instance->current_block].data);
  287. if(error != Gen4PollerErrorNone) {
  288. FURI_LOG_D(TAG, "Failed to write %d block: %d", instance->current_block, error);
  289. instance->state = Gen4PollerStateFail;
  290. break;
  291. }
  292. } else {
  293. instance->state = Gen4PollerStateSuccess;
  294. break;
  295. }
  296. instance->current_block++;
  297. } while(false);
  298. return command;
  299. }
  300. static NfcCommand gen4_poller_write_mf_ultralight(Gen4Poller* instance) {
  301. NfcCommand command = NfcCommandContinue;
  302. do {
  303. const MfUltralightData* mfu_data = instance->data;
  304. const Iso14443_3aData* iso3_data = mfu_data->iso14443_3a_data;
  305. if(instance->current_block == 0) {
  306. instance->total_blocks = 64;
  307. instance->config.data_parsed.protocol = Gen4ProtocolMfUltralight;
  308. switch(mfu_data->type) {
  309. case MfUltralightTypeNTAG203:
  310. case MfUltralightTypeNTAG213:
  311. case MfUltralightTypeNTAG215:
  312. case MfUltralightTypeNTAG216:
  313. case MfUltralightTypeNTAGI2C1K:
  314. case MfUltralightTypeNTAGI2C2K:
  315. case MfUltralightTypeNTAGI2CPlus1K:
  316. case MfUltralightTypeNTAGI2CPlus2K:
  317. FURI_LOG_D(TAG, "NTAG type");
  318. instance->config.data_parsed.mfu_mode = Gen4UltralightModeNTAG;
  319. instance->total_blocks = 64 * 2;
  320. break;
  321. case MfUltralightTypeUnknown:
  322. FURI_LOG_D(TAG, "Ultralight type");
  323. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL;
  324. break;
  325. case MfUltralightTypeMfulC:
  326. FURI_LOG_D(TAG, "MfulC type");
  327. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_C;
  328. break;
  329. case MfUltralightTypeUL11:
  330. case MfUltralightTypeUL21:
  331. default:
  332. FURI_LOG_D(TAG, "EV1 type");
  333. instance->config.data_parsed.mfu_mode = Gen4UltralightModeUL_EV1;
  334. break;
  335. }
  336. if(iso3_data->uid_len == 4) {
  337. instance->config.data_parsed.uid_len_code = Gen4UIDLengthSingle;
  338. } else if(iso3_data->uid_len == 7) {
  339. instance->config.data_parsed.uid_len_code = Gen4UIDLengthDouble;
  340. } else {
  341. FURI_LOG_E(TAG, "Unsupported UID len: %d", iso3_data->uid_len);
  342. instance->state = Gen4PollerStateFail;
  343. break;
  344. }
  345. instance->config.data_parsed.gtu_mode = Gen4ShadowModeHighSpeedDisabled;
  346. instance->config.data_parsed.atqa[0] = iso3_data->atqa[0];
  347. instance->config.data_parsed.atqa[1] = iso3_data->atqa[1];
  348. instance->config.data_parsed.sak = iso3_data->sak;
  349. instance->config.data_parsed.total_blocks = instance->total_blocks - 1;
  350. instance->config.data_parsed.direct_write_mode = Gen4DirectWriteBlock0ModeDisabled;
  351. Gen4PollerError error = gen4_poller_set_config(
  352. instance, instance->password, &instance->config, GEN4_CONFIG_SIZE, false);
  353. if(error != Gen4PollerErrorNone) {
  354. FURI_LOG_D(TAG, "Failed to write config: %d", error);
  355. instance->state = Gen4PollerStateFail;
  356. break;
  357. }
  358. }
  359. if(instance->current_block < mfu_data->pages_read) {
  360. FURI_LOG_D(
  361. TAG, "Writing page %zu / %zu", instance->current_block, mfu_data->pages_read);
  362. Gen4PollerError error = gen4_poller_write_block(
  363. instance,
  364. instance->password,
  365. instance->current_block,
  366. mfu_data->page[instance->current_block].data);
  367. if(error != Gen4PollerErrorNone) {
  368. FURI_LOG_D(TAG, "Failed to write %d page: %d", instance->current_block, error);
  369. instance->state = Gen4PollerStateFail;
  370. break;
  371. }
  372. instance->current_block++;
  373. } else {
  374. uint8_t block[GEN4_POLLER_BLOCK_SIZE] = {};
  375. bool write_success = true;
  376. if(mf_ultralight_support_feature(
  377. mf_ultralight_get_feature_support_set(mfu_data->type),
  378. MfUltralightFeatureSupportReadSignature)) {
  379. FURI_LOG_D(TAG, "Writing Signature");
  380. for(size_t i = 0; i < 8; i++) {
  381. memcpy(block, &mfu_data->signature.data[i * 4], 4); //-V1086
  382. Gen4PollerError error =
  383. gen4_poller_write_block(instance, instance->password, 0xF2 + i, block);
  384. if(error != Gen4PollerErrorNone) {
  385. write_success = false;
  386. break;
  387. }
  388. }
  389. if(!write_success) {
  390. FURI_LOG_E(TAG, "Failed to write Signature");
  391. instance->state = Gen4PollerStateFail;
  392. break;
  393. }
  394. } else {
  395. FURI_LOG_D(TAG, "Signature is not supported, skipping");
  396. }
  397. if(mf_ultralight_support_feature(
  398. mf_ultralight_get_feature_support_set(mfu_data->type),
  399. MfUltralightFeatureSupportReadVersion)) {
  400. FURI_LOG_D(TAG, "Writing Version part 1");
  401. block[0] = mfu_data->version.header;
  402. block[1] = mfu_data->version.vendor_id;
  403. block[2] = mfu_data->version.prod_type;
  404. block[3] = mfu_data->version.prod_subtype;
  405. Gen4PollerError error =
  406. gen4_poller_write_block(instance, instance->password, 0xFA, block);
  407. if(error != Gen4PollerErrorNone) {
  408. FURI_LOG_E(TAG, "Failed to write 1st part Version");
  409. instance->state = Gen4PollerStateFail;
  410. break;
  411. }
  412. FURI_LOG_D(TAG, "Writing Version part 2");
  413. block[0] = mfu_data->version.prod_ver_major;
  414. block[1] = mfu_data->version.prod_ver_minor;
  415. block[2] = mfu_data->version.storage_size;
  416. block[3] = mfu_data->version.protocol_type;
  417. error = gen4_poller_write_block(instance, instance->password, 0xFB, block);
  418. if(error != Gen4PollerErrorNone) {
  419. FURI_LOG_E(TAG, "Failed to write 2nd part Version");
  420. instance->state = Gen4PollerStateFail;
  421. break;
  422. }
  423. } else {
  424. FURI_LOG_D(TAG, "Version is not supported, skipping");
  425. }
  426. if(mf_ultralight_support_feature(
  427. mf_ultralight_get_feature_support_set(mfu_data->type),
  428. MfUltralightFeatureSupportPasswordAuth)) {
  429. FURI_LOG_D(TAG, "Writing Password");
  430. MfUltralightConfigPages* config_pages = NULL;
  431. if(mf_ultralight_get_config_page(mfu_data, &config_pages)) {
  432. block[0] = config_pages->password.data[0];
  433. block[1] = config_pages->password.data[1];
  434. block[2] = config_pages->password.data[2];
  435. block[3] = config_pages->password.data[3];
  436. Gen4PollerError error =
  437. gen4_poller_write_block(instance, instance->password, 0xE5, block);
  438. if(error != Gen4PollerErrorNone) {
  439. FURI_LOG_E(TAG, "Failed to write Password to sector E5");
  440. instance->state = Gen4PollerStateFail;
  441. break;
  442. }
  443. error = gen4_poller_write_block(instance, instance->password, 0xF0, block);
  444. if(error != Gen4PollerErrorNone) {
  445. FURI_LOG_E(TAG, "Failed to write Password to sector F0");
  446. instance->state = Gen4PollerStateFail;
  447. break;
  448. }
  449. FURI_LOG_D(TAG, "Writing PACK");
  450. block[0] = config_pages->pack.data[0];
  451. block[1] = config_pages->pack.data[1];
  452. block[2] = 0x00;
  453. block[3] = 0x00;
  454. error = gen4_poller_write_block(instance, instance->password, 0xE6, block);
  455. if(error != Gen4PollerErrorNone) {
  456. FURI_LOG_E(TAG, "Failed to write PACK to sector E6");
  457. instance->state = Gen4PollerStateFail;
  458. break;
  459. }
  460. error = gen4_poller_write_block(instance, instance->password, 0xF1, block);
  461. if(error != Gen4PollerErrorNone) {
  462. FURI_LOG_E(TAG, "Failed to write PACK to sector F1");
  463. instance->state = Gen4PollerStateFail;
  464. break;
  465. }
  466. }
  467. } else {
  468. FURI_LOG_D(TAG, "Password is not supported, skipping");
  469. }
  470. instance->state = Gen4PollerStateSuccess;
  471. }
  472. } while(false);
  473. return command;
  474. }
  475. NfcCommand gen4_poller_write_handler(Gen4Poller* instance) {
  476. NfcCommand command = NfcCommandContinue;
  477. memcpy(
  478. instance->config.data_raw,
  479. gen4_poller_default_config.data_raw,
  480. GEN4_POLLER_DEFAULT_CONFIG_SIZE);
  481. memcpy(
  482. instance->config.data_parsed.password.bytes, instance->password.bytes, GEN4_PASSWORD_LEN);
  483. memset(&instance->config.data_raw[7], 0, 17);
  484. if(instance->protocol == NfcProtocolMfClassic) {
  485. command = gen4_poller_write_mf_classic(instance);
  486. } else if(instance->protocol == NfcProtocolMfUltralight) {
  487. command = gen4_poller_write_mf_ultralight(instance);
  488. } else {
  489. furi_crash("Unsupported protocol to write");
  490. }
  491. return command;
  492. }
  493. NfcCommand gen4_poller_change_password_handler(Gen4Poller* instance) {
  494. NfcCommand command = NfcCommandContinue;
  495. do {
  496. instance->gen4_event.type = Gen4PollerEventTypeRequestNewPassword;
  497. command = instance->callback(instance->gen4_event, instance->context);
  498. if(command != NfcCommandContinue) break;
  499. Gen4Password new_password = instance->gen4_event_data.request_password.password;
  500. Gen4PollerError error =
  501. gen4_poller_change_password(instance, instance->password, new_password);
  502. if(error != Gen4PollerErrorNone) {
  503. FURI_LOG_E(TAG, "Failed to change password: %d", error);
  504. instance->state = Gen4PollerStateFail;
  505. break;
  506. }
  507. instance->password = new_password;
  508. instance->state = Gen4PollerStateSuccess;
  509. } while(false);
  510. return command;
  511. }
  512. NfcCommand gen4_poller_set_default_cfg_handler(Gen4Poller* instance) {
  513. NfcCommand command = NfcCommandContinue;
  514. do {
  515. Gen4PollerError error = gen4_poller_set_config(
  516. instance,
  517. instance->password,
  518. &gen4_poller_default_config,
  519. GEN4_POLLER_DEFAULT_CONFIG_SIZE,
  520. false);
  521. if(error != Gen4PollerErrorNone) {
  522. FURI_LOG_E(TAG, "Failed to set default config: %d", error);
  523. instance->state = Gen4PollerStateFail;
  524. break;
  525. }
  526. instance->state = Gen4PollerStateSuccess;
  527. } while(false);
  528. return command;
  529. }
  530. NfcCommand gen4_poller_get_current_cfg_handler(Gen4Poller* instance) {
  531. NfcCommand command = NfcCommandContinue;
  532. do {
  533. Gen4Config config;
  534. Gen4PollerError error = gen4_poller_get_config(instance, instance->password, &config);
  535. if(error != Gen4PollerErrorNone) {
  536. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  537. instance->state = Gen4PollerStateFail;
  538. break;
  539. }
  540. // Copy config data to event data buffer
  541. memcpy(instance->gen4_data->config.data_raw, config.data_raw, sizeof(config));
  542. instance->state = Gen4PollerStateSuccess;
  543. } while(false);
  544. return command;
  545. }
  546. NfcCommand gen4_poller_get_revision_handler(Gen4Poller* instance) {
  547. NfcCommand command = NfcCommandContinue;
  548. do {
  549. Gen4Revision revision;
  550. Gen4PollerError error = gen4_poller_get_revision(instance, instance->password, &revision);
  551. if(error != Gen4PollerErrorNone) {
  552. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  553. instance->state = Gen4PollerStateFail;
  554. break;
  555. }
  556. // Copy revision data to event data buffer
  557. memcpy(instance->gen4_data->revision.data, revision.data, sizeof(revision));
  558. instance->state = Gen4PollerStateSuccess;
  559. } while(false);
  560. return command;
  561. }
  562. NfcCommand gen4_poller_get_info_handler(Gen4Poller* instance) {
  563. NfcCommand command = NfcCommandContinue;
  564. do {
  565. Gen4 gen4_data;
  566. Gen4PollerError error =
  567. gen4_poller_get_revision(instance, instance->password, &gen4_data.revision);
  568. if(error != Gen4PollerErrorNone) {
  569. FURI_LOG_E(TAG, "Failed to get revision: %d", error);
  570. instance->state = Gen4PollerStateFail;
  571. break;
  572. }
  573. error = gen4_poller_get_config(instance, instance->password, &gen4_data.config);
  574. if(error != Gen4PollerErrorNone) {
  575. FURI_LOG_E(TAG, "Failed to get current config: %d", error);
  576. instance->state = Gen4PollerStateFail;
  577. break;
  578. }
  579. // Copy config&&revision data to event data buffer
  580. gen4_copy(instance->gen4_data, &gen4_data);
  581. instance->state = Gen4PollerStateSuccess;
  582. } while(false);
  583. return command;
  584. }
  585. NfcCommand gen4_poller_set_shadow_mode_handler(Gen4Poller* instance) {
  586. NfcCommand command = NfcCommandContinue;
  587. do {
  588. Gen4PollerError error =
  589. gen4_poller_set_shadow_mode(instance, instance->password, instance->shadow_mode);
  590. if(error != Gen4PollerErrorNone) {
  591. FURI_LOG_E(TAG, "Failed to set shadow mode: %d", error);
  592. instance->state = Gen4PollerStateFail;
  593. break;
  594. }
  595. instance->state = Gen4PollerStateSuccess;
  596. } while(false);
  597. return command;
  598. }
  599. NfcCommand gen4_poller_set_direct_write_block_0_mode_handler(Gen4Poller* instance) {
  600. NfcCommand command = NfcCommandContinue;
  601. do {
  602. Gen4PollerError error = gen4_poller_set_direct_write_block_0_mode(
  603. instance, instance->password, instance->direct_write_block_0_mode);
  604. if(error != Gen4PollerErrorNone) {
  605. FURI_LOG_E(TAG, "Failed to set direct write to block 0 mode: %d", error);
  606. instance->state = Gen4PollerStateFail;
  607. break;
  608. }
  609. instance->state = Gen4PollerStateSuccess;
  610. } while(false);
  611. return command;
  612. }
  613. NfcCommand gen4_poller_success_handler(Gen4Poller* instance) {
  614. NfcCommand command = NfcCommandContinue;
  615. instance->gen4_event.type = Gen4PollerEventTypeSuccess;
  616. command = instance->callback(instance->gen4_event, instance->context);
  617. if(command != NfcCommandStop) {
  618. furi_delay_ms(100);
  619. }
  620. return command;
  621. }
  622. NfcCommand gen4_poller_fail_handler(Gen4Poller* instance) {
  623. NfcCommand command = NfcCommandContinue;
  624. instance->gen4_event.type = Gen4PollerEventTypeFail;
  625. command = instance->callback(instance->gen4_event, instance->context);
  626. if(command != NfcCommandStop) {
  627. furi_delay_ms(100);
  628. }
  629. return command;
  630. }
  631. static const Gen4PollerStateHandler gen4_poller_state_handlers[Gen4PollerStateNum] = {
  632. [Gen4PollerStateIdle] = gen4_poller_idle_handler,
  633. [Gen4PollerStateRequestMode] = gen4_poller_request_mode_handler,
  634. [Gen4PollerStateRequestWriteData] = gen4_poller_request_write_data_handler,
  635. [Gen4PollerStateWrite] = gen4_poller_write_handler,
  636. [Gen4PollerStateWipe] = gen4_poller_wipe_handler,
  637. [Gen4PollerStateChangePassword] = gen4_poller_change_password_handler,
  638. [Gen4PollerStateGetInfo] = gen4_poller_get_info_handler,
  639. [Gen4PollerStateSetDefaultConfig] = gen4_poller_set_default_cfg_handler,
  640. [Gen4PollerStateSetShadowMode] = gen4_poller_set_shadow_mode_handler,
  641. [Gen4PollerStateSetDirectWriteBlock0] = gen4_poller_set_direct_write_block_0_mode_handler,
  642. [Gen4PollerStateSuccess] = gen4_poller_success_handler,
  643. [Gen4PollerStateFail] = gen4_poller_fail_handler,
  644. };
  645. static NfcCommand gen4_poller_callback(NfcGenericEvent event, void* context) {
  646. furi_assert(context);
  647. furi_assert(event.protocol == NfcProtocolIso14443_3a);
  648. furi_assert(event.event_data);
  649. furi_assert(event.instance);
  650. NfcCommand command = NfcCommandContinue;
  651. Gen4Poller* instance = context;
  652. instance->iso3_poller = event.instance;
  653. Iso14443_3aPollerEvent* iso3_event = event.event_data;
  654. if(iso3_event->type == Iso14443_3aPollerEventTypeReady) {
  655. command = gen4_poller_state_handlers[instance->state](instance);
  656. }
  657. return command;
  658. }
  659. void gen4_poller_start(Gen4Poller* instance, Gen4PollerCallback callback, void* context) {
  660. furi_assert(instance);
  661. furi_assert(callback);
  662. instance->callback = callback;
  663. instance->context = context;
  664. nfc_poller_start(instance->poller, gen4_poller_callback, instance);
  665. }
  666. void gen4_poller_stop(Gen4Poller* instance) {
  667. furi_assert(instance);
  668. nfc_poller_stop(instance->poller);
  669. }
  670. const Gen4* gen4_poller_get_gen4_data(const Gen4Poller* instance) {
  671. furi_assert(instance);
  672. return instance->gen4_data;
  673. }
  674. void gen4_poller_struct_set_direct_write_block_0_mode(
  675. Gen4Poller* instance,
  676. Gen4DirectWriteBlock0Mode mode) {
  677. furi_assert(instance);
  678. instance->direct_write_block_0_mode = mode;
  679. }
  680. void gen4_poller_struct_set_shadow_mode(Gen4Poller* instance, Gen4ShadowMode mode) {
  681. furi_assert(instance);
  682. instance->shadow_mode = mode;
  683. }