nfc_device.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "nfc_device.h"
  2. #include "nfc_types.h"
  3. #include <lib/toolbox/path.h>
  4. #include <lib/toolbox/hex.h>
  5. #include "protocols/nfc_util.h"
  6. #include <flipper_format/flipper_format.h>
  7. #define TAG "NfcDevice"
  8. #define NFC_DEVICE_KEYS_FOLDER EXT_PATH("nfc/.cache")
  9. #define NFC_DEVICE_KEYS_EXTENSION ".keys"
  10. static const char* nfc_file_header = "Flipper NFC device";
  11. static const uint32_t nfc_file_version = 3;
  12. static const char* nfc_keys_file_header = "Flipper NFC keys";
  13. static const uint32_t nfc_keys_file_version = 1;
  14. // Protocols format versions
  15. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  16. NfcDevice* nfc_device_alloc() {
  17. NfcDevice* nfc_dev = malloc(sizeof(NfcDevice));
  18. nfc_dev->storage = furi_record_open(RECORD_STORAGE);
  19. nfc_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  20. nfc_dev->load_path = furi_string_alloc();
  21. nfc_dev->dev_data.parsed_data = furi_string_alloc();
  22. nfc_dev->folder = furi_string_alloc();
  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. furi_string_free(nfc_dev->load_path);
  31. if(nfc_dev->dev_data.parsed_data != NULL) {
  32. furi_string_free(nfc_dev->dev_data.parsed_data);
  33. }
  34. furi_string_free(nfc_dev->folder);
  35. free(nfc_dev);
  36. }
  37. static void nfc_device_prepare_format_string(NfcDevice* dev, FuriString* format_string) {
  38. if(dev->format == NfcDeviceSaveFormatUid) {
  39. furi_string_set(format_string, "UID");
  40. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  41. furi_string_set(format_string, "Mifare Classic");
  42. } else {
  43. furi_string_set(format_string, "Unknown");
  44. }
  45. }
  46. static bool nfc_device_parse_format_string(NfcDevice* dev, FuriString* format_string) {
  47. if(furi_string_start_with_str(format_string, "UID")) {
  48. dev->format = NfcDeviceSaveFormatUid;
  49. dev->dev_data.protocol = NfcDeviceProtocolUnknown;
  50. return true;
  51. }
  52. // Check Mifare
  53. if(furi_string_start_with_str(format_string, "Mifare Classic")) {
  54. dev->format = NfcDeviceSaveFormatMifareClassic;
  55. dev->dev_data.protocol = NfcDeviceProtocolMifareClassic;
  56. return true;
  57. }
  58. return false;
  59. }
  60. static void nfc_device_write_mifare_classic_block(
  61. FuriString* block_str,
  62. MfClassicData* data,
  63. uint8_t block_num) {
  64. furi_string_reset(block_str);
  65. bool is_sec_trailer = mf_classic_is_sector_trailer(block_num);
  66. if(is_sec_trailer) {
  67. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  68. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
  69. // Write key A
  70. for(size_t i = 0; i < sizeof(sec_tr->key_a); i++) {
  71. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) {
  72. furi_string_cat_printf(block_str, "%02X ", sec_tr->key_a[i]);
  73. } else {
  74. furi_string_cat_printf(block_str, "?? ");
  75. }
  76. }
  77. // Write Access bytes
  78. for(size_t i = 0; i < MF_CLASSIC_ACCESS_BYTES_SIZE; i++) {
  79. if(mf_classic_is_block_read(data, block_num)) {
  80. furi_string_cat_printf(block_str, "%02X ", sec_tr->access_bits[i]);
  81. } else {
  82. furi_string_cat_printf(block_str, "?? ");
  83. }
  84. }
  85. // Write key B
  86. for(size_t i = 0; i < sizeof(sec_tr->key_b); i++) {
  87. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) {
  88. furi_string_cat_printf(block_str, "%02X ", sec_tr->key_b[i]);
  89. } else {
  90. furi_string_cat_printf(block_str, "?? ");
  91. }
  92. }
  93. } else {
  94. // Write data block
  95. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  96. if(mf_classic_is_block_read(data, block_num)) {
  97. furi_string_cat_printf(block_str, "%02X ", data->block[block_num].value[i]);
  98. } else {
  99. furi_string_cat_printf(block_str, "?? ");
  100. }
  101. }
  102. }
  103. furi_string_trim(block_str);
  104. }
  105. static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  106. bool saved = false;
  107. MfClassicData* data = &dev->dev_data.mf_classic_data;
  108. FuriString* temp_str;
  109. temp_str = furi_string_alloc();
  110. uint16_t blocks = 0;
  111. // Save Mifare Classic specific data
  112. do {
  113. if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
  114. if(data->type == MfClassicTypeMini) {
  115. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "MINI")) break;
  116. blocks = 20;
  117. } else if(data->type == MfClassicType1k) {
  118. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  119. blocks = 64;
  120. } else if(data->type == MfClassicType4k) {
  121. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  122. blocks = 256;
  123. }
  124. if(!flipper_format_write_uint32(
  125. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  126. break;
  127. if(!flipper_format_write_comment_cstr(
  128. file, "Mifare Classic blocks, \'??\' means unknown data"))
  129. break;
  130. bool block_saved = true;
  131. FuriString* block_str;
  132. block_str = furi_string_alloc();
  133. for(size_t i = 0; i < blocks; i++) {
  134. furi_string_printf(temp_str, "Block %d", i);
  135. nfc_device_write_mifare_classic_block(block_str, data, i);
  136. if(!flipper_format_write_string(file, furi_string_get_cstr(temp_str), block_str)) {
  137. block_saved = false;
  138. break;
  139. }
  140. }
  141. furi_string_free(block_str);
  142. if(!block_saved) break;
  143. saved = true;
  144. } while(false);
  145. furi_string_free(temp_str);
  146. return saved;
  147. }
  148. static void nfc_device_load_mifare_classic_block(
  149. FuriString* block_str,
  150. MfClassicData* data,
  151. uint8_t block_num) {
  152. furi_string_trim(block_str);
  153. MfClassicBlock block_tmp = {};
  154. bool is_sector_trailer = mf_classic_is_sector_trailer(block_num);
  155. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  156. uint16_t block_unknown_bytes_mask = 0;
  157. furi_string_trim(block_str);
  158. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  159. char hi = furi_string_get_char(block_str, 3 * i);
  160. char low = furi_string_get_char(block_str, 3 * i + 1);
  161. uint8_t byte = 0;
  162. if(hex_char_to_uint8(hi, low, &byte)) {
  163. block_tmp.value[i] = byte;
  164. } else {
  165. FURI_BIT_SET(block_unknown_bytes_mask, i);
  166. }
  167. }
  168. if(block_unknown_bytes_mask == 0xffff) {
  169. // All data is unknown, exit
  170. return;
  171. }
  172. if(is_sector_trailer) {
  173. MfClassicSectorTrailer* sec_tr_tmp = (MfClassicSectorTrailer*)&block_tmp;
  174. // Load Key A
  175. // Key A mask 0b0000000000111111 = 0x003f
  176. if((block_unknown_bytes_mask & 0x003f) == 0) {
  177. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_a, sizeof(sec_tr_tmp->key_a));
  178. mf_classic_set_key_found(data, sector_num, MfClassicKeyA, key);
  179. }
  180. // Load Access Bits
  181. // Access bits mask 0b0000001111000000 = 0x03c0
  182. if((block_unknown_bytes_mask & 0x03c0) == 0) {
  183. mf_classic_set_block_read(data, block_num, &block_tmp);
  184. }
  185. // Load Key B
  186. // Key B mask 0b1111110000000000 = 0xfc00
  187. if((block_unknown_bytes_mask & 0xfc00) == 0) {
  188. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_b, sizeof(sec_tr_tmp->key_b));
  189. mf_classic_set_key_found(data, sector_num, MfClassicKeyB, key);
  190. }
  191. } else {
  192. if(block_unknown_bytes_mask == 0) {
  193. mf_classic_set_block_read(data, block_num, &block_tmp);
  194. }
  195. }
  196. }
  197. static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  198. bool parsed = false;
  199. MfClassicData* data = &dev->dev_data.mf_classic_data;
  200. FuriString* temp_str;
  201. uint32_t data_format_version = 0;
  202. temp_str = furi_string_alloc();
  203. uint16_t data_blocks = 0;
  204. memset(data, 0, sizeof(MfClassicData));
  205. do {
  206. // Read Mifare Classic type
  207. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  208. if(!furi_string_cmp(temp_str, "MINI")) {
  209. data->type = MfClassicTypeMini;
  210. data_blocks = 20;
  211. } else if(!furi_string_cmp(temp_str, "1K")) {
  212. data->type = MfClassicType1k;
  213. data_blocks = 64;
  214. } else if(!furi_string_cmp(temp_str, "4K")) {
  215. data->type = MfClassicType4k;
  216. data_blocks = 256;
  217. } else {
  218. break;
  219. }
  220. bool old_format = false;
  221. // Read Mifare Classic format version
  222. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  223. // Load unread sectors with zero keys access for backward compatibility
  224. if(!flipper_format_rewind(file)) break;
  225. old_format = true;
  226. } else {
  227. if(data_format_version < nfc_mifare_classic_data_format_version) {
  228. old_format = true;
  229. }
  230. }
  231. // Read Mifare Classic blocks
  232. bool block_read = true;
  233. FuriString* block_str;
  234. block_str = furi_string_alloc();
  235. for(size_t i = 0; i < data_blocks; i++) {
  236. furi_string_printf(temp_str, "Block %d", i);
  237. if(!flipper_format_read_string(file, furi_string_get_cstr(temp_str), block_str)) {
  238. block_read = false;
  239. break;
  240. }
  241. nfc_device_load_mifare_classic_block(block_str, data, i);
  242. }
  243. furi_string_free(block_str);
  244. if(!block_read) break;
  245. // Set keys and blocks as unknown for backward compatibility
  246. if(old_format) {
  247. data->key_a_mask = 0ULL;
  248. data->key_b_mask = 0ULL;
  249. memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
  250. }
  251. parsed = true;
  252. } while(false);
  253. furi_string_free(temp_str);
  254. return parsed;
  255. }
  256. static void nfc_device_get_key_cache_file_path(NfcDevice* dev, FuriString* file_path) {
  257. uint8_t* uid = dev->dev_data.nfc_data.uid;
  258. uint8_t uid_len = dev->dev_data.nfc_data.uid_len;
  259. furi_string_set(file_path, NFC_DEVICE_KEYS_FOLDER "/");
  260. for(size_t i = 0; i < uid_len; i++) {
  261. furi_string_cat_printf(file_path, "%02X", uid[i]);
  262. }
  263. furi_string_cat_printf(file_path, NFC_DEVICE_KEYS_EXTENSION);
  264. }
  265. static bool nfc_device_save_mifare_classic_keys(NfcDevice* dev) {
  266. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  267. MfClassicData* data = &dev->dev_data.mf_classic_data;
  268. FuriString* temp_str;
  269. temp_str = furi_string_alloc();
  270. nfc_device_get_key_cache_file_path(dev, temp_str);
  271. bool save_success = false;
  272. do {
  273. if(!storage_simply_mkdir(dev->storage, NFC_DEVICE_KEYS_FOLDER)) break;
  274. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(temp_str))) break;
  275. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  276. if(!flipper_format_write_header_cstr(file, nfc_keys_file_header, nfc_keys_file_version))
  277. break;
  278. if(data->type == MfClassicTypeMini) {
  279. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "MINI")) break;
  280. } else if(data->type == MfClassicType1k) {
  281. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  282. } else if(data->type == MfClassicType4k) {
  283. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  284. }
  285. if(!flipper_format_write_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  286. if(!flipper_format_write_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  287. uint8_t sector_num = mf_classic_get_total_sectors_num(data->type);
  288. bool key_save_success = true;
  289. for(size_t i = 0; (i < sector_num) && (key_save_success); i++) {
  290. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  291. if(FURI_BIT(data->key_a_mask, i)) {
  292. furi_string_printf(temp_str, "Key A sector %d", i);
  293. key_save_success = flipper_format_write_hex(
  294. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  295. }
  296. if(!key_save_success) break;
  297. if(FURI_BIT(data->key_b_mask, i)) {
  298. furi_string_printf(temp_str, "Key B sector %d", i);
  299. key_save_success = flipper_format_write_hex(
  300. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  301. }
  302. }
  303. save_success = key_save_success;
  304. } while(false);
  305. flipper_format_free(file);
  306. furi_string_free(temp_str);
  307. return save_success;
  308. }
  309. bool nfc_device_load_key_cache(NfcDevice* dev) {
  310. furi_assert(dev);
  311. FuriString* temp_str;
  312. temp_str = furi_string_alloc();
  313. MfClassicData* data = &dev->dev_data.mf_classic_data;
  314. nfc_device_get_key_cache_file_path(dev, temp_str);
  315. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  316. bool load_success = false;
  317. do {
  318. if(storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK)
  319. break;
  320. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  321. uint32_t version = 0;
  322. if(!flipper_format_read_header(file, temp_str, &version)) break;
  323. if(furi_string_cmp_str(temp_str, nfc_keys_file_header)) break;
  324. if(version != nfc_keys_file_version) break;
  325. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  326. if(!furi_string_cmp(temp_str, "MINI")) {
  327. data->type = MfClassicTypeMini;
  328. } else if(!furi_string_cmp(temp_str, "1K")) {
  329. data->type = MfClassicType1k;
  330. } else if(!furi_string_cmp(temp_str, "4K")) {
  331. data->type = MfClassicType4k;
  332. } else {
  333. break;
  334. }
  335. if(!flipper_format_read_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  336. if(!flipper_format_read_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  337. uint8_t sectors = mf_classic_get_total_sectors_num(data->type);
  338. bool key_read_success = true;
  339. for(size_t i = 0; (i < sectors) && (key_read_success); i++) {
  340. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  341. if(FURI_BIT(data->key_a_mask, i)) {
  342. furi_string_printf(temp_str, "Key A sector %d", i);
  343. key_read_success = flipper_format_read_hex(
  344. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  345. }
  346. if(!key_read_success) break;
  347. if(FURI_BIT(data->key_b_mask, i)) {
  348. furi_string_printf(temp_str, "Key B sector %d", i);
  349. key_read_success = flipper_format_read_hex(
  350. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  351. }
  352. }
  353. load_success = key_read_success;
  354. } while(false);
  355. furi_string_free(temp_str);
  356. flipper_format_free(file);
  357. return load_success;
  358. }
  359. void nfc_device_set_name(NfcDevice* dev, const char* name) {
  360. furi_assert(dev);
  361. strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
  362. }
  363. static void nfc_device_get_path_without_ext(FuriString* orig_path, FuriString* shadow_path) {
  364. // TODO: this won't work if there is ".nfc" anywhere in the path other than
  365. // at the end
  366. size_t ext_start = furi_string_search(orig_path, NFC_APP_FILENAME_EXTENSION);
  367. furi_string_set_n(shadow_path, orig_path, 0, ext_start);
  368. }
  369. static void nfc_device_get_shadow_path(FuriString* orig_path, FuriString* shadow_path) {
  370. nfc_device_get_path_without_ext(orig_path, shadow_path);
  371. furi_string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
  372. }
  373. static void nfc_device_get_folder_from_path(FuriString* path, FuriString* folder) {
  374. size_t last_slash = furi_string_search_rchar(path, '/');
  375. if(last_slash == FURI_STRING_FAILURE) {
  376. // No slashes in the path, treat the whole path as a folder
  377. furi_string_set(folder, path);
  378. } else {
  379. furi_string_set_n(folder, path, 0, last_slash);
  380. }
  381. }
  382. bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
  383. return false;
  384. furi_assert(dev);
  385. bool saved = false;
  386. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  387. FurryHalNfcDevData* data = &dev->dev_data.nfc_data;
  388. FuriString* temp_str;
  389. temp_str = furi_string_alloc();
  390. do {
  391. // Create directory if necessary
  392. FuriString* folder = furi_string_alloc();
  393. // Get folder from filename (filename is in the form of "folder/filename.nfc", so the folder is "folder/")
  394. furi_string_set(temp_str, dev_name);
  395. // Get folder from filename
  396. nfc_device_get_folder_from_path(temp_str, folder);
  397. FURI_LOG_I("Nfc", "Saving to folder %s", furi_string_get_cstr(folder));
  398. if(!storage_simply_mkdir(dev->storage, furi_string_get_cstr(folder))) {
  399. FURI_LOG_E("Nfc", "Failed to create folder %s", furi_string_get_cstr(folder));
  400. break;
  401. }
  402. furi_string_free(folder);
  403. // First remove nfc device file if it was saved
  404. // Open file
  405. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  406. // Write header
  407. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  408. // Write nfc device type
  409. if(!flipper_format_write_comment_cstr(
  410. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic or ISO15693"))
  411. break;
  412. nfc_device_prepare_format_string(dev, temp_str);
  413. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  414. // Write UID
  415. if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) break;
  416. if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
  417. //if(dev->format != NfcDeviceSaveFormatNfcV) {
  418. // Write ATQA, SAK
  419. if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break;
  420. // Save ATQA in MSB order for correct companion apps display
  421. uint8_t atqa[2] = {data->atqa[1], data->atqa[0]};
  422. if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break;
  423. if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
  424. //}
  425. // Save more data if necessary
  426. if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  427. // Save data
  428. if(!nfc_device_save_mifare_classic_data(file, dev)) break;
  429. // Save keys cache
  430. if(!nfc_device_save_mifare_classic_keys(dev)) break;
  431. }
  432. saved = true;
  433. } while(0);
  434. if(!saved) { //-V547
  435. dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
  436. }
  437. furi_string_free(temp_str);
  438. flipper_format_free(file);
  439. return saved;
  440. }
  441. bool nfc_device_save_shadow(NfcDevice* dev, const char* path) {
  442. return false;
  443. dev->shadow_file_exist = true;
  444. // Replace extension from .nfc to .shd if necessary
  445. FuriString* orig_path = furi_string_alloc();
  446. furi_string_set_str(orig_path, path);
  447. FuriString* shadow_path = furi_string_alloc();
  448. nfc_device_get_shadow_path(orig_path, shadow_path);
  449. bool file_saved = nfc_device_save(dev, furi_string_get_cstr(shadow_path));
  450. furi_string_free(orig_path);
  451. furi_string_free(shadow_path);
  452. return file_saved;
  453. }
  454. static bool nfc_device_load_data(NfcDevice* dev, FuriString* path, bool show_dialog) {
  455. bool parsed = false;
  456. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  457. FurryHalNfcDevData* data = &dev->dev_data.nfc_data;
  458. uint32_t data_cnt = 0;
  459. FuriString* temp_str;
  460. temp_str = furi_string_alloc();
  461. bool deprecated_version = false;
  462. // Version 2 of file format had ATQA bytes swapped
  463. uint32_t version_with_lsb_atqa = 2;
  464. if(dev->loading_cb) {
  465. dev->loading_cb(dev->loading_cb_ctx, true);
  466. }
  467. do {
  468. // Check existence of shadow file
  469. nfc_device_get_shadow_path(path, temp_str);
  470. dev->shadow_file_exist =
  471. storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK;
  472. // Open shadow file if it exists. If not - open original
  473. if(dev->shadow_file_exist) {
  474. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  475. } else {
  476. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  477. }
  478. // Read and verify file header
  479. uint32_t version = 0;
  480. if(!flipper_format_read_header(file, temp_str, &version)) break;
  481. if(furi_string_cmp_str(temp_str, nfc_file_header)) break;
  482. if(version != nfc_file_version) {
  483. if(version < version_with_lsb_atqa) {
  484. deprecated_version = true;
  485. break;
  486. }
  487. }
  488. // Read Nfc device type
  489. if(!flipper_format_read_string(file, "Device type", temp_str)) break;
  490. if(!nfc_device_parse_format_string(dev, temp_str)) break;
  491. // Read and parse UID, ATQA and SAK
  492. if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
  493. if(!(data_cnt == 4 || data_cnt == 7 || data_cnt == 8)) break;
  494. data->uid_len = data_cnt;
  495. if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
  496. //if(dev->format != NfcDeviceSaveFormatNfcV) {
  497. if(version == version_with_lsb_atqa) {
  498. if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
  499. } else {
  500. uint8_t atqa[2] = {};
  501. if(!flipper_format_read_hex(file, "ATQA", atqa, 2)) break;
  502. data->atqa[0] = atqa[1];
  503. data->atqa[1] = atqa[0];
  504. }
  505. if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
  506. //}
  507. // Load CUID
  508. uint8_t* cuid_start = data->uid;
  509. if(data->uid_len == 7) {
  510. cuid_start = &data->uid[3];
  511. }
  512. data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
  513. (cuid_start[3]);
  514. // Parse other data
  515. if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  516. if(!nfc_device_load_mifare_classic_data(file, dev)) break;
  517. }
  518. parsed = true;
  519. } while(false);
  520. if(dev->loading_cb) {
  521. dev->loading_cb(dev->loading_cb_ctx, false);
  522. }
  523. if((!parsed) && (show_dialog)) {
  524. if(deprecated_version) {
  525. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  526. } else {
  527. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  528. }
  529. }
  530. furi_string_free(temp_str);
  531. flipper_format_free(file);
  532. return parsed;
  533. }
  534. bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
  535. furi_assert(dev);
  536. furi_assert(file_path);
  537. // Load device data
  538. furi_string_set(dev->load_path, file_path);
  539. bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
  540. if(dev_load) {
  541. // Set device name
  542. FuriString* filename;
  543. filename = furi_string_alloc();
  544. path_extract_filename_no_ext(file_path, filename);
  545. nfc_device_set_name(dev, furi_string_get_cstr(filename));
  546. furi_string_free(filename);
  547. }
  548. return dev_load;
  549. }
  550. void nfc_device_data_clear(NfcDeviceData* dev_data) {
  551. if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
  552. memset(&dev_data->mf_classic_data, 0, sizeof(MfClassicData));
  553. }
  554. memset(&dev_data->nfc_data, 0, sizeof(FurryHalNfcDevData));
  555. dev_data->protocol = NfcDeviceProtocolUnknown;
  556. if(dev_data->parsed_data != NULL) {
  557. furi_string_reset(dev_data->parsed_data);
  558. }
  559. }
  560. void nfc_device_clear(NfcDevice* dev) {
  561. furi_assert(dev);
  562. nfc_device_set_name(dev, "");
  563. nfc_device_data_clear(&dev->dev_data);
  564. dev->format = NfcDeviceSaveFormatUid;
  565. furi_string_reset(dev->load_path);
  566. }
  567. bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
  568. furi_assert(dev);
  569. bool deleted = false;
  570. FuriString* file_path;
  571. file_path = furi_string_alloc();
  572. do {
  573. // Delete original file
  574. if(use_load_path && !furi_string_empty(dev->load_path)) {
  575. furi_string_set(file_path, dev->load_path);
  576. } else {
  577. furi_string_printf(
  578. file_path,
  579. "%s/%s%s",
  580. furi_string_get_cstr(dev->folder),
  581. dev->dev_name,
  582. NFC_APP_FILENAME_EXTENSION);
  583. }
  584. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  585. // Delete shadow file if it exists
  586. if(dev->shadow_file_exist) {
  587. if(use_load_path && !furi_string_empty(dev->load_path)) {
  588. nfc_device_get_shadow_path(dev->load_path, file_path);
  589. } else {
  590. furi_string_printf(
  591. file_path,
  592. "%s/%s%s",
  593. furi_string_get_cstr(dev->folder),
  594. dev->dev_name,
  595. NFC_APP_SHADOW_EXTENSION);
  596. }
  597. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  598. }
  599. deleted = true;
  600. } while(0);
  601. if(!deleted) {
  602. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  603. }
  604. furi_string_free(file_path);
  605. return deleted;
  606. }
  607. bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
  608. furi_assert(dev);
  609. furi_assert(dev->shadow_file_exist);
  610. bool restored = false;
  611. FuriString* path;
  612. path = furi_string_alloc();
  613. do {
  614. if(use_load_path && !furi_string_empty(dev->load_path)) {
  615. nfc_device_get_shadow_path(dev->load_path, path);
  616. } else {
  617. furi_string_printf(
  618. path,
  619. "%s/%s%s",
  620. furi_string_get_cstr(dev->folder),
  621. dev->dev_name,
  622. NFC_APP_SHADOW_EXTENSION);
  623. }
  624. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(path))) break;
  625. dev->shadow_file_exist = false;
  626. if(use_load_path && !furi_string_empty(dev->load_path)) {
  627. furi_string_set(path, dev->load_path);
  628. } else {
  629. furi_string_printf(
  630. path,
  631. "%s/%s%s",
  632. furi_string_get_cstr(dev->folder),
  633. dev->dev_name,
  634. NFC_APP_FILENAME_EXTENSION);
  635. }
  636. if(!nfc_device_load_data(dev, path, true)) break;
  637. restored = true;
  638. } while(0);
  639. furi_string_free(path);
  640. return restored;
  641. }
  642. void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context) {
  643. furi_assert(dev);
  644. dev->loading_cb = callback;
  645. dev->loading_cb_ctx = context;
  646. }