2021-11-09 00:55:06 +03:00
|
|
|
#include "nfc_device.h"
|
2022-05-27 14:19:21 +03:00
|
|
|
#include "assets_icons.h"
|
2021-12-21 15:33:17 +03:00
|
|
|
#include "nfc_types.h"
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2022-07-26 18:30:49 +03:00
|
|
|
#include <lib/toolbox/path.h>
|
|
|
|
#include <lib/toolbox/hex.h>
|
|
|
|
#include <lib/nfc/protocols/nfc_util.h>
|
2022-02-18 22:53:46 +03:00
|
|
|
#include <flipper_format/flipper_format.h>
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2022-11-28 20:14:13 +03:00
|
|
|
#define TAG "NfcDevice"
|
2022-12-07 13:52:44 +03:00
|
|
|
#define NFC_DEVICE_KEYS_FOLDER EXT_PATH("nfc/.cache")
|
2022-07-26 18:30:49 +03:00
|
|
|
#define NFC_DEVICE_KEYS_EXTENSION ".keys"
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
static const char* nfc_file_header = "Flipper NFC device";
|
2022-12-07 13:52:44 +03:00
|
|
|
static const uint32_t nfc_file_version = 3;
|
2021-11-09 00:55:06 +03:00
|
|
|
|
2022-07-26 18:30:49 +03:00
|
|
|
static const char* nfc_keys_file_header = "Flipper NFC keys";
|
|
|
|
static const uint32_t nfc_keys_file_version = 1;
|
|
|
|
|
2022-05-24 17:00:15 +03:00
|
|
|
// Protocols format versions
|
2022-07-26 18:30:49 +03:00
|
|
|
static const uint32_t nfc_mifare_classic_data_format_version = 2;
|
2022-08-07 18:09:00 +03:00
|
|
|
static const uint32_t nfc_mifare_ultralight_data_format_version = 1;
|
2022-05-24 17:00:15 +03:00
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
NfcDevice* nfc_device_alloc() {
|
2022-02-18 22:53:46 +03:00
|
|
|
NfcDevice* nfc_dev = malloc(sizeof(NfcDevice));
|
2022-07-26 15:21:51 +03:00
|
|
|
nfc_dev->storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
nfc_dev->dialogs = furi_record_open(RECORD_DIALOGS);
|
2022-10-05 18:15:23 +03:00
|
|
|
nfc_dev->load_path = furi_string_alloc();
|
|
|
|
nfc_dev->dev_data.parsed_data = furi_string_alloc();
|
2022-12-19 22:57:48 +03:00
|
|
|
nfc_dev->folder = furi_string_alloc();
|
2022-12-07 13:52:44 +03:00
|
|
|
|
|
|
|
// Rename cache folder name for backward compatibility
|
|
|
|
if(storage_common_stat(nfc_dev->storage, "/ext/nfc/cache", NULL) == FSE_OK) {
|
|
|
|
storage_common_rename(nfc_dev->storage, "/ext/nfc/cache", NFC_DEVICE_KEYS_FOLDER);
|
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
return nfc_dev;
|
|
|
|
}
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
void nfc_device_free(NfcDevice* nfc_dev) {
|
|
|
|
furi_assert(nfc_dev);
|
2022-03-23 16:45:37 +03:00
|
|
|
nfc_device_clear(nfc_dev);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
furi_record_close(RECORD_DIALOGS);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(nfc_dev->load_path);
|
|
|
|
furi_string_free(nfc_dev->dev_data.parsed_data);
|
2022-12-19 22:57:48 +03:00
|
|
|
furi_string_free(nfc_dev->folder);
|
2021-11-09 00:55:06 +03:00
|
|
|
free(nfc_dev);
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static void nfc_device_prepare_format_string(NfcDevice* dev, FuriString* format_string) {
|
2021-07-22 09:05:07 +03:00
|
|
|
if(dev->format == NfcDeviceSaveFormatUid) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(format_string, "UID");
|
2021-07-22 09:05:07 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(format_string, "Bank card");
|
2021-07-22 09:05:07 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareUl) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
|
2022-03-24 01:14:34 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(format_string, "Mifare Classic");
|
2022-03-23 16:45:37 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(format_string, "Mifare DESFire");
|
2023-06-08 08:30:53 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatNfcV) {
|
|
|
|
furi_string_set(format_string, "ISO15693");
|
2021-07-22 09:05:07 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(format_string, "Unknown");
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static bool nfc_device_parse_format_string(NfcDevice* dev, FuriString* format_string) {
|
|
|
|
if(furi_string_start_with_str(format_string, "UID")) {
|
2021-07-22 09:05:07 +03:00
|
|
|
dev->format = NfcDeviceSaveFormatUid;
|
2022-04-19 18:23:58 +03:00
|
|
|
dev->dev_data.protocol = NfcDeviceProtocolUnknown;
|
2021-07-22 09:05:07 +03:00
|
|
|
return true;
|
2021-12-21 15:33:17 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_start_with_str(format_string, "Bank card")) {
|
2021-07-22 09:05:07 +03:00
|
|
|
dev->format = NfcDeviceSaveFormatBankCard;
|
2022-04-19 18:23:58 +03:00
|
|
|
dev->dev_data.protocol = NfcDeviceProtocolEMV;
|
2021-07-22 09:05:07 +03:00
|
|
|
return true;
|
2021-12-21 15:33:17 +03:00
|
|
|
}
|
|
|
|
// Check Mifare Ultralight types
|
|
|
|
for(MfUltralightType type = MfUltralightTypeUnknown; type < MfUltralightTypeNum; type++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_equal(format_string, nfc_mf_ul_type(type, true))) {
|
2021-12-21 15:33:17 +03:00
|
|
|
dev->format = NfcDeviceSaveFormatMifareUl;
|
2022-04-19 18:23:58 +03:00
|
|
|
dev->dev_data.protocol = NfcDeviceProtocolMifareUl;
|
2021-12-21 15:33:17 +03:00
|
|
|
dev->dev_data.mf_ul_data.type = type;
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_start_with_str(format_string, "Mifare Classic")) {
|
2022-03-24 01:14:34 +03:00
|
|
|
dev->format = NfcDeviceSaveFormatMifareClassic;
|
2022-04-19 18:23:58 +03:00
|
|
|
dev->dev_data.protocol = NfcDeviceProtocolMifareClassic;
|
2022-03-24 01:14:34 +03:00
|
|
|
return true;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_start_with_str(format_string, "Mifare DESFire")) {
|
2022-03-23 16:45:37 +03:00
|
|
|
dev->format = NfcDeviceSaveFormatMifareDesfire;
|
2022-04-19 18:23:58 +03:00
|
|
|
dev->dev_data.protocol = NfcDeviceProtocolMifareDesfire;
|
2022-03-23 16:45:37 +03:00
|
|
|
return true;
|
|
|
|
}
|
2023-06-08 08:30:53 +03:00
|
|
|
if(furi_string_start_with_str(format_string, "ISO15693")) {
|
|
|
|
dev->format = NfcDeviceSaveFormatNfcV;
|
|
|
|
dev->dev_data.protocol = NfcDeviceProtocolNfcV;
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-22 09:05:07 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-11-09 00:55:06 +03:00
|
|
|
bool saved = false;
|
2022-04-19 18:23:58 +03:00
|
|
|
MfUltralightData* data = &dev->dev_data.mf_ul_data;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2021-07-22 09:05:07 +03:00
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
// Save Mifare Ultralight specific data
|
2021-07-22 09:05:07 +03:00
|
|
|
do {
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_comment_cstr(file, "Mifare Ultralight specific data")) break;
|
2022-08-07 18:09:00 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
|
|
|
file, "Data format version", &nfc_mifare_ultralight_data_format_version, 1))
|
|
|
|
break;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_hex(file, "Signature", data->signature, sizeof(data->signature)))
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_hex(
|
2021-11-09 00:55:06 +03:00
|
|
|
file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
2021-11-09 00:55:06 +03:00
|
|
|
// Write conters and tearing flags data
|
|
|
|
bool counters_saved = true;
|
|
|
|
for(uint8_t i = 0; i < 3; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Counter %d", i);
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(temp_str), &data->counter[i], 1)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
counters_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Tearing %d", i);
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), &data->tearing[i], 1)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
counters_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!counters_saved) break;
|
|
|
|
// Write pages data
|
|
|
|
uint32_t pages_total = data->data_size / 4;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_uint32(file, "Pages total", &pages_total, 1)) break;
|
2022-08-07 18:09:00 +03:00
|
|
|
uint32_t pages_read = data->data_read / 4;
|
|
|
|
if(!flipper_format_write_uint32(file, "Pages read", &pages_read, 1)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
bool pages_saved = true;
|
|
|
|
for(uint16_t i = 0; i < data->data_size; i += 4) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Page %d", i / 4);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(temp_str), &data->data[i], 4)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
pages_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!pages_saved) break;
|
2022-06-21 18:04:35 +03:00
|
|
|
|
|
|
|
// Write authentication counter
|
|
|
|
uint32_t auth_counter = data->curr_authlim;
|
|
|
|
if(!flipper_format_write_uint32(file, "Failed authentication attempts", &auth_counter, 1))
|
|
|
|
break;
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
saved = true;
|
|
|
|
} while(false);
|
2021-07-22 09:05:07 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2021-11-09 00:55:06 +03:00
|
|
|
return saved;
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
bool nfc_device_load_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-07-22 09:05:07 +03:00
|
|
|
bool parsed = false;
|
2022-04-19 18:23:58 +03:00
|
|
|
MfUltralightData* data = &dev->dev_data.mf_ul_data;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-08-07 18:09:00 +03:00
|
|
|
uint32_t data_format_version = 0;
|
2021-07-22 09:05:07 +03:00
|
|
|
|
|
|
|
do {
|
2022-08-07 18:09:00 +03:00
|
|
|
// Read Mifare Ultralight format version
|
|
|
|
if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
|
|
|
|
if(!flipper_format_rewind(file)) break;
|
|
|
|
}
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
// Read signature
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(file, "Signature", data->signature, sizeof(data->signature)))
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
2021-11-09 00:55:06 +03:00
|
|
|
// Read Mifare version
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(
|
2021-11-09 00:55:06 +03:00
|
|
|
file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
|
|
|
// Read counters and tearing flags
|
2021-11-09 00:55:06 +03:00
|
|
|
bool counters_parsed = true;
|
2021-07-22 09:05:07 +03:00
|
|
|
for(uint8_t i = 0; i < 3; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Counter %d", i);
|
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
file, furi_string_get_cstr(temp_str), &data->counter[i], 1)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
counters_parsed = false;
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Tearing %d", i);
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), &data->tearing[i], 1)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
counters_parsed = false;
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!counters_parsed) break;
|
|
|
|
// Read pages
|
2022-08-07 18:09:00 +03:00
|
|
|
uint32_t pages_total = 0;
|
|
|
|
if(!flipper_format_read_uint32(file, "Pages total", &pages_total, 1)) break;
|
|
|
|
uint32_t pages_read = 0;
|
|
|
|
if(data_format_version < nfc_mifare_ultralight_data_format_version) {
|
|
|
|
pages_read = pages_total;
|
|
|
|
} else {
|
|
|
|
if(!flipper_format_read_uint32(file, "Pages read", &pages_read, 1)) break;
|
|
|
|
}
|
|
|
|
data->data_size = pages_total * 4;
|
|
|
|
data->data_read = pages_read * 4;
|
2022-09-05 14:42:59 +03:00
|
|
|
if(data->data_size > MF_UL_MAX_DUMP_SIZE || data->data_read > MF_UL_MAX_DUMP_SIZE) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
bool pages_parsed = true;
|
2022-08-07 18:09:00 +03:00
|
|
|
for(uint16_t i = 0; i < pages_total; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Page %d", i);
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), &data->data[i * 4], 4)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
pages_parsed = false;
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!pages_parsed) break;
|
2022-06-21 18:04:35 +03:00
|
|
|
|
|
|
|
// Read authentication counter
|
|
|
|
uint32_t auth_counter;
|
|
|
|
if(!flipper_format_read_uint32(file, "Failed authentication attempts", &auth_counter, 1))
|
|
|
|
auth_counter = 0;
|
2022-11-28 21:16:22 +03:00
|
|
|
data->curr_authlim = auth_counter;
|
|
|
|
|
|
|
|
data->auth_success = mf_ul_is_full_capture(data);
|
2022-06-21 18:04:35 +03:00
|
|
|
|
2021-07-22 09:05:07 +03:00
|
|
|
parsed = true;
|
2021-11-09 00:55:06 +03:00
|
|
|
} while(false);
|
2021-07-22 09:05:07 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2021-07-22 09:05:07 +03:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:45:37 +03:00
|
|
|
static bool nfc_device_save_mifare_df_key_settings(
|
|
|
|
FlipperFormat* file,
|
|
|
|
MifareDesfireKeySettings* ks,
|
|
|
|
const char* prefix) {
|
|
|
|
bool saved = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* key;
|
|
|
|
key = furi_string_alloc();
|
2022-03-23 16:45:37 +03:00
|
|
|
|
|
|
|
do {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Change Key ID", prefix);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &ks->change_key_id, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Config Changeable", prefix);
|
|
|
|
if(!flipper_format_write_bool(file, furi_string_get_cstr(key), &ks->config_changeable, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Free Create Delete", prefix);
|
|
|
|
if(!flipper_format_write_bool(file, furi_string_get_cstr(key), &ks->free_create_delete, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Free Directory List", prefix);
|
|
|
|
if(!flipper_format_write_bool(file, furi_string_get_cstr(key), &ks->free_directory_list, 1))
|
|
|
|
break;
|
|
|
|
furi_string_printf(key, "%s Key Changeable", prefix);
|
|
|
|
if(!flipper_format_write_bool(
|
|
|
|
file, furi_string_get_cstr(key), &ks->master_key_changeable, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-05-24 17:05:46 +03:00
|
|
|
if(ks->flags) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Flags", prefix);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &ks->flags, 1)) break;
|
2022-05-24 17:05:46 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Max Keys", prefix);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &ks->max_keys, 1)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
for(MifareDesfireKeyVersion* kv = ks->key_version_head; kv; kv = kv->next) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Key %d Version", prefix, kv->id);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &kv->version, 1)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(key);
|
2022-03-23 16:45:37 +03:00
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_load_mifare_df_key_settings(
|
|
|
|
FlipperFormat* file,
|
|
|
|
MifareDesfireKeySettings* ks,
|
|
|
|
const char* prefix) {
|
|
|
|
bool parsed = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* key;
|
|
|
|
key = furi_string_alloc();
|
2022-03-23 16:45:37 +03:00
|
|
|
|
|
|
|
do {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Change Key ID", prefix);
|
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &ks->change_key_id, 1)) break;
|
|
|
|
furi_string_printf(key, "%s Config Changeable", prefix);
|
|
|
|
if(!flipper_format_read_bool(file, furi_string_get_cstr(key), &ks->config_changeable, 1))
|
|
|
|
break;
|
|
|
|
furi_string_printf(key, "%s Free Create Delete", prefix);
|
|
|
|
if(!flipper_format_read_bool(file, furi_string_get_cstr(key), &ks->free_create_delete, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Free Directory List", prefix);
|
|
|
|
if(!flipper_format_read_bool(file, furi_string_get_cstr(key), &ks->free_directory_list, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Key Changeable", prefix);
|
|
|
|
if(!flipper_format_read_bool(
|
|
|
|
file, furi_string_get_cstr(key), &ks->master_key_changeable, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Flags", prefix);
|
|
|
|
if(flipper_format_key_exist(file, furi_string_get_cstr(key))) {
|
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &ks->flags, 1)) break;
|
2022-05-24 17:05:46 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Max Keys", prefix);
|
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &ks->max_keys, 1)) break;
|
2022-05-24 17:05:46 +03:00
|
|
|
ks->flags |= ks->max_keys >> 4;
|
|
|
|
ks->max_keys &= 0xF;
|
2022-03-23 16:45:37 +03:00
|
|
|
MifareDesfireKeyVersion** kv_head = &ks->key_version_head;
|
|
|
|
for(int key_id = 0; key_id < ks->max_keys; key_id++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s Key %d Version", prefix, key_id);
|
2022-03-23 16:45:37 +03:00
|
|
|
uint8_t version;
|
2022-10-05 18:15:23 +03:00
|
|
|
if(flipper_format_read_hex(file, furi_string_get_cstr(key), &version, 1)) {
|
2022-03-23 16:45:37 +03:00
|
|
|
MifareDesfireKeyVersion* kv = malloc(sizeof(MifareDesfireKeyVersion));
|
|
|
|
memset(kv, 0, sizeof(MifareDesfireKeyVersion));
|
|
|
|
kv->id = key_id;
|
|
|
|
kv->version = version;
|
|
|
|
*kv_head = kv;
|
|
|
|
kv_head = &kv->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed = true;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(key);
|
2022-03-23 16:45:37 +03:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfc_device_save_mifare_df_app(FlipperFormat* file, MifareDesfireApplication* app) {
|
|
|
|
bool saved = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString *prefix, *key;
|
|
|
|
prefix =
|
|
|
|
furi_string_alloc_printf("Application %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
|
|
|
|
key = furi_string_alloc();
|
2022-03-23 16:45:37 +03:00
|
|
|
uint8_t* tmp = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(app->key_settings) {
|
|
|
|
if(!nfc_device_save_mifare_df_key_settings(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, app->key_settings, furi_string_get_cstr(prefix)))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-04-25 17:59:48 +03:00
|
|
|
if(!app->file_head) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
uint32_t n_files = 0;
|
|
|
|
for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
|
|
|
|
n_files++;
|
|
|
|
}
|
|
|
|
tmp = malloc(n_files);
|
|
|
|
int i = 0;
|
|
|
|
for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
|
|
|
|
tmp[i++] = f->id;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File IDs", furi_string_get_cstr(prefix));
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), tmp, n_files)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
bool saved_files = true;
|
|
|
|
for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
|
|
|
|
saved_files = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Type", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &f->type, 1)) break;
|
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Communication Settings", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &f->comm, 1)) break;
|
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Access Rights", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_hex(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), (uint8_t*)&f->access_rights, 2))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
uint16_t size = 0;
|
|
|
|
if(f->type == MifareDesfireFileTypeStandard ||
|
|
|
|
f->type == MifareDesfireFileTypeBackup) {
|
|
|
|
size = f->settings.data.size;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.data.size, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
} else if(f->type == MifareDesfireFileTypeValue) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Hi Limit", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.value.hi_limit, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Lo Limit", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.value.lo_limit, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Limited Credit Value", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.value.limited_credit_value, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Limited Credit Enabled", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_bool(
|
2022-10-05 18:15:23 +03:00
|
|
|
file,
|
|
|
|
furi_string_get_cstr(key),
|
|
|
|
&f->settings.value.limited_credit_enabled,
|
|
|
|
1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
size = 4;
|
|
|
|
} else if(
|
|
|
|
f->type == MifareDesfireFileTypeLinearRecord ||
|
|
|
|
f->type == MifareDesfireFileTypeCyclicRecord) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.record.size, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Max", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.record.max, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Cur", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.record.cur, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
size = f->settings.record.size * f->settings.record.cur;
|
|
|
|
}
|
|
|
|
if(f->contents) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(!flipper_format_write_hex(file, furi_string_get_cstr(key), f->contents, size))
|
|
|
|
break;
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
saved_files = true;
|
|
|
|
}
|
|
|
|
if(!saved_files) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
free(tmp);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(prefix);
|
|
|
|
furi_string_free(key);
|
2022-03-23 16:45:37 +03:00
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_load_mifare_df_app(FlipperFormat* file, MifareDesfireApplication* app) {
|
|
|
|
bool parsed = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString *prefix, *key;
|
|
|
|
prefix =
|
|
|
|
furi_string_alloc_printf("Application %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
|
|
|
|
key = furi_string_alloc();
|
2022-03-23 16:45:37 +03:00
|
|
|
uint8_t* tmp = NULL;
|
|
|
|
MifareDesfireFile* f = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
app->key_settings = malloc(sizeof(MifareDesfireKeySettings));
|
|
|
|
memset(app->key_settings, 0, sizeof(MifareDesfireKeySettings));
|
|
|
|
if(!nfc_device_load_mifare_df_key_settings(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, app->key_settings, furi_string_get_cstr(prefix))) {
|
2022-03-23 16:45:37 +03:00
|
|
|
free(app->key_settings);
|
|
|
|
app->key_settings = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File IDs", furi_string_get_cstr(prefix));
|
2022-03-23 16:45:37 +03:00
|
|
|
uint32_t n_files;
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_get_value_count(file, furi_string_get_cstr(key), &n_files)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
tmp = malloc(n_files);
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), tmp, n_files)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
MifareDesfireFile** file_head = &app->file_head;
|
|
|
|
bool parsed_files = true;
|
2022-05-06 16:37:10 +03:00
|
|
|
for(uint32_t i = 0; i < n_files; i++) {
|
2022-03-23 16:45:37 +03:00
|
|
|
parsed_files = false;
|
|
|
|
f = malloc(sizeof(MifareDesfireFile));
|
|
|
|
memset(f, 0, sizeof(MifareDesfireFile));
|
|
|
|
f->id = tmp[i];
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Type", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &f->type, 1)) break;
|
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Communication Settings", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &f->comm, 1)) break;
|
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Access Rights", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, furi_string_get_cstr(key), (uint8_t*)&f->access_rights, 2))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
if(f->type == MifareDesfireFileTypeStandard ||
|
|
|
|
f->type == MifareDesfireFileTypeBackup) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.data.size, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
} else if(f->type == MifareDesfireFileTypeValue) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Hi Limit", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.value.hi_limit, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Lo Limit", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.value.lo_limit, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Limited Credit Value", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.value.limited_credit_value, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
|
|
|
key, "%s File %d Limited Credit Enabled", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_bool(
|
2022-10-05 18:15:23 +03:00
|
|
|
file,
|
|
|
|
furi_string_get_cstr(key),
|
|
|
|
&f->settings.value.limited_credit_enabled,
|
|
|
|
1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
} else if(
|
|
|
|
f->type == MifareDesfireFileTypeLinearRecord ||
|
|
|
|
f->type == MifareDesfireFileTypeCyclicRecord) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.record.size, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Max", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.record.max, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d Cur", furi_string_get_cstr(prefix), f->id);
|
2022-03-23 16:45:37 +03:00
|
|
|
if(!flipper_format_read_uint32(
|
2022-10-05 18:15:23 +03:00
|
|
|
file, furi_string_get_cstr(key), &f->settings.record.cur, 1))
|
2022-03-23 16:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(key, "%s File %d", furi_string_get_cstr(prefix), f->id);
|
|
|
|
if(flipper_format_key_exist(file, furi_string_get_cstr(key))) {
|
2022-03-23 16:45:37 +03:00
|
|
|
uint32_t size;
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_get_value_count(file, furi_string_get_cstr(key), &size)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
f->contents = malloc(size);
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_read_hex(file, furi_string_get_cstr(key), f->contents, size))
|
|
|
|
break;
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
*file_head = f;
|
|
|
|
file_head = &f->next;
|
|
|
|
f = NULL;
|
|
|
|
parsed_files = true;
|
|
|
|
}
|
|
|
|
if(!parsed_files) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parsed = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
if(f) {
|
|
|
|
free(f->contents);
|
|
|
|
free(f);
|
|
|
|
}
|
|
|
|
free(tmp);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(prefix);
|
|
|
|
furi_string_free(key);
|
2022-03-23 16:45:37 +03:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfc_device_save_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool saved = false;
|
|
|
|
MifareDesfireData* data = &dev->dev_data.mf_df_data;
|
|
|
|
uint8_t* tmp = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Mifare DESFire specific data")) break;
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "PICC Version", (uint8_t*)&data->version, sizeof(data->version)))
|
|
|
|
break;
|
|
|
|
if(data->free_memory) {
|
|
|
|
if(!flipper_format_write_uint32(file, "PICC Free Memory", &data->free_memory->bytes, 1))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(data->master_key_settings) {
|
|
|
|
if(!nfc_device_save_mifare_df_key_settings(file, data->master_key_settings, "PICC"))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint32_t n_apps = 0;
|
|
|
|
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
|
|
|
|
n_apps++;
|
|
|
|
}
|
|
|
|
if(!flipper_format_write_uint32(file, "Application Count", &n_apps, 1)) break;
|
2022-06-21 17:58:22 +03:00
|
|
|
if(n_apps) {
|
|
|
|
tmp = malloc(n_apps * 3);
|
|
|
|
int i = 0;
|
|
|
|
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
|
2022-12-26 15:13:30 +03:00
|
|
|
memcpy(tmp + i, app->id, 3); //-V769
|
2022-06-21 17:58:22 +03:00
|
|
|
i += 3;
|
|
|
|
}
|
|
|
|
if(!flipper_format_write_hex(file, "Application IDs", tmp, n_apps * 3)) break;
|
|
|
|
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
|
|
|
|
if(!nfc_device_save_mifare_df_app(file, app)) break;
|
|
|
|
}
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
free(tmp);
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool parsed = false;
|
|
|
|
MifareDesfireData* data = &dev->dev_data.mf_df_data;
|
|
|
|
memset(data, 0, sizeof(MifareDesfireData));
|
|
|
|
uint8_t* tmp = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "PICC Version", (uint8_t*)&data->version, sizeof(data->version)))
|
|
|
|
break;
|
|
|
|
if(flipper_format_key_exist(file, "PICC Free Memory")) {
|
|
|
|
data->free_memory = malloc(sizeof(MifareDesfireFreeMemory));
|
|
|
|
memset(data->free_memory, 0, sizeof(MifareDesfireFreeMemory));
|
|
|
|
if(!flipper_format_read_uint32(
|
|
|
|
file, "PICC Free Memory", &data->free_memory->bytes, 1)) {
|
|
|
|
free(data->free_memory);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 17:58:22 +03:00
|
|
|
if(flipper_format_key_exist(file, "PICC Change Key ID")) {
|
|
|
|
data->master_key_settings = malloc(sizeof(MifareDesfireKeySettings));
|
|
|
|
memset(data->master_key_settings, 0, sizeof(MifareDesfireKeySettings));
|
|
|
|
if(!nfc_device_load_mifare_df_key_settings(file, data->master_key_settings, "PICC")) {
|
|
|
|
free(data->master_key_settings);
|
|
|
|
data->master_key_settings = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
uint32_t n_apps;
|
|
|
|
if(!flipper_format_read_uint32(file, "Application Count", &n_apps, 1)) break;
|
2022-06-21 17:58:22 +03:00
|
|
|
if(n_apps) {
|
|
|
|
tmp = malloc(n_apps * 3);
|
|
|
|
if(!flipper_format_read_hex(file, "Application IDs", tmp, n_apps * 3)) break;
|
|
|
|
bool parsed_apps = true;
|
|
|
|
MifareDesfireApplication** app_head = &data->app_head;
|
|
|
|
for(uint32_t i = 0; i < n_apps; i++) {
|
|
|
|
MifareDesfireApplication* app = malloc(sizeof(MifareDesfireApplication));
|
|
|
|
memset(app, 0, sizeof(MifareDesfireApplication));
|
|
|
|
memcpy(app->id, &tmp[i * 3], 3);
|
|
|
|
if(!nfc_device_load_mifare_df_app(file, app)) {
|
|
|
|
free(app);
|
|
|
|
parsed_apps = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*app_head = app;
|
|
|
|
app_head = &app->next;
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
2022-11-28 20:14:13 +03:00
|
|
|
if(!parsed_apps) {
|
|
|
|
// accept non-parsed apps, just log a warning:
|
|
|
|
FURI_LOG_W(TAG, "Non-parsed apps found!");
|
|
|
|
}
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
parsed = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
free(tmp);
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2023-06-28 20:44:34 +03:00
|
|
|
static bool nfc_device_save_slix_data(
|
|
|
|
FlipperFormat* file,
|
|
|
|
NfcDevice* dev,
|
|
|
|
SlixTypeFeatures features,
|
|
|
|
const char* type) {
|
2023-06-08 08:30:53 +03:00
|
|
|
bool saved = false;
|
|
|
|
NfcVSlixData* data = &dev->dev_data.nfcv_data.sub_data.slix;
|
|
|
|
|
|
|
|
do {
|
2023-06-28 20:44:34 +03:00
|
|
|
char msg[64];
|
|
|
|
snprintf(msg, sizeof(msg), "%s specific data", type);
|
|
|
|
if(!flipper_format_write_comment_cstr(file, msg)) break;
|
|
|
|
if(!flipper_format_write_comment_cstr(
|
|
|
|
file, "Passwords are optional. If password is omitted, any password is accepted"))
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
|
2023-06-28 20:44:34 +03:00
|
|
|
if(features & SlixFeatureRead) {
|
|
|
|
if(data->flags & NfcVSlixDataFlagsHasKeyRead) {
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Password Read", data->key_read, sizeof(data->key_read)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureWrite) {
|
|
|
|
if(data->flags & NfcVSlixDataFlagsHasKeyWrite) {
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Password Write", data->key_write, sizeof(data->key_write)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeaturePrivacy) {
|
|
|
|
if(data->flags & NfcVSlixDataFlagsHasKeyPrivacy) {
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureDestroy) {
|
|
|
|
if(data->flags & NfcVSlixDataFlagsHasKeyDestroy) {
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureEas) {
|
|
|
|
if(data->flags & NfcVSlixDataFlagsHasKeyEas) {
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Password EAS", data->key_eas, sizeof(data->key_eas)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureSignature) {
|
|
|
|
if(!flipper_format_write_comment_cstr(
|
|
|
|
file,
|
|
|
|
"This is the card's secp128r1 elliptic curve signature. It can not be calculated without knowing NXP's private key."))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Signature", data->signature, sizeof(data->signature)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(features & SlixFeaturePrivacy) {
|
|
|
|
bool privacy = (data->flags & NfcVSlixDataFlagsPrivacy) ? true : false;
|
|
|
|
if(!flipper_format_write_bool(file, "Privacy Mode", &privacy, 1)) break;
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureProtection) {
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Protection pointer configuration")) break;
|
|
|
|
if(!flipper_format_write_hex(file, "Protection pointer", &data->pp_pointer, 1)) break;
|
|
|
|
if(!flipper_format_write_hex(file, "Protection condition", &data->pp_condition, 1))
|
|
|
|
break;
|
|
|
|
}
|
2023-06-08 08:30:53 +03:00
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
2023-06-28 20:44:34 +03:00
|
|
|
bool nfc_device_load_slix_data(FlipperFormat* file, NfcDevice* dev, SlixTypeFeatures features) {
|
2023-06-08 08:30:53 +03:00
|
|
|
bool parsed = false;
|
|
|
|
NfcVSlixData* data = &dev->dev_data.nfcv_data.sub_data.slix;
|
|
|
|
memset(data, 0, sizeof(NfcVSlixData));
|
|
|
|
|
|
|
|
do {
|
2023-06-28 20:44:34 +03:00
|
|
|
data->flags = 0;
|
2023-06-08 08:30:53 +03:00
|
|
|
|
2023-06-28 20:44:34 +03:00
|
|
|
if(features & SlixFeatureRead) {
|
|
|
|
if(flipper_format_key_exist(file, "Password Read")) {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "Password Read", data->key_read, sizeof(data->key_read))) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Password Read");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data->flags |= NfcVSlixDataFlagsHasKeyRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureWrite) {
|
|
|
|
if(flipper_format_key_exist(file, "Password Write")) {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "Password Write", data->key_write, sizeof(data->key_write))) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Password Write");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data->flags |= NfcVSlixDataFlagsHasKeyWrite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeaturePrivacy) {
|
|
|
|
if(flipper_format_key_exist(file, "Password Privacy")) {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "Password Privacy", data->key_privacy, sizeof(data->key_privacy))) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Password Privacy");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data->flags |= NfcVSlixDataFlagsHasKeyPrivacy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureDestroy) {
|
|
|
|
if(flipper_format_key_exist(file, "Password Destroy")) {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "Password Destroy", data->key_destroy, sizeof(data->key_destroy))) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Password Destroy");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data->flags |= NfcVSlixDataFlagsHasKeyDestroy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureEas) {
|
|
|
|
if(flipper_format_key_exist(file, "Password EAS")) {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "Password EAS", data->key_eas, sizeof(data->key_eas))) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Password EAS");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data->flags |= NfcVSlixDataFlagsHasKeyEas;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureSignature) {
|
|
|
|
if(!flipper_format_read_hex(
|
|
|
|
file, "Signature", data->signature, sizeof(data->signature))) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Signature");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeaturePrivacy) {
|
|
|
|
bool privacy;
|
|
|
|
if(!flipper_format_read_bool(file, "Privacy Mode", &privacy, 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Privacy Mode");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(privacy) {
|
|
|
|
data->flags |= NfcVSlixDataFlagsPrivacy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(features & SlixFeatureProtection) {
|
|
|
|
if(!flipper_format_read_hex(file, "Protection pointer", &(data->pp_pointer), 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Protection pointer");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_hex(file, "Protection condition", &(data->pp_condition), 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Protection condition");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 08:30:53 +03:00
|
|
|
parsed = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfc_device_save_nfcv_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool saved = false;
|
|
|
|
NfcVData* data = &dev->dev_data.nfcv_data;
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint32_t temp_uint32 = 0;
|
|
|
|
uint8_t temp_uint8 = 0;
|
|
|
|
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Data Storage Format Identifier")) break;
|
|
|
|
if(!flipper_format_write_hex(file, "DSFID", &(data->dsfid), 1)) break;
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Application Family Identifier")) break;
|
|
|
|
if(!flipper_format_write_hex(file, "AFI", &(data->afi), 1)) break;
|
|
|
|
if(!flipper_format_write_hex(file, "IC Reference", &(data->ic_ref), 1)) break;
|
|
|
|
temp_uint32 = data->block_num;
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Number of memory blocks, usually 0 to 256"))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_uint32(file, "Block Count", &temp_uint32, 1)) break;
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Size of a single memory block, usually 4"))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_hex(file, "Block Size", &(data->block_size), 1)) break;
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Data Content", data->data, data->block_num * data->block_size))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_comment_cstr(
|
2023-06-28 20:44:34 +03:00
|
|
|
file,
|
|
|
|
"First byte: DSFID (0x01) / AFI (0x02) / EAS (0x04) / PPL (0x08) lock info, others: block lock info"))
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
file, "Security Status", data->security_status, 1 + data->block_num))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_comment_cstr(
|
|
|
|
file,
|
|
|
|
"Subtype of this card (0 = ISO15693, 1 = SLIX, 2 = SLIX-S, 3 = SLIX-L, 4 = SLIX2)"))
|
|
|
|
break;
|
|
|
|
temp_uint8 = (uint8_t)data->sub_type;
|
|
|
|
if(!flipper_format_write_hex(file, "Subtype", &temp_uint8, 1)) break;
|
|
|
|
|
|
|
|
switch(data->sub_type) {
|
|
|
|
case NfcVTypePlain:
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "End of ISO15693 parameters")) break;
|
|
|
|
saved = true;
|
|
|
|
break;
|
|
|
|
case NfcVTypeSlix:
|
2023-06-28 20:44:34 +03:00
|
|
|
saved = nfc_device_save_slix_data(file, dev, SlixFeatureSlix, "SLIX");
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
case NfcVTypeSlixS:
|
2023-06-28 20:44:34 +03:00
|
|
|
saved = nfc_device_save_slix_data(file, dev, SlixFeatureSlixS, "SLIX-S");
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
case NfcVTypeSlixL:
|
2023-06-28 20:44:34 +03:00
|
|
|
saved = nfc_device_save_slix_data(file, dev, SlixFeatureSlixL, "SLIX-L");
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
case NfcVTypeSlix2:
|
2023-06-28 20:44:34 +03:00
|
|
|
saved = nfc_device_save_slix_data(file, dev, SlixFeatureSlix2, "SLIX2");
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_load_nfcv_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool parsed = false;
|
|
|
|
NfcVData* data = &dev->dev_data.nfcv_data;
|
|
|
|
|
|
|
|
memset(data, 0x00, sizeof(NfcVData));
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint32_t temp_uint32 = 0;
|
|
|
|
uint8_t temp_value = 0;
|
|
|
|
|
2023-06-28 20:44:34 +03:00
|
|
|
if(!flipper_format_read_hex(file, "DSFID", &(data->dsfid), 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading DSFID");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_hex(file, "AFI", &(data->afi), 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading AFI");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_hex(file, "IC Reference", &(data->ic_ref), 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading IC Reference");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_uint32(file, "Block Count", &temp_uint32, 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Block Count");
|
|
|
|
break;
|
|
|
|
}
|
2023-06-08 08:30:53 +03:00
|
|
|
data->block_num = temp_uint32;
|
2023-06-28 20:44:34 +03:00
|
|
|
if(!flipper_format_read_hex(file, "Block Size", &(data->block_size), 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Block Size");
|
|
|
|
break;
|
|
|
|
}
|
2023-06-08 08:30:53 +03:00
|
|
|
if(!flipper_format_read_hex(
|
2023-06-28 20:44:34 +03:00
|
|
|
file, "Data Content", data->data, data->block_num * data->block_size)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Data Content");
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
2023-06-28 20:44:34 +03:00
|
|
|
}
|
2023-06-08 08:30:53 +03:00
|
|
|
|
|
|
|
/* optional, as added later */
|
|
|
|
if(flipper_format_key_exist(file, "Security Status")) {
|
|
|
|
if(!flipper_format_read_hex(
|
2023-06-28 20:44:34 +03:00
|
|
|
file, "Security Status", data->security_status, 1 + data->block_num)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Security Status");
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
2023-06-28 20:44:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_hex(file, "Subtype", &temp_value, 1)) {
|
|
|
|
FURI_LOG_D(TAG, "Failed reading Subtype");
|
|
|
|
break;
|
2023-06-08 08:30:53 +03:00
|
|
|
}
|
|
|
|
data->sub_type = temp_value;
|
|
|
|
|
|
|
|
switch(data->sub_type) {
|
|
|
|
case NfcVTypePlain:
|
|
|
|
parsed = true;
|
|
|
|
break;
|
|
|
|
case NfcVTypeSlix:
|
2023-06-28 20:44:34 +03:00
|
|
|
parsed = nfc_device_load_slix_data(file, dev, SlixFeatureSlix);
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
case NfcVTypeSlixS:
|
2023-06-28 20:44:34 +03:00
|
|
|
parsed = nfc_device_load_slix_data(file, dev, SlixFeatureSlixS);
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
case NfcVTypeSlixL:
|
2023-06-28 20:44:34 +03:00
|
|
|
parsed = nfc_device_load_slix_data(file, dev, SlixFeatureSlixL);
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
case NfcVTypeSlix2:
|
2023-06-28 20:44:34 +03:00
|
|
|
parsed = nfc_device_load_slix_data(file, dev, SlixFeatureSlix2);
|
2023-06-08 08:30:53 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfc_device_save_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool saved = false;
|
|
|
|
EmvData* data = &dev->dev_data.emv_data;
|
|
|
|
uint32_t data_temp = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Write Bank card specific data
|
|
|
|
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;
|
|
|
|
if(data->exp_mon) {
|
|
|
|
uint8_t exp_data[2] = {data->exp_mon, data->exp_year};
|
|
|
|
if(!flipper_format_write_hex(file, "Exp data", exp_data, sizeof(exp_data))) break;
|
|
|
|
}
|
|
|
|
if(data->country_code) {
|
|
|
|
data_temp = data->country_code;
|
|
|
|
if(!flipper_format_write_uint32(file, "Country code", &data_temp, 1)) break;
|
|
|
|
}
|
|
|
|
if(data->currency_code) {
|
|
|
|
data_temp = data->currency_code;
|
|
|
|
if(!flipper_format_write_uint32(file, "Currency code", &data_temp, 1)) break;
|
|
|
|
}
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
|
2021-07-22 09:05:07 +03:00
|
|
|
bool parsed = false;
|
2022-04-19 18:23:58 +03:00
|
|
|
EmvData* data = &dev->dev_data.emv_data;
|
|
|
|
memset(data, 0, sizeof(EmvData));
|
2021-11-09 00:55:06 +03:00
|
|
|
uint32_t data_cnt = 0;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2021-07-22 09:05:07 +03:00
|
|
|
|
|
|
|
do {
|
2021-11-09 00:55:06 +03:00
|
|
|
// Load essential data
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_get_value_count(file, "AID", &data_cnt)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
data->aid_len = data_cnt;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(file, "AID", data->aid, data->aid_len)) break;
|
|
|
|
if(!flipper_format_read_string(file, "Name", temp_str)) break;
|
2022-10-05 18:15:23 +03:00
|
|
|
strlcpy(data->name, furi_string_get_cstr(temp_str), sizeof(data->name));
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_get_value_count(file, "Number", &data_cnt)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
data->number_len = data_cnt;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(file, "Number", data->number, data->number_len)) break;
|
2021-07-22 09:05:07 +03:00
|
|
|
parsed = true;
|
2021-11-09 00:55:06 +03:00
|
|
|
// Load optional data
|
|
|
|
uint8_t exp_data[2] = {};
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_read_hex(file, "Exp data", exp_data, 2)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
data->exp_mon = exp_data[0];
|
|
|
|
data->exp_year = exp_data[1];
|
2021-08-20 22:56:43 +03:00
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_read_uint32(file, "Country code", &data_cnt, 1)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
data->country_code = data_cnt;
|
2021-08-20 22:56:43 +03:00
|
|
|
}
|
2022-02-18 22:53:46 +03:00
|
|
|
if(flipper_format_read_uint32(file, "Currency code", &data_cnt, 1)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
data->currency_code = data_cnt;
|
2021-08-04 21:58:11 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
} while(false);
|
2021-07-22 09:05:07 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2021-07-22 09:05:07 +03:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:30:49 +03:00
|
|
|
static void nfc_device_write_mifare_classic_block(
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* block_str,
|
2022-07-26 18:30:49 +03:00
|
|
|
MfClassicData* data,
|
|
|
|
uint8_t block_num) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_reset(block_str);
|
2022-07-26 18:30:49 +03:00
|
|
|
bool is_sec_trailer = mf_classic_is_sector_trailer(block_num);
|
|
|
|
if(is_sec_trailer) {
|
|
|
|
uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
|
|
|
|
MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
|
|
|
|
// Write key A
|
|
|
|
for(size_t i = 0; i < sizeof(sec_tr->key_a); i++) {
|
|
|
|
if(mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "%02X ", sec_tr->key_a[i]);
|
2022-07-26 18:30:49 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "?? ");
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write Access bytes
|
|
|
|
for(size_t i = 0; i < MF_CLASSIC_ACCESS_BYTES_SIZE; i++) {
|
|
|
|
if(mf_classic_is_block_read(data, block_num)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "%02X ", sec_tr->access_bits[i]);
|
2022-07-26 18:30:49 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "?? ");
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write key B
|
|
|
|
for(size_t i = 0; i < sizeof(sec_tr->key_b); i++) {
|
|
|
|
if(mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "%02X ", sec_tr->key_b[i]);
|
2022-07-26 18:30:49 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "?? ");
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Write data block
|
|
|
|
for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
|
|
|
|
if(mf_classic_is_block_read(data, block_num)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "%02X ", data->block[block_num].value[i]);
|
2022-07-26 18:30:49 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(block_str, "?? ");
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_trim(block_str);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool saved = false;
|
|
|
|
MfClassicData* data = &dev->dev_data.mf_classic_data;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-03-24 01:14:34 +03:00
|
|
|
uint16_t blocks = 0;
|
|
|
|
|
|
|
|
// Save Mifare Classic specific data
|
|
|
|
do {
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
|
2022-05-24 17:00:15 +03:00
|
|
|
|
2023-01-27 08:51:47 +03:00
|
|
|
if(data->type == MfClassicTypeMini) {
|
|
|
|
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "MINI")) break;
|
|
|
|
blocks = 20;
|
|
|
|
} else if(data->type == MfClassicType1k) {
|
2022-03-24 01:14:34 +03:00
|
|
|
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
|
|
|
|
blocks = 64;
|
|
|
|
} else if(data->type == MfClassicType4k) {
|
|
|
|
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
|
|
|
|
blocks = 256;
|
|
|
|
}
|
2022-05-24 17:00:15 +03:00
|
|
|
if(!flipper_format_write_uint32(
|
|
|
|
file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
|
|
|
|
break;
|
|
|
|
if(!flipper_format_write_comment_cstr(
|
2022-07-26 18:30:49 +03:00
|
|
|
file, "Mifare Classic blocks, \'??\' means unknown data"))
|
2022-05-24 17:00:15 +03:00
|
|
|
break;
|
2022-03-24 01:14:34 +03:00
|
|
|
bool block_saved = true;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* block_str;
|
|
|
|
block_str = furi_string_alloc();
|
2022-03-24 01:14:34 +03:00
|
|
|
for(size_t i = 0; i < blocks; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Block %d", i);
|
2022-07-26 18:30:49 +03:00
|
|
|
nfc_device_write_mifare_classic_block(block_str, data, i);
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_write_string(file, furi_string_get_cstr(temp_str), block_str)) {
|
2022-03-24 01:14:34 +03:00
|
|
|
block_saved = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(block_str);
|
2022-03-24 01:14:34 +03:00
|
|
|
if(!block_saved) break;
|
|
|
|
saved = true;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-03-24 01:14:34 +03:00
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:30:49 +03:00
|
|
|
static void nfc_device_load_mifare_classic_block(
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* block_str,
|
2022-07-26 18:30:49 +03:00
|
|
|
MfClassicData* data,
|
|
|
|
uint8_t block_num) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_trim(block_str);
|
2022-07-26 18:30:49 +03:00
|
|
|
MfClassicBlock block_tmp = {};
|
|
|
|
bool is_sector_trailer = mf_classic_is_sector_trailer(block_num);
|
|
|
|
uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
|
|
|
|
uint16_t block_unknown_bytes_mask = 0;
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_trim(block_str);
|
2022-07-26 18:30:49 +03:00
|
|
|
for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
char hi = furi_string_get_char(block_str, 3 * i);
|
|
|
|
char low = furi_string_get_char(block_str, 3 * i + 1);
|
2022-07-26 18:30:49 +03:00
|
|
|
uint8_t byte = 0;
|
2022-08-29 18:04:17 +03:00
|
|
|
if(hex_char_to_uint8(hi, low, &byte)) {
|
2022-07-26 18:30:49 +03:00
|
|
|
block_tmp.value[i] = byte;
|
|
|
|
} else {
|
|
|
|
FURI_BIT_SET(block_unknown_bytes_mask, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(block_unknown_bytes_mask == 0xffff) {
|
|
|
|
// All data is unknown, exit
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_sector_trailer) {
|
|
|
|
MfClassicSectorTrailer* sec_tr_tmp = (MfClassicSectorTrailer*)&block_tmp;
|
|
|
|
// Load Key A
|
|
|
|
// Key A mask 0b0000000000111111 = 0x003f
|
|
|
|
if((block_unknown_bytes_mask & 0x003f) == 0) {
|
|
|
|
uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_a, sizeof(sec_tr_tmp->key_a));
|
|
|
|
mf_classic_set_key_found(data, sector_num, MfClassicKeyA, key);
|
|
|
|
}
|
|
|
|
// Load Access Bits
|
|
|
|
// Access bits mask 0b0000001111000000 = 0x03c0
|
|
|
|
if((block_unknown_bytes_mask & 0x03c0) == 0) {
|
|
|
|
mf_classic_set_block_read(data, block_num, &block_tmp);
|
|
|
|
}
|
|
|
|
// Load Key B
|
|
|
|
// Key B mask 0b1111110000000000 = 0xfc00
|
|
|
|
if((block_unknown_bytes_mask & 0xfc00) == 0) {
|
|
|
|
uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_b, sizeof(sec_tr_tmp->key_b));
|
|
|
|
mf_classic_set_key_found(data, sector_num, MfClassicKeyB, key);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(block_unknown_bytes_mask == 0) {
|
|
|
|
mf_classic_set_block_read(data, block_num, &block_tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
|
|
|
|
bool parsed = false;
|
|
|
|
MfClassicData* data = &dev->dev_data.mf_classic_data;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
2022-05-24 17:00:15 +03:00
|
|
|
uint32_t data_format_version = 0;
|
2022-10-05 18:15:23 +03:00
|
|
|
temp_str = furi_string_alloc();
|
2022-03-24 01:14:34 +03:00
|
|
|
uint16_t data_blocks = 0;
|
2022-07-26 18:30:49 +03:00
|
|
|
memset(data, 0, sizeof(MfClassicData));
|
2022-03-24 01:14:34 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Read Mifare Classic type
|
|
|
|
if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
|
2023-01-27 08:51:47 +03:00
|
|
|
if(!furi_string_cmp(temp_str, "MINI")) {
|
|
|
|
data->type = MfClassicTypeMini;
|
|
|
|
data_blocks = 20;
|
|
|
|
} else if(!furi_string_cmp(temp_str, "1K")) {
|
2022-03-24 01:14:34 +03:00
|
|
|
data->type = MfClassicType1k;
|
|
|
|
data_blocks = 64;
|
2022-10-05 18:15:23 +03:00
|
|
|
} else if(!furi_string_cmp(temp_str, "4K")) {
|
2022-03-24 01:14:34 +03:00
|
|
|
data->type = MfClassicType4k;
|
|
|
|
data_blocks = 256;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2022-05-24 17:00:15 +03:00
|
|
|
|
2022-07-26 18:30:49 +03:00
|
|
|
bool old_format = false;
|
2022-05-24 17:00:15 +03:00
|
|
|
// Read Mifare Classic format version
|
|
|
|
if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
|
2023-01-06 19:06:50 +03:00
|
|
|
// Load unread sectors with zero keys access for backward compatibility
|
2022-05-24 17:00:15 +03:00
|
|
|
if(!flipper_format_rewind(file)) break;
|
2022-07-26 18:30:49 +03:00
|
|
|
old_format = true;
|
2022-05-24 17:00:15 +03:00
|
|
|
} else {
|
2022-07-26 18:30:49 +03:00
|
|
|
if(data_format_version < nfc_mifare_classic_data_format_version) {
|
|
|
|
old_format = true;
|
|
|
|
}
|
2022-05-24 17:00:15 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
// Read Mifare Classic blocks
|
|
|
|
bool block_read = true;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* block_str;
|
|
|
|
block_str = furi_string_alloc();
|
2022-03-24 01:14:34 +03:00
|
|
|
for(size_t i = 0; i < data_blocks; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Block %d", i);
|
|
|
|
if(!flipper_format_read_string(file, furi_string_get_cstr(temp_str), block_str)) {
|
2022-03-24 01:14:34 +03:00
|
|
|
block_read = false;
|
|
|
|
break;
|
|
|
|
}
|
2022-07-26 18:30:49 +03:00
|
|
|
nfc_device_load_mifare_classic_block(block_str, data, i);
|
2022-03-24 01:14:34 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(block_str);
|
2022-03-24 01:14:34 +03:00
|
|
|
if(!block_read) break;
|
2022-07-26 18:30:49 +03:00
|
|
|
|
|
|
|
// Set keys and blocks as unknown for backward compatibility
|
|
|
|
if(old_format) {
|
|
|
|
data->key_a_mask = 0ULL;
|
|
|
|
data->key_b_mask = 0ULL;
|
|
|
|
memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
|
|
|
|
}
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
parsed = true;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-03-24 01:14:34 +03:00
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static void nfc_device_get_key_cache_file_path(NfcDevice* dev, FuriString* file_path) {
|
2022-07-26 18:30:49 +03:00
|
|
|
uint8_t* uid = dev->dev_data.nfc_data.uid;
|
|
|
|
uint8_t uid_len = dev->dev_data.nfc_data.uid_len;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(file_path, NFC_DEVICE_KEYS_FOLDER "/");
|
2022-07-26 18:30:49 +03:00
|
|
|
for(size_t i = 0; i < uid_len; i++) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(file_path, "%02X", uid[i]);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(file_path, NFC_DEVICE_KEYS_EXTENSION);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool nfc_device_save_mifare_classic_keys(NfcDevice* dev) {
|
|
|
|
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
|
|
|
|
MfClassicData* data = &dev->dev_data.mf_classic_data;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-07-26 18:30:49 +03:00
|
|
|
|
|
|
|
nfc_device_get_key_cache_file_path(dev, temp_str);
|
|
|
|
bool save_success = false;
|
|
|
|
do {
|
|
|
|
if(!storage_simply_mkdir(dev->storage, NFC_DEVICE_KEYS_FOLDER)) break;
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(temp_str))) break;
|
|
|
|
if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
|
2022-07-26 18:30:49 +03:00
|
|
|
if(!flipper_format_write_header_cstr(file, nfc_keys_file_header, nfc_keys_file_version))
|
|
|
|
break;
|
2023-01-27 08:51:47 +03:00
|
|
|
if(data->type == MfClassicTypeMini) {
|
|
|
|
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "MINI")) break;
|
|
|
|
} else if(data->type == MfClassicType1k) {
|
2022-07-26 18:30:49 +03:00
|
|
|
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
|
|
|
|
} else if(data->type == MfClassicType4k) {
|
|
|
|
if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_write_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
|
|
|
|
if(!flipper_format_write_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
|
|
|
|
uint8_t sector_num = mf_classic_get_total_sectors_num(data->type);
|
|
|
|
bool key_save_success = true;
|
|
|
|
for(size_t i = 0; (i < sector_num) && (key_save_success); i++) {
|
|
|
|
MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
|
|
|
|
if(FURI_BIT(data->key_a_mask, i)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Key A sector %d", i);
|
|
|
|
key_save_success = flipper_format_write_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
if(!key_save_success) break;
|
2022-10-17 20:10:41 +03:00
|
|
|
if(FURI_BIT(data->key_b_mask, i)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Key B sector %d", i);
|
|
|
|
key_save_success = flipper_format_write_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
save_success = key_save_success;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
flipper_format_free(file);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-07-26 18:30:49 +03:00
|
|
|
return save_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_device_load_key_cache(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-07-26 18:30:49 +03:00
|
|
|
|
|
|
|
MfClassicData* data = &dev->dev_data.mf_classic_data;
|
|
|
|
nfc_device_get_key_cache_file_path(dev, temp_str);
|
|
|
|
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
|
|
|
|
|
|
|
|
bool load_success = false;
|
|
|
|
do {
|
2022-10-05 18:15:23 +03:00
|
|
|
if(storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK)
|
|
|
|
break;
|
|
|
|
if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
|
2022-07-26 18:30:49 +03:00
|
|
|
uint32_t version = 0;
|
|
|
|
if(!flipper_format_read_header(file, temp_str, &version)) break;
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_cmp_str(temp_str, nfc_keys_file_header)) break;
|
2022-07-26 18:30:49 +03:00
|
|
|
if(version != nfc_keys_file_version) break;
|
|
|
|
if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
|
2023-01-27 08:51:47 +03:00
|
|
|
if(!furi_string_cmp(temp_str, "MINI")) {
|
|
|
|
data->type = MfClassicTypeMini;
|
|
|
|
} else if(!furi_string_cmp(temp_str, "1K")) {
|
2022-07-26 18:30:49 +03:00
|
|
|
data->type = MfClassicType1k;
|
2022-10-05 18:15:23 +03:00
|
|
|
} else if(!furi_string_cmp(temp_str, "4K")) {
|
2022-07-26 18:30:49 +03:00
|
|
|
data->type = MfClassicType4k;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
|
|
|
|
if(!flipper_format_read_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
|
|
|
|
uint8_t sectors = mf_classic_get_total_sectors_num(data->type);
|
|
|
|
bool key_read_success = true;
|
|
|
|
for(size_t i = 0; (i < sectors) && (key_read_success); i++) {
|
|
|
|
MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
|
|
|
|
if(FURI_BIT(data->key_a_mask, i)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Key A sector %d", i);
|
|
|
|
key_read_success = flipper_format_read_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
if(!key_read_success) break;
|
|
|
|
if(FURI_BIT(data->key_b_mask, i)) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(temp_str, "Key B sector %d", i);
|
|
|
|
key_read_success = flipper_format_read_hex(
|
|
|
|
file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
|
2022-07-26 18:30:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
load_success = key_read_success;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-07-26 18:30:49 +03:00
|
|
|
flipper_format_free(file);
|
|
|
|
|
|
|
|
return load_success;
|
|
|
|
}
|
|
|
|
|
2021-07-02 16:44:10 +03:00
|
|
|
void nfc_device_set_name(NfcDevice* dev, const char* name) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
|
|
|
strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static void nfc_device_get_path_without_ext(FuriString* orig_path, FuriString* shadow_path) {
|
2022-05-27 14:19:21 +03:00
|
|
|
// TODO: this won't work if there is ".nfc" anywhere in the path other than
|
|
|
|
// at the end
|
2023-09-01 08:57:49 +03:00
|
|
|
size_t ext_start = furi_string_search(orig_path, NFC_APP_FILENAME_EXTENSION);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set_n(shadow_path, orig_path, 0, ext_start);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static void nfc_device_get_shadow_path(FuriString* orig_path, FuriString* shadow_path) {
|
2022-05-27 14:19:21 +03:00
|
|
|
nfc_device_get_path_without_ext(orig_path, shadow_path);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
2022-12-19 22:57:48 +03:00
|
|
|
static void nfc_device_get_folder_from_path(FuriString* path, FuriString* folder) {
|
|
|
|
size_t last_slash = furi_string_search_rchar(path, '/');
|
|
|
|
if(last_slash == FURI_STRING_FAILURE) {
|
|
|
|
// No slashes in the path, treat the whole path as a folder
|
|
|
|
furi_string_set(folder, path);
|
|
|
|
} else {
|
|
|
|
furi_string_set_n(folder, path, 0, last_slash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 19:20:35 +03:00
|
|
|
bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
|
2021-07-02 16:44:10 +03:00
|
|
|
furi_assert(dev);
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
bool saved = false;
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
|
2022-04-19 18:23:58 +03:00
|
|
|
FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2021-07-22 09:05:07 +03:00
|
|
|
do {
|
2022-12-19 22:57:48 +03:00
|
|
|
// Create directory if necessary
|
|
|
|
FuriString* folder = furi_string_alloc();
|
|
|
|
// Get folder from filename (filename is in the form of "folder/filename.nfc", so the folder is "folder/")
|
|
|
|
furi_string_set(temp_str, dev_name);
|
|
|
|
// Get folder from filename
|
|
|
|
nfc_device_get_folder_from_path(temp_str, folder);
|
|
|
|
FURI_LOG_I("Nfc", "Saving to folder %s", furi_string_get_cstr(folder));
|
|
|
|
if(!storage_simply_mkdir(dev->storage, furi_string_get_cstr(folder))) {
|
|
|
|
FURI_LOG_E("Nfc", "Failed to create folder %s", furi_string_get_cstr(folder));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
furi_string_free(folder);
|
2022-11-10 19:20:35 +03:00
|
|
|
// First remove nfc device file if it was saved
|
2021-07-22 09:05:07 +03:00
|
|
|
// Open file
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
// Write header
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
// Write nfc device type
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_comment_cstr(
|
2023-06-08 08:30:53 +03:00
|
|
|
file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic or ISO15693"))
|
2021-07-22 09:05:07 +03:00
|
|
|
break;
|
2021-11-09 00:55:06 +03:00
|
|
|
nfc_device_prepare_format_string(dev, temp_str);
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_string(file, "Device type", temp_str)) break;
|
2023-06-08 08:30:53 +03:00
|
|
|
// Write UID
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) break;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
|
2023-06-08 08:30:53 +03:00
|
|
|
|
|
|
|
if(dev->format != NfcDeviceSaveFormatNfcV) {
|
|
|
|
// Write ATQA, SAK
|
|
|
|
if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break;
|
|
|
|
// Save ATQA in MSB order for correct companion apps display
|
|
|
|
uint8_t atqa[2] = {data->atqa[1], data->atqa[0]};
|
|
|
|
if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break;
|
|
|
|
if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:05:07 +03:00
|
|
|
// Save more data if necessary
|
|
|
|
if(dev->format == NfcDeviceSaveFormatMifareUl) {
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!nfc_device_save_mifare_ul_data(file, dev)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
|
|
|
|
if(!nfc_device_save_mifare_df_data(file, dev)) break;
|
2023-06-08 08:30:53 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatNfcV) {
|
|
|
|
if(!nfc_device_save_nfcv_data(file, dev)) break;
|
|
|
|
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
|
|
|
|
if(!nfc_device_save_bank_card_data(file, dev)) break;
|
2022-03-24 01:14:34 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
|
2022-07-26 18:30:49 +03:00
|
|
|
// Save data
|
2022-03-24 01:14:34 +03:00
|
|
|
if(!nfc_device_save_mifare_classic_data(file, dev)) break;
|
2022-07-26 18:30:49 +03:00
|
|
|
// Save keys cache
|
|
|
|
if(!nfc_device_save_mifare_classic_keys(dev)) break;
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
saved = true;
|
2021-07-22 09:05:07 +03:00
|
|
|
} while(0);
|
|
|
|
|
2022-12-26 15:13:30 +03:00
|
|
|
if(!saved) { //-V547
|
2021-11-09 00:55:06 +03:00
|
|
|
dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(file);
|
2021-11-09 00:55:06 +03:00
|
|
|
return saved;
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
2022-11-10 19:20:35 +03:00
|
|
|
bool nfc_device_save_shadow(NfcDevice* dev, const char* path) {
|
2021-08-17 00:45:04 +03:00
|
|
|
dev->shadow_file_exist = true;
|
2022-11-10 19:20:35 +03:00
|
|
|
// Replace extension from .nfc to .shd if necessary
|
|
|
|
FuriString* orig_path = furi_string_alloc();
|
|
|
|
furi_string_set_str(orig_path, path);
|
|
|
|
FuriString* shadow_path = furi_string_alloc();
|
|
|
|
nfc_device_get_shadow_path(orig_path, shadow_path);
|
|
|
|
|
|
|
|
bool file_saved = nfc_device_save(dev, furi_string_get_cstr(shadow_path));
|
|
|
|
furi_string_free(orig_path);
|
|
|
|
furi_string_free(shadow_path);
|
|
|
|
|
|
|
|
return file_saved;
|
2021-08-17 00:45:04 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
static bool nfc_device_load_data(NfcDevice* dev, FuriString* path, bool show_dialog) {
|
2021-07-22 09:05:07 +03:00
|
|
|
bool parsed = false;
|
2022-02-18 22:53:46 +03:00
|
|
|
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
|
2022-04-19 18:23:58 +03:00
|
|
|
FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
|
2021-11-09 00:55:06 +03:00
|
|
|
uint32_t data_cnt = 0;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-05-10 17:10:03 +03:00
|
|
|
bool deprecated_version = false;
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2022-12-07 13:52:44 +03:00
|
|
|
// Version 2 of file format had ATQA bytes swapped
|
|
|
|
uint32_t version_with_lsb_atqa = 2;
|
|
|
|
|
[FL-1962, FL-2464, FL-2465, FL-2466, FL-2560, FL-2637, FL-2595] Ibutton, Infrared, LfRFID GUI fixes (#1392)
* Ibutton, Infrared, LfRFID GUI fixes
* Loading screens update
Co-authored-by: あく <alleteam@gmail.com>
2022-07-17 10:41:16 +03:00
|
|
|
if(dev->loading_cb) {
|
|
|
|
dev->loading_cb(dev->loading_cb_ctx, true);
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:05:07 +03:00
|
|
|
do {
|
2023-01-06 19:06:50 +03:00
|
|
|
// Check existence of shadow file
|
2022-05-27 14:19:21 +03:00
|
|
|
nfc_device_get_shadow_path(path, temp_str);
|
2021-11-09 00:55:06 +03:00
|
|
|
dev->shadow_file_exist =
|
2022-10-05 18:15:23 +03:00
|
|
|
storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK;
|
2021-08-17 00:45:04 +03:00
|
|
|
// Open shadow file if it exists. If not - open original
|
|
|
|
if(dev->shadow_file_exist) {
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
|
2021-08-17 00:45:04 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
|
2021-08-17 00:45:04 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
// Read and verify file header
|
|
|
|
uint32_t version = 0;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_header(file, temp_str, &version)) break;
|
2022-12-07 13:52:44 +03:00
|
|
|
if(furi_string_cmp_str(temp_str, nfc_file_header)) break;
|
|
|
|
if(version != nfc_file_version) {
|
|
|
|
if(version < version_with_lsb_atqa) {
|
|
|
|
deprecated_version = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
// Read Nfc device type
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_string(file, "Device type", temp_str)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!nfc_device_parse_format_string(dev, temp_str)) break;
|
|
|
|
// Read and parse UID, ATQA and SAK
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
|
2023-06-08 08:30:53 +03:00
|
|
|
if(!(data_cnt == 4 || data_cnt == 7 || data_cnt == 8)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
data->uid_len = data_cnt;
|
2022-02-18 22:53:46 +03:00
|
|
|
if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
|
2023-06-08 08:30:53 +03:00
|
|
|
if(dev->format != NfcDeviceSaveFormatNfcV) {
|
|
|
|
if(version == version_with_lsb_atqa) {
|
|
|
|
if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
|
|
|
|
} else {
|
|
|
|
uint8_t atqa[2] = {};
|
|
|
|
if(!flipper_format_read_hex(file, "ATQA", atqa, 2)) break;
|
|
|
|
data->atqa[0] = atqa[1];
|
|
|
|
data->atqa[1] = atqa[0];
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
|
2022-12-07 13:52:44 +03:00
|
|
|
}
|
2022-10-28 19:10:16 +03:00
|
|
|
// Load CUID
|
|
|
|
uint8_t* cuid_start = data->uid;
|
|
|
|
if(data->uid_len == 7) {
|
|
|
|
cuid_start = &data->uid[3];
|
|
|
|
}
|
|
|
|
data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
|
|
|
|
(cuid_start[3]);
|
2021-07-22 09:05:07 +03:00
|
|
|
// Parse other data
|
|
|
|
if(dev->format == NfcDeviceSaveFormatMifareUl) {
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!nfc_device_load_mifare_ul_data(file, dev)) break;
|
2022-03-24 01:14:34 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
|
|
|
|
if(!nfc_device_load_mifare_classic_data(file, dev)) break;
|
2022-03-23 16:45:37 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
|
|
|
|
if(!nfc_device_load_mifare_df_data(file, dev)) break;
|
2023-06-08 08:30:53 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatNfcV) {
|
|
|
|
if(!nfc_device_load_nfcv_data(file, dev)) break;
|
2021-07-22 09:05:07 +03:00
|
|
|
} else if(dev->format == NfcDeviceSaveFormatBankCard) {
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!nfc_device_load_bank_card_data(file, dev)) break;
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
|
|
|
parsed = true;
|
2021-11-09 00:55:06 +03:00
|
|
|
} while(false);
|
2021-07-02 16:44:10 +03:00
|
|
|
|
[FL-1962, FL-2464, FL-2465, FL-2466, FL-2560, FL-2637, FL-2595] Ibutton, Infrared, LfRFID GUI fixes (#1392)
* Ibutton, Infrared, LfRFID GUI fixes
* Loading screens update
Co-authored-by: あく <alleteam@gmail.com>
2022-07-17 10:41:16 +03:00
|
|
|
if(dev->loading_cb) {
|
|
|
|
dev->loading_cb(dev->loading_cb_ctx, false);
|
|
|
|
}
|
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
if((!parsed) && (show_dialog)) {
|
2022-05-10 17:10:03 +03:00
|
|
|
if(deprecated_version) {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
|
2021-11-09 00:55:06 +03:00
|
|
|
} else {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
|
|
|
|
}
|
2021-10-05 08:14:19 +03:00
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-02-18 22:53:46 +03:00
|
|
|
flipper_format_free(file);
|
2021-07-22 09:05:07 +03:00
|
|
|
return parsed;
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
2022-07-04 16:09:46 +03:00
|
|
|
bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
|
2021-07-02 16:44:10 +03:00
|
|
|
furi_assert(dev);
|
2021-07-16 00:39:21 +03:00
|
|
|
furi_assert(file_path);
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2021-07-16 00:39:21 +03:00
|
|
|
// Load device data
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(dev->load_path, file_path);
|
2022-07-04 16:09:46 +03:00
|
|
|
bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
|
2021-07-16 00:39:21 +03:00
|
|
|
if(dev_load) {
|
|
|
|
// Set device name
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* filename;
|
|
|
|
filename = furi_string_alloc();
|
2022-05-27 14:19:21 +03:00
|
|
|
path_extract_filename_no_ext(file_path, filename);
|
2022-10-05 18:15:23 +03:00
|
|
|
nfc_device_set_name(dev, furi_string_get_cstr(filename));
|
|
|
|
furi_string_free(filename);
|
2021-07-16 00:39:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return dev_load;
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nfc_file_select(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
2022-12-19 22:57:48 +03:00
|
|
|
const char* folder = furi_string_get_cstr(dev->folder);
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2022-05-27 14:19:21 +03:00
|
|
|
// Input events and views are managed by file_browser
|
2022-09-14 19:11:38 +03:00
|
|
|
|
|
|
|
const DialogsFileBrowserOptions browser_options = {
|
2023-09-01 08:57:49 +03:00
|
|
|
.extension = NFC_APP_FILENAME_EXTENSION,
|
2022-09-14 19:11:38 +03:00
|
|
|
.skip_assets = true,
|
2022-12-07 13:52:44 +03:00
|
|
|
.hide_dot_files = true,
|
2022-09-14 19:11:38 +03:00
|
|
|
.icon = &I_Nfc_10px,
|
|
|
|
.hide_ext = true,
|
|
|
|
.item_loader_callback = NULL,
|
|
|
|
.item_loader_context = NULL,
|
2022-12-19 22:57:48 +03:00
|
|
|
.base_path = folder,
|
2022-09-14 19:11:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
bool res =
|
2022-11-10 19:20:35 +03:00
|
|
|
dialog_file_browser_show(dev->dialogs, dev->load_path, dev->load_path, &browser_options);
|
2022-09-14 19:11:38 +03:00
|
|
|
|
2021-07-02 16:44:10 +03:00
|
|
|
if(res) {
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* filename;
|
|
|
|
filename = furi_string_alloc();
|
2022-05-27 14:19:21 +03:00
|
|
|
path_extract_filename(dev->load_path, filename, true);
|
2022-10-05 18:15:23 +03:00
|
|
|
strncpy(dev->dev_name, furi_string_get_cstr(filename), NFC_DEV_NAME_MAX_LEN);
|
2022-07-04 16:09:46 +03:00
|
|
|
res = nfc_device_load_data(dev, dev->load_path, true);
|
2021-07-02 16:44:10 +03:00
|
|
|
if(res) {
|
2022-05-27 14:19:21 +03:00
|
|
|
nfc_device_set_name(dev, dev->dev_name);
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(filename);
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2021-07-22 09:05:07 +03:00
|
|
|
|
2022-03-23 16:45:37 +03:00
|
|
|
void nfc_device_data_clear(NfcDeviceData* dev_data) {
|
2022-04-19 18:23:58 +03:00
|
|
|
if(dev_data->protocol == NfcDeviceProtocolMifareDesfire) {
|
2022-03-23 16:45:37 +03:00
|
|
|
mf_df_clear(&dev_data->mf_df_data);
|
2022-07-26 18:30:49 +03:00
|
|
|
} else if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
|
|
|
|
memset(&dev_data->mf_classic_data, 0, sizeof(MfClassicData));
|
|
|
|
} else if(dev_data->protocol == NfcDeviceProtocolMifareUl) {
|
2022-08-07 18:09:00 +03:00
|
|
|
mf_ul_reset(&dev_data->mf_ul_data);
|
2022-07-26 18:30:49 +03:00
|
|
|
} else if(dev_data->protocol == NfcDeviceProtocolEMV) {
|
|
|
|
memset(&dev_data->emv_data, 0, sizeof(EmvData));
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
2023-08-22 21:40:05 +03:00
|
|
|
|
|
|
|
furi_string_reset(dev_data->parsed_data);
|
|
|
|
|
2022-07-26 18:30:49 +03:00
|
|
|
memset(&dev_data->nfc_data, 0, sizeof(FuriHalNfcDevData));
|
|
|
|
dev_data->protocol = NfcDeviceProtocolUnknown;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_reset(dev_data->parsed_data);
|
2022-03-23 16:45:37 +03:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:05:07 +03:00
|
|
|
void nfc_device_clear(NfcDevice* dev) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
2022-08-17 18:08:13 +03:00
|
|
|
nfc_device_set_name(dev, "");
|
2022-03-23 16:45:37 +03:00
|
|
|
nfc_device_data_clear(&dev->dev_data);
|
2021-07-22 09:05:07 +03:00
|
|
|
dev->format = NfcDeviceSaveFormatUid;
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_reset(dev->load_path);
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
|
|
|
|
2022-05-27 14:19:21 +03:00
|
|
|
bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
|
2021-07-22 09:05:07 +03:00
|
|
|
furi_assert(dev);
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
bool deleted = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* file_path;
|
|
|
|
file_path = furi_string_alloc();
|
2021-08-17 00:45:04 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Delete original file
|
2022-10-05 18:15:23 +03:00
|
|
|
if(use_load_path && !furi_string_empty(dev->load_path)) {
|
|
|
|
furi_string_set(file_path, dev->load_path);
|
2022-05-27 14:19:21 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
2022-12-19 22:57:48 +03:00
|
|
|
file_path,
|
|
|
|
"%s/%s%s",
|
|
|
|
furi_string_get_cstr(dev->folder),
|
|
|
|
dev->dev_name,
|
2023-09-01 08:57:49 +03:00
|
|
|
NFC_APP_FILENAME_EXTENSION);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
|
2021-08-17 00:45:04 +03:00
|
|
|
// Delete shadow file if it exists
|
|
|
|
if(dev->shadow_file_exist) {
|
2022-10-05 18:15:23 +03:00
|
|
|
if(use_load_path && !furi_string_empty(dev->load_path)) {
|
2022-05-27 14:19:21 +03:00
|
|
|
nfc_device_get_shadow_path(dev->load_path, file_path);
|
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
2022-12-19 22:57:48 +03:00
|
|
|
file_path,
|
|
|
|
"%s/%s%s",
|
|
|
|
furi_string_get_cstr(dev->folder),
|
|
|
|
dev->dev_name,
|
|
|
|
NFC_APP_SHADOW_EXTENSION);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
|
2021-08-17 00:45:04 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
deleted = true;
|
2021-08-17 00:45:04 +03:00
|
|
|
} while(0);
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!deleted) {
|
|
|
|
dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(file_path);
|
2021-11-09 00:55:06 +03:00
|
|
|
return deleted;
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2021-08-17 00:45:04 +03:00
|
|
|
|
2022-05-27 14:19:21 +03:00
|
|
|
bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
|
2021-08-17 00:45:04 +03:00
|
|
|
furi_assert(dev);
|
|
|
|
furi_assert(dev->shadow_file_exist);
|
|
|
|
|
2021-11-09 00:55:06 +03:00
|
|
|
bool restored = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* path;
|
2021-08-17 00:45:04 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
path = furi_string_alloc();
|
2022-05-27 14:19:21 +03:00
|
|
|
|
2021-08-17 00:45:04 +03:00
|
|
|
do {
|
2022-10-05 18:15:23 +03:00
|
|
|
if(use_load_path && !furi_string_empty(dev->load_path)) {
|
2022-05-27 14:19:21 +03:00
|
|
|
nfc_device_get_shadow_path(dev->load_path, path);
|
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_printf(
|
2022-12-19 22:57:48 +03:00
|
|
|
path,
|
|
|
|
"%s/%s%s",
|
|
|
|
furi_string_get_cstr(dev->folder),
|
|
|
|
dev->dev_name,
|
|
|
|
NFC_APP_SHADOW_EXTENSION);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(path))) break;
|
2021-08-17 00:45:04 +03:00
|
|
|
dev->shadow_file_exist = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
if(use_load_path && !furi_string_empty(dev->load_path)) {
|
|
|
|
furi_string_set(path, dev->load_path);
|
2022-05-27 14:19:21 +03:00
|
|
|
} else {
|
2022-12-19 22:57:48 +03:00
|
|
|
furi_string_printf(
|
|
|
|
path,
|
|
|
|
"%s/%s%s",
|
|
|
|
furi_string_get_cstr(dev->folder),
|
|
|
|
dev->dev_name,
|
2023-09-01 08:57:49 +03:00
|
|
|
NFC_APP_FILENAME_EXTENSION);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
2022-07-04 16:09:46 +03:00
|
|
|
if(!nfc_device_load_data(dev, path, true)) break;
|
2021-11-09 00:55:06 +03:00
|
|
|
restored = true;
|
2021-08-17 00:45:04 +03:00
|
|
|
} while(0);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(path);
|
2021-11-09 00:55:06 +03:00
|
|
|
return restored;
|
2021-08-17 00:45:04 +03:00
|
|
|
}
|
[FL-1962, FL-2464, FL-2465, FL-2466, FL-2560, FL-2637, FL-2595] Ibutton, Infrared, LfRFID GUI fixes (#1392)
* Ibutton, Infrared, LfRFID GUI fixes
* Loading screens update
Co-authored-by: あく <alleteam@gmail.com>
2022-07-17 10:41:16 +03:00
|
|
|
|
|
|
|
void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context) {
|
|
|
|
furi_assert(dev);
|
|
|
|
|
|
|
|
dev->loading_cb = callback;
|
|
|
|
dev->loading_cb_ctx = context;
|
|
|
|
}
|