mifare_ultralight.c 73 KB

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