2021-11-08 21:55:06 +00:00
|
|
|
#include "nfc_device.h"
|
2021-12-21 12:33:17 +00:00
|
|
|
#include "nfc_types.h"
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
#include <toolbox/path.h>
|
|
|
|
#include <flipper_format/flipper_format.h>
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
static const char* nfc_file_header = "Flipper NFC device";
|
|
|
|
static const uint32_t nfc_file_version = 2;
|
|
|
|
|
|
|
|
NfcDevice* nfc_device_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
NfcDevice* nfc_dev = malloc(sizeof(NfcDevice));
|
2021-11-08 21:55:06 +00:00
|
|
|
nfc_dev->storage = furi_record_open("storage");
|
|
|
|
nfc_dev->dialogs = furi_record_open("dialogs");
|
|
|
|
return nfc_dev;
|
|
|
|
}
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
void nfc_device_free(NfcDevice* nfc_dev) {
|
|
|
|
furi_assert(nfc_dev);
|
|
|
|
furi_record_close("storage");
|
|
|
|
furi_record_close("dialogs");
|
|
|
|
free(nfc_dev);
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_string) {
|
2021-07-22 06:05:07 +00:00
|
|
|
if(dev->format == NfcDeviceSaveFormatUid) {
|
2021-11-08 21:55:06 +00:00
|
|
|
string_set_str(format_string, "UID");
|
2021-07-22 06:05:07 +00:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
|
2021-11-08 21:55:06 +00:00
|
|
|
string_set_str(format_string, "Bank card");
|
2021-07-22 06:05:07 +00:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareUl) {
|
2021-12-21 12:33:17 +00:00
|
|
|
string_set_str(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
|
2021-07-22 06:05:07 +00:00
|
|
|
} else {
|
2021-11-08 21:55:06 +00:00
|
|
|
string_set_str(format_string, "Unknown");
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
|
|
|
|
if(string_start_with_str_p(format_string, "UID")) {
|
|
|
|
dev->format = NfcDeviceSaveFormatUid;
|
|
|
|
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolUnknown;
|
|
|
|
return true;
|
2021-12-21 12:33:17 +00:00
|
|
|
}
|
|
|
|
if(string_start_with_str_p(format_string, "Bank card")) {
|
2021-07-22 06:05:07 +00:00
|
|
|
dev->format = NfcDeviceSaveFormatBankCard;
|
|
|
|
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolEMV;
|
|
|
|
return true;
|
2021-12-21 12:33:17 +00:00
|
|
|
}
|
|
|
|
// Check Mifare Ultralight types
|
|
|
|
for(MfUltralightType type = MfUltralightTypeUnknown; type < MfUltralightTypeNum; type++) {
|
|
|
|
if(string_start_with_str_p(format_string, nfc_mf_ul_type(type, true))) {
|
|
|
|
dev->format = NfcDeviceSaveFormatMifareUl;
|
|
|
|
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareUl;
|
|
|
|
dev->dev_data.mf_ul_data.type = type;
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-11-08 21:55:06 +00:00
|
|
|
bool saved = false;
|
|
|
|
MifareUlData* data = &dev->dev_data.mf_ul_data;
|
|
|
|
string_t temp_str;
|
|
|
|
string_init(temp_str);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
// Save Mifare Ultralight specific data
|
2021-07-22 06:05:07 +00:00
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_comment_cstr(file, "Mifare Ultralight specific data")) break;
|
|
|
|
if(!flipper_format_write_hex(file, "Signature", data->signature, sizeof(data->signature)))
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(
|
2021-11-08 21:55:06 +00:00
|
|
|
file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
2021-11-08 21:55:06 +00:00
|
|
|
// Write conters and tearing flags data
|
|
|
|
bool counters_saved = true;
|
|
|
|
for(uint8_t i = 0; i < 3; i++) {
|
|
|
|
string_printf(temp_str, "Counter %d", i);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_uint32(
|
|
|
|
file, string_get_cstr(temp_str), &data->counter[i], 1)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
counters_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
string_printf(temp_str, "Tearing %d", i);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(file, string_get_cstr(temp_str), &data->tearing[i], 1)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
counters_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!counters_saved) break;
|
|
|
|
// Write pages data
|
|
|
|
uint32_t pages_total = data->data_size / 4;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_uint32(file, "Pages total", &pages_total, 1)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
bool pages_saved = true;
|
|
|
|
for(uint16_t i = 0; i < data->data_size; i += 4) {
|
|
|
|
string_printf(temp_str, "Page %d", i / 4);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(file, string_get_cstr(temp_str), &data->data[i], 4)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
pages_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!pages_saved) break;
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
string_clear(temp_str);
|
|
|
|
return saved;
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
bool nfc_device_load_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-07-22 06:05:07 +00:00
|
|
|
bool parsed = false;
|
2021-11-08 21:55:06 +00:00
|
|
|
MifareUlData* data = &dev->dev_data.mf_ul_data;
|
|
|
|
string_t temp_str;
|
|
|
|
string_init(temp_str);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
|
|
|
do {
|
2021-11-08 21:55:06 +00:00
|
|
|
// Read signature
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(file, "Signature", data->signature, sizeof(data->signature)))
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
2021-11-08 21:55:06 +00:00
|
|
|
// Read Mifare version
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(
|
2021-11-08 21:55:06 +00:00
|
|
|
file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
|
|
|
// Read counters and tearing flags
|
2021-11-08 21:55:06 +00:00
|
|
|
bool counters_parsed = true;
|
2021-07-22 06:05:07 +00:00
|
|
|
for(uint8_t i = 0; i < 3; i++) {
|
2021-11-08 21:55:06 +00:00
|
|
|
string_printf(temp_str, "Counter %d", i);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_uint32(file, string_get_cstr(temp_str), &data->counter[i], 1)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
counters_parsed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
string_printf(temp_str, "Tearing %d", i);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(file, string_get_cstr(temp_str), &data->tearing[i], 1)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
counters_parsed = false;
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!counters_parsed) break;
|
|
|
|
// Read pages
|
|
|
|
uint32_t pages = 0;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_uint32(file, "Pages total", &pages, 1)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
data->data_size = pages * 4;
|
|
|
|
bool pages_parsed = true;
|
|
|
|
for(uint16_t i = 0; i < pages; i++) {
|
|
|
|
string_printf(temp_str, "Page %d", i);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(file, string_get_cstr(temp_str), &data->data[i * 4], 4)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
pages_parsed = false;
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!pages_parsed) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
parsed = true;
|
2021-11-08 21:55:06 +00:00
|
|
|
} while(false);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
string_clear(temp_str);
|
2021-07-22 06:05:07 +00:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
static bool nfc_device_save_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-11-08 21:55:06 +00:00
|
|
|
bool saved = false;
|
2021-07-22 06:05:07 +00:00
|
|
|
NfcEmvData* data = &dev->dev_data.emv_data;
|
2021-11-08 21:55:06 +00:00
|
|
|
uint32_t data_temp = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Write Bank card specific data
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_comment_cstr(file, "Bank card specific data")) break;
|
|
|
|
if(!flipper_format_write_hex(file, "AID", data->aid, data->aid_len)) break;
|
|
|
|
if(!flipper_format_write_string_cstr(file, "Name", data->name)) break;
|
|
|
|
if(!flipper_format_write_hex(file, "Number", data->number, data->number_len)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
if(data->exp_mon) {
|
|
|
|
uint8_t exp_data[2] = {data->exp_mon, data->exp_year};
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(file, "Exp data", exp_data, sizeof(exp_data))) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
}
|
|
|
|
if(data->country_code) {
|
|
|
|
data_temp = data->country_code;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_uint32(file, "Country code", &data_temp, 1)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
}
|
|
|
|
if(data->currency_code) {
|
|
|
|
data_temp = data->currency_code;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_uint32(file, "Currency code", &data_temp, 1)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
}
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return saved;
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-07-22 06:05:07 +00:00
|
|
|
bool parsed = false;
|
2021-11-08 21:55:06 +00:00
|
|
|
NfcEmvData* data = &dev->dev_data.emv_data;
|
2021-07-22 06:05:07 +00:00
|
|
|
memset(data, 0, sizeof(NfcEmvData));
|
2021-11-08 21:55:06 +00:00
|
|
|
uint32_t data_cnt = 0;
|
|
|
|
string_t temp_str;
|
|
|
|
string_init(temp_str);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
|
|
|
do {
|
2021-11-08 21:55:06 +00:00
|
|
|
// Load essential data
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_get_value_count(file, "AID", &data_cnt)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
data->aid_len = data_cnt;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(file, "AID", data->aid, data->aid_len)) break;
|
|
|
|
if(!flipper_format_read_string(file, "Name", temp_str)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
strlcpy(data->name, string_get_cstr(temp_str), sizeof(data->name));
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_get_value_count(file, "Number", &data_cnt)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
data->number_len = data_cnt;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(file, "Number", data->number, data->number_len)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
parsed = true;
|
2021-11-08 21:55:06 +00:00
|
|
|
// Load optional data
|
|
|
|
uint8_t exp_data[2] = {};
|
2022-02-18 19:53:46 +00:00
|
|
|
if(flipper_format_read_hex(file, "Exp data", exp_data, 2)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
data->exp_mon = exp_data[0];
|
|
|
|
data->exp_year = exp_data[1];
|
2021-08-20 19:56:43 +00:00
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(flipper_format_read_uint32(file, "Country code", &data_cnt, 1)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
data->country_code = data_cnt;
|
2021-08-20 19:56:43 +00:00
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(flipper_format_read_uint32(file, "Currency code", &data_cnt, 1)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
data->currency_code = data_cnt;
|
2021-08-04 18:58:11 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
} while(false);
|
2021-07-22 06:05:07 +00:00
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
string_clear(temp_str);
|
2021-07-22 06:05:07 +00:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:44:10 +00:00
|
|
|
void nfc_device_set_name(NfcDevice* dev, const char* name) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
|
|
|
strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
|
|
|
|
}
|
|
|
|
|
2021-08-16 21:45:04 +00:00
|
|
|
static bool nfc_device_save_file(
|
|
|
|
NfcDevice* dev,
|
|
|
|
const char* dev_name,
|
|
|
|
const char* folder,
|
|
|
|
const char* extension) {
|
2021-07-02 13:44:10 +00:00
|
|
|
furi_assert(dev);
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
bool saved = false;
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
|
2021-11-08 21:55:06 +00:00
|
|
|
NfcDeviceCommonData* data = &dev->dev_data.nfc_data;
|
2021-07-22 06:05:07 +00:00
|
|
|
string_t temp_str;
|
|
|
|
string_init(temp_str);
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-07-22 06:05:07 +00:00
|
|
|
do {
|
|
|
|
// Create nfc directory if necessary
|
2022-01-29 09:39:10 +00:00
|
|
|
if(!storage_simply_mkdir(dev->storage, NFC_APP_FOLDER)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
// First remove nfc device file if it was saved
|
2021-11-08 21:55:06 +00:00
|
|
|
string_printf(temp_str, "%s/%s%s", folder, dev_name, extension);
|
2021-07-22 06:05:07 +00:00
|
|
|
// Open file
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_always(file, string_get_cstr(temp_str))) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
// Write header
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
// Write nfc device type
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_comment_cstr(
|
2021-11-08 21:55:06 +00:00
|
|
|
file, "Nfc device type can be UID, Mifare Ultralight, Bank card"))
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
2021-11-08 21:55:06 +00:00
|
|
|
nfc_device_prepare_format_string(dev, temp_str);
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_string(file, "Device type", temp_str)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
// Write UID, ATQA, SAK
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_comment_cstr(file, "UID, ATQA and SAK are common for all formats"))
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
|
|
|
|
if(!flipper_format_write_hex(file, "ATQA", data->atqa, 2)) break;
|
|
|
|
if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
// Save more data if necessary
|
|
|
|
if(dev->format == NfcDeviceSaveFormatMifareUl) {
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!nfc_device_save_mifare_ul_data(file, dev)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!nfc_device_save_bank_card_data(file, dev)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
saved = true;
|
2021-07-22 06:05:07 +00:00
|
|
|
} while(0);
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!saved) {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
|
|
|
|
}
|
2021-07-22 06:05:07 +00:00
|
|
|
string_clear(temp_str);
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(file);
|
2021-11-08 21:55:06 +00:00
|
|
|
return saved;
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 21:45:04 +00:00
|
|
|
bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
|
2022-01-29 09:39:10 +00:00
|
|
|
return nfc_device_save_file(dev, dev_name, NFC_APP_FOLDER, NFC_APP_EXTENSION);
|
2021-08-16 21:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_save_shadow(NfcDevice* dev, const char* dev_name) {
|
|
|
|
dev->shadow_file_exist = true;
|
2022-01-29 09:39:10 +00:00
|
|
|
return nfc_device_save_file(dev, dev_name, NFC_APP_FOLDER, NFC_APP_SHADOW_EXTENSION);
|
2021-08-16 21:45:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
|
2021-07-22 06:05:07 +00:00
|
|
|
bool parsed = false;
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
|
2021-11-08 21:55:06 +00:00
|
|
|
NfcDeviceCommonData* data = &dev->dev_data.nfc_data;
|
|
|
|
uint32_t data_cnt = 0;
|
|
|
|
string_t temp_str;
|
|
|
|
string_init(temp_str);
|
|
|
|
bool depricated_version = false;
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-07-22 06:05:07 +00:00
|
|
|
do {
|
2021-08-16 21:45:04 +00:00
|
|
|
// Check existance of shadow file
|
2022-01-29 09:39:10 +00:00
|
|
|
size_t ext_start = string_search_str(path, NFC_APP_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
string_set_n(temp_str, path, 0, ext_start);
|
2022-01-29 09:39:10 +00:00
|
|
|
string_cat_printf(temp_str, "%s", NFC_APP_SHADOW_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
dev->shadow_file_exist =
|
|
|
|
storage_common_stat(dev->storage, string_get_cstr(temp_str), NULL) == FSE_OK;
|
2021-08-16 21:45:04 +00:00
|
|
|
// Open shadow file if it exists. If not - open original
|
|
|
|
if(dev->shadow_file_exist) {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_existing(file, string_get_cstr(temp_str))) break;
|
2021-08-16 21:45:04 +00:00
|
|
|
} else {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_existing(file, string_get_cstr(path))) break;
|
2021-08-16 21:45:04 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
// Read and verify file header
|
|
|
|
uint32_t version = 0;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_header(file, temp_str, &version)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
if(string_cmp_str(temp_str, nfc_file_header) || (version != nfc_file_version)) {
|
|
|
|
depricated_version = true;
|
2021-07-22 06:05:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
// Read Nfc device type
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_string(file, "Device type", temp_str)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!nfc_device_parse_format_string(dev, temp_str)) break;
|
|
|
|
// Read and parse UID, ATQA and SAK
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
|
2021-11-08 21:55:06 +00:00
|
|
|
data->uid_len = data_cnt;
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
|
|
|
|
if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
|
|
|
|
if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
// Parse other data
|
|
|
|
if(dev->format == NfcDeviceSaveFormatMifareUl) {
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!nfc_device_load_mifare_ul_data(file, dev)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!nfc_device_load_bank_card_data(file, dev)) break;
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
|
|
|
parsed = true;
|
2021-11-08 21:55:06 +00:00
|
|
|
} while(false);
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-10-05 05:14:19 +00:00
|
|
|
if(!parsed) {
|
2021-11-08 21:55:06 +00:00
|
|
|
if(depricated_version) {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "File format depricated");
|
|
|
|
} else {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
|
|
|
|
}
|
2021-10-05 05:14:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
string_clear(temp_str);
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(file);
|
2021-07-22 06:05:07 +00:00
|
|
|
return parsed;
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 21:39:21 +00:00
|
|
|
bool nfc_device_load(NfcDevice* dev, const char* file_path) {
|
2021-07-02 13:44:10 +00:00
|
|
|
furi_assert(dev);
|
2021-07-15 21:39:21 +00:00
|
|
|
furi_assert(file_path);
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-07-15 21:39:21 +00:00
|
|
|
// Load device data
|
|
|
|
string_t path;
|
|
|
|
string_init_set_str(path, file_path);
|
2021-11-08 21:55:06 +00:00
|
|
|
bool dev_load = nfc_device_load_data(dev, path);
|
2021-07-15 21:39:21 +00:00
|
|
|
if(dev_load) {
|
|
|
|
// Set device name
|
|
|
|
path_extract_filename_no_ext(file_path, path);
|
|
|
|
nfc_device_set_name(dev, string_get_cstr(path));
|
|
|
|
}
|
|
|
|
string_clear(path);
|
|
|
|
|
|
|
|
return dev_load;
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_file_select(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
|
|
|
// Input events and views are managed by file_select
|
2021-11-08 21:55:06 +00:00
|
|
|
bool res = dialog_file_select_show(
|
|
|
|
dev->dialogs,
|
2022-01-29 09:39:10 +00:00
|
|
|
NFC_APP_FOLDER,
|
|
|
|
NFC_APP_EXTENSION,
|
2021-07-02 13:44:10 +00:00
|
|
|
dev->file_name,
|
|
|
|
sizeof(dev->file_name),
|
2021-10-05 05:14:19 +00:00
|
|
|
dev->dev_name);
|
2021-07-02 13:44:10 +00:00
|
|
|
if(res) {
|
|
|
|
string_t dev_str;
|
|
|
|
// Get key file path
|
2022-01-29 09:39:10 +00:00
|
|
|
string_init_printf(dev_str, "%s/%s%s", NFC_APP_FOLDER, dev->file_name, NFC_APP_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
res = nfc_device_load_data(dev, dev_str);
|
2021-07-02 13:44:10 +00:00
|
|
|
if(res) {
|
|
|
|
nfc_device_set_name(dev, dev->file_name);
|
|
|
|
}
|
|
|
|
string_clear(dev_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2021-07-22 06:05:07 +00:00
|
|
|
|
|
|
|
void nfc_device_clear(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
|
|
|
memset(&dev->dev_data, 0, sizeof(dev->dev_data));
|
|
|
|
dev->format = NfcDeviceSaveFormatUid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_delete(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
bool deleted = false;
|
2021-07-22 06:05:07 +00:00
|
|
|
string_t file_path;
|
2021-11-08 21:55:06 +00:00
|
|
|
string_init(file_path);
|
2021-08-16 21:45:04 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Delete original file
|
2022-01-29 09:39:10 +00:00
|
|
|
string_init_printf(file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!storage_simply_remove(dev->storage, string_get_cstr(file_path))) break;
|
2021-08-16 21:45:04 +00:00
|
|
|
// Delete shadow file if it exists
|
|
|
|
if(dev->shadow_file_exist) {
|
|
|
|
string_printf(
|
2022-01-29 09:39:10 +00:00
|
|
|
file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!storage_simply_remove(dev->storage, string_get_cstr(file_path))) break;
|
2021-08-16 21:45:04 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00:00
|
|
|
deleted = true;
|
2021-08-16 21:45:04 +00:00
|
|
|
} while(0);
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!deleted) {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:05:07 +00:00
|
|
|
string_clear(file_path);
|
2021-11-08 21:55:06 +00:00
|
|
|
return deleted;
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
2021-08-16 21:45:04 +00:00
|
|
|
|
|
|
|
bool nfc_device_restore(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
|
|
|
furi_assert(dev->shadow_file_exist);
|
|
|
|
|
2021-11-08 21:55:06 +00:00
|
|
|
bool restored = false;
|
2021-08-16 21:45:04 +00:00
|
|
|
string_t path;
|
|
|
|
|
|
|
|
do {
|
|
|
|
string_init_printf(
|
2022-01-29 09:39:10 +00:00
|
|
|
path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!storage_simply_remove(dev->storage, string_get_cstr(path))) break;
|
2021-08-16 21:45:04 +00:00
|
|
|
dev->shadow_file_exist = false;
|
2022-01-29 09:39:10 +00:00
|
|
|
string_printf(path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!nfc_device_load_data(dev, path)) break;
|
|
|
|
restored = true;
|
2021-08-16 21:45:04 +00:00
|
|
|
} while(0);
|
|
|
|
|
|
|
|
string_clear(path);
|
2021-11-08 21:55:06 +00:00
|
|
|
return restored;
|
2021-08-16 21:45:04 +00:00
|
|
|
}
|