nfc_device.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. #include "nfc_device.h"
  2. #include "assets_icons.h"
  3. #include "m-string.h"
  4. #include "nfc_types.h"
  5. #include <lib/toolbox/path.h>
  6. #include <lib/toolbox/hex.h>
  7. #include <lib/nfc/protocols/nfc_util.h>
  8. #include <flipper_format/flipper_format.h>
  9. #define NFC_DEVICE_KEYS_FOLDER EXT_PATH("nfc/cache")
  10. #define NFC_DEVICE_KEYS_EXTENSION ".keys"
  11. static const char* nfc_file_header = "Flipper NFC device";
  12. static const uint32_t nfc_file_version = 2;
  13. static const char* nfc_keys_file_header = "Flipper NFC keys";
  14. static const uint32_t nfc_keys_file_version = 1;
  15. // Protocols format versions
  16. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  17. NfcDevice* nfc_device_alloc() {
  18. NfcDevice* nfc_dev = malloc(sizeof(NfcDevice));
  19. nfc_dev->storage = furi_record_open(RECORD_STORAGE);
  20. nfc_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  21. string_init(nfc_dev->load_path);
  22. string_init(nfc_dev->dev_data.parsed_data);
  23. return nfc_dev;
  24. }
  25. void nfc_device_free(NfcDevice* nfc_dev) {
  26. furi_assert(nfc_dev);
  27. nfc_device_clear(nfc_dev);
  28. furi_record_close(RECORD_STORAGE);
  29. furi_record_close(RECORD_DIALOGS);
  30. string_clear(nfc_dev->load_path);
  31. string_clear(nfc_dev->dev_data.parsed_data);
  32. free(nfc_dev);
  33. }
  34. static void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
  35. if(dev->format == NfcDeviceSaveFormatUid) {
  36. string_set_str(format_string, "UID");
  37. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  38. string_set_str(format_string, "Bank card");
  39. } else if(dev->format == NfcDeviceSaveFormatMifareUl) {
  40. string_set_str(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
  41. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  42. string_set_str(format_string, "Mifare Classic");
  43. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  44. string_set_str(format_string, "Mifare DESFire");
  45. } else {
  46. string_set_str(format_string, "Unknown");
  47. }
  48. }
  49. static bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
  50. if(string_start_with_str_p(format_string, "UID")) {
  51. dev->format = NfcDeviceSaveFormatUid;
  52. dev->dev_data.protocol = NfcDeviceProtocolUnknown;
  53. return true;
  54. }
  55. if(string_start_with_str_p(format_string, "Bank card")) {
  56. dev->format = NfcDeviceSaveFormatBankCard;
  57. dev->dev_data.protocol = NfcDeviceProtocolEMV;
  58. return true;
  59. }
  60. // Check Mifare Ultralight types
  61. for(MfUltralightType type = MfUltralightTypeUnknown; type < MfUltralightTypeNum; type++) {
  62. if(string_equal_str_p(format_string, nfc_mf_ul_type(type, true))) {
  63. dev->format = NfcDeviceSaveFormatMifareUl;
  64. dev->dev_data.protocol = NfcDeviceProtocolMifareUl;
  65. dev->dev_data.mf_ul_data.type = type;
  66. return true;
  67. }
  68. }
  69. if(string_start_with_str_p(format_string, "Mifare Classic")) {
  70. dev->format = NfcDeviceSaveFormatMifareClassic;
  71. dev->dev_data.protocol = NfcDeviceProtocolMifareClassic;
  72. return true;
  73. }
  74. if(string_start_with_str_p(format_string, "Mifare DESFire")) {
  75. dev->format = NfcDeviceSaveFormatMifareDesfire;
  76. dev->dev_data.protocol = NfcDeviceProtocolMifareDesfire;
  77. return true;
  78. }
  79. return false;
  80. }
  81. static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
  82. bool saved = false;
  83. MfUltralightData* data = &dev->dev_data.mf_ul_data;
  84. string_t temp_str;
  85. string_init(temp_str);
  86. // Save Mifare Ultralight specific data
  87. do {
  88. if(!flipper_format_write_comment_cstr(file, "Mifare Ultralight specific data")) break;
  89. if(!flipper_format_write_hex(file, "Signature", data->signature, sizeof(data->signature)))
  90. break;
  91. if(!flipper_format_write_hex(
  92. file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
  93. break;
  94. // Write conters and tearing flags data
  95. bool counters_saved = true;
  96. for(uint8_t i = 0; i < 3; i++) {
  97. string_printf(temp_str, "Counter %d", i);
  98. if(!flipper_format_write_uint32(
  99. file, string_get_cstr(temp_str), &data->counter[i], 1)) {
  100. counters_saved = false;
  101. break;
  102. }
  103. string_printf(temp_str, "Tearing %d", i);
  104. if(!flipper_format_write_hex(file, string_get_cstr(temp_str), &data->tearing[i], 1)) {
  105. counters_saved = false;
  106. break;
  107. }
  108. }
  109. if(!counters_saved) break;
  110. // Write pages data
  111. uint32_t pages_total = data->data_size / 4;
  112. if(!flipper_format_write_uint32(file, "Pages total", &pages_total, 1)) break;
  113. bool pages_saved = true;
  114. for(uint16_t i = 0; i < data->data_size; i += 4) {
  115. string_printf(temp_str, "Page %d", i / 4);
  116. if(!flipper_format_write_hex(file, string_get_cstr(temp_str), &data->data[i], 4)) {
  117. pages_saved = false;
  118. break;
  119. }
  120. }
  121. if(!pages_saved) break;
  122. // Write authentication counter
  123. uint32_t auth_counter = data->curr_authlim;
  124. if(!flipper_format_write_uint32(file, "Failed authentication attempts", &auth_counter, 1))
  125. break;
  126. saved = true;
  127. } while(false);
  128. string_clear(temp_str);
  129. return saved;
  130. }
  131. bool nfc_device_load_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
  132. bool parsed = false;
  133. MfUltralightData* data = &dev->dev_data.mf_ul_data;
  134. string_t temp_str;
  135. string_init(temp_str);
  136. do {
  137. // Read signature
  138. if(!flipper_format_read_hex(file, "Signature", data->signature, sizeof(data->signature)))
  139. break;
  140. // Read Mifare version
  141. if(!flipper_format_read_hex(
  142. file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
  143. break;
  144. // Read counters and tearing flags
  145. bool counters_parsed = true;
  146. for(uint8_t i = 0; i < 3; i++) {
  147. string_printf(temp_str, "Counter %d", i);
  148. if(!flipper_format_read_uint32(file, string_get_cstr(temp_str), &data->counter[i], 1)) {
  149. counters_parsed = false;
  150. break;
  151. }
  152. string_printf(temp_str, "Tearing %d", i);
  153. if(!flipper_format_read_hex(file, string_get_cstr(temp_str), &data->tearing[i], 1)) {
  154. counters_parsed = false;
  155. break;
  156. }
  157. }
  158. if(!counters_parsed) break;
  159. // Read pages
  160. uint32_t pages = 0;
  161. if(!flipper_format_read_uint32(file, "Pages total", &pages, 1)) break;
  162. data->data_size = pages * 4;
  163. bool pages_parsed = true;
  164. for(uint16_t i = 0; i < pages; i++) {
  165. string_printf(temp_str, "Page %d", i);
  166. if(!flipper_format_read_hex(file, string_get_cstr(temp_str), &data->data[i * 4], 4)) {
  167. pages_parsed = false;
  168. break;
  169. }
  170. }
  171. if(!pages_parsed) break;
  172. // Read authentication counter
  173. uint32_t auth_counter;
  174. if(!flipper_format_read_uint32(file, "Failed authentication attempts", &auth_counter, 1))
  175. auth_counter = 0;
  176. parsed = true;
  177. } while(false);
  178. string_clear(temp_str);
  179. return parsed;
  180. }
  181. static bool nfc_device_save_mifare_df_key_settings(
  182. FlipperFormat* file,
  183. MifareDesfireKeySettings* ks,
  184. const char* prefix) {
  185. bool saved = false;
  186. string_t key;
  187. string_init(key);
  188. do {
  189. string_printf(key, "%s Change Key ID", prefix);
  190. if(!flipper_format_write_hex(file, string_get_cstr(key), &ks->change_key_id, 1)) break;
  191. string_printf(key, "%s Config Changeable", prefix);
  192. if(!flipper_format_write_bool(file, string_get_cstr(key), &ks->config_changeable, 1))
  193. break;
  194. string_printf(key, "%s Free Create Delete", prefix);
  195. if(!flipper_format_write_bool(file, string_get_cstr(key), &ks->free_create_delete, 1))
  196. break;
  197. string_printf(key, "%s Free Directory List", prefix);
  198. if(!flipper_format_write_bool(file, string_get_cstr(key), &ks->free_directory_list, 1))
  199. break;
  200. string_printf(key, "%s Key Changeable", prefix);
  201. if(!flipper_format_write_bool(file, string_get_cstr(key), &ks->master_key_changeable, 1))
  202. break;
  203. if(ks->flags) {
  204. string_printf(key, "%s Flags", prefix);
  205. if(!flipper_format_write_hex(file, string_get_cstr(key), &ks->flags, 1)) break;
  206. }
  207. string_printf(key, "%s Max Keys", prefix);
  208. if(!flipper_format_write_hex(file, string_get_cstr(key), &ks->max_keys, 1)) break;
  209. for(MifareDesfireKeyVersion* kv = ks->key_version_head; kv; kv = kv->next) {
  210. string_printf(key, "%s Key %d Version", prefix, kv->id);
  211. if(!flipper_format_write_hex(file, string_get_cstr(key), &kv->version, 1)) break;
  212. }
  213. saved = true;
  214. } while(false);
  215. string_clear(key);
  216. return saved;
  217. }
  218. bool nfc_device_load_mifare_df_key_settings(
  219. FlipperFormat* file,
  220. MifareDesfireKeySettings* ks,
  221. const char* prefix) {
  222. bool parsed = false;
  223. string_t key;
  224. string_init(key);
  225. do {
  226. string_printf(key, "%s Change Key ID", prefix);
  227. if(!flipper_format_read_hex(file, string_get_cstr(key), &ks->change_key_id, 1)) break;
  228. string_printf(key, "%s Config Changeable", prefix);
  229. if(!flipper_format_read_bool(file, string_get_cstr(key), &ks->config_changeable, 1)) break;
  230. string_printf(key, "%s Free Create Delete", prefix);
  231. if(!flipper_format_read_bool(file, string_get_cstr(key), &ks->free_create_delete, 1))
  232. break;
  233. string_printf(key, "%s Free Directory List", prefix);
  234. if(!flipper_format_read_bool(file, string_get_cstr(key), &ks->free_directory_list, 1))
  235. break;
  236. string_printf(key, "%s Key Changeable", prefix);
  237. if(!flipper_format_read_bool(file, string_get_cstr(key), &ks->master_key_changeable, 1))
  238. break;
  239. string_printf(key, "%s Flags", prefix);
  240. if(flipper_format_key_exist(file, string_get_cstr(key))) {
  241. if(!flipper_format_read_hex(file, string_get_cstr(key), &ks->flags, 1)) break;
  242. }
  243. string_printf(key, "%s Max Keys", prefix);
  244. if(!flipper_format_read_hex(file, string_get_cstr(key), &ks->max_keys, 1)) break;
  245. ks->flags |= ks->max_keys >> 4;
  246. ks->max_keys &= 0xF;
  247. MifareDesfireKeyVersion** kv_head = &ks->key_version_head;
  248. for(int key_id = 0; key_id < ks->max_keys; key_id++) {
  249. string_printf(key, "%s Key %d Version", prefix, key_id);
  250. uint8_t version;
  251. if(flipper_format_read_hex(file, string_get_cstr(key), &version, 1)) {
  252. MifareDesfireKeyVersion* kv = malloc(sizeof(MifareDesfireKeyVersion));
  253. memset(kv, 0, sizeof(MifareDesfireKeyVersion));
  254. kv->id = key_id;
  255. kv->version = version;
  256. *kv_head = kv;
  257. kv_head = &kv->next;
  258. }
  259. }
  260. parsed = true;
  261. } while(false);
  262. string_clear(key);
  263. return parsed;
  264. }
  265. static bool nfc_device_save_mifare_df_app(FlipperFormat* file, MifareDesfireApplication* app) {
  266. bool saved = false;
  267. string_t prefix, key;
  268. string_init_printf(prefix, "Application %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
  269. string_init(key);
  270. uint8_t* tmp = NULL;
  271. do {
  272. if(app->key_settings) {
  273. if(!nfc_device_save_mifare_df_key_settings(
  274. file, app->key_settings, string_get_cstr(prefix)))
  275. break;
  276. }
  277. if(!app->file_head) break;
  278. uint32_t n_files = 0;
  279. for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
  280. n_files++;
  281. }
  282. tmp = malloc(n_files);
  283. int i = 0;
  284. for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
  285. tmp[i++] = f->id;
  286. }
  287. string_printf(key, "%s File IDs", string_get_cstr(prefix));
  288. if(!flipper_format_write_hex(file, string_get_cstr(key), tmp, n_files)) break;
  289. bool saved_files = true;
  290. for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
  291. saved_files = false;
  292. string_printf(key, "%s File %d Type", string_get_cstr(prefix), f->id);
  293. if(!flipper_format_write_hex(file, string_get_cstr(key), &f->type, 1)) break;
  294. string_printf(
  295. key, "%s File %d Communication Settings", string_get_cstr(prefix), f->id);
  296. if(!flipper_format_write_hex(file, string_get_cstr(key), &f->comm, 1)) break;
  297. string_printf(key, "%s File %d Access Rights", string_get_cstr(prefix), f->id);
  298. if(!flipper_format_write_hex(
  299. file, string_get_cstr(key), (uint8_t*)&f->access_rights, 2))
  300. break;
  301. uint16_t size = 0;
  302. if(f->type == MifareDesfireFileTypeStandard ||
  303. f->type == MifareDesfireFileTypeBackup) {
  304. size = f->settings.data.size;
  305. string_printf(key, "%s File %d Size", string_get_cstr(prefix), f->id);
  306. if(!flipper_format_write_uint32(
  307. file, string_get_cstr(key), &f->settings.data.size, 1))
  308. break;
  309. } else if(f->type == MifareDesfireFileTypeValue) {
  310. string_printf(key, "%s File %d Hi Limit", string_get_cstr(prefix), f->id);
  311. if(!flipper_format_write_uint32(
  312. file, string_get_cstr(key), &f->settings.value.hi_limit, 1))
  313. break;
  314. string_printf(key, "%s File %d Lo Limit", string_get_cstr(prefix), f->id);
  315. if(!flipper_format_write_uint32(
  316. file, string_get_cstr(key), &f->settings.value.lo_limit, 1))
  317. break;
  318. string_printf(
  319. key, "%s File %d Limited Credit Value", string_get_cstr(prefix), f->id);
  320. if(!flipper_format_write_uint32(
  321. file, string_get_cstr(key), &f->settings.value.limited_credit_value, 1))
  322. break;
  323. string_printf(
  324. key, "%s File %d Limited Credit Enabled", string_get_cstr(prefix), f->id);
  325. if(!flipper_format_write_bool(
  326. file, string_get_cstr(key), &f->settings.value.limited_credit_enabled, 1))
  327. break;
  328. size = 4;
  329. } else if(
  330. f->type == MifareDesfireFileTypeLinearRecord ||
  331. f->type == MifareDesfireFileTypeCyclicRecord) {
  332. string_printf(key, "%s File %d Size", string_get_cstr(prefix), f->id);
  333. if(!flipper_format_write_uint32(
  334. file, string_get_cstr(key), &f->settings.record.size, 1))
  335. break;
  336. string_printf(key, "%s File %d Max", string_get_cstr(prefix), f->id);
  337. if(!flipper_format_write_uint32(
  338. file, string_get_cstr(key), &f->settings.record.max, 1))
  339. break;
  340. string_printf(key, "%s File %d Cur", string_get_cstr(prefix), f->id);
  341. if(!flipper_format_write_uint32(
  342. file, string_get_cstr(key), &f->settings.record.cur, 1))
  343. break;
  344. size = f->settings.record.size * f->settings.record.cur;
  345. }
  346. if(f->contents) {
  347. string_printf(key, "%s File %d", string_get_cstr(prefix), f->id);
  348. if(!flipper_format_write_hex(file, string_get_cstr(key), f->contents, size)) break;
  349. }
  350. saved_files = true;
  351. }
  352. if(!saved_files) {
  353. break;
  354. }
  355. saved = true;
  356. } while(false);
  357. free(tmp);
  358. string_clear(prefix);
  359. string_clear(key);
  360. return saved;
  361. }
  362. bool nfc_device_load_mifare_df_app(FlipperFormat* file, MifareDesfireApplication* app) {
  363. bool parsed = false;
  364. string_t prefix, key;
  365. string_init_printf(prefix, "Application %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
  366. string_init(key);
  367. uint8_t* tmp = NULL;
  368. MifareDesfireFile* f = NULL;
  369. do {
  370. app->key_settings = malloc(sizeof(MifareDesfireKeySettings));
  371. memset(app->key_settings, 0, sizeof(MifareDesfireKeySettings));
  372. if(!nfc_device_load_mifare_df_key_settings(
  373. file, app->key_settings, string_get_cstr(prefix))) {
  374. free(app->key_settings);
  375. app->key_settings = NULL;
  376. break;
  377. }
  378. string_printf(key, "%s File IDs", string_get_cstr(prefix));
  379. uint32_t n_files;
  380. if(!flipper_format_get_value_count(file, string_get_cstr(key), &n_files)) break;
  381. tmp = malloc(n_files);
  382. if(!flipper_format_read_hex(file, string_get_cstr(key), tmp, n_files)) break;
  383. MifareDesfireFile** file_head = &app->file_head;
  384. bool parsed_files = true;
  385. for(uint32_t i = 0; i < n_files; i++) {
  386. parsed_files = false;
  387. f = malloc(sizeof(MifareDesfireFile));
  388. memset(f, 0, sizeof(MifareDesfireFile));
  389. f->id = tmp[i];
  390. string_printf(key, "%s File %d Type", string_get_cstr(prefix), f->id);
  391. if(!flipper_format_read_hex(file, string_get_cstr(key), &f->type, 1)) break;
  392. string_printf(
  393. key, "%s File %d Communication Settings", string_get_cstr(prefix), f->id);
  394. if(!flipper_format_read_hex(file, string_get_cstr(key), &f->comm, 1)) break;
  395. string_printf(key, "%s File %d Access Rights", string_get_cstr(prefix), f->id);
  396. if(!flipper_format_read_hex(file, string_get_cstr(key), (uint8_t*)&f->access_rights, 2))
  397. break;
  398. if(f->type == MifareDesfireFileTypeStandard ||
  399. f->type == MifareDesfireFileTypeBackup) {
  400. string_printf(key, "%s File %d Size", string_get_cstr(prefix), f->id);
  401. if(!flipper_format_read_uint32(
  402. file, string_get_cstr(key), &f->settings.data.size, 1))
  403. break;
  404. } else if(f->type == MifareDesfireFileTypeValue) {
  405. string_printf(key, "%s File %d Hi Limit", string_get_cstr(prefix), f->id);
  406. if(!flipper_format_read_uint32(
  407. file, string_get_cstr(key), &f->settings.value.hi_limit, 1))
  408. break;
  409. string_printf(key, "%s File %d Lo Limit", string_get_cstr(prefix), f->id);
  410. if(!flipper_format_read_uint32(
  411. file, string_get_cstr(key), &f->settings.value.lo_limit, 1))
  412. break;
  413. string_printf(
  414. key, "%s File %d Limited Credit Value", string_get_cstr(prefix), f->id);
  415. if(!flipper_format_read_uint32(
  416. file, string_get_cstr(key), &f->settings.value.limited_credit_value, 1))
  417. break;
  418. string_printf(
  419. key, "%s File %d Limited Credit Enabled", string_get_cstr(prefix), f->id);
  420. if(!flipper_format_read_bool(
  421. file, string_get_cstr(key), &f->settings.value.limited_credit_enabled, 1))
  422. break;
  423. } else if(
  424. f->type == MifareDesfireFileTypeLinearRecord ||
  425. f->type == MifareDesfireFileTypeCyclicRecord) {
  426. string_printf(key, "%s File %d Size", string_get_cstr(prefix), f->id);
  427. if(!flipper_format_read_uint32(
  428. file, string_get_cstr(key), &f->settings.record.size, 1))
  429. break;
  430. string_printf(key, "%s File %d Max", string_get_cstr(prefix), f->id);
  431. if(!flipper_format_read_uint32(
  432. file, string_get_cstr(key), &f->settings.record.max, 1))
  433. break;
  434. string_printf(key, "%s File %d Cur", string_get_cstr(prefix), f->id);
  435. if(!flipper_format_read_uint32(
  436. file, string_get_cstr(key), &f->settings.record.cur, 1))
  437. break;
  438. }
  439. string_printf(key, "%s File %d", string_get_cstr(prefix), f->id);
  440. if(flipper_format_key_exist(file, string_get_cstr(key))) {
  441. uint32_t size;
  442. if(!flipper_format_get_value_count(file, string_get_cstr(key), &size)) break;
  443. f->contents = malloc(size);
  444. if(!flipper_format_read_hex(file, string_get_cstr(key), f->contents, size)) break;
  445. }
  446. *file_head = f;
  447. file_head = &f->next;
  448. f = NULL;
  449. parsed_files = true;
  450. }
  451. if(!parsed_files) {
  452. break;
  453. }
  454. parsed = true;
  455. } while(false);
  456. if(f) {
  457. free(f->contents);
  458. free(f);
  459. }
  460. free(tmp);
  461. string_clear(prefix);
  462. string_clear(key);
  463. return parsed;
  464. }
  465. static bool nfc_device_save_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
  466. bool saved = false;
  467. MifareDesfireData* data = &dev->dev_data.mf_df_data;
  468. uint8_t* tmp = NULL;
  469. do {
  470. if(!flipper_format_write_comment_cstr(file, "Mifare DESFire specific data")) break;
  471. if(!flipper_format_write_hex(
  472. file, "PICC Version", (uint8_t*)&data->version, sizeof(data->version)))
  473. break;
  474. if(data->free_memory) {
  475. if(!flipper_format_write_uint32(file, "PICC Free Memory", &data->free_memory->bytes, 1))
  476. break;
  477. }
  478. if(data->master_key_settings) {
  479. if(!nfc_device_save_mifare_df_key_settings(file, data->master_key_settings, "PICC"))
  480. break;
  481. }
  482. uint32_t n_apps = 0;
  483. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  484. n_apps++;
  485. }
  486. if(!flipper_format_write_uint32(file, "Application Count", &n_apps, 1)) break;
  487. if(n_apps) {
  488. tmp = malloc(n_apps * 3);
  489. int i = 0;
  490. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  491. memcpy(tmp + i, app->id, 3);
  492. i += 3;
  493. }
  494. if(!flipper_format_write_hex(file, "Application IDs", tmp, n_apps * 3)) break;
  495. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  496. if(!nfc_device_save_mifare_df_app(file, app)) break;
  497. }
  498. }
  499. saved = true;
  500. } while(false);
  501. free(tmp);
  502. return saved;
  503. }
  504. bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
  505. bool parsed = false;
  506. MifareDesfireData* data = &dev->dev_data.mf_df_data;
  507. memset(data, 0, sizeof(MifareDesfireData));
  508. uint8_t* tmp = NULL;
  509. do {
  510. if(!flipper_format_read_hex(
  511. file, "PICC Version", (uint8_t*)&data->version, sizeof(data->version)))
  512. break;
  513. if(flipper_format_key_exist(file, "PICC Free Memory")) {
  514. data->free_memory = malloc(sizeof(MifareDesfireFreeMemory));
  515. memset(data->free_memory, 0, sizeof(MifareDesfireFreeMemory));
  516. if(!flipper_format_read_uint32(
  517. file, "PICC Free Memory", &data->free_memory->bytes, 1)) {
  518. free(data->free_memory);
  519. break;
  520. }
  521. }
  522. if(flipper_format_key_exist(file, "PICC Change Key ID")) {
  523. data->master_key_settings = malloc(sizeof(MifareDesfireKeySettings));
  524. memset(data->master_key_settings, 0, sizeof(MifareDesfireKeySettings));
  525. if(!nfc_device_load_mifare_df_key_settings(file, data->master_key_settings, "PICC")) {
  526. free(data->master_key_settings);
  527. data->master_key_settings = NULL;
  528. break;
  529. }
  530. }
  531. uint32_t n_apps;
  532. if(!flipper_format_read_uint32(file, "Application Count", &n_apps, 1)) break;
  533. if(n_apps) {
  534. tmp = malloc(n_apps * 3);
  535. if(!flipper_format_read_hex(file, "Application IDs", tmp, n_apps * 3)) break;
  536. bool parsed_apps = true;
  537. MifareDesfireApplication** app_head = &data->app_head;
  538. for(uint32_t i = 0; i < n_apps; i++) {
  539. MifareDesfireApplication* app = malloc(sizeof(MifareDesfireApplication));
  540. memset(app, 0, sizeof(MifareDesfireApplication));
  541. memcpy(app->id, &tmp[i * 3], 3);
  542. if(!nfc_device_load_mifare_df_app(file, app)) {
  543. free(app);
  544. parsed_apps = false;
  545. break;
  546. }
  547. *app_head = app;
  548. app_head = &app->next;
  549. }
  550. if(!parsed_apps) break;
  551. }
  552. parsed = true;
  553. } while(false);
  554. free(tmp);
  555. return parsed;
  556. }
  557. static bool nfc_device_save_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
  558. bool saved = false;
  559. EmvData* data = &dev->dev_data.emv_data;
  560. uint32_t data_temp = 0;
  561. do {
  562. // Write Bank card specific data
  563. if(!flipper_format_write_comment_cstr(file, "Bank card specific data")) break;
  564. if(!flipper_format_write_hex(file, "AID", data->aid, data->aid_len)) break;
  565. if(!flipper_format_write_string_cstr(file, "Name", data->name)) break;
  566. if(!flipper_format_write_hex(file, "Number", data->number, data->number_len)) break;
  567. if(data->exp_mon) {
  568. uint8_t exp_data[2] = {data->exp_mon, data->exp_year};
  569. if(!flipper_format_write_hex(file, "Exp data", exp_data, sizeof(exp_data))) break;
  570. }
  571. if(data->country_code) {
  572. data_temp = data->country_code;
  573. if(!flipper_format_write_uint32(file, "Country code", &data_temp, 1)) break;
  574. }
  575. if(data->currency_code) {
  576. data_temp = data->currency_code;
  577. if(!flipper_format_write_uint32(file, "Currency code", &data_temp, 1)) break;
  578. }
  579. saved = true;
  580. } while(false);
  581. return saved;
  582. }
  583. bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
  584. bool parsed = false;
  585. EmvData* data = &dev->dev_data.emv_data;
  586. memset(data, 0, sizeof(EmvData));
  587. uint32_t data_cnt = 0;
  588. string_t temp_str;
  589. string_init(temp_str);
  590. do {
  591. // Load essential data
  592. if(!flipper_format_get_value_count(file, "AID", &data_cnt)) break;
  593. data->aid_len = data_cnt;
  594. if(!flipper_format_read_hex(file, "AID", data->aid, data->aid_len)) break;
  595. if(!flipper_format_read_string(file, "Name", temp_str)) break;
  596. strlcpy(data->name, string_get_cstr(temp_str), sizeof(data->name));
  597. if(!flipper_format_get_value_count(file, "Number", &data_cnt)) break;
  598. data->number_len = data_cnt;
  599. if(!flipper_format_read_hex(file, "Number", data->number, data->number_len)) break;
  600. parsed = true;
  601. // Load optional data
  602. uint8_t exp_data[2] = {};
  603. if(flipper_format_read_hex(file, "Exp data", exp_data, 2)) {
  604. data->exp_mon = exp_data[0];
  605. data->exp_year = exp_data[1];
  606. }
  607. if(flipper_format_read_uint32(file, "Country code", &data_cnt, 1)) {
  608. data->country_code = data_cnt;
  609. }
  610. if(flipper_format_read_uint32(file, "Currency code", &data_cnt, 1)) {
  611. data->currency_code = data_cnt;
  612. }
  613. } while(false);
  614. string_clear(temp_str);
  615. return parsed;
  616. }
  617. static void nfc_device_write_mifare_classic_block(
  618. string_t block_str,
  619. MfClassicData* data,
  620. uint8_t block_num) {
  621. string_reset(block_str);
  622. bool is_sec_trailer = mf_classic_is_sector_trailer(block_num);
  623. if(is_sec_trailer) {
  624. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  625. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
  626. // Write key A
  627. for(size_t i = 0; i < sizeof(sec_tr->key_a); i++) {
  628. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) {
  629. string_cat_printf(block_str, "%02X ", sec_tr->key_a[i]);
  630. } else {
  631. string_cat_printf(block_str, "?? ");
  632. }
  633. }
  634. // Write Access bytes
  635. for(size_t i = 0; i < MF_CLASSIC_ACCESS_BYTES_SIZE; i++) {
  636. if(mf_classic_is_block_read(data, block_num)) {
  637. string_cat_printf(block_str, "%02X ", sec_tr->access_bits[i]);
  638. } else {
  639. string_cat_printf(block_str, "?? ");
  640. }
  641. }
  642. // Write key B
  643. for(size_t i = 0; i < sizeof(sec_tr->key_b); i++) {
  644. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) {
  645. string_cat_printf(block_str, "%02X ", sec_tr->key_b[i]);
  646. } else {
  647. string_cat_printf(block_str, "?? ");
  648. }
  649. }
  650. } else {
  651. // Write data block
  652. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  653. if(mf_classic_is_block_read(data, block_num)) {
  654. string_cat_printf(block_str, "%02X ", data->block[block_num].value[i]);
  655. } else {
  656. string_cat_printf(block_str, "?? ");
  657. }
  658. }
  659. }
  660. string_strim(block_str);
  661. }
  662. static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  663. bool saved = false;
  664. MfClassicData* data = &dev->dev_data.mf_classic_data;
  665. string_t temp_str;
  666. string_init(temp_str);
  667. uint16_t blocks = 0;
  668. // Save Mifare Classic specific data
  669. do {
  670. if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
  671. if(data->type == MfClassicType1k) {
  672. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  673. blocks = 64;
  674. } else if(data->type == MfClassicType4k) {
  675. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  676. blocks = 256;
  677. }
  678. if(!flipper_format_write_uint32(
  679. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  680. break;
  681. if(!flipper_format_write_comment_cstr(
  682. file, "Mifare Classic blocks, \'??\' means unknown data"))
  683. break;
  684. bool block_saved = true;
  685. string_t block_str;
  686. string_init(block_str);
  687. for(size_t i = 0; i < blocks; i++) {
  688. string_printf(temp_str, "Block %d", i);
  689. nfc_device_write_mifare_classic_block(block_str, data, i);
  690. if(!flipper_format_write_string(file, string_get_cstr(temp_str), block_str)) {
  691. block_saved = false;
  692. break;
  693. }
  694. }
  695. string_clear(block_str);
  696. if(!block_saved) break;
  697. saved = true;
  698. } while(false);
  699. string_clear(temp_str);
  700. return saved;
  701. }
  702. static void nfc_device_load_mifare_classic_block(
  703. string_t block_str,
  704. MfClassicData* data,
  705. uint8_t block_num) {
  706. string_strim(block_str);
  707. MfClassicBlock block_tmp = {};
  708. bool is_sector_trailer = mf_classic_is_sector_trailer(block_num);
  709. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  710. uint16_t block_unknown_bytes_mask = 0;
  711. string_strim(block_str);
  712. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  713. char hi = string_get_char(block_str, 3 * i);
  714. char low = string_get_char(block_str, 3 * i + 1);
  715. uint8_t byte = 0;
  716. if(hex_chars_to_uint8(hi, low, &byte)) {
  717. block_tmp.value[i] = byte;
  718. } else {
  719. FURI_BIT_SET(block_unknown_bytes_mask, i);
  720. }
  721. }
  722. if(block_unknown_bytes_mask == 0xffff) {
  723. // All data is unknown, exit
  724. return;
  725. }
  726. if(is_sector_trailer) {
  727. MfClassicSectorTrailer* sec_tr_tmp = (MfClassicSectorTrailer*)&block_tmp;
  728. // Load Key A
  729. // Key A mask 0b0000000000111111 = 0x003f
  730. if((block_unknown_bytes_mask & 0x003f) == 0) {
  731. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_a, sizeof(sec_tr_tmp->key_a));
  732. mf_classic_set_key_found(data, sector_num, MfClassicKeyA, key);
  733. }
  734. // Load Access Bits
  735. // Access bits mask 0b0000001111000000 = 0x03c0
  736. if((block_unknown_bytes_mask & 0x03c0) == 0) {
  737. mf_classic_set_block_read(data, block_num, &block_tmp);
  738. }
  739. // Load Key B
  740. // Key B mask 0b1111110000000000 = 0xfc00
  741. if((block_unknown_bytes_mask & 0xfc00) == 0) {
  742. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_b, sizeof(sec_tr_tmp->key_b));
  743. mf_classic_set_key_found(data, sector_num, MfClassicKeyB, key);
  744. }
  745. } else {
  746. if(block_unknown_bytes_mask == 0) {
  747. mf_classic_set_block_read(data, block_num, &block_tmp);
  748. }
  749. }
  750. }
  751. static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  752. bool parsed = false;
  753. MfClassicData* data = &dev->dev_data.mf_classic_data;
  754. string_t temp_str;
  755. uint32_t data_format_version = 0;
  756. string_init(temp_str);
  757. uint16_t data_blocks = 0;
  758. memset(data, 0, sizeof(MfClassicData));
  759. do {
  760. // Read Mifare Classic type
  761. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  762. if(!string_cmp_str(temp_str, "1K")) {
  763. data->type = MfClassicType1k;
  764. data_blocks = 64;
  765. } else if(!string_cmp_str(temp_str, "4K")) {
  766. data->type = MfClassicType4k;
  767. data_blocks = 256;
  768. } else {
  769. break;
  770. }
  771. bool old_format = false;
  772. // Read Mifare Classic format version
  773. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  774. // Load unread sectors with zero keys access for backward compatability
  775. if(!flipper_format_rewind(file)) break;
  776. old_format = true;
  777. } else {
  778. if(data_format_version < nfc_mifare_classic_data_format_version) {
  779. old_format = true;
  780. }
  781. }
  782. // Read Mifare Classic blocks
  783. bool block_read = true;
  784. string_t block_str;
  785. string_init(block_str);
  786. for(size_t i = 0; i < data_blocks; i++) {
  787. string_printf(temp_str, "Block %d", i);
  788. if(!flipper_format_read_string(file, string_get_cstr(temp_str), block_str)) {
  789. block_read = false;
  790. break;
  791. }
  792. nfc_device_load_mifare_classic_block(block_str, data, i);
  793. }
  794. string_clear(block_str);
  795. if(!block_read) break;
  796. // Set keys and blocks as unknown for backward compatibility
  797. if(old_format) {
  798. data->key_a_mask = 0ULL;
  799. data->key_b_mask = 0ULL;
  800. memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
  801. }
  802. parsed = true;
  803. } while(false);
  804. string_clear(temp_str);
  805. return parsed;
  806. }
  807. static void nfc_device_get_key_cache_file_path(NfcDevice* dev, string_t file_path) {
  808. uint8_t* uid = dev->dev_data.nfc_data.uid;
  809. uint8_t uid_len = dev->dev_data.nfc_data.uid_len;
  810. string_set_str(file_path, NFC_DEVICE_KEYS_FOLDER "/");
  811. for(size_t i = 0; i < uid_len; i++) {
  812. string_cat_printf(file_path, "%02X", uid[i]);
  813. }
  814. string_cat_printf(file_path, NFC_DEVICE_KEYS_EXTENSION);
  815. }
  816. static bool nfc_device_save_mifare_classic_keys(NfcDevice* dev) {
  817. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  818. MfClassicData* data = &dev->dev_data.mf_classic_data;
  819. string_t temp_str;
  820. string_init(temp_str);
  821. nfc_device_get_key_cache_file_path(dev, temp_str);
  822. bool save_success = false;
  823. do {
  824. if(!storage_simply_mkdir(dev->storage, NFC_DEVICE_KEYS_FOLDER)) break;
  825. if(!storage_simply_remove(dev->storage, string_get_cstr(temp_str))) break;
  826. if(!flipper_format_file_open_always(file, string_get_cstr(temp_str))) break;
  827. if(!flipper_format_write_header_cstr(file, nfc_keys_file_header, nfc_keys_file_version))
  828. break;
  829. if(data->type == MfClassicType1k) {
  830. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  831. } else if(data->type == MfClassicType4k) {
  832. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  833. }
  834. if(!flipper_format_write_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  835. if(!flipper_format_write_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  836. uint8_t sector_num = mf_classic_get_total_sectors_num(data->type);
  837. bool key_save_success = true;
  838. for(size_t i = 0; (i < sector_num) && (key_save_success); i++) {
  839. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  840. if(FURI_BIT(data->key_a_mask, i)) {
  841. string_printf(temp_str, "Key A sector %d", i);
  842. key_save_success =
  843. flipper_format_write_hex(file, string_get_cstr(temp_str), sec_tr->key_a, 6);
  844. }
  845. if(!key_save_success) break;
  846. if(FURI_BIT(data->key_a_mask, i)) {
  847. string_printf(temp_str, "Key B sector %d", i);
  848. key_save_success =
  849. flipper_format_write_hex(file, string_get_cstr(temp_str), sec_tr->key_b, 6);
  850. }
  851. }
  852. save_success = key_save_success;
  853. } while(false);
  854. flipper_format_free(file);
  855. string_clear(temp_str);
  856. return save_success;
  857. }
  858. bool nfc_device_load_key_cache(NfcDevice* dev) {
  859. furi_assert(dev);
  860. string_t temp_str;
  861. string_init(temp_str);
  862. MfClassicData* data = &dev->dev_data.mf_classic_data;
  863. nfc_device_get_key_cache_file_path(dev, temp_str);
  864. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  865. bool load_success = false;
  866. do {
  867. if(storage_common_stat(dev->storage, string_get_cstr(temp_str), NULL) != FSE_OK) break;
  868. if(!flipper_format_file_open_existing(file, string_get_cstr(temp_str))) break;
  869. uint32_t version = 0;
  870. if(!flipper_format_read_header(file, temp_str, &version)) break;
  871. if(string_cmp_str(temp_str, nfc_keys_file_header)) break;
  872. if(version != nfc_keys_file_version) break;
  873. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  874. if(!string_cmp_str(temp_str, "1K")) {
  875. data->type = MfClassicType1k;
  876. } else if(!string_cmp_str(temp_str, "4K")) {
  877. data->type = MfClassicType4k;
  878. } else {
  879. break;
  880. }
  881. if(!flipper_format_read_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  882. if(!flipper_format_read_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  883. uint8_t sectors = mf_classic_get_total_sectors_num(data->type);
  884. bool key_read_success = true;
  885. for(size_t i = 0; (i < sectors) && (key_read_success); i++) {
  886. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  887. if(FURI_BIT(data->key_a_mask, i)) {
  888. string_printf(temp_str, "Key A sector %d", i);
  889. key_read_success =
  890. flipper_format_read_hex(file, string_get_cstr(temp_str), sec_tr->key_a, 6);
  891. }
  892. if(!key_read_success) break;
  893. if(FURI_BIT(data->key_b_mask, i)) {
  894. string_printf(temp_str, "Key B sector %d", i);
  895. key_read_success =
  896. flipper_format_read_hex(file, string_get_cstr(temp_str), sec_tr->key_b, 6);
  897. }
  898. }
  899. load_success = key_read_success;
  900. } while(false);
  901. string_clear(temp_str);
  902. flipper_format_free(file);
  903. return load_success;
  904. }
  905. void nfc_device_set_name(NfcDevice* dev, const char* name) {
  906. furi_assert(dev);
  907. strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
  908. }
  909. static void nfc_device_get_path_without_ext(string_t orig_path, string_t shadow_path) {
  910. // TODO: this won't work if there is ".nfc" anywhere in the path other than
  911. // at the end
  912. size_t ext_start = string_search_str(orig_path, NFC_APP_EXTENSION);
  913. string_set_n(shadow_path, orig_path, 0, ext_start);
  914. }
  915. static void nfc_device_get_shadow_path(string_t orig_path, string_t shadow_path) {
  916. nfc_device_get_path_without_ext(orig_path, shadow_path);
  917. string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
  918. }
  919. static bool nfc_device_save_file(
  920. NfcDevice* dev,
  921. const char* dev_name,
  922. const char* folder,
  923. const char* extension,
  924. bool use_load_path) {
  925. furi_assert(dev);
  926. bool saved = false;
  927. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  928. FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
  929. string_t temp_str;
  930. string_init(temp_str);
  931. do {
  932. if(use_load_path && !string_empty_p(dev->load_path)) {
  933. // Get directory name
  934. path_extract_dirname(string_get_cstr(dev->load_path), temp_str);
  935. // Create nfc directory if necessary
  936. if(!storage_simply_mkdir(dev->storage, string_get_cstr(temp_str))) break;
  937. // Make path to file to save
  938. string_cat_printf(temp_str, "/%s%s", dev_name, extension);
  939. } else {
  940. // Create nfc directory if necessary
  941. if(!storage_simply_mkdir(dev->storage, NFC_APP_FOLDER)) break;
  942. // First remove nfc device file if it was saved
  943. string_printf(temp_str, "%s/%s%s", folder, dev_name, extension);
  944. }
  945. // Open file
  946. if(!flipper_format_file_open_always(file, string_get_cstr(temp_str))) break;
  947. // Write header
  948. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  949. // Write nfc device type
  950. if(!flipper_format_write_comment_cstr(
  951. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic, Bank card"))
  952. break;
  953. nfc_device_prepare_format_string(dev, temp_str);
  954. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  955. // Write UID, ATQA, SAK
  956. if(!flipper_format_write_comment_cstr(file, "UID, ATQA and SAK are common for all formats"))
  957. break;
  958. if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
  959. if(!flipper_format_write_hex(file, "ATQA", data->atqa, 2)) break;
  960. if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
  961. // Save more data if necessary
  962. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  963. if(!nfc_device_save_mifare_ul_data(file, dev)) break;
  964. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  965. if(!nfc_device_save_mifare_df_data(file, dev)) break;
  966. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  967. if(!nfc_device_save_bank_card_data(file, dev)) break;
  968. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  969. // Save data
  970. if(!nfc_device_save_mifare_classic_data(file, dev)) break;
  971. // Save keys cache
  972. if(!nfc_device_save_mifare_classic_keys(dev)) break;
  973. }
  974. saved = true;
  975. } while(0);
  976. if(!saved) {
  977. dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
  978. }
  979. string_clear(temp_str);
  980. flipper_format_free(file);
  981. return saved;
  982. }
  983. bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
  984. return nfc_device_save_file(dev, dev_name, NFC_APP_FOLDER, NFC_APP_EXTENSION, true);
  985. }
  986. bool nfc_device_save_shadow(NfcDevice* dev, const char* dev_name) {
  987. dev->shadow_file_exist = true;
  988. return nfc_device_save_file(dev, dev_name, NFC_APP_FOLDER, NFC_APP_SHADOW_EXTENSION, true);
  989. }
  990. static bool nfc_device_load_data(NfcDevice* dev, string_t path, bool show_dialog) {
  991. bool parsed = false;
  992. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  993. FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
  994. uint32_t data_cnt = 0;
  995. string_t temp_str;
  996. string_init(temp_str);
  997. bool deprecated_version = false;
  998. if(dev->loading_cb) {
  999. dev->loading_cb(dev->loading_cb_ctx, true);
  1000. }
  1001. do {
  1002. // Check existance of shadow file
  1003. nfc_device_get_shadow_path(path, temp_str);
  1004. dev->shadow_file_exist =
  1005. storage_common_stat(dev->storage, string_get_cstr(temp_str), NULL) == FSE_OK;
  1006. // Open shadow file if it exists. If not - open original
  1007. if(dev->shadow_file_exist) {
  1008. if(!flipper_format_file_open_existing(file, string_get_cstr(temp_str))) break;
  1009. } else {
  1010. if(!flipper_format_file_open_existing(file, string_get_cstr(path))) break;
  1011. }
  1012. // Read and verify file header
  1013. uint32_t version = 0;
  1014. if(!flipper_format_read_header(file, temp_str, &version)) break;
  1015. if(string_cmp_str(temp_str, nfc_file_header) || (version != nfc_file_version)) {
  1016. deprecated_version = true;
  1017. break;
  1018. }
  1019. // Read Nfc device type
  1020. if(!flipper_format_read_string(file, "Device type", temp_str)) break;
  1021. if(!nfc_device_parse_format_string(dev, temp_str)) break;
  1022. // Read and parse UID, ATQA and SAK
  1023. if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
  1024. if(!(data_cnt == 4 || data_cnt == 7)) break;
  1025. data->uid_len = data_cnt;
  1026. if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
  1027. if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
  1028. if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
  1029. // Parse other data
  1030. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  1031. if(!nfc_device_load_mifare_ul_data(file, dev)) break;
  1032. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  1033. if(!nfc_device_load_mifare_classic_data(file, dev)) break;
  1034. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  1035. if(!nfc_device_load_mifare_df_data(file, dev)) break;
  1036. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  1037. if(!nfc_device_load_bank_card_data(file, dev)) break;
  1038. }
  1039. parsed = true;
  1040. } while(false);
  1041. if(dev->loading_cb) {
  1042. dev->loading_cb(dev->loading_cb_ctx, false);
  1043. }
  1044. if((!parsed) && (show_dialog)) {
  1045. if(deprecated_version) {
  1046. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  1047. } else {
  1048. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  1049. }
  1050. }
  1051. string_clear(temp_str);
  1052. flipper_format_free(file);
  1053. return parsed;
  1054. }
  1055. bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
  1056. furi_assert(dev);
  1057. furi_assert(file_path);
  1058. // Load device data
  1059. string_set_str(dev->load_path, file_path);
  1060. bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
  1061. if(dev_load) {
  1062. // Set device name
  1063. string_t filename;
  1064. string_init(filename);
  1065. path_extract_filename_no_ext(file_path, filename);
  1066. nfc_device_set_name(dev, string_get_cstr(filename));
  1067. string_clear(filename);
  1068. }
  1069. return dev_load;
  1070. }
  1071. bool nfc_file_select(NfcDevice* dev) {
  1072. furi_assert(dev);
  1073. // Input events and views are managed by file_browser
  1074. string_t nfc_app_folder;
  1075. string_init_set_str(nfc_app_folder, NFC_APP_FOLDER);
  1076. bool res = dialog_file_browser_show(
  1077. dev->dialogs, dev->load_path, nfc_app_folder, NFC_APP_EXTENSION, true, &I_Nfc_10px, true);
  1078. string_clear(nfc_app_folder);
  1079. if(res) {
  1080. string_t filename;
  1081. string_init(filename);
  1082. path_extract_filename(dev->load_path, filename, true);
  1083. strncpy(dev->dev_name, string_get_cstr(filename), NFC_DEV_NAME_MAX_LEN);
  1084. res = nfc_device_load_data(dev, dev->load_path, true);
  1085. if(res) {
  1086. nfc_device_set_name(dev, dev->dev_name);
  1087. }
  1088. string_clear(filename);
  1089. }
  1090. return res;
  1091. }
  1092. void nfc_device_data_clear(NfcDeviceData* dev_data) {
  1093. if(dev_data->protocol == NfcDeviceProtocolMifareDesfire) {
  1094. mf_df_clear(&dev_data->mf_df_data);
  1095. } else if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
  1096. memset(&dev_data->mf_classic_data, 0, sizeof(MfClassicData));
  1097. } else if(dev_data->protocol == NfcDeviceProtocolMifareUl) {
  1098. memset(&dev_data->mf_ul_data, 0, sizeof(MfUltralightData));
  1099. } else if(dev_data->protocol == NfcDeviceProtocolEMV) {
  1100. memset(&dev_data->emv_data, 0, sizeof(EmvData));
  1101. }
  1102. memset(&dev_data->nfc_data, 0, sizeof(FuriHalNfcDevData));
  1103. dev_data->protocol = NfcDeviceProtocolUnknown;
  1104. string_reset(dev_data->parsed_data);
  1105. }
  1106. void nfc_device_clear(NfcDevice* dev) {
  1107. furi_assert(dev);
  1108. nfc_device_data_clear(&dev->dev_data);
  1109. dev->format = NfcDeviceSaveFormatUid;
  1110. string_reset(dev->load_path);
  1111. }
  1112. bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
  1113. furi_assert(dev);
  1114. bool deleted = false;
  1115. string_t file_path;
  1116. string_init(file_path);
  1117. do {
  1118. // Delete original file
  1119. if(use_load_path && !string_empty_p(dev->load_path)) {
  1120. string_set(file_path, dev->load_path);
  1121. } else {
  1122. string_printf(file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
  1123. }
  1124. if(!storage_simply_remove(dev->storage, string_get_cstr(file_path))) break;
  1125. // Delete shadow file if it exists
  1126. if(dev->shadow_file_exist) {
  1127. if(use_load_path && !string_empty_p(dev->load_path)) {
  1128. nfc_device_get_shadow_path(dev->load_path, file_path);
  1129. } else {
  1130. string_printf(
  1131. file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
  1132. }
  1133. if(!storage_simply_remove(dev->storage, string_get_cstr(file_path))) break;
  1134. }
  1135. deleted = true;
  1136. } while(0);
  1137. if(!deleted) {
  1138. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  1139. }
  1140. string_clear(file_path);
  1141. return deleted;
  1142. }
  1143. bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
  1144. furi_assert(dev);
  1145. furi_assert(dev->shadow_file_exist);
  1146. bool restored = false;
  1147. string_t path;
  1148. string_init(path);
  1149. do {
  1150. if(use_load_path && !string_empty_p(dev->load_path)) {
  1151. nfc_device_get_shadow_path(dev->load_path, path);
  1152. } else {
  1153. string_printf(
  1154. path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
  1155. }
  1156. if(!storage_simply_remove(dev->storage, string_get_cstr(path))) break;
  1157. dev->shadow_file_exist = false;
  1158. if(use_load_path && !string_empty_p(dev->load_path)) {
  1159. string_set(path, dev->load_path);
  1160. } else {
  1161. string_printf(path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
  1162. }
  1163. if(!nfc_device_load_data(dev, path, true)) break;
  1164. restored = true;
  1165. } while(0);
  1166. string_clear(path);
  1167. return restored;
  1168. }
  1169. void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context) {
  1170. furi_assert(dev);
  1171. dev->loading_cb = callback;
  1172. dev->loading_cb_ctx = context;
  1173. }