mifare_ultralight.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. #include <limits.h>
  2. #include "mifare_ultralight.h"
  3. #include <furi.h>
  4. #include <m-string.h>
  5. #define TAG "MfUltralight"
  6. bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  7. if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) {
  8. return true;
  9. }
  10. return false;
  11. }
  12. static MfUltralightFeatures mf_ul_get_features(MfUltralightType type) {
  13. switch(type) {
  14. case MfUltralightTypeUL11:
  15. case MfUltralightTypeUL21:
  16. return MfUltralightSupportFastRead | MfUltralightSupportCompatWrite |
  17. MfUltralightSupportReadCounter | MfUltralightSupportIncrCounter |
  18. MfUltralightSupportAuth | MfUltralightSupportSignature |
  19. MfUltralightSupportTearingFlags | MfUltralightSupportVcsl;
  20. case MfUltralightTypeNTAG213:
  21. case MfUltralightTypeNTAG215:
  22. case MfUltralightTypeNTAG216:
  23. return MfUltralightSupportFastRead | MfUltralightSupportCompatWrite |
  24. MfUltralightSupportReadCounter | MfUltralightSupportAuth |
  25. MfUltralightSupportSignature | MfUltralightSupportSingleCounter |
  26. MfUltralightSupportAsciiMirror;
  27. case MfUltralightTypeNTAGI2C1K:
  28. case MfUltralightTypeNTAGI2C2K:
  29. return MfUltralightSupportFastRead | MfUltralightSupportSectorSelect;
  30. case MfUltralightTypeNTAGI2CPlus1K:
  31. case MfUltralightTypeNTAGI2CPlus2K:
  32. return MfUltralightSupportFastRead | MfUltralightSupportAuth |
  33. MfUltralightSupportFastWrite | MfUltralightSupportSignature |
  34. MfUltralightSupportSectorSelect;
  35. default:
  36. // Assumed original MFUL 512-bit
  37. return MfUltralightSupportNone;
  38. }
  39. }
  40. static void mf_ul_set_default_version(MfUltralightReader* reader, MfUltralightData* data) {
  41. data->type = MfUltralightTypeUnknown;
  42. reader->pages_to_read = 16;
  43. }
  44. bool mf_ultralight_read_version(
  45. FuriHalNfcTxRxContext* tx_rx,
  46. MfUltralightReader* reader,
  47. MfUltralightData* data) {
  48. bool version_read = false;
  49. do {
  50. FURI_LOG_D(TAG, "Reading version");
  51. tx_rx->tx_data[0] = MF_UL_GET_VERSION_CMD;
  52. tx_rx->tx_bits = 8;
  53. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  54. if(!furi_hal_nfc_tx_rx(tx_rx, 50)) {
  55. FURI_LOG_D(TAG, "Failed reading version");
  56. mf_ul_set_default_version(reader, data);
  57. furi_hal_nfc_sleep();
  58. furi_hal_nfc_activate_nfca(300, NULL);
  59. break;
  60. }
  61. MfUltralightVersion* version = (MfUltralightVersion*)tx_rx->rx_data;
  62. data->version = *version;
  63. if(version->storage_size == 0x0B || version->storage_size == 0x00) {
  64. data->type = MfUltralightTypeUL11;
  65. reader->pages_to_read = 20;
  66. } else if(version->storage_size == 0x0E) {
  67. data->type = MfUltralightTypeUL21;
  68. reader->pages_to_read = 41;
  69. } else if(version->storage_size == 0x0F) {
  70. data->type = MfUltralightTypeNTAG213;
  71. reader->pages_to_read = 45;
  72. } else if(version->storage_size == 0x11) {
  73. data->type = MfUltralightTypeNTAG215;
  74. reader->pages_to_read = 135;
  75. } else if(version->prod_subtype == 5 && version->prod_ver_major == 2) {
  76. // NTAG I2C
  77. bool known = false;
  78. if(version->prod_ver_minor == 1) {
  79. if(version->storage_size == 0x13) {
  80. data->type = MfUltralightTypeNTAGI2C1K;
  81. reader->pages_to_read = 231;
  82. known = true;
  83. } else if(version->storage_size == 0x15) {
  84. data->type = MfUltralightTypeNTAGI2C2K;
  85. reader->pages_to_read = 485;
  86. known = true;
  87. }
  88. } else if(version->prod_ver_minor == 2) {
  89. if(version->storage_size == 0x13) {
  90. data->type = MfUltralightTypeNTAGI2CPlus1K;
  91. reader->pages_to_read = 236;
  92. known = true;
  93. } else if(version->storage_size == 0x15) {
  94. data->type = MfUltralightTypeNTAGI2CPlus2K;
  95. reader->pages_to_read = 492;
  96. known = true;
  97. }
  98. }
  99. if(!known) {
  100. mf_ul_set_default_version(reader, data);
  101. }
  102. } else if(version->storage_size == 0x13) {
  103. data->type = MfUltralightTypeNTAG216;
  104. reader->pages_to_read = 231;
  105. } else {
  106. mf_ul_set_default_version(reader, data);
  107. break;
  108. }
  109. version_read = true;
  110. } while(false);
  111. reader->supported_features = mf_ul_get_features(data->type);
  112. return version_read;
  113. }
  114. static int16_t mf_ultralight_page_addr_to_tag_addr(uint8_t sector, uint8_t page) {
  115. return sector * 256 + page;
  116. }
  117. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_1k(
  118. int16_t linear_address,
  119. uint8_t* sector,
  120. int16_t* valid_pages) {
  121. // 0 - 226: sector 0
  122. // 227 - 228: config registers
  123. // 229 - 230: session registers
  124. if(linear_address > 230) {
  125. *valid_pages = 0;
  126. return -1;
  127. } else if(linear_address >= 229) {
  128. *sector = 3;
  129. *valid_pages = 2 - (linear_address - 229);
  130. return linear_address - 229 + 248;
  131. } else if(linear_address >= 227) {
  132. *sector = 0;
  133. *valid_pages = 2 - (linear_address - 227);
  134. return linear_address - 227 + 232;
  135. } else {
  136. *sector = 0;
  137. *valid_pages = 227 - linear_address;
  138. return linear_address;
  139. }
  140. }
  141. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_2k(
  142. int16_t linear_address,
  143. uint8_t* sector,
  144. int16_t* valid_pages) {
  145. // 0 - 255: sector 0
  146. // 256 - 480: sector 1
  147. // 481 - 482: config registers
  148. // 483 - 484: session registers
  149. if(linear_address > 484) {
  150. *valid_pages = 0;
  151. return -1;
  152. } else if(linear_address >= 483) {
  153. *sector = 3;
  154. *valid_pages = 2 - (linear_address - 483);
  155. return linear_address - 483 + 248;
  156. } else if(linear_address >= 481) {
  157. *sector = 1;
  158. *valid_pages = 2 - (linear_address - 481);
  159. return linear_address - 481 + 232;
  160. } else if(linear_address >= 256) {
  161. *sector = 1;
  162. *valid_pages = 225 - (linear_address - 256);
  163. return linear_address - 256;
  164. } else {
  165. *sector = 0;
  166. *valid_pages = 256 - linear_address;
  167. return linear_address;
  168. }
  169. }
  170. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_1k(
  171. int16_t linear_address,
  172. uint8_t* sector,
  173. int16_t* valid_pages) {
  174. // 0 - 233: sector 0 + registers
  175. // 234 - 235: session registers
  176. if(linear_address > 235) {
  177. *valid_pages = 0;
  178. return -1;
  179. } else if(linear_address >= 234) {
  180. *sector = 0;
  181. *valid_pages = 2 - (linear_address - 234);
  182. return linear_address - 234 + 236;
  183. } else {
  184. *sector = 0;
  185. *valid_pages = 234 - linear_address;
  186. return linear_address;
  187. }
  188. }
  189. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_2k(
  190. int16_t linear_address,
  191. uint8_t* sector,
  192. int16_t* valid_pages) {
  193. // 0 - 233: sector 0 + registers
  194. // 234 - 235: session registers
  195. // 236 - 491: sector 1
  196. if(linear_address > 491) {
  197. *valid_pages = 0;
  198. return -1;
  199. } else if(linear_address >= 236) {
  200. *sector = 1;
  201. *valid_pages = 256 - (linear_address - 236);
  202. return linear_address - 236;
  203. } else if(linear_address >= 234) {
  204. *sector = 0;
  205. *valid_pages = 2 - (linear_address - 234);
  206. return linear_address - 234 + 236;
  207. } else {
  208. *sector = 0;
  209. *valid_pages = 234 - linear_address;
  210. return linear_address;
  211. }
  212. }
  213. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag(
  214. MfUltralightData* data,
  215. MfUltralightReader* reader,
  216. int16_t linear_address,
  217. uint8_t* sector,
  218. int16_t* valid_pages) {
  219. switch(data->type) {
  220. case MfUltralightTypeNTAGI2C1K:
  221. return mf_ultralight_ntag_i2c_addr_lin_to_tag_1k(linear_address, sector, valid_pages);
  222. case MfUltralightTypeNTAGI2C2K:
  223. return mf_ultralight_ntag_i2c_addr_lin_to_tag_2k(linear_address, sector, valid_pages);
  224. case MfUltralightTypeNTAGI2CPlus1K:
  225. return mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_1k(linear_address, sector, valid_pages);
  226. case MfUltralightTypeNTAGI2CPlus2K:
  227. return mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_2k(linear_address, sector, valid_pages);
  228. default:
  229. *sector = 0xff;
  230. *valid_pages = reader->pages_to_read - linear_address;
  231. return linear_address;
  232. }
  233. }
  234. static int16_t
  235. mf_ultralight_ntag_i2c_addr_tag_to_lin_1k(uint8_t page, uint8_t sector, uint16_t* valid_pages) {
  236. bool valid = false;
  237. int16_t translated_page;
  238. if(sector == 0) {
  239. if(page <= 226) {
  240. *valid_pages = 227 - page;
  241. translated_page = page;
  242. valid = true;
  243. } else if(page >= 232 && page <= 233) {
  244. *valid_pages = 2 - (page - 232);
  245. translated_page = page - 232 + 227;
  246. valid = true;
  247. }
  248. } else if(sector == 3) {
  249. if(page >= 248 && page <= 249) {
  250. *valid_pages = 2 - (page - 248);
  251. translated_page = page - 248 + 229;
  252. valid = true;
  253. }
  254. }
  255. if(!valid) {
  256. *valid_pages = 0;
  257. translated_page = -1;
  258. }
  259. return translated_page;
  260. }
  261. static int16_t
  262. mf_ultralight_ntag_i2c_addr_tag_to_lin_2k(uint8_t page, uint8_t sector, uint16_t* valid_pages) {
  263. bool valid = false;
  264. int16_t translated_page;
  265. if(sector == 0) {
  266. *valid_pages = 256 - page;
  267. translated_page = page;
  268. valid = true;
  269. } else if(sector == 1) {
  270. if(page <= 224) {
  271. *valid_pages = 225 - page;
  272. translated_page = 256 + page;
  273. valid = true;
  274. } else if(page >= 232 && page <= 233) {
  275. *valid_pages = 2 - (page - 232);
  276. translated_page = page - 232 + 481;
  277. valid = true;
  278. }
  279. } else if(sector == 3) {
  280. if(page >= 248 && page <= 249) {
  281. *valid_pages = 2 - (page - 248);
  282. translated_page = page - 248 + 483;
  283. valid = true;
  284. }
  285. }
  286. if(!valid) {
  287. *valid_pages = 0;
  288. translated_page = -1;
  289. }
  290. return translated_page;
  291. }
  292. static int16_t mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_1k(
  293. uint8_t page,
  294. uint8_t sector,
  295. uint16_t* valid_pages) {
  296. bool valid = false;
  297. int16_t translated_page;
  298. if(sector == 0) {
  299. if(page <= 233) {
  300. *valid_pages = 234 - page;
  301. translated_page = page;
  302. valid = true;
  303. } else if(page >= 236 && page <= 237) {
  304. *valid_pages = 2 - (page - 236);
  305. translated_page = page - 236 + 234;
  306. valid = true;
  307. }
  308. } else if(sector == 3) {
  309. if(page >= 248 && page <= 249) {
  310. *valid_pages = 2 - (page - 248);
  311. translated_page = page - 248 + 234;
  312. valid = true;
  313. }
  314. }
  315. if(!valid) {
  316. *valid_pages = 0;
  317. translated_page = -1;
  318. }
  319. return translated_page;
  320. }
  321. static int16_t mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_2k(
  322. uint8_t page,
  323. uint8_t sector,
  324. uint16_t* valid_pages) {
  325. bool valid = false;
  326. int16_t translated_page;
  327. if(sector == 0) {
  328. if(page <= 233) {
  329. *valid_pages = 234 - page;
  330. translated_page = page;
  331. valid = true;
  332. } else if(page >= 236 && page <= 237) {
  333. *valid_pages = 2 - (page - 236);
  334. translated_page = page - 236 + 234;
  335. valid = true;
  336. }
  337. } else if(sector == 1) {
  338. *valid_pages = 256 - page;
  339. translated_page = page + 236;
  340. valid = true;
  341. } else if(sector == 3) {
  342. if(page >= 248 && page <= 249) {
  343. *valid_pages = 2 - (page - 248);
  344. translated_page = page - 248 + 234;
  345. valid = true;
  346. }
  347. }
  348. if(!valid) {
  349. *valid_pages = 0;
  350. translated_page = -1;
  351. }
  352. return translated_page;
  353. }
  354. static int16_t mf_ultralight_ntag_i2c_addr_tag_to_lin(
  355. MfUltralightData* data,
  356. uint8_t page,
  357. uint8_t sector,
  358. uint16_t* valid_pages) {
  359. switch(data->type) {
  360. case MfUltralightTypeNTAGI2C1K:
  361. return mf_ultralight_ntag_i2c_addr_tag_to_lin_1k(page, sector, valid_pages);
  362. case MfUltralightTypeNTAGI2C2K:
  363. return mf_ultralight_ntag_i2c_addr_tag_to_lin_2k(page, sector, valid_pages);
  364. case MfUltralightTypeNTAGI2CPlus1K:
  365. return mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_1k(page, sector, valid_pages);
  366. case MfUltralightTypeNTAGI2CPlus2K:
  367. return mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_2k(page, sector, valid_pages);
  368. default:
  369. *valid_pages = data->data_size / 4 - page;
  370. return page;
  371. }
  372. }
  373. static MfUltralightConfigPages* mf_ultralight_get_config_pages(MfUltralightData* data) {
  374. if(data->type >= MfUltralightTypeUL11 && data->type <= MfUltralightTypeNTAG216) {
  375. return (MfUltralightConfigPages*)&data->data[data->data_size - 4 * 4];
  376. } else if(
  377. data->type >= MfUltralightTypeNTAGI2CPlus1K &&
  378. data->type <= MfUltralightTypeNTAGI2CPlus2K) {
  379. return (MfUltralightConfigPages*)&data->data[0xe3 * 4];
  380. } else {
  381. return NULL;
  382. }
  383. }
  384. static uint16_t mf_ultralight_calc_auth_count(MfUltralightData* data) {
  385. if(mf_ul_get_features(data->type) & MfUltralightSupportAuth) {
  386. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  387. uint16_t scaled_authlim = config->access.authlim;
  388. // NTAG I2C Plus uses 2^AUTHLIM attempts rather than the direct number
  389. if(scaled_authlim > 0 && data->type >= MfUltralightTypeNTAGI2CPlus1K &&
  390. data->type <= MfUltralightTypeNTAGI2CPlus2K) {
  391. scaled_authlim = 1 << scaled_authlim;
  392. }
  393. return scaled_authlim;
  394. }
  395. return 0;
  396. }
  397. // NTAG21x will NAK if NFC_CNT_EN unset, so preempt
  398. static bool mf_ultralight_should_read_counters(MfUltralightData* data) {
  399. if(data->type < MfUltralightTypeNTAG213 || data->type > MfUltralightTypeNTAG216) return true;
  400. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  401. return config->access.nfc_cnt_en;
  402. }
  403. static bool mf_ultralight_sector_select(FuriHalNfcTxRxContext* tx_rx, uint8_t sector) {
  404. FURI_LOG_D(TAG, "Selecting sector %u", sector);
  405. tx_rx->tx_data[0] = MF_UL_SECTOR_SELECT;
  406. tx_rx->tx_data[1] = 0xff;
  407. tx_rx->tx_bits = 16;
  408. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  409. if(!furi_hal_nfc_tx_rx(tx_rx, 50)) {
  410. FURI_LOG_D(TAG, "Failed to issue sector select command");
  411. return false;
  412. }
  413. tx_rx->tx_data[0] = sector;
  414. tx_rx->tx_data[1] = 0x00;
  415. tx_rx->tx_data[2] = 0x00;
  416. tx_rx->tx_data[3] = 0x00;
  417. tx_rx->tx_bits = 32;
  418. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  419. // This is NOT a typo! The tag ACKs by not sending a response within 1ms.
  420. if(furi_hal_nfc_tx_rx(tx_rx, 20)) {
  421. // TODO: what gets returned when an actual NAK is received?
  422. FURI_LOG_D(TAG, "Sector %u select NAK'd", sector);
  423. return false;
  424. }
  425. return true;
  426. }
  427. bool mf_ultralight_read_pages(
  428. FuriHalNfcTxRxContext* tx_rx,
  429. MfUltralightReader* reader,
  430. MfUltralightData* data) {
  431. uint8_t pages_read_cnt = 0;
  432. uint8_t curr_sector_index = 0xff;
  433. reader->pages_read = 0;
  434. for(size_t i = 0; i < reader->pages_to_read; i += pages_read_cnt) {
  435. uint8_t tag_sector;
  436. int16_t valid_pages;
  437. int16_t tag_page = mf_ultralight_ntag_i2c_addr_lin_to_tag(
  438. data, reader, (int16_t)i, &tag_sector, &valid_pages);
  439. furi_assert(tag_page != -1);
  440. if(curr_sector_index != tag_sector) {
  441. if(!mf_ultralight_sector_select(tx_rx, tag_sector)) return false;
  442. curr_sector_index = tag_sector;
  443. }
  444. FURI_LOG_D(TAG, "Reading pages %d - %d", i, i + (valid_pages > 4 ? 4 : valid_pages) - 1);
  445. tx_rx->tx_data[0] = MF_UL_READ_CMD;
  446. tx_rx->tx_data[1] = tag_page;
  447. tx_rx->tx_bits = 16;
  448. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  449. if(!furi_hal_nfc_tx_rx(tx_rx, 50) || tx_rx->rx_bits < 16 * 8) {
  450. FURI_LOG_D(
  451. TAG,
  452. "Failed to read pages %d - %d",
  453. i,
  454. i + (valid_pages > 4 ? 4 : valid_pages) - 1);
  455. break;
  456. }
  457. if(valid_pages > 4) {
  458. pages_read_cnt = 4;
  459. } else {
  460. pages_read_cnt = valid_pages;
  461. }
  462. reader->pages_read += pages_read_cnt;
  463. data->data_size = reader->pages_read * 4;
  464. memcpy(&data->data[i * 4], tx_rx->rx_data, pages_read_cnt * 4);
  465. }
  466. return reader->pages_read == reader->pages_to_read;
  467. }
  468. bool mf_ultralight_fast_read_pages(
  469. FuriHalNfcTxRxContext* tx_rx,
  470. MfUltralightReader* reader,
  471. MfUltralightData* data) {
  472. uint8_t curr_sector_index = 0xff;
  473. reader->pages_read = 0;
  474. while(reader->pages_read < reader->pages_to_read) {
  475. uint8_t tag_sector;
  476. int16_t valid_pages;
  477. int16_t tag_page = mf_ultralight_ntag_i2c_addr_lin_to_tag(
  478. data, reader, reader->pages_read, &tag_sector, &valid_pages);
  479. furi_assert(tag_page != -1);
  480. if(curr_sector_index != tag_sector) {
  481. if(!mf_ultralight_sector_select(tx_rx, tag_sector)) return false;
  482. curr_sector_index = tag_sector;
  483. }
  484. FURI_LOG_D(
  485. TAG, "Reading pages %d - %d", reader->pages_read, reader->pages_read + valid_pages - 1);
  486. tx_rx->tx_data[0] = MF_UL_FAST_READ_CMD;
  487. tx_rx->tx_data[1] = tag_page;
  488. tx_rx->tx_data[2] = valid_pages - 1;
  489. tx_rx->tx_bits = 24;
  490. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  491. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  492. memcpy(&data->data[reader->pages_read * 4], tx_rx->rx_data, valid_pages * 4);
  493. reader->pages_read += valid_pages;
  494. data->data_size = reader->pages_read * 4;
  495. } else {
  496. FURI_LOG_D(
  497. TAG,
  498. "Failed to read pages %d - %d",
  499. reader->pages_read,
  500. reader->pages_read + valid_pages - 1);
  501. break;
  502. }
  503. }
  504. return reader->pages_read == reader->pages_to_read;
  505. }
  506. bool mf_ultralight_read_signature(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  507. bool signature_read = false;
  508. FURI_LOG_D(TAG, "Reading signature");
  509. tx_rx->tx_data[0] = MF_UL_READ_SIG;
  510. tx_rx->tx_data[1] = 0;
  511. tx_rx->tx_bits = 16;
  512. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  513. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  514. memcpy(data->signature, tx_rx->rx_data, sizeof(data->signature));
  515. signature_read = true;
  516. } else {
  517. FURI_LOG_D(TAG, "Failed redaing signature");
  518. }
  519. return signature_read;
  520. }
  521. bool mf_ultralight_read_counters(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  522. uint8_t counter_read = 0;
  523. FURI_LOG_D(TAG, "Reading counters");
  524. bool is_single_counter = (mf_ul_get_features(data->type) & MfUltralightSupportSingleCounter) !=
  525. 0;
  526. for(size_t i = is_single_counter ? 2 : 0; i < 3; i++) {
  527. tx_rx->tx_data[0] = MF_UL_READ_CNT;
  528. tx_rx->tx_data[1] = i;
  529. tx_rx->tx_bits = 16;
  530. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  531. if(!furi_hal_nfc_tx_rx(tx_rx, 50)) {
  532. FURI_LOG_D(TAG, "Failed to read %d counter", i);
  533. break;
  534. }
  535. data->counter[i] = (tx_rx->rx_data[2] << 16) | (tx_rx->rx_data[1] << 8) |
  536. tx_rx->rx_data[0];
  537. counter_read++;
  538. }
  539. return counter_read == (is_single_counter ? 1 : 3);
  540. }
  541. bool mf_ultralight_read_tearing_flags(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  542. uint8_t flag_read = 0;
  543. FURI_LOG_D(TAG, "Reading tearing flags");
  544. for(size_t i = 0; i < 3; i++) {
  545. tx_rx->tx_data[0] = MF_UL_CHECK_TEARING;
  546. tx_rx->rx_data[1] = i;
  547. tx_rx->tx_bits = 16;
  548. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  549. if(!furi_hal_nfc_tx_rx(tx_rx, 50)) {
  550. FURI_LOG_D(TAG, "Failed to read %d tearing flag", i);
  551. break;
  552. }
  553. data->tearing[i] = tx_rx->rx_data[0];
  554. flag_read++;
  555. }
  556. return flag_read == 2;
  557. }
  558. bool mf_ul_read_card(
  559. FuriHalNfcTxRxContext* tx_rx,
  560. MfUltralightReader* reader,
  561. MfUltralightData* data) {
  562. furi_assert(tx_rx);
  563. furi_assert(reader);
  564. furi_assert(data);
  565. bool card_read = false;
  566. // Read Mifare Ultralight version
  567. if(mf_ultralight_read_version(tx_rx, reader, data)) {
  568. if(reader->supported_features & MfUltralightSupportSignature) {
  569. // Read Signature
  570. mf_ultralight_read_signature(tx_rx, data);
  571. }
  572. }
  573. card_read = mf_ultralight_read_pages(tx_rx, reader, data);
  574. if(card_read) {
  575. if(reader->supported_features & MfUltralightSupportReadCounter &&
  576. mf_ultralight_should_read_counters(data)) {
  577. mf_ultralight_read_counters(tx_rx, data);
  578. }
  579. if(reader->supported_features & MfUltralightSupportTearingFlags) {
  580. mf_ultralight_read_tearing_flags(tx_rx, data);
  581. }
  582. data->curr_authlim = 0;
  583. }
  584. return card_read;
  585. }
  586. static void mf_ul_protect_auth_data_on_read_command_i2c(
  587. uint8_t* tx_buff,
  588. uint8_t start_page,
  589. uint8_t end_page,
  590. MfUltralightEmulator* emulator) {
  591. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  592. // Blank out PWD and PACK
  593. if(start_page <= 229 && end_page >= 229) {
  594. uint16_t offset = (229 - start_page) * 4;
  595. uint8_t count = 4;
  596. if(end_page >= 230) count += 2;
  597. memset(&tx_buff[offset], 0, count);
  598. }
  599. // Handle AUTH0 for sector 0
  600. if(!emulator->auth_success) {
  601. if(emulator->config_cache.access.prot) {
  602. uint8_t auth0 = emulator->config_cache.auth0;
  603. if(auth0 < end_page) {
  604. // start_page is always < auth0; otherwise is NAK'd already
  605. uint8_t page_offset = auth0 - start_page;
  606. uint8_t page_count = end_page - auth0;
  607. memset(&tx_buff[page_offset * 4], 0, page_count * 4);
  608. }
  609. }
  610. }
  611. }
  612. }
  613. static void mf_ul_ntag_i2c_fill_cross_area_read(
  614. uint8_t* tx_buff,
  615. uint8_t start_page,
  616. uint8_t end_page,
  617. MfUltralightEmulator* emulator) {
  618. // For copying config or session registers in fast read
  619. int16_t tx_page_offset;
  620. int16_t data_page_offset;
  621. uint8_t page_length;
  622. bool apply = false;
  623. MfUltralightType type = emulator->data.type;
  624. if(emulator->curr_sector == 0) {
  625. if(type == MfUltralightTypeNTAGI2C1K) {
  626. if(start_page <= 233 && end_page >= 232) {
  627. tx_page_offset = start_page - 232;
  628. data_page_offset = 227;
  629. page_length = 2;
  630. apply = true;
  631. }
  632. } else if(type == MfUltralightTypeNTAGI2CPlus1K || type == MfUltralightTypeNTAGI2CPlus2K) {
  633. if(start_page <= 237 && end_page >= 236) {
  634. tx_page_offset = start_page - 236;
  635. data_page_offset = 234;
  636. page_length = 2;
  637. apply = true;
  638. }
  639. }
  640. } else if(emulator->curr_sector == 1) {
  641. if(type == MfUltralightTypeNTAGI2C2K) {
  642. if(start_page <= 233 && end_page >= 232) {
  643. tx_page_offset = start_page - 232;
  644. data_page_offset = 483;
  645. page_length = 2;
  646. apply = true;
  647. }
  648. }
  649. }
  650. if(apply) {
  651. while(tx_page_offset < 0 && page_length > 0) {
  652. ++tx_page_offset;
  653. ++data_page_offset;
  654. --page_length;
  655. }
  656. memcpy(
  657. &tx_buff[tx_page_offset * 4],
  658. &emulator->data.data[data_page_offset * 4],
  659. page_length * 4);
  660. }
  661. }
  662. static bool mf_ul_check_auth(MfUltralightEmulator* emulator, uint8_t start_page, bool is_write) {
  663. if(!emulator->auth_success) {
  664. if(start_page >= emulator->config_cache.auth0 &&
  665. (emulator->config_cache.access.prot || is_write))
  666. return false;
  667. }
  668. if(is_write && emulator->config_cache.access.cfglck) {
  669. uint16_t config_start_page = emulator->page_num - 4;
  670. if(start_page == config_start_page || start_page == config_start_page + 1) return false;
  671. }
  672. return true;
  673. }
  674. static bool mf_ul_ntag_i2c_plus_check_auth(
  675. MfUltralightEmulator* emulator,
  676. uint8_t start_page,
  677. bool is_write) {
  678. if(!emulator->auth_success) {
  679. // Check NFC_PROT
  680. if(emulator->curr_sector == 0 && (emulator->config_cache.access.prot || is_write)) {
  681. if(start_page >= emulator->config_cache.auth0) return false;
  682. } else if(emulator->curr_sector == 1) {
  683. // We don't have to specifically check for type because this is done
  684. // by address translator
  685. uint8_t pt_i2c = emulator->data.data[231 * 4];
  686. // Check 2K_PROT
  687. if(pt_i2c & 0x08) return false;
  688. }
  689. }
  690. if(emulator->curr_sector == 1) {
  691. // Check NFC_DIS_SEC1
  692. if(emulator->config_cache.access.nfc_dis_sec1) return false;
  693. }
  694. return true;
  695. }
  696. static int16_t mf_ul_get_dynamic_lock_page_addr(MfUltralightData* data) {
  697. switch(data->type) {
  698. case MfUltralightTypeUL21:
  699. case MfUltralightTypeNTAG213:
  700. case MfUltralightTypeNTAG215:
  701. case MfUltralightTypeNTAG216:
  702. return data->data_size / 4 - 5;
  703. case MfUltralightTypeNTAGI2C1K:
  704. case MfUltralightTypeNTAGI2CPlus1K:
  705. case MfUltralightTypeNTAGI2CPlus2K:
  706. return 0xe2;
  707. case MfUltralightTypeNTAGI2C2K:
  708. return 0x1e0;
  709. default:
  710. return -1; // No dynamic lock bytes
  711. }
  712. }
  713. // Returns true if page not locked
  714. // write_page is tag address
  715. static bool mf_ul_check_lock(MfUltralightEmulator* emulator, int16_t write_page) {
  716. if(write_page < 2) return false; // Page 0-1 is always locked
  717. if(write_page == 2) return true; // Page 2 does not have a lock flag
  718. // Check static lock bytes
  719. if(write_page <= 15) {
  720. uint16_t static_lock_bytes = emulator->data.data[10] | (emulator->data.data[11] << 8);
  721. return (static_lock_bytes & (1 << write_page)) == 0;
  722. }
  723. // Check dynamic lock bytes
  724. // Check max page
  725. switch(emulator->data.type) {
  726. case MfUltralightTypeUL21:
  727. case MfUltralightTypeNTAG213:
  728. case MfUltralightTypeNTAG215:
  729. case MfUltralightTypeNTAG216:
  730. if(write_page >= emulator->page_num - 5) return true;
  731. break;
  732. case MfUltralightTypeNTAGI2C1K:
  733. case MfUltralightTypeNTAGI2CPlus1K:
  734. if(write_page > 225) return true;
  735. break;
  736. case MfUltralightTypeNTAGI2C2K:
  737. if(write_page > 479) return true;
  738. break;
  739. case MfUltralightTypeNTAGI2CPlus2K:
  740. if(write_page >= 226 && write_page <= 255) return true;
  741. if(write_page >= 512) return true;
  742. break;
  743. default:
  744. furi_assert(false);
  745. return true;
  746. }
  747. int16_t dynamic_lock_index = mf_ul_get_dynamic_lock_page_addr(&emulator->data);
  748. if(dynamic_lock_index == -1) return true;
  749. // Run address through converter because NTAG I2C 2K is special
  750. uint16_t valid_pages; // unused
  751. dynamic_lock_index =
  752. mf_ultralight_ntag_i2c_addr_tag_to_lin(
  753. &emulator->data, dynamic_lock_index & 0xff, dynamic_lock_index >> 8, &valid_pages) *
  754. 4;
  755. uint16_t dynamic_lock_bytes = emulator->data.data[dynamic_lock_index] |
  756. (emulator->data.data[dynamic_lock_index + 1] << 8);
  757. uint8_t shift;
  758. switch(emulator->data.type) {
  759. // low byte LSB range, MSB range
  760. case MfUltralightTypeUL21:
  761. case MfUltralightTypeNTAG213:
  762. // 16-17, 30-31
  763. shift = (write_page - 16) / 2;
  764. break;
  765. case MfUltralightTypeNTAG215:
  766. case MfUltralightTypeNTAG216:
  767. case MfUltralightTypeNTAGI2C1K:
  768. case MfUltralightTypeNTAGI2CPlus1K:
  769. // 16-31, 128-129
  770. // 16-31, 128-143
  771. shift = (write_page - 16) / 16;
  772. break;
  773. case MfUltralightTypeNTAGI2C2K:
  774. // 16-47, 240-271
  775. shift = (write_page - 16) / 32;
  776. break;
  777. case MfUltralightTypeNTAGI2CPlus2K:
  778. // 16-47, 256-271
  779. if(write_page >= 208 && write_page <= 225)
  780. shift = 6;
  781. else if(write_page >= 256 && write_page <= 271)
  782. shift = 7;
  783. else
  784. shift = (write_page - 16) / 32;
  785. break;
  786. default:
  787. furi_assert(false);
  788. shift = 0;
  789. break;
  790. }
  791. return (dynamic_lock_bytes & (1 << shift)) == 0;
  792. }
  793. static void mf_ul_make_ascii_mirror(MfUltralightEmulator* emulator, string_t str) {
  794. // Locals to improve readability
  795. uint8_t mirror_page = emulator->config->mirror_page;
  796. uint8_t mirror_byte = emulator->config->mirror.mirror_byte;
  797. MfUltralightMirrorConf mirror_conf = emulator->config_cache.mirror.mirror_conf;
  798. uint16_t last_user_page_index = emulator->page_num - 6;
  799. bool uid_printed = false;
  800. if(mirror_conf == MfUltralightMirrorUid || mirror_conf == MfUltralightMirrorUidCounter) {
  801. // UID range check
  802. if(mirror_page < 4 || mirror_page > last_user_page_index - 3 ||
  803. (mirror_page == last_user_page_index - 3 && mirror_byte > 2)) {
  804. if(mirror_conf == MfUltralightMirrorUid) return;
  805. // NTAG21x has the peculiar behavior when UID+counter selected, if UID does not fit but
  806. // counter will fit, it will actually mirror the counter
  807. string_cat_str(str, " ");
  808. } else {
  809. for(int i = 0; i < 3; ++i) {
  810. string_cat_printf(str, "%02X", emulator->data.data[i]);
  811. }
  812. // Skip BCC0
  813. for(int i = 4; i < 8; ++i) {
  814. string_cat_printf(str, "%02X", emulator->data.data[i]);
  815. }
  816. uid_printed = true;
  817. }
  818. uint16_t next_byte_offset = mirror_page * 4 + mirror_byte + 14;
  819. if(mirror_conf == MfUltralightMirrorUidCounter) ++next_byte_offset;
  820. mirror_page = next_byte_offset / 4;
  821. mirror_byte = next_byte_offset % 4;
  822. }
  823. if(mirror_conf == MfUltralightMirrorCounter || mirror_conf == MfUltralightMirrorUidCounter) {
  824. // Counter is only printed if counter enabled
  825. if(emulator->config_cache.access.nfc_cnt_en) {
  826. // Counter protection check
  827. if(emulator->config_cache.access.nfc_cnt_pwd_prot && !emulator->auth_success) return;
  828. // Counter range check
  829. if(mirror_page < 4) return;
  830. if(mirror_page > last_user_page_index - 1) return;
  831. if(mirror_page == last_user_page_index - 1 && mirror_byte > 2) return;
  832. if(mirror_conf == MfUltralightMirrorUidCounter)
  833. string_cat_str(str, uid_printed ? "x" : " ");
  834. string_cat_printf(str, "%06X", emulator->data.counter[2]);
  835. }
  836. }
  837. }
  838. static void mf_ul_increment_single_counter(MfUltralightEmulator* emulator) {
  839. if(!emulator->read_counter_incremented && emulator->config_cache.access.nfc_cnt_en) {
  840. if(emulator->data.counter[2] < 0xFFFFFF) {
  841. ++emulator->data.counter[2];
  842. emulator->data_changed = true;
  843. }
  844. emulator->read_counter_incremented = true;
  845. }
  846. }
  847. static void mf_ul_emulate_write(
  848. MfUltralightEmulator* emulator,
  849. int16_t tag_addr,
  850. int16_t write_page,
  851. uint8_t* page_buff) {
  852. // Assumption: all access checks have been completed
  853. if(tag_addr == 2) {
  854. // Handle static locks
  855. uint16_t orig_static_locks = emulator->data.data[write_page * 4 + 2] |
  856. (emulator->data.data[write_page * 4 + 3] << 8);
  857. uint16_t new_static_locks = page_buff[2] | (page_buff[3] << 8);
  858. if(orig_static_locks & 1) new_static_locks &= ~0x08;
  859. if(orig_static_locks & 2) new_static_locks &= ~0xF0;
  860. if(orig_static_locks & 4) new_static_locks &= 0xFF;
  861. new_static_locks |= orig_static_locks;
  862. page_buff[0] = emulator->data.data[write_page * 4];
  863. page_buff[1] = emulator->data.data[write_page * 4 + 1];
  864. page_buff[2] = new_static_locks & 0xff;
  865. page_buff[3] = new_static_locks >> 8;
  866. } else if(tag_addr == 3) {
  867. // Handle OTP/capability container
  868. *(uint32_t*)page_buff |= *(uint32_t*)&emulator->data.data[write_page * 4];
  869. } else if(tag_addr == mf_ul_get_dynamic_lock_page_addr(&emulator->data)) {
  870. // Handle dynamic locks
  871. uint16_t orig_locks = emulator->data.data[write_page * 4] |
  872. (emulator->data.data[write_page * 4 + 1] << 8);
  873. uint8_t orig_block_locks = emulator->data.data[write_page * 4 + 2];
  874. uint16_t new_locks = page_buff[0] | (page_buff[1] << 8);
  875. uint8_t new_block_locks = page_buff[2];
  876. int block_lock_count;
  877. switch(emulator->data.type) {
  878. case MfUltralightTypeUL21:
  879. block_lock_count = 5;
  880. break;
  881. case MfUltralightTypeNTAG213:
  882. block_lock_count = 6;
  883. break;
  884. case MfUltralightTypeNTAG215:
  885. block_lock_count = 4;
  886. break;
  887. case MfUltralightTypeNTAG216:
  888. case MfUltralightTypeNTAGI2C1K:
  889. case MfUltralightTypeNTAGI2CPlus1K:
  890. block_lock_count = 7;
  891. break;
  892. case MfUltralightTypeNTAGI2C2K:
  893. case MfUltralightTypeNTAGI2CPlus2K:
  894. block_lock_count = 8;
  895. break;
  896. default:
  897. furi_assert(false);
  898. block_lock_count = 0;
  899. break;
  900. }
  901. for(int i = 0; i < block_lock_count; ++i) {
  902. if(orig_block_locks & (1 << i)) new_locks &= ~(3 << (2 * i));
  903. }
  904. new_locks |= orig_locks;
  905. new_block_locks |= orig_block_locks;
  906. page_buff[0] = new_locks & 0xff;
  907. page_buff[1] = new_locks >> 8;
  908. page_buff[2] = new_block_locks;
  909. if(emulator->data.type >= MfUltralightTypeUL21 &&
  910. emulator->data.type <= MfUltralightTypeNTAG216)
  911. page_buff[3] = MF_UL_TEARING_FLAG_DEFAULT;
  912. else
  913. page_buff[3] = 0;
  914. }
  915. memcpy(&emulator->data.data[write_page * 4], page_buff, 4);
  916. emulator->data_changed = true;
  917. }
  918. void mf_ul_reset_emulation(MfUltralightEmulator* emulator, bool is_power_cycle) {
  919. emulator->curr_sector = 0;
  920. emulator->ntag_i2c_plus_sector3_lockout = false;
  921. emulator->auth_success = false;
  922. if(is_power_cycle) {
  923. if(emulator->config != NULL) emulator->config_cache = *emulator->config;
  924. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  925. emulator->read_counter_incremented = false;
  926. }
  927. } else {
  928. if(emulator->config != NULL) {
  929. // ACCESS (less CFGLCK) and AUTH0 are updated when reactivated
  930. // MIRROR_CONF is not; don't know about STRG_MOD_EN, but we're not using that anyway
  931. emulator->config_cache.access.value = (emulator->config->access.value & 0xBF) |
  932. (emulator->config_cache.access.value & 0x40);
  933. emulator->config_cache.auth0 = emulator->config->auth0;
  934. }
  935. }
  936. }
  937. void mf_ul_prepare_emulation(MfUltralightEmulator* emulator, MfUltralightData* data) {
  938. FURI_LOG_D(TAG, "Prepare emulation");
  939. emulator->data = *data;
  940. emulator->supported_features = mf_ul_get_features(data->type);
  941. emulator->config = mf_ultralight_get_config_pages(&emulator->data);
  942. emulator->page_num = emulator->data.data_size / 4;
  943. emulator->data_changed = false;
  944. emulator->comp_write_cmd_started = false;
  945. emulator->sector_select_cmd_started = false;
  946. mf_ul_reset_emulation(emulator, true);
  947. }
  948. bool mf_ul_prepare_emulation_response(
  949. uint8_t* buff_rx,
  950. uint16_t buff_rx_len,
  951. uint8_t* buff_tx,
  952. uint16_t* buff_tx_len,
  953. uint32_t* data_type,
  954. void* context) {
  955. furi_assert(context);
  956. MfUltralightEmulator* emulator = context;
  957. uint16_t tx_bytes = 0;
  958. uint16_t tx_bits = 0;
  959. bool command_parsed = false;
  960. bool send_ack = false;
  961. bool respond_nothing = false;
  962. bool reset_idle = false;
  963. #ifdef FURI_DEBUG
  964. string_t debug_buf;
  965. string_init(debug_buf);
  966. for(int i = 0; i < (buff_rx_len + 7) / 8; ++i) {
  967. string_cat_printf(debug_buf, "%02x ", buff_rx[i]);
  968. }
  969. string_strim(debug_buf);
  970. FURI_LOG_T(TAG, "Emu RX (%d): %s", buff_rx_len, string_get_cstr(debug_buf));
  971. string_reset(debug_buf);
  972. #endif
  973. // Check composite commands
  974. if(emulator->comp_write_cmd_started) {
  975. // Compatibility write is the only one composit command
  976. if(buff_rx_len == 16 * 8) {
  977. mf_ul_emulate_write(
  978. emulator, emulator->comp_write_page_addr, emulator->comp_write_page_addr, buff_rx);
  979. send_ack = true;
  980. command_parsed = true;
  981. }
  982. emulator->comp_write_cmd_started = false;
  983. } else if(emulator->sector_select_cmd_started) {
  984. if(buff_rx_len == 4 * 8) {
  985. if(buff_rx[0] <= 0xFE) {
  986. emulator->curr_sector = buff_rx[0] > 3 ? 0 : buff_rx[0];
  987. emulator->ntag_i2c_plus_sector3_lockout = false;
  988. command_parsed = true;
  989. respond_nothing = true;
  990. FURI_LOG_D(TAG, "Changing sector to %d", emulator->curr_sector);
  991. }
  992. }
  993. emulator->sector_select_cmd_started = false;
  994. } else if(buff_rx_len >= 8) {
  995. uint8_t cmd = buff_rx[0];
  996. if(cmd == MF_UL_GET_VERSION_CMD) {
  997. if(emulator->data.type != MfUltralightTypeUnknown) {
  998. if(buff_rx_len == 1 * 8) {
  999. tx_bytes = sizeof(emulator->data.version);
  1000. memcpy(buff_tx, &emulator->data.version, tx_bytes);
  1001. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1002. command_parsed = true;
  1003. }
  1004. }
  1005. } else if(cmd == MF_UL_READ_CMD) {
  1006. if(buff_rx_len == (1 + 1) * 8) {
  1007. int16_t start_page = buff_rx[1];
  1008. tx_bytes = 16;
  1009. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1010. if(start_page < emulator->page_num) {
  1011. do {
  1012. uint8_t copied_pages = 0;
  1013. uint8_t src_page = start_page;
  1014. uint8_t last_page_plus_one = start_page + 4;
  1015. uint8_t pwd_page = emulator->page_num - 2;
  1016. string_t ascii_mirror;
  1017. size_t ascii_mirror_len = 0;
  1018. const char* ascii_mirror_cptr = NULL;
  1019. uint8_t ascii_mirror_curr_page = 0;
  1020. uint8_t ascii_mirror_curr_byte = 0;
  1021. if(last_page_plus_one > emulator->page_num)
  1022. last_page_plus_one = emulator->page_num;
  1023. if(emulator->supported_features & MfUltralightSupportAuth) {
  1024. if(!mf_ul_check_auth(emulator, start_page, false)) break;
  1025. if(!emulator->auth_success && emulator->config_cache.access.prot &&
  1026. emulator->config_cache.auth0 < last_page_plus_one)
  1027. last_page_plus_one = emulator->config_cache.auth0;
  1028. }
  1029. if(emulator->supported_features & MfUltralightSupportSingleCounter)
  1030. mf_ul_increment_single_counter(emulator);
  1031. if(emulator->supported_features & MfUltralightSupportAsciiMirror &&
  1032. emulator->config_cache.mirror.mirror_conf !=
  1033. MfUltralightMirrorNone) {
  1034. ascii_mirror_curr_byte = emulator->config->mirror.mirror_byte;
  1035. ascii_mirror_curr_page = emulator->config->mirror_page;
  1036. // Try to avoid wasting time making mirror if we won't copy it
  1037. // Conservatively check with UID+counter mirror size
  1038. if(last_page_plus_one > ascii_mirror_curr_page &&
  1039. start_page + 3 >= ascii_mirror_curr_page &&
  1040. start_page <= ascii_mirror_curr_page + 6) {
  1041. string_init(ascii_mirror);
  1042. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1043. ascii_mirror_len = string_length_u(ascii_mirror);
  1044. ascii_mirror_cptr = string_get_cstr(ascii_mirror);
  1045. // Move pointer to where it should be to start copying
  1046. if(ascii_mirror_len > 0 &&
  1047. ascii_mirror_curr_page < start_page &&
  1048. ascii_mirror_curr_byte != 0) {
  1049. uint8_t diff = 4 - ascii_mirror_curr_byte;
  1050. ascii_mirror_len -= diff;
  1051. ascii_mirror_cptr += diff;
  1052. ascii_mirror_curr_byte = 0;
  1053. ++ascii_mirror_curr_page;
  1054. }
  1055. while(ascii_mirror_len > 0 &&
  1056. ascii_mirror_curr_page < start_page) {
  1057. uint8_t diff = ascii_mirror_len > 4 ? 4 : ascii_mirror_len;
  1058. ascii_mirror_len -= diff;
  1059. ascii_mirror_cptr += diff;
  1060. ++ascii_mirror_curr_page;
  1061. }
  1062. }
  1063. }
  1064. uint8_t* dest_ptr = buff_tx;
  1065. while(copied_pages < 4) {
  1066. // Copy page
  1067. memcpy(dest_ptr, &emulator->data.data[src_page * 4], 4);
  1068. // Note: don't have to worry about roll-over with ASCII mirror because
  1069. // lowest valid page for it is 4, while roll-over will at best read
  1070. // pages 0-2
  1071. if(ascii_mirror_len > 0 && src_page == ascii_mirror_curr_page) {
  1072. // Copy ASCII mirror
  1073. size_t copy_len = 4 - ascii_mirror_curr_byte;
  1074. if(copy_len > ascii_mirror_len) copy_len = ascii_mirror_len;
  1075. for(size_t i = 0; i < copy_len; ++i) {
  1076. if(*ascii_mirror_cptr != ' ')
  1077. dest_ptr[ascii_mirror_curr_byte] =
  1078. (uint8_t)*ascii_mirror_cptr;
  1079. ++ascii_mirror_curr_byte;
  1080. ++ascii_mirror_cptr;
  1081. }
  1082. ascii_mirror_len -= copy_len;
  1083. // Don't care if this is inaccurate after ascii_mirror_len = 0
  1084. ascii_mirror_curr_byte = 0;
  1085. ++ascii_mirror_curr_page;
  1086. }
  1087. if(emulator->supported_features & MfUltralightSupportAuth) {
  1088. if(src_page == pwd_page || src_page == pwd_page + 1) {
  1089. // Blank out PWD and PACK pages
  1090. memset(dest_ptr, 0, 4);
  1091. }
  1092. }
  1093. dest_ptr += 4;
  1094. ++copied_pages;
  1095. ++src_page;
  1096. if(src_page >= last_page_plus_one) src_page = 0;
  1097. }
  1098. if(ascii_mirror_cptr != NULL) {
  1099. string_clear(ascii_mirror);
  1100. }
  1101. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1102. command_parsed = true;
  1103. } while(false);
  1104. }
  1105. } else {
  1106. uint16_t valid_pages;
  1107. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1108. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1109. if(start_page != -1) {
  1110. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1111. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1112. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1113. emulator->curr_sector == 3 && valid_pages == 1) {
  1114. // Rewind back a sector to match behavior on a real tag
  1115. --start_page;
  1116. ++valid_pages;
  1117. }
  1118. uint16_t copy_count = (valid_pages > 4 ? 4 : valid_pages) * 4;
  1119. FURI_LOG_D(
  1120. TAG,
  1121. "NTAG I2C Emu: page valid, %02x:%02x -> %d, %d",
  1122. emulator->curr_sector,
  1123. buff_rx[1],
  1124. start_page,
  1125. valid_pages);
  1126. memcpy(buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1127. // For NTAG I2C, there's no roll-over; remainder is filled by null bytes
  1128. if(copy_count < tx_bytes)
  1129. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1130. // Special case: NTAG I2C Plus sector 0 page 233 read crosses into page 236
  1131. if(start_page == 233)
  1132. memcpy(
  1133. &buff_tx[12], &emulator->data.data[(start_page + 1) * 4], 4);
  1134. mf_ul_protect_auth_data_on_read_command_i2c(
  1135. buff_tx, start_page, start_page + copy_count / 4 - 1, emulator);
  1136. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1137. command_parsed = true;
  1138. }
  1139. } else {
  1140. FURI_LOG_D(
  1141. TAG,
  1142. "NTAG I2C Emu: page invalid, %02x:%02x",
  1143. emulator->curr_sector,
  1144. buff_rx[1]);
  1145. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1146. emulator->curr_sector == 3 &&
  1147. !emulator->ntag_i2c_plus_sector3_lockout) {
  1148. // NTAG I2C Plus has a weird behavior where if you read sector 3
  1149. // at an invalid address, it responds with zeroes then locks
  1150. // the read out, while if you read the mirrored session registers,
  1151. // it returns both session registers on either pages
  1152. memset(buff_tx, 0, tx_bytes);
  1153. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1154. command_parsed = true;
  1155. emulator->ntag_i2c_plus_sector3_lockout = true;
  1156. }
  1157. }
  1158. }
  1159. if(!command_parsed) tx_bytes = 0;
  1160. }
  1161. } else if(cmd == MF_UL_FAST_READ_CMD) {
  1162. if(emulator->supported_features & MfUltralightSupportFastRead) {
  1163. if(buff_rx_len == (1 + 2) * 8) {
  1164. int16_t start_page = buff_rx[1];
  1165. uint8_t end_page = buff_rx[2];
  1166. if(start_page <= end_page) {
  1167. tx_bytes = ((end_page + 1) - start_page) * 4;
  1168. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1169. if((start_page < emulator->page_num) &&
  1170. (end_page < emulator->page_num)) {
  1171. do {
  1172. if(emulator->supported_features & MfUltralightSupportAuth) {
  1173. // NAK if not authenticated and requested pages cross over AUTH0
  1174. if(!emulator->auth_success &&
  1175. emulator->config_cache.access.prot &&
  1176. (start_page >= emulator->config_cache.auth0 ||
  1177. end_page >= emulator->config_cache.auth0))
  1178. break;
  1179. }
  1180. if(emulator->supported_features &
  1181. MfUltralightSupportSingleCounter)
  1182. mf_ul_increment_single_counter(emulator);
  1183. // Copy requested pages
  1184. memcpy(
  1185. buff_tx, &emulator->data.data[start_page * 4], tx_bytes);
  1186. if(emulator->supported_features &
  1187. MfUltralightSupportAsciiMirror &&
  1188. emulator->config_cache.mirror.mirror_conf !=
  1189. MfUltralightMirrorNone) {
  1190. // Copy ASCII mirror
  1191. // Less stringent check here, because expecting FAST_READ to
  1192. // only be issued once rather than repeatedly
  1193. string_t ascii_mirror;
  1194. string_init(ascii_mirror);
  1195. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1196. size_t ascii_mirror_len = string_length_u(ascii_mirror);
  1197. const char* ascii_mirror_cptr =
  1198. string_get_cstr(ascii_mirror);
  1199. int16_t mirror_start_offset =
  1200. (emulator->config->mirror_page - start_page) * 4 +
  1201. emulator->config->mirror.mirror_byte;
  1202. if(mirror_start_offset < 0) {
  1203. if(mirror_start_offset < -(int16_t)ascii_mirror_len) {
  1204. // Past ASCII mirror, don't copy
  1205. ascii_mirror_len = 0;
  1206. } else {
  1207. ascii_mirror_cptr += -mirror_start_offset;
  1208. ascii_mirror_len -= -mirror_start_offset;
  1209. mirror_start_offset = 0;
  1210. }
  1211. }
  1212. if(ascii_mirror_len > 0) {
  1213. int16_t mirror_end_offset =
  1214. mirror_start_offset + ascii_mirror_len;
  1215. if(mirror_end_offset > (end_page + 1) * 4) {
  1216. mirror_end_offset = (end_page + 1) * 4;
  1217. ascii_mirror_len =
  1218. mirror_end_offset - mirror_start_offset;
  1219. }
  1220. for(size_t i = 0; i < ascii_mirror_len; ++i) {
  1221. if(*ascii_mirror_cptr != ' ')
  1222. buff_tx[mirror_start_offset] =
  1223. (uint8_t)*ascii_mirror_cptr;
  1224. ++mirror_start_offset;
  1225. ++ascii_mirror_cptr;
  1226. }
  1227. }
  1228. string_clear(ascii_mirror);
  1229. }
  1230. if(emulator->supported_features & MfUltralightSupportAuth) {
  1231. // Clear PWD and PACK pages
  1232. uint8_t pwd_page = emulator->page_num - 2;
  1233. int16_t pwd_page_offset = pwd_page - start_page;
  1234. // PWD page
  1235. if(pwd_page_offset >= 0 && pwd_page <= end_page) {
  1236. memset(&buff_tx[pwd_page_offset * 4], 0, 4);
  1237. // PACK page
  1238. if(pwd_page + 1 <= end_page)
  1239. memset(&buff_tx[(pwd_page_offset + 1) * 4], 0, 4);
  1240. }
  1241. }
  1242. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1243. command_parsed = true;
  1244. } while(false);
  1245. }
  1246. } else {
  1247. uint16_t valid_pages;
  1248. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1249. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1250. if(start_page != -1) {
  1251. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1252. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1253. uint16_t copy_count = tx_bytes;
  1254. if(copy_count > valid_pages * 4) copy_count = valid_pages * 4;
  1255. memcpy(
  1256. buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1257. if(copy_count < tx_bytes)
  1258. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1259. mf_ul_ntag_i2c_fill_cross_area_read(
  1260. buff_tx, buff_rx[1], buff_rx[2], emulator);
  1261. mf_ul_protect_auth_data_on_read_command_i2c(
  1262. buff_tx,
  1263. start_page,
  1264. start_page + copy_count / 4 - 1,
  1265. emulator);
  1266. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1267. command_parsed = true;
  1268. }
  1269. }
  1270. }
  1271. if(!command_parsed) tx_bytes = 0;
  1272. }
  1273. }
  1274. }
  1275. } else if(cmd == MF_UL_WRITE) {
  1276. if(buff_rx_len == (1 + 5) * 8) {
  1277. do {
  1278. uint8_t orig_write_page = buff_rx[1];
  1279. int16_t write_page = orig_write_page;
  1280. uint16_t valid_pages; // unused
  1281. write_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1282. &emulator->data, write_page, emulator->curr_sector, &valid_pages);
  1283. if(write_page == -1) // NTAG I2C range check
  1284. break;
  1285. else if(write_page < 2 || write_page >= emulator->page_num) // Other MFUL/NTAG range check
  1286. break;
  1287. if(emulator->supported_features & MfUltralightSupportAuth) {
  1288. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  1289. if(!mf_ul_ntag_i2c_plus_check_auth(emulator, orig_write_page, true))
  1290. break;
  1291. } else {
  1292. if(!mf_ul_check_auth(emulator, orig_write_page, true)) break;
  1293. }
  1294. }
  1295. int16_t tag_addr = mf_ultralight_page_addr_to_tag_addr(
  1296. emulator->curr_sector, orig_write_page);
  1297. if(!mf_ul_check_lock(emulator, tag_addr)) break;
  1298. mf_ul_emulate_write(emulator, tag_addr, write_page, &buff_rx[2]);
  1299. send_ack = true;
  1300. command_parsed = true;
  1301. } while(false);
  1302. }
  1303. } else if(cmd == MF_UL_FAST_WRITE) {
  1304. if(emulator->supported_features & MfUltralightSupportFastWrite) {
  1305. if(buff_rx_len == (1 + 66) * 8) {
  1306. if(buff_rx[1] == 0xF0 && buff_rx[2] == 0xFF) {
  1307. // TODO: update when SRAM emulation implemented
  1308. send_ack = true;
  1309. command_parsed = true;
  1310. }
  1311. }
  1312. }
  1313. } else if(cmd == MF_UL_COMP_WRITE) {
  1314. if(emulator->supported_features & MfUltralightSupportCompatWrite) {
  1315. if(buff_rx_len == (1 + 1) * 8) {
  1316. uint8_t write_page = buff_rx[1];
  1317. do {
  1318. if(write_page < 2 || write_page >= emulator->page_num) break;
  1319. if(emulator->supported_features & MfUltralightSupportAuth &&
  1320. !mf_ul_check_auth(emulator, write_page, true))
  1321. break;
  1322. // Note we don't convert to tag addr here because there's only one sector
  1323. if(!mf_ul_check_lock(emulator, write_page)) break;
  1324. emulator->comp_write_cmd_started = true;
  1325. emulator->comp_write_page_addr = write_page;
  1326. send_ack = true;
  1327. command_parsed = true;
  1328. } while(false);
  1329. }
  1330. }
  1331. } else if(cmd == MF_UL_READ_CNT) {
  1332. if(emulator->supported_features & MfUltralightSupportReadCounter) {
  1333. if(buff_rx_len == (1 + 1) * 8) {
  1334. do {
  1335. uint8_t cnt_num = buff_rx[1];
  1336. // NTAG21x checks
  1337. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  1338. if(cnt_num != 2) break; // Only counter 2 is available
  1339. if(!emulator->config_cache.access.nfc_cnt_en)
  1340. break; // NAK if counter not enabled
  1341. if(emulator->config_cache.access.nfc_cnt_pwd_prot &&
  1342. !emulator->auth_success)
  1343. break;
  1344. }
  1345. if(cnt_num < 3) {
  1346. buff_tx[0] = emulator->data.counter[cnt_num] & 0xFF;
  1347. buff_tx[1] = (emulator->data.counter[cnt_num] >> 8) & 0xFF;
  1348. buff_tx[2] = (emulator->data.counter[cnt_num] >> 16) & 0xFF;
  1349. tx_bytes = 3;
  1350. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1351. command_parsed = true;
  1352. }
  1353. } while(false);
  1354. }
  1355. }
  1356. } else if(cmd == MF_UL_INC_CNT) {
  1357. if(emulator->supported_features & MfUltralightSupportIncrCounter) {
  1358. if(buff_rx_len == (1 + 5) * 8) {
  1359. uint8_t cnt_num = buff_rx[1];
  1360. uint32_t inc = (buff_rx[2] | (buff_rx[3] << 8) | (buff_rx[4] << 16));
  1361. // TODO: can you increment by 0 when counter is at 0xffffff?
  1362. if((cnt_num < 3) && (emulator->data.counter[cnt_num] != 0x00FFFFFF) &&
  1363. (emulator->data.counter[cnt_num] + inc <= 0x00FFFFFF)) {
  1364. emulator->data.counter[cnt_num] += inc;
  1365. // We're RAM-backed, so tearing never happens
  1366. emulator->data.tearing[cnt_num] = MF_UL_TEARING_FLAG_DEFAULT;
  1367. emulator->data_changed = true;
  1368. send_ack = true;
  1369. command_parsed = true;
  1370. }
  1371. }
  1372. }
  1373. } else if(cmd == MF_UL_AUTH) {
  1374. if(emulator->supported_features & MfUltralightSupportAuth) {
  1375. if(buff_rx_len == (1 + 4) * 8) {
  1376. uint16_t scaled_authlim = mf_ultralight_calc_auth_count(&emulator->data);
  1377. if(scaled_authlim != 0 && emulator->data.curr_authlim >= scaled_authlim) {
  1378. if(emulator->data.curr_authlim != UINT16_MAX) {
  1379. // Handle case where AUTHLIM has been lowered or changed from 0
  1380. emulator->data.curr_authlim = UINT16_MAX;
  1381. emulator->data_changed = true;
  1382. }
  1383. // AUTHLIM reached, always fail
  1384. buff_tx[0] = MF_UL_NAK_AUTHLIM_REACHED;
  1385. tx_bits = 4;
  1386. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1387. mf_ul_reset_emulation(emulator, false);
  1388. command_parsed = true;
  1389. } else {
  1390. if(memcmp(&buff_rx[1], emulator->config->auth_data.pwd.raw, 4) == 0) {
  1391. // Correct password
  1392. buff_tx[0] = emulator->config->auth_data.pack.raw[0];
  1393. buff_tx[1] = emulator->config->auth_data.pack.raw[1];
  1394. tx_bytes = 2;
  1395. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1396. emulator->auth_success = true;
  1397. command_parsed = true;
  1398. if(emulator->data.curr_authlim != 0) {
  1399. // Reset current AUTHLIM
  1400. emulator->data.curr_authlim = 0;
  1401. emulator->data_changed = true;
  1402. }
  1403. } else if(!emulator->config->auth_data.pwd.value) {
  1404. // Unknown password, pretend to be an Amiibo
  1405. buff_tx[0] = 0x80;
  1406. buff_tx[1] = 0x80;
  1407. tx_bytes = 2;
  1408. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1409. emulator->auth_success = true;
  1410. command_parsed = true;
  1411. } else {
  1412. // Wrong password, increase negative verification count
  1413. if(emulator->data.curr_authlim < UINT16_MAX) {
  1414. ++emulator->data.curr_authlim;
  1415. emulator->data_changed = true;
  1416. }
  1417. if(scaled_authlim != 0 &&
  1418. emulator->data.curr_authlim >= scaled_authlim) {
  1419. emulator->data.curr_authlim = UINT16_MAX;
  1420. buff_tx[0] = MF_UL_NAK_AUTHLIM_REACHED;
  1421. tx_bits = 4;
  1422. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1423. mf_ul_reset_emulation(emulator, false);
  1424. command_parsed = true;
  1425. } else {
  1426. // Should delay here to slow brute forcing
  1427. }
  1428. }
  1429. }
  1430. }
  1431. }
  1432. } else if(cmd == MF_UL_READ_SIG) {
  1433. if(emulator->supported_features & MfUltralightSupportSignature) {
  1434. // Check 2nd byte = 0x00 - RFU
  1435. if(buff_rx_len == (1 + 1) * 8 && buff_rx[1] == 0x00) {
  1436. tx_bytes = sizeof(emulator->data.signature);
  1437. memcpy(buff_tx, emulator->data.signature, tx_bytes);
  1438. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1439. command_parsed = true;
  1440. }
  1441. }
  1442. } else if(cmd == MF_UL_CHECK_TEARING) {
  1443. if(emulator->supported_features & MfUltralightSupportTearingFlags) {
  1444. if(buff_rx_len == (1 + 1) * 8) {
  1445. uint8_t cnt_num = buff_rx[1];
  1446. if(cnt_num < 3) {
  1447. buff_tx[0] = emulator->data.tearing[cnt_num];
  1448. tx_bytes = 1;
  1449. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1450. command_parsed = true;
  1451. }
  1452. }
  1453. }
  1454. } else if(cmd == MF_UL_HALT_START) {
  1455. reset_idle = true;
  1456. FURI_LOG_D(TAG, "Received HLTA");
  1457. } else if(cmd == MF_UL_SECTOR_SELECT) {
  1458. if(emulator->supported_features & MfUltralightSupportSectorSelect) {
  1459. if(buff_rx_len == (1 + 1) * 8 && buff_rx[1] == 0xFF) {
  1460. // Send ACK
  1461. emulator->sector_select_cmd_started = true;
  1462. send_ack = true;
  1463. command_parsed = true;
  1464. }
  1465. }
  1466. } else if(cmd == MF_UL_READ_VCSL) {
  1467. if(emulator->supported_features & MfUltralightSupportVcsl) {
  1468. if(buff_rx_len == (1 + 20) * 8) {
  1469. buff_tx[0] = emulator->config_cache.vctid;
  1470. tx_bytes = 1;
  1471. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1472. command_parsed = true;
  1473. }
  1474. }
  1475. } else {
  1476. reset_idle = true;
  1477. FURI_LOG_D(TAG, "Received invalid command");
  1478. }
  1479. } else {
  1480. reset_idle = true;
  1481. FURI_LOG_D(TAG, "Received invalid buffer less than 8 bits in length");
  1482. }
  1483. if(reset_idle) {
  1484. mf_ul_reset_emulation(emulator, false);
  1485. tx_bits = 0;
  1486. command_parsed = true;
  1487. }
  1488. if(!command_parsed) {
  1489. // Send NACK
  1490. buff_tx[0] = MF_UL_NAK_INVALID_ARGUMENT;
  1491. tx_bits = 4;
  1492. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1493. // Every NAK should cause reset to IDLE
  1494. mf_ul_reset_emulation(emulator, false);
  1495. } else if(send_ack) {
  1496. buff_tx[0] = MF_UL_ACK;
  1497. tx_bits = 4;
  1498. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1499. }
  1500. if(respond_nothing) {
  1501. *buff_tx_len = UINT16_MAX;
  1502. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1503. } else {
  1504. // Return tx buffer size in bits
  1505. if(tx_bytes) {
  1506. tx_bits = tx_bytes * 8;
  1507. }
  1508. *buff_tx_len = tx_bits;
  1509. }
  1510. #ifdef FURI_DEBUG
  1511. if(*buff_tx_len == UINT16_MAX) {
  1512. FURI_LOG_T(TAG, "Emu TX: no reply");
  1513. } else if(*buff_tx_len > 0) {
  1514. int count = (*buff_tx_len + 7) / 8;
  1515. for(int i = 0; i < count; ++i) {
  1516. string_cat_printf(debug_buf, "%02x ", buff_tx[i]);
  1517. }
  1518. string_strim(debug_buf);
  1519. FURI_LOG_T(TAG, "Emu TX (%d): %s", *buff_tx_len, string_get_cstr(debug_buf));
  1520. string_clear(debug_buf);
  1521. } else {
  1522. FURI_LOG_T(TAG, "Emu TX: HALT");
  1523. }
  1524. #endif
  1525. return tx_bits > 0;
  1526. }