astyle --formatted --mode=c --indent=spaces=2 --indent-switches --indent-preprocessor --keep-one-line-blocks --max-instatement-indent=60 --brackets=linux --pad-oper --unpad-paren --pad-header
This commit is contained in:
parent
562205cc14
commit
01303fab0d
59 changed files with 3178 additions and 3178 deletions
|
|
@ -51,7 +51,7 @@
|
|||
* The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
|
||||
*/
|
||||
bool
|
||||
nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
|
||||
nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
|
||||
{
|
||||
uint8_t abtRx[265];
|
||||
size_t szParamLen;
|
||||
|
|
@ -71,19 +71,19 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui
|
|||
// Authenticate command
|
||||
case MC_AUTH_A:
|
||||
case MC_AUTH_B:
|
||||
szParamLen = sizeof (struct mifare_param_auth);
|
||||
szParamLen = sizeof(struct mifare_param_auth);
|
||||
break;
|
||||
|
||||
// Data command
|
||||
case MC_WRITE:
|
||||
szParamLen = sizeof (struct mifare_param_data);
|
||||
szParamLen = sizeof(struct mifare_param_data);
|
||||
break;
|
||||
|
||||
// Value command
|
||||
case MC_DECREMENT:
|
||||
case MC_INCREMENT:
|
||||
case MC_TRANSFER:
|
||||
szParamLen = sizeof (struct mifare_param_value);
|
||||
szParamLen = sizeof(struct mifare_param_value);
|
||||
break;
|
||||
|
||||
// Please fix your code, you never should reach this statement
|
||||
|
|
@ -94,24 +94,24 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui
|
|||
|
||||
// When available, copy the parameter bytes
|
||||
if (szParamLen)
|
||||
memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen);
|
||||
memcpy(abtCmd + 2, (uint8_t *) pmp, szParamLen);
|
||||
|
||||
// FIXME: Save and restore bEasyFraming
|
||||
// bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
|
||||
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) {
|
||||
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
return false;
|
||||
}
|
||||
// Fire the mifare command
|
||||
int res;
|
||||
if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) {
|
||||
if ((res = nfc_initiator_transceive_bytes(pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) {
|
||||
if (res == NFC_ERFTRANS) {
|
||||
// "Invalid received frame", usual means we are
|
||||
// authenticated on a sector but the requested MIFARE cmd (read, write)
|
||||
// is not permitted by current acces bytes;
|
||||
// So there is nothing to do here.
|
||||
} else {
|
||||
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
|
||||
nfc_perror(pnd, "nfc_initiator_transceive_bytes");
|
||||
}
|
||||
// XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
|
||||
return false;
|
||||
|
|
@ -126,7 +126,7 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui
|
|||
// When we have executed a read command, copy the received bytes into the param
|
||||
if (mc == MC_READ) {
|
||||
if (res == 16) {
|
||||
memcpy (pmp->mpd.abtData, abtRx, 16);
|
||||
memcpy(pmp->mpd.abtData, abtRx, 16);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ typedef union {
|
|||
// Reset struct alignment to default
|
||||
# pragma pack()
|
||||
|
||||
bool nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp);
|
||||
bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp);
|
||||
|
||||
// Compiler directive, set struct alignment to 1 uint8_t for compatibility
|
||||
# pragma pack(1)
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ uint8_t nfcforum_capability_container[] = {
|
|||
#define ISO144434A_RATS 0xE0
|
||||
|
||||
static int
|
||||
nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const size_t data_in_len, uint8_t *data_out, const size_t data_out_len)
|
||||
nfcforum_tag4_io(struct nfc_emulator *emulator, const uint8_t *data_in, const size_t data_in_len, uint8_t *data_out, const size_t data_out_len)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
|
|
@ -130,11 +130,11 @@ nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const s
|
|||
|
||||
// Show transmitted command
|
||||
if (!quiet_output) {
|
||||
printf (" In: ");
|
||||
print_hex (data_in, data_in_len);
|
||||
printf(" In: ");
|
||||
print_hex(data_in, data_in_len);
|
||||
}
|
||||
|
||||
if(data_in_len >= 4) {
|
||||
if (data_in_len >= 4) {
|
||||
if (data_in[CLA] != 0x00)
|
||||
return -ENOTSUP;
|
||||
|
||||
|
|
@ -142,7 +142,7 @@ nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const s
|
|||
#define ISO7816_READ_BINARY 0xB0
|
||||
#define ISO7816_UPDATE_BINARY 0xD6
|
||||
|
||||
switch(data_in[INS]) {
|
||||
switch (data_in[INS]) {
|
||||
case ISO7816_SELECT:
|
||||
|
||||
switch (data_in[P1]) {
|
||||
|
|
@ -152,14 +152,14 @@ nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const s
|
|||
|
||||
const uint8_t ndef_capability_container[] = { 0xE1, 0x03 };
|
||||
const uint8_t ndef_file[] = { 0xE1, 0x04 };
|
||||
if ((data_in[LC] == sizeof (ndef_capability_container)) && (0 == memcmp (ndef_capability_container, data_in + DATA, data_in[LC]))) {
|
||||
memcpy (data_out, "\x90\x00", res = 2);
|
||||
if ((data_in[LC] == sizeof(ndef_capability_container)) && (0 == memcmp(ndef_capability_container, data_in + DATA, data_in[LC]))) {
|
||||
memcpy(data_out, "\x90\x00", res = 2);
|
||||
state_machine_data->current_file = CC_FILE;
|
||||
} else if ((data_in[LC] == sizeof (ndef_file)) && (0 == memcmp (ndef_file, data_in + DATA, data_in[LC]))) {
|
||||
memcpy (data_out, "\x90\x00", res = 2);
|
||||
} else if ((data_in[LC] == sizeof(ndef_file)) && (0 == memcmp(ndef_file, data_in + DATA, data_in[LC]))) {
|
||||
memcpy(data_out, "\x90\x00", res = 2);
|
||||
state_machine_data->current_file = NDEF_FILE;
|
||||
} else {
|
||||
memcpy (data_out, "\x6a\x00", res = 2);
|
||||
memcpy(data_out, "\x6a\x00", res = 2);
|
||||
state_machine_data->current_file = NONE;
|
||||
}
|
||||
|
||||
|
|
@ -170,12 +170,12 @@ nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const s
|
|||
|
||||
const uint8_t ndef_tag_application_name_v1[] = { 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00 };
|
||||
const uint8_t ndef_tag_application_name_v2[] = { 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01 };
|
||||
if ((type4v == 1) && (data_in[LC] == sizeof (ndef_tag_application_name_v1)) && (0 == memcmp (ndef_tag_application_name_v1, data_in + DATA, data_in[LC])))
|
||||
memcpy (data_out, "\x90\x00", res = 2);
|
||||
else if ((type4v == 2) && (data_in[LC] == sizeof (ndef_tag_application_name_v2)) && (0 == memcmp (ndef_tag_application_name_v2, data_in + DATA, data_in[LC])))
|
||||
memcpy (data_out, "\x90\x00", res = 2);
|
||||
if ((type4v == 1) && (data_in[LC] == sizeof(ndef_tag_application_name_v1)) && (0 == memcmp(ndef_tag_application_name_v1, data_in + DATA, data_in[LC])))
|
||||
memcpy(data_out, "\x90\x00", res = 2);
|
||||
else if ((type4v == 2) && (data_in[LC] == sizeof(ndef_tag_application_name_v2)) && (0 == memcmp(ndef_tag_application_name_v2, data_in + DATA, data_in[LC])))
|
||||
memcpy(data_out, "\x90\x00", res = 2);
|
||||
else
|
||||
memcpy (data_out, "\x6a\x82", res = 2);
|
||||
memcpy(data_out, "\x6a\x82", res = 2);
|
||||
|
||||
break;
|
||||
default:
|
||||
|
|
@ -189,27 +189,27 @@ nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const s
|
|||
}
|
||||
switch (state_machine_data->current_file) {
|
||||
case NONE:
|
||||
memcpy (data_out, "\x6a\x82", res = 2);
|
||||
memcpy(data_out, "\x6a\x82", res = 2);
|
||||
break;
|
||||
case CC_FILE:
|
||||
memcpy (data_out, nfcforum_capability_container + (data_in[P1] << 8) + data_in[P2], data_in[LC]);
|
||||
memcpy (data_out + data_in[LC], "\x90\x00", 2);
|
||||
memcpy(data_out, nfcforum_capability_container + (data_in[P1] << 8) + data_in[P2], data_in[LC]);
|
||||
memcpy(data_out + data_in[LC], "\x90\x00", 2);
|
||||
res = data_in[LC] + 2;
|
||||
break;
|
||||
case NDEF_FILE:
|
||||
memcpy (data_out, ndef_data->ndef_file + (data_in[P1] << 8) + data_in[P2], data_in[LC]);
|
||||
memcpy (data_out + data_in[LC], "\x90\x00", 2);
|
||||
memcpy(data_out, ndef_data->ndef_file + (data_in[P1] << 8) + data_in[P2], data_in[LC]);
|
||||
memcpy(data_out + data_in[LC], "\x90\x00", 2);
|
||||
res = data_in[LC] + 2;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case ISO7816_UPDATE_BINARY:
|
||||
memcpy (ndef_data->ndef_file + (data_in[P1] << 8) + data_in[P2], data_in + DATA, data_in[LC]);
|
||||
memcpy(ndef_data->ndef_file + (data_in[P1] << 8) + data_in[P2], data_in + DATA, data_in[LC]);
|
||||
if ((data_in[P1] << 8) + data_in[P2] == 0) {
|
||||
ndef_data->ndef_file_len = (ndef_data->ndef_file[0] << 8) + ndef_data->ndef_file[1] + 2;
|
||||
}
|
||||
memcpy (data_out, "\x90\x00", res = 2);
|
||||
memcpy(data_out, "\x90\x00", res = 2);
|
||||
break;
|
||||
default: // Unknown
|
||||
if (!quiet_output) {
|
||||
|
|
@ -224,34 +224,34 @@ nfcforum_tag4_io (struct nfc_emulator *emulator, const uint8_t *data_in, const s
|
|||
// Show transmitted command
|
||||
if (!quiet_output) {
|
||||
if (res < 0) {
|
||||
ERR ("%s (%d)", strerror (-res), -res);
|
||||
ERR("%s (%d)", strerror(-res), -res);
|
||||
} else {
|
||||
printf (" Out: ");
|
||||
print_hex (data_out, res);
|
||||
printf(" Out: ");
|
||||
print_hex(data_out, res);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static void stop_emulation (int sig)
|
||||
static void stop_emulation(int sig)
|
||||
{
|
||||
(void) sig;
|
||||
if (pnd)
|
||||
nfc_abort_command (pnd);
|
||||
nfc_abort_command(pnd);
|
||||
else
|
||||
exit (EXIT_FAILURE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static size_t
|
||||
ndef_message_load (char *filename, struct nfcforum_tag4_ndef_data *tag_data)
|
||||
ndef_message_load(char *filename, struct nfcforum_tag4_ndef_data *tag_data)
|
||||
{
|
||||
struct stat sb;
|
||||
if (stat (filename, &sb) < 0)
|
||||
if (stat(filename, &sb) < 0)
|
||||
return 0;
|
||||
|
||||
/* Check file size */
|
||||
if (sb.st_size > 0xFFFF) {
|
||||
errx (EXIT_FAILURE, "file size too large '%s'", filename);
|
||||
errx(EXIT_FAILURE, "file size too large '%s'", filename);
|
||||
}
|
||||
|
||||
tag_data->ndef_file_len = sb.st_size + 2;
|
||||
|
|
@ -260,41 +260,41 @@ ndef_message_load (char *filename, struct nfcforum_tag4_ndef_data *tag_data)
|
|||
tag_data->ndef_file[1] = (uint8_t)(sb.st_size);
|
||||
|
||||
FILE *F;
|
||||
if (!(F = fopen (filename, "r")))
|
||||
err (EXIT_FAILURE, "fopen (%s, \"r\")", filename);
|
||||
if (!(F = fopen(filename, "r")))
|
||||
err(EXIT_FAILURE, "fopen (%s, \"r\")", filename);
|
||||
|
||||
if (1 != fread (tag_data->ndef_file + 2, sb.st_size, 1, F))
|
||||
err (EXIT_FAILURE, "Can't read from %s", filename);
|
||||
if (1 != fread(tag_data->ndef_file + 2, sb.st_size, 1, F))
|
||||
err(EXIT_FAILURE, "Can't read from %s", filename);
|
||||
|
||||
fclose (F);
|
||||
fclose(F);
|
||||
return sb.st_size;
|
||||
}
|
||||
|
||||
static size_t
|
||||
ndef_message_save (char *filename, struct nfcforum_tag4_ndef_data *tag_data)
|
||||
ndef_message_save(char *filename, struct nfcforum_tag4_ndef_data *tag_data)
|
||||
{
|
||||
FILE *F;
|
||||
if (!(F = fopen (filename, "w")))
|
||||
err (EXIT_FAILURE, "fopen (%s, w)", filename);
|
||||
if (!(F = fopen(filename, "w")))
|
||||
err(EXIT_FAILURE, "fopen (%s, w)", filename);
|
||||
|
||||
if (1 != fwrite (tag_data->ndef_file + 2, tag_data->ndef_file_len - 2, 1, F)) {
|
||||
err (EXIT_FAILURE, "fwrite (%d)", (int) tag_data->ndef_file_len - 2);
|
||||
if (1 != fwrite(tag_data->ndef_file + 2, tag_data->ndef_file_len - 2, 1, F)) {
|
||||
err(EXIT_FAILURE, "fwrite (%d)", (int) tag_data->ndef_file_len - 2);
|
||||
}
|
||||
|
||||
fclose (F);
|
||||
fclose(F);
|
||||
|
||||
return tag_data->ndef_file_len - 2;
|
||||
}
|
||||
|
||||
static void
|
||||
usage (char *progname)
|
||||
usage(char *progname)
|
||||
{
|
||||
fprintf (stderr, "usage: %s [-1] [infile [outfile]]\n", progname);
|
||||
fprintf (stderr, " -1: force Tag Type 4 v1.0 (default is v2.0)\n");
|
||||
fprintf(stderr, "usage: %s [-1] [infile [outfile]]\n", progname);
|
||||
fprintf(stderr, " -1: force Tag Type 4 v1.0 (default is v2.0)\n");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int options = 0;
|
||||
nfc_target nt = {
|
||||
|
|
@ -342,56 +342,56 @@ main (int argc, char *argv[])
|
|||
.user_data = &nfcforum_tag4_data,
|
||||
};
|
||||
|
||||
if ((argc > (1 + options)) && (0 == strcmp ("-h", argv[1 + options]))) {
|
||||
usage (argv[0]);
|
||||
exit (EXIT_SUCCESS);
|
||||
if ((argc > (1 + options)) && (0 == strcmp("-h", argv[1 + options]))) {
|
||||
usage(argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if ((argc > (1 + options)) && (0 == strcmp ("-1", argv[1 + options]))) {
|
||||
if ((argc > (1 + options)) && (0 == strcmp("-1", argv[1 + options]))) {
|
||||
type4v = 1;
|
||||
nfcforum_capability_container[2] = 0x10;
|
||||
options += 1;
|
||||
}
|
||||
|
||||
if (argc > (3 + options)) {
|
||||
usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// If some file is provided load it
|
||||
if (argc >= (2 + options)) {
|
||||
if (!ndef_message_load (argv[1 + options], &nfcforum_tag4_data)) {
|
||||
err (EXIT_FAILURE, "Can't load NDEF file '%s'", argv[1 + options]);
|
||||
if (!ndef_message_load(argv[1 + options], &nfcforum_tag4_data)) {
|
||||
err(EXIT_FAILURE, "Can't load NDEF file '%s'", argv[1 + options]);
|
||||
}
|
||||
}
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
// Try to open the NFC reader
|
||||
pnd = nfc_open (NULL, NULL);
|
||||
pnd = nfc_open(NULL, NULL);
|
||||
|
||||
if (pnd == NULL) {
|
||||
ERR("Unable to open NFC device");
|
||||
exit (EXIT_FAILURE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
signal (SIGINT, stop_emulation);
|
||||
signal(SIGINT, stop_emulation);
|
||||
|
||||
printf ("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
printf ("Emulating NDEF tag now, please touch it with a second NFC device\n");
|
||||
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
printf("Emulating NDEF tag now, please touch it with a second NFC device\n");
|
||||
|
||||
if (0 != nfc_emulate_target (pnd, &emulator)) { // contains already nfc_target_init() call
|
||||
nfc_perror (pnd, "nfc_emulate_target");
|
||||
if (0 != nfc_emulate_target(pnd, &emulator)) { // contains already nfc_target_init() call
|
||||
nfc_perror(pnd, "nfc_emulate_target");
|
||||
}
|
||||
|
||||
nfc_close(pnd);
|
||||
|
||||
if (argc == (3 + options)) {
|
||||
if (!(ndef_message_save (argv[2 + options], &nfcforum_tag4_data))) {
|
||||
err (EXIT_FAILURE, "Can't save NDEF file '%s'", argv[2 + options]);
|
||||
if (!(ndef_message_save(argv[2 + options], &nfcforum_tag4_data))) {
|
||||
err(EXIT_FAILURE, "Can't save NDEF file '%s'", argv[2 + options]);
|
||||
}
|
||||
}
|
||||
|
||||
nfc_exit (NULL);
|
||||
exit (EXIT_SUCCESS);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
108
utils/nfc-list.c
108
utils/nfc-list.c
|
|
@ -59,14 +59,14 @@
|
|||
static nfc_device *pnd;
|
||||
|
||||
static void
|
||||
print_usage (const char* progname)
|
||||
print_usage(const char* progname)
|
||||
{
|
||||
printf ("usage: %s [-v]\n", progname);
|
||||
printf (" -v\t verbose display\n");
|
||||
printf("usage: %s [-v]\n", progname);
|
||||
printf(" -v\t verbose display\n");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, const char *argv[])
|
||||
main(int argc, const char *argv[])
|
||||
{
|
||||
(void) argc;
|
||||
const char *acLibnfcVersion;
|
||||
|
|
@ -74,29 +74,29 @@ main (int argc, const char *argv[])
|
|||
bool verbose = false;
|
||||
int res = 0;
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
// Display libnfc version
|
||||
acLibnfcVersion = nfc_version ();
|
||||
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
acLibnfcVersion = nfc_version();
|
||||
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
if (argc != 1) {
|
||||
if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) {
|
||||
if ((argc == 2) && (0 == strcmp("-v", argv[1]))) {
|
||||
verbose = true;
|
||||
} else {
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBUSB
|
||||
# ifdef DEBUG
|
||||
usb_set_debug (4);
|
||||
usb_set_debug(4);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* Lazy way to open an NFC device */
|
||||
#if 0
|
||||
pnd = nfc_open (NULL, NULL);
|
||||
pnd = nfc_open(NULL, NULL);
|
||||
#endif
|
||||
|
||||
/* If specific device is wanted, i.e. an ARYGON device on /dev/ttyUSB0 */
|
||||
|
|
@ -105,7 +105,7 @@ main (int argc, const char *argv[])
|
|||
ndd.pcDriver = "ARYGON";
|
||||
ndd.pcPort = "/dev/ttyUSB0";
|
||||
ndd.uiSpeed = 115200;
|
||||
pnd = nfc_open (NULL, &ndd);
|
||||
pnd = nfc_open(NULL, &ndd);
|
||||
#endif
|
||||
|
||||
/* If specific device is wanted, i.e. a SCL3711 on USB */
|
||||
|
|
@ -113,125 +113,125 @@ main (int argc, const char *argv[])
|
|||
nfc_device_desc_t ndd;
|
||||
ndd.pcDriver = "PN533_USB";
|
||||
strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW");
|
||||
pnd = nfc_open (NULL, &ndd);
|
||||
pnd = nfc_open(NULL, &ndd);
|
||||
#endif
|
||||
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
||||
size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
|
||||
size_t szDeviceFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT);
|
||||
|
||||
if (szDeviceFound == 0) {
|
||||
printf ("No NFC device found.\n");
|
||||
printf("No NFC device found.\n");
|
||||
}
|
||||
|
||||
for (i = 0; i < szDeviceFound; i++) {
|
||||
nfc_target ant[MAX_TARGET_COUNT];
|
||||
pnd = nfc_open (NULL, connstrings[i]);
|
||||
pnd = nfc_open(NULL, connstrings[i]);
|
||||
|
||||
if (pnd == NULL) {
|
||||
ERR ("Unable to open NFC device: %s", connstrings[i]);
|
||||
ERR("Unable to open NFC device: %s", connstrings[i]);
|
||||
continue;
|
||||
}
|
||||
if (nfc_initiator_init (pnd) < 0) {
|
||||
nfc_perror (pnd, "nfc_initiator_init");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
|
||||
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
||||
nfc_modulation nm;
|
||||
|
||||
nm.nmt = NMT_ISO14443A;
|
||||
nm.nbr = NBR_106;
|
||||
// List ISO14443A targets
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_iso14443a_info (ant[n].nti.nai, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_iso14443a_info(ant[n].nti.nai, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
nm.nmt = NMT_FELICA;
|
||||
nm.nbr = NBR_212;
|
||||
// List Felica tags
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_felica_info (ant[n].nti.nfi, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_felica_info(ant[n].nti.nfi, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
nm.nbr = NBR_424;
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_felica_info (ant[n].nti.nfi, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_felica_info(ant[n].nti.nfi, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
nm.nmt = NMT_ISO14443B;
|
||||
nm.nbr = NBR_106;
|
||||
// List ISO14443B targets
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_iso14443b_info (ant[n].nti.nbi, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_iso14443b_info(ant[n].nti.nbi, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
nm.nmt = NMT_ISO14443BI;
|
||||
nm.nbr = NBR_106;
|
||||
// List ISO14443B' targets
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_iso14443bi_info (ant[n].nti.nii, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_iso14443bi_info(ant[n].nti.nii, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
nm.nmt = NMT_ISO14443B2SR;
|
||||
nm.nbr = NBR_106;
|
||||
// List ISO14443B-2 ST SRx family targets
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_iso14443b2sr_info (ant[n].nti.nsi, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_iso14443b2sr_info(ant[n].nti.nsi, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
nm.nmt = NMT_ISO14443B2CT;
|
||||
nm.nbr = NBR_106;
|
||||
// List ISO14443B-2 ASK CTx family targets
|
||||
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
|
||||
int n;
|
||||
if (verbose || (res > 0)) {
|
||||
printf ("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
printf("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_iso14443b2ct_info (ant[n].nti.nci, verbose);
|
||||
printf ("\n");
|
||||
print_nfc_iso14443b2ct_info(ant[n].nti.nci, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -243,14 +243,14 @@ main (int argc, const char *argv[])
|
|||
if (verbose || (res > 0)) {
|
||||
printf("%d Jewel passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
|
||||
}
|
||||
for(n = 0; n < res; n++) {
|
||||
print_nfc_jewel_info (ant[n].nti.nji, verbose);
|
||||
for (n = 0; n < res; n++) {
|
||||
print_nfc_jewel_info(ant[n].nti.nji, verbose);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
nfc_close (pnd);
|
||||
nfc_close(pnd);
|
||||
}
|
||||
|
||||
nfc_exit (NULL);
|
||||
nfc_exit(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ static const nfc_modulation nmMifare = {
|
|||
.nbr = NBR_106,
|
||||
};
|
||||
|
||||
static size_t num_keys = sizeof (keys) / 6;
|
||||
static size_t num_keys = sizeof(keys) / 6;
|
||||
|
||||
#define MAX_FRAME_LEN 264
|
||||
|
||||
|
|
@ -91,51 +91,51 @@ uint8_t abtUnlock1[1] = { 0x40 };
|
|||
uint8_t abtUnlock2[1] = { 0x43 };
|
||||
|
||||
static bool
|
||||
transmit_bits (const uint8_t *pbtTx, const size_t szTxBits)
|
||||
transmit_bits(const uint8_t *pbtTx, const size_t szTxBits)
|
||||
{
|
||||
// Show transmitted command
|
||||
printf ("Sent bits: ");
|
||||
print_hex_bits (pbtTx, szTxBits);
|
||||
printf("Sent bits: ");
|
||||
print_hex_bits(pbtTx, szTxBits);
|
||||
// Transmit the bit frame command, we don't use the arbitrary parity feature
|
||||
if ((szRxBits = nfc_initiator_transceive_bits (pnd, pbtTx, szTxBits, NULL, abtRx, NULL)) < 0)
|
||||
if ((szRxBits = nfc_initiator_transceive_bits(pnd, pbtTx, szTxBits, NULL, abtRx, NULL)) < 0)
|
||||
return false;
|
||||
|
||||
// Show received answer
|
||||
printf ("Received bits: ");
|
||||
print_hex_bits (abtRx, szRxBits);
|
||||
printf("Received bits: ");
|
||||
print_hex_bits(abtRx, szRxBits);
|
||||
// Succesful transfer
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
transmit_bytes (const uint8_t *pbtTx, const size_t szTx)
|
||||
transmit_bytes(const uint8_t *pbtTx, const size_t szTx)
|
||||
{
|
||||
// Show transmitted command
|
||||
printf ("Sent bits: ");
|
||||
print_hex (pbtTx, szTx);
|
||||
printf("Sent bits: ");
|
||||
print_hex(pbtTx, szTx);
|
||||
// Transmit the command bytes
|
||||
int res;
|
||||
if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
|
||||
if ((res = nfc_initiator_transceive_bytes(pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0)
|
||||
return false;
|
||||
|
||||
// Show received answer
|
||||
printf ("Received bits: ");
|
||||
print_hex (abtRx, res);
|
||||
printf("Received bits: ");
|
||||
print_hex(abtRx, res);
|
||||
// Succesful transfer
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
print_success_or_failure (bool bFailure, uint32_t *uiBlockCounter)
|
||||
print_success_or_failure(bool bFailure, uint32_t *uiBlockCounter)
|
||||
{
|
||||
printf ("%c", (bFailure) ? 'x' : '.');
|
||||
printf("%c", (bFailure) ? 'x' : '.');
|
||||
if (uiBlockCounter && !bFailure)
|
||||
*uiBlockCounter += (*uiBlockCounter < 128) ? 4 : 16;
|
||||
}
|
||||
|
||||
static bool
|
||||
is_first_block (uint32_t uiBlock)
|
||||
is_first_block(uint32_t uiBlock)
|
||||
{
|
||||
// Test if we are in the small or big sectors
|
||||
if (uiBlock < 128)
|
||||
|
|
@ -145,7 +145,7 @@ is_first_block (uint32_t uiBlock)
|
|||
}
|
||||
|
||||
static bool
|
||||
is_trailer_block (uint32_t uiBlock)
|
||||
is_trailer_block(uint32_t uiBlock)
|
||||
{
|
||||
// Test if we are in the small or big sectors
|
||||
if (uiBlock < 128)
|
||||
|
|
@ -155,7 +155,7 @@ is_trailer_block (uint32_t uiBlock)
|
|||
}
|
||||
|
||||
static uint32_t
|
||||
get_trailer_block (uint32_t uiFirstBlock)
|
||||
get_trailer_block(uint32_t uiFirstBlock)
|
||||
{
|
||||
// Test if we are in the small or big sectors
|
||||
uint32_t trailer_block = 0;
|
||||
|
|
@ -168,14 +168,14 @@ get_trailer_block (uint32_t uiFirstBlock)
|
|||
}
|
||||
|
||||
static bool
|
||||
authenticate (uint32_t uiBlock)
|
||||
authenticate(uint32_t uiBlock)
|
||||
{
|
||||
mifare_cmd mc;
|
||||
uint32_t uiTrailerBlock;
|
||||
size_t key_index;
|
||||
|
||||
// Set the authentication information (uid)
|
||||
memcpy (mp.mpa.abtUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4);
|
||||
memcpy(mp.mpa.abtUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4);
|
||||
|
||||
// Should we use key A or B?
|
||||
mc = (bUseKeyA) ? MC_AUTH_A : MC_AUTH_B;
|
||||
|
|
@ -184,29 +184,29 @@ authenticate (uint32_t uiBlock)
|
|||
if (bUseKeyFile) {
|
||||
|
||||
// Locate the trailer (with the keys) used for this sector
|
||||
uiTrailerBlock = get_trailer_block (uiBlock);
|
||||
uiTrailerBlock = get_trailer_block(uiBlock);
|
||||
|
||||
// Extract the right key from dump file
|
||||
if (bUseKeyA)
|
||||
memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6);
|
||||
memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyA, 6);
|
||||
else
|
||||
memcpy (mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, 6);
|
||||
memcpy(mp.mpa.abtKey, mtKeys.amb[uiTrailerBlock].mbt.abtKeyB, 6);
|
||||
|
||||
// Try to authenticate for the current sector
|
||||
if (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &mp))
|
||||
if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp))
|
||||
return true;
|
||||
} else {
|
||||
// Try to guess the right key
|
||||
for (key_index = 0; key_index < num_keys; key_index++) {
|
||||
memcpy (mp.mpa.abtKey, keys + (key_index * 6), 6);
|
||||
if (nfc_initiator_mifare_cmd (pnd, mc, uiBlock, &mp)) {
|
||||
memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6);
|
||||
if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) {
|
||||
if (bUseKeyA)
|
||||
memcpy (mtKeys.amb[uiBlock].mbt.abtKeyA, &mp.mpa.abtKey, 6);
|
||||
memcpy(mtKeys.amb[uiBlock].mbt.abtKeyA, &mp.mpa.abtKey, 6);
|
||||
else
|
||||
memcpy (mtKeys.amb[uiBlock].mbt.abtKeyB, &mp.mpa.abtKey, 6);
|
||||
memcpy(mtKeys.amb[uiBlock].mbt.abtKeyB, &mp.mpa.abtKey, 6);
|
||||
return true;
|
||||
}
|
||||
nfc_initiator_select_passive_target (pnd, nmMifare, nt.nti.nai.abtUid, nt.nti.nai.szUidLen, NULL);
|
||||
nfc_initiator_select_passive_target(pnd, nmMifare, nt.nti.nai.abtUid, nt.nti.nai.szUidLen, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -214,170 +214,170 @@ authenticate (uint32_t uiBlock)
|
|||
}
|
||||
|
||||
static bool
|
||||
unlock_card (void)
|
||||
unlock_card(void)
|
||||
{
|
||||
printf ("Unlocking card\n");
|
||||
printf("Unlocking card\n");
|
||||
|
||||
// Configure the CRC
|
||||
if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, false) < 0) {
|
||||
nfc_perror (pnd, "nfc_configure");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_configure");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Use raw send/receive methods
|
||||
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) {
|
||||
nfc_perror (pnd, "nfc_configure");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_configure");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
iso14443a_crc_append(abtHalt, 2);
|
||||
transmit_bytes (abtHalt, 4);
|
||||
transmit_bytes(abtHalt, 4);
|
||||
// now send unlock
|
||||
if (!transmit_bits (abtUnlock1, 7)) {
|
||||
if (!transmit_bits(abtUnlock1, 7)) {
|
||||
printf("unlock failure!\n");
|
||||
return false;
|
||||
}
|
||||
if (!transmit_bytes (abtUnlock2, 1)) {
|
||||
if (!transmit_bytes(abtUnlock2, 1)) {
|
||||
printf("unlock failure!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// reset reader
|
||||
// Configure the CRC
|
||||
if (nfc_device_set_property_bool (pnd, NP_HANDLE_CRC, true) < 0) {
|
||||
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Switch off raw send/receive methods
|
||||
if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) {
|
||||
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
read_card (int read_unlocked)
|
||||
read_card(int read_unlocked)
|
||||
{
|
||||
int32_t iBlock;
|
||||
bool bFailure = false;
|
||||
uint32_t uiReadBlocks = 0;
|
||||
|
||||
if(read_unlocked)
|
||||
if (read_unlocked)
|
||||
if (!unlock_card())
|
||||
return false;
|
||||
|
||||
|
||||
printf ("Reading out %d blocks |", uiBlocks + 1);
|
||||
printf("Reading out %d blocks |", uiBlocks + 1);
|
||||
|
||||
// Read the card from end to begin
|
||||
for (iBlock = uiBlocks; iBlock >= 0; iBlock--) {
|
||||
// Authenticate everytime we reach a trailer block
|
||||
if (is_trailer_block (iBlock)) {
|
||||
if (is_trailer_block(iBlock)) {
|
||||
// Skip this the first time, bFailure it means nothing (yet)
|
||||
if (iBlock != uiBlocks)
|
||||
print_success_or_failure (bFailure, &uiReadBlocks);
|
||||
print_success_or_failure(bFailure, &uiReadBlocks);
|
||||
|
||||
// Show if the readout went well
|
||||
if (bFailure) {
|
||||
// When a failure occured we need to redo the anti-collision
|
||||
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
printf ("!\nError: tag was removed\n");
|
||||
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
printf("!\nError: tag was removed\n");
|
||||
return false;
|
||||
}
|
||||
bFailure = false;
|
||||
}
|
||||
|
||||
fflush (stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// Try to authenticate for the current sector
|
||||
if (!read_unlocked && !authenticate (iBlock)) {
|
||||
printf ("!\nError: authentication failed for block 0x%02x\n", iBlock);
|
||||
if (!read_unlocked && !authenticate(iBlock)) {
|
||||
printf("!\nError: authentication failed for block 0x%02x\n", iBlock);
|
||||
return false;
|
||||
}
|
||||
// Try to read out the trailer
|
||||
if (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &mp)) {
|
||||
if(read_unlocked) {
|
||||
memcpy (mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
|
||||
if (nfc_initiator_mifare_cmd(pnd, MC_READ, iBlock, &mp)) {
|
||||
if (read_unlocked) {
|
||||
memcpy(mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
|
||||
} else {
|
||||
// Copy the keys over from our key dump and store the retrieved access bits
|
||||
memcpy (mtDump.amb[iBlock].mbt.abtKeyA, mtKeys.amb[iBlock].mbt.abtKeyA, 6);
|
||||
memcpy (mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4);
|
||||
memcpy (mtDump.amb[iBlock].mbt.abtKeyB, mtKeys.amb[iBlock].mbt.abtKeyB, 6);
|
||||
memcpy(mtDump.amb[iBlock].mbt.abtKeyA, mtKeys.amb[iBlock].mbt.abtKeyA, 6);
|
||||
memcpy(mtDump.amb[iBlock].mbt.abtAccessBits, mp.mpd.abtData + 6, 4);
|
||||
memcpy(mtDump.amb[iBlock].mbt.abtKeyB, mtKeys.amb[iBlock].mbt.abtKeyB, 6);
|
||||
}
|
||||
} else {
|
||||
printf ("!\nError: unable to read trailer block 0x%02x\n", iBlock);
|
||||
printf("!\nError: unable to read trailer block 0x%02x\n", iBlock);
|
||||
}
|
||||
} else {
|
||||
// Make sure a earlier readout did not fail
|
||||
if (!bFailure) {
|
||||
// Try to read out the data block
|
||||
if (nfc_initiator_mifare_cmd (pnd, MC_READ, iBlock, &mp)) {
|
||||
memcpy (mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
|
||||
if (nfc_initiator_mifare_cmd(pnd, MC_READ, iBlock, &mp)) {
|
||||
memcpy(mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16);
|
||||
} else {
|
||||
bFailure = true;
|
||||
printf ("!\nError: unable to read block 0x%02x\n", iBlock);
|
||||
printf("!\nError: unable to read block 0x%02x\n", iBlock);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
print_success_or_failure (bFailure, &uiReadBlocks);
|
||||
printf ("|\n");
|
||||
printf ("Done, %d of %d blocks read.\n", uiReadBlocks, uiBlocks + 1);
|
||||
fflush (stdout);
|
||||
print_success_or_failure(bFailure, &uiReadBlocks);
|
||||
printf("|\n");
|
||||
printf("Done, %d of %d blocks read.\n", uiReadBlocks, uiBlocks + 1);
|
||||
fflush(stdout);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
write_card (int write_block_zero)
|
||||
write_card(int write_block_zero)
|
||||
{
|
||||
uint32_t uiBlock;
|
||||
bool bFailure = false;
|
||||
uint32_t uiWriteBlocks = 0;
|
||||
|
||||
|
||||
if(write_block_zero)
|
||||
if (write_block_zero)
|
||||
if (!unlock_card())
|
||||
return false;
|
||||
|
||||
printf ("Writing %d blocks |", uiBlocks + 1);
|
||||
printf("Writing %d blocks |", uiBlocks + 1);
|
||||
// Write the card from begin to end;
|
||||
for (uiBlock = 0; uiBlock <= uiBlocks; uiBlock++) {
|
||||
// Authenticate everytime we reach the first sector of a new block
|
||||
if (is_first_block (uiBlock)) {
|
||||
if (is_first_block(uiBlock)) {
|
||||
// Skip this the first time, bFailure it means nothing (yet)
|
||||
if (uiBlock != 0)
|
||||
print_success_or_failure (bFailure, &uiWriteBlocks);
|
||||
print_success_or_failure(bFailure, &uiWriteBlocks);
|
||||
|
||||
// Show if the readout went well
|
||||
if (bFailure) {
|
||||
// When a failure occured we need to redo the anti-collision
|
||||
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
printf ("!\nError: tag was removed\n");
|
||||
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
printf("!\nError: tag was removed\n");
|
||||
return false;
|
||||
}
|
||||
bFailure = false;
|
||||
}
|
||||
|
||||
fflush (stdout);
|
||||
fflush(stdout);
|
||||
|
||||
// Try to authenticate for the current sector
|
||||
if (!write_block_zero && !authenticate (uiBlock)) {
|
||||
printf ("!\nError: authentication failed for block %02x\n", uiBlock);
|
||||
if (!write_block_zero && !authenticate(uiBlock)) {
|
||||
printf("!\nError: authentication failed for block %02x\n", uiBlock);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_trailer_block (uiBlock)) {
|
||||
if (is_trailer_block(uiBlock)) {
|
||||
// Copy the keys over from our key dump and store the retrieved access bits
|
||||
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbt.abtKeyA, 6);
|
||||
memcpy (mp.mpd.abtData + 6, mtDump.amb[uiBlock].mbt.abtAccessBits, 4);
|
||||
memcpy (mp.mpd.abtData + 10, mtDump.amb[uiBlock].mbt.abtKeyB, 6);
|
||||
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbt.abtKeyA, 6);
|
||||
memcpy(mp.mpd.abtData + 6, mtDump.amb[uiBlock].mbt.abtAccessBits, 4);
|
||||
memcpy(mp.mpd.abtData + 10, mtDump.amb[uiBlock].mbt.abtKeyB, 6);
|
||||
|
||||
// Try to write the trailer
|
||||
if (nfc_initiator_mifare_cmd (pnd, MC_WRITE, uiBlock, &mp) == false) {
|
||||
printf ("failed to write trailer block %d \n", uiBlock);
|
||||
if (nfc_initiator_mifare_cmd(pnd, MC_WRITE, uiBlock, &mp) == false) {
|
||||
printf("failed to write trailer block %d \n", uiBlock);
|
||||
bFailure = true;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -389,23 +389,23 @@ write_card (int write_block_zero)
|
|||
// Make sure a earlier write did not fail
|
||||
if (!bFailure) {
|
||||
// Try to write the data block
|
||||
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16);
|
||||
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, 16);
|
||||
// do not write a block 0 with incorrect BCC - card will be made invalid!
|
||||
if (uiBlock == 0) {
|
||||
if((mp.mpd.abtData[0] ^ mp.mpd.abtData[1] ^ mp.mpd.abtData[2] ^ mp.mpd.abtData[3] ^ mp.mpd.abtData[4]) != 0x00) {
|
||||
printf ("!\nError: incorrect BCC in MFD file!\n");
|
||||
if ((mp.mpd.abtData[0] ^ mp.mpd.abtData[1] ^ mp.mpd.abtData[2] ^ mp.mpd.abtData[3] ^ mp.mpd.abtData[4]) != 0x00) {
|
||||
printf("!\nError: incorrect BCC in MFD file!\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, uiBlock, &mp))
|
||||
if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, uiBlock, &mp))
|
||||
bFailure = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
print_success_or_failure (bFailure, &uiWriteBlocks);
|
||||
printf ("|\n");
|
||||
printf ("Done, %d of %d blocks written.\n", uiWriteBlocks, uiBlocks + 1);
|
||||
fflush (stdout);
|
||||
print_success_or_failure(bFailure, &uiWriteBlocks);
|
||||
printf("|\n");
|
||||
printf("Done, %d of %d blocks written.\n", uiWriteBlocks, uiBlocks + 1);
|
||||
fflush(stdout);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -417,21 +417,21 @@ typedef enum {
|
|||
} action_t;
|
||||
|
||||
static void
|
||||
print_usage (const char *pcProgramName)
|
||||
print_usage(const char *pcProgramName)
|
||||
{
|
||||
printf ("Usage: ");
|
||||
printf ("%s r|R|w|W a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName);
|
||||
printf (" r|R|w|W - Perform read from (r) or unlocked read from (R) or write to (w) or unlocked write to (W) card\n");
|
||||
printf (" *** note that unlocked write will attempt to overwrite block 0 including UID\n");
|
||||
printf (" *** unlocked read does not require authentication and will reveal A and B keys\n");
|
||||
printf (" *** unlocking only works with special Mifare 1K cards (Chinese clones)\n");
|
||||
printf (" a|b - Use A or B keys for action\n");
|
||||
printf (" <dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
|
||||
printf (" <keys.mfd> - MiFare Dump (MFD) that contain the keys (optional)\n");
|
||||
printf("Usage: ");
|
||||
printf("%s r|R|w|W a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName);
|
||||
printf(" r|R|w|W - Perform read from (r) or unlocked read from (R) or write to (w) or unlocked write to (W) card\n");
|
||||
printf(" *** note that unlocked write will attempt to overwrite block 0 including UID\n");
|
||||
printf(" *** unlocked read does not require authentication and will reveal A and B keys\n");
|
||||
printf(" *** unlocking only works with special Mifare 1K cards (Chinese clones)\n");
|
||||
printf(" a|b - Use A or B keys for action\n");
|
||||
printf(" <dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
|
||||
printf(" <keys.mfd> - MiFare Dump (MFD) that contain the keys (optional)\n");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, const char *argv[])
|
||||
main(int argc, const char *argv[])
|
||||
{
|
||||
action_t atAction = ACTION_USAGE;
|
||||
uint8_t *pbtUID;
|
||||
|
|
@ -440,107 +440,107 @@ main (int argc, const char *argv[])
|
|||
int unlock = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
const char *command = argv[1];
|
||||
|
||||
if (strcmp (command, "r") == 0 || strcmp (command, "R") == 0) {
|
||||
if (strcmp(command, "r") == 0 || strcmp(command, "R") == 0) {
|
||||
if (argc < 4) {
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
atAction = ACTION_READ;
|
||||
if (strcmp (command, "R") == 0)
|
||||
if (strcmp(command, "R") == 0)
|
||||
unlock = 1;
|
||||
bUseKeyA = tolower ((int) ((unsigned char) * (argv[2]))) == 'a';
|
||||
bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a';
|
||||
bUseKeyFile = (argc > 4);
|
||||
} else if (strcmp (command, "w") == 0 || strcmp (command, "W") == 0) {
|
||||
} else if (strcmp(command, "w") == 0 || strcmp(command, "W") == 0) {
|
||||
if (argc < 4) {
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
atAction = ACTION_WRITE;
|
||||
if (strcmp (command, "W") == 0)
|
||||
if (strcmp(command, "W") == 0)
|
||||
unlock = 1;
|
||||
bUseKeyA = tolower ((int) ((unsigned char) * (argv[2]))) == 'a';
|
||||
bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a';
|
||||
bUseKeyFile = (argc > 4);
|
||||
}
|
||||
|
||||
switch (atAction) {
|
||||
case ACTION_USAGE:
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
case ACTION_READ:
|
||||
case ACTION_WRITE:
|
||||
if (bUseKeyFile) {
|
||||
pfKeys = fopen (argv[4], "rb");
|
||||
pfKeys = fopen(argv[4], "rb");
|
||||
if (pfKeys == NULL) {
|
||||
printf ("Could not open keys file: %s\n", argv[4]);
|
||||
exit (EXIT_FAILURE);
|
||||
printf("Could not open keys file: %s\n", argv[4]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (fread (&mtKeys, 1, sizeof (mtKeys), pfKeys) != sizeof (mtKeys)) {
|
||||
printf ("Could not read keys file: %s\n", argv[4]);
|
||||
fclose (pfKeys);
|
||||
exit (EXIT_FAILURE);
|
||||
if (fread(&mtKeys, 1, sizeof(mtKeys), pfKeys) != sizeof(mtKeys)) {
|
||||
printf("Could not read keys file: %s\n", argv[4]);
|
||||
fclose(pfKeys);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fclose (pfKeys);
|
||||
fclose(pfKeys);
|
||||
}
|
||||
|
||||
if (atAction == ACTION_READ) {
|
||||
memset (&mtDump, 0x00, sizeof (mtDump));
|
||||
memset(&mtDump, 0x00, sizeof(mtDump));
|
||||
} else {
|
||||
pfDump = fopen (argv[3], "rb");
|
||||
pfDump = fopen(argv[3], "rb");
|
||||
|
||||
if (pfDump == NULL) {
|
||||
printf ("Could not open dump file: %s\n", argv[3]);
|
||||
exit (EXIT_FAILURE);
|
||||
printf("Could not open dump file: %s\n", argv[3]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
||||
printf ("Could not read dump file: %s\n", argv[3]);
|
||||
fclose (pfDump);
|
||||
exit (EXIT_FAILURE);
|
||||
if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
|
||||
printf("Could not read dump file: %s\n", argv[3]);
|
||||
fclose(pfDump);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fclose (pfDump);
|
||||
fclose(pfDump);
|
||||
}
|
||||
// printf("Successfully opened required files\n");
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
// Try to open the NFC reader
|
||||
pnd = nfc_open (NULL, NULL);
|
||||
pnd = nfc_open(NULL, NULL);
|
||||
if (pnd == NULL) {
|
||||
printf ("Error opening NFC reader\n");
|
||||
exit (EXIT_FAILURE);
|
||||
printf("Error opening NFC reader\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (nfc_initiator_init (pnd) < 0) {
|
||||
nfc_perror (pnd, "nfc_initiator_init");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
exit(EXIT_FAILURE);
|
||||
};
|
||||
|
||||
// Let the reader only try once to find a tag
|
||||
if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) {
|
||||
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.
|
||||
nfc_device_set_property_bool (pnd, NP_AUTO_ISO14443_4, false);
|
||||
nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false);
|
||||
|
||||
printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd));
|
||||
printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
||||
// Try to find a MIFARE Classic tag
|
||||
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
printf ("Error: no tag was found\n");
|
||||
nfc_close (pnd);
|
||||
nfc_exit (NULL);
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
printf("Error: no tag was found\n");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Test if we are dealing with a MIFARE compatible tag
|
||||
if ((nt.nti.nai.btSak & 0x08) == 0) {
|
||||
printf ("Warning: tag is probably not a MFC!\n");
|
||||
printf("Warning: tag is probably not a MFC!\n");
|
||||
}
|
||||
|
||||
// Get the info from the current tag
|
||||
|
|
@ -548,15 +548,15 @@ main (int argc, const char *argv[])
|
|||
|
||||
if (bUseKeyFile) {
|
||||
uint8_t fileUid[4];
|
||||
memcpy (fileUid, mtKeys.amb[0].mbm.abtUID, 4);
|
||||
memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4);
|
||||
// Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes
|
||||
if (memcmp (pbtUID, fileUid, 4) != 0) {
|
||||
printf ("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n",
|
||||
fileUid[0], fileUid[1], fileUid[2], fileUid[3]);
|
||||
if (memcmp(pbtUID, fileUid, 4) != 0) {
|
||||
printf("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n",
|
||||
fileUid[0], fileUid[1], fileUid[2], fileUid[3]);
|
||||
}
|
||||
}
|
||||
printf ("Found MIFARE Classic card:\n");
|
||||
print_nfc_iso14443a_info (nt.nti.nai, false);
|
||||
printf("Found MIFARE Classic card:\n");
|
||||
print_nfc_iso14443a_info(nt.nti.nai, false);
|
||||
|
||||
// Guessing size
|
||||
if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02)
|
||||
|
|
@ -569,32 +569,32 @@ main (int argc, const char *argv[])
|
|||
// 1K
|
||||
// TODO: for MFP it is 0x7f (2K) but how to be sure it's a MFP? Try to get RATS?
|
||||
uiBlocks = 0x3f;
|
||||
printf ("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16);
|
||||
printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16);
|
||||
|
||||
if (atAction == ACTION_READ) {
|
||||
if (read_card (unlock)) {
|
||||
printf ("Writing data to file: %s ...", argv[3]);
|
||||
fflush (stdout);
|
||||
pfDump = fopen (argv[3], "wb");
|
||||
if (read_card(unlock)) {
|
||||
printf("Writing data to file: %s ...", argv[3]);
|
||||
fflush(stdout);
|
||||
pfDump = fopen(argv[3], "wb");
|
||||
if (pfDump == NULL) {
|
||||
printf ("Could not open dump file: %s\n", argv[3]);
|
||||
exit (EXIT_FAILURE);
|
||||
printf("Could not open dump file: %s\n", argv[3]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
||||
printf ("\nCould not write to file: %s\n", argv[3]);
|
||||
exit (EXIT_FAILURE);
|
||||
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
|
||||
printf("\nCould not write to file: %s\n", argv[3]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf ("Done.\n");
|
||||
fclose (pfDump);
|
||||
printf("Done.\n");
|
||||
fclose(pfDump);
|
||||
}
|
||||
} else if (atAction == ACTION_WRITE) {
|
||||
write_card (unlock);
|
||||
write_card(unlock);
|
||||
}
|
||||
|
||||
nfc_close (pnd);
|
||||
nfc_close(pnd);
|
||||
break;
|
||||
};
|
||||
|
||||
nfc_exit (NULL);
|
||||
exit (EXIT_SUCCESS);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,45 +63,45 @@ static const nfc_modulation nmMifare = {
|
|||
};
|
||||
|
||||
static void
|
||||
print_success_or_failure (bool bFailure, uint32_t * uiCounter)
|
||||
print_success_or_failure(bool bFailure, uint32_t * uiCounter)
|
||||
{
|
||||
printf ("%c", (bFailure) ? 'x' : '.');
|
||||
printf("%c", (bFailure) ? 'x' : '.');
|
||||
if (uiCounter)
|
||||
*uiCounter += (bFailure) ? 0 : 1;
|
||||
}
|
||||
|
||||
static bool
|
||||
read_card (void)
|
||||
read_card(void)
|
||||
{
|
||||
uint32_t page;
|
||||
bool bFailure = false;
|
||||
uint32_t uiReadedPages = 0;
|
||||
|
||||
printf ("Reading %d pages |", uiBlocks + 1);
|
||||
printf("Reading %d pages |", uiBlocks + 1);
|
||||
|
||||
for (page = 0; page <= uiBlocks; page += 4) {
|
||||
// Try to read out the data block
|
||||
if (nfc_initiator_mifare_cmd (pnd, MC_READ, page, &mp)) {
|
||||
memcpy (mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
|
||||
if (nfc_initiator_mifare_cmd(pnd, MC_READ, page, &mp)) {
|
||||
memcpy(mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
|
||||
} else {
|
||||
bFailure = true;
|
||||
break;
|
||||
}
|
||||
|
||||
print_success_or_failure (bFailure, &uiReadedPages);
|
||||
print_success_or_failure (bFailure, &uiReadedPages);
|
||||
print_success_or_failure (bFailure, &uiReadedPages);
|
||||
print_success_or_failure (bFailure, &uiReadedPages);
|
||||
print_success_or_failure(bFailure, &uiReadedPages);
|
||||
print_success_or_failure(bFailure, &uiReadedPages);
|
||||
print_success_or_failure(bFailure, &uiReadedPages);
|
||||
print_success_or_failure(bFailure, &uiReadedPages);
|
||||
}
|
||||
printf ("|\n");
|
||||
printf ("Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
|
||||
fflush (stdout);
|
||||
printf("|\n");
|
||||
printf("Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
|
||||
fflush(stdout);
|
||||
|
||||
return (!bFailure);
|
||||
}
|
||||
|
||||
static bool
|
||||
write_card (void)
|
||||
write_card(void)
|
||||
{
|
||||
uint32_t uiBlock = 0;
|
||||
bool bFailure = false;
|
||||
|
|
@ -112,38 +112,38 @@ write_card (void)
|
|||
bool write_otp;
|
||||
bool write_lock;
|
||||
|
||||
printf ("Write OTP bytes ? [yN] ");
|
||||
if (!fgets (buffer, BUFSIZ, stdin)) {
|
||||
ERR ("Unable to read standard input.");
|
||||
printf("Write OTP bytes ? [yN] ");
|
||||
if (!fgets(buffer, BUFSIZ, stdin)) {
|
||||
ERR("Unable to read standard input.");
|
||||
}
|
||||
write_otp = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
||||
printf ("Write Lock bytes ? [yN] ");
|
||||
if (!fgets (buffer, BUFSIZ, stdin)) {
|
||||
ERR ("Unable to read standard input.");
|
||||
printf("Write Lock bytes ? [yN] ");
|
||||
if (!fgets(buffer, BUFSIZ, stdin)) {
|
||||
ERR("Unable to read standard input.");
|
||||
}
|
||||
write_lock = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
||||
|
||||
printf ("Writing %d pages |", uiBlocks + 1);
|
||||
printf("Writing %d pages |", uiBlocks + 1);
|
||||
/* We need to skip 2 first pages. */
|
||||
printf ("ss");
|
||||
printf("ss");
|
||||
uiSkippedPages = 2;
|
||||
|
||||
for (int page = 0x2; page <= 0xF; page++) {
|
||||
if ((page == 0x2) && (!write_lock)) {
|
||||
printf ("s");
|
||||
printf("s");
|
||||
uiSkippedPages++;
|
||||
continue;
|
||||
}
|
||||
if ((page == 0x3) && (!write_otp)) {
|
||||
printf ("s");
|
||||
printf("s");
|
||||
uiSkippedPages++;
|
||||
continue;
|
||||
}
|
||||
// Show if the readout went well
|
||||
if (bFailure) {
|
||||
// When a failure occured we need to redo the anti-collision
|
||||
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
ERR ("tag was removed");
|
||||
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
ERR("tag was removed");
|
||||
return false;
|
||||
}
|
||||
bFailure = false;
|
||||
|
|
@ -153,123 +153,123 @@ write_card (void)
|
|||
// page (4 bytes). The Ultralight-specific Write command only
|
||||
// writes one page at a time.
|
||||
uiBlock = page / 4;
|
||||
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
|
||||
if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, page, &mp))
|
||||
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
|
||||
if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp))
|
||||
bFailure = true;
|
||||
|
||||
print_success_or_failure (bFailure, &uiWritenPages);
|
||||
print_success_or_failure(bFailure, &uiWritenPages);
|
||||
}
|
||||
printf ("|\n");
|
||||
printf ("Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages);
|
||||
printf("|\n");
|
||||
printf("Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, const char *argv[])
|
||||
main(int argc, const char *argv[])
|
||||
{
|
||||
bool bReadAction;
|
||||
FILE *pfDump;
|
||||
|
||||
if (argc < 3) {
|
||||
printf ("\n");
|
||||
printf ("%s r|w <dump.mfd>\n", argv[0]);
|
||||
printf ("\n");
|
||||
printf ("r|w - Perform read from or write to card\n");
|
||||
printf ("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
|
||||
printf ("\n");
|
||||
printf("\n");
|
||||
printf("%s r|w <dump.mfd>\n", argv[0]);
|
||||
printf("\n");
|
||||
printf("r|w - Perform read from or write to card\n");
|
||||
printf("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
|
||||
printf("\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
DBG ("\nChecking arguments and settings\n");
|
||||
DBG("\nChecking arguments and settings\n");
|
||||
|
||||
bReadAction = tolower ((int) ((unsigned char) * (argv[1])) == 'r');
|
||||
bReadAction = tolower((int)((unsigned char) * (argv[1])) == 'r');
|
||||
|
||||
if (bReadAction) {
|
||||
memset (&mtDump, 0x00, sizeof (mtDump));
|
||||
memset(&mtDump, 0x00, sizeof(mtDump));
|
||||
} else {
|
||||
pfDump = fopen (argv[2], "rb");
|
||||
pfDump = fopen(argv[2], "rb");
|
||||
|
||||
if (pfDump == NULL) {
|
||||
ERR ("Could not open dump file: %s\n", argv[2]);
|
||||
ERR("Could not open dump file: %s\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
||||
ERR ("Could not read from dump file: %s\n", argv[2]);
|
||||
fclose (pfDump);
|
||||
if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
|
||||
ERR("Could not read from dump file: %s\n", argv[2]);
|
||||
fclose(pfDump);
|
||||
return 1;
|
||||
}
|
||||
fclose (pfDump);
|
||||
fclose(pfDump);
|
||||
}
|
||||
DBG ("Successfully opened the dump file\n");
|
||||
DBG("Successfully opened the dump file\n");
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
// Try to open the NFC device
|
||||
pnd = nfc_open (NULL, NULL);
|
||||
pnd = nfc_open(NULL, NULL);
|
||||
if (pnd == NULL) {
|
||||
ERR ("Error opening NFC device\n");
|
||||
ERR("Error opening NFC device\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nfc_initiator_init (pnd) < 0) {
|
||||
nfc_perror (pnd, "nfc_initiator_init");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Let the device only try once to find a tag
|
||||
if (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0) {
|
||||
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
|
||||
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
||||
// Try to find a MIFARE Ultralight tag
|
||||
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
ERR ("no tag was found\n");
|
||||
nfc_close (pnd);
|
||||
nfc_exit (NULL);
|
||||
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
|
||||
ERR("no tag was found\n");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(NULL);
|
||||
return 1;
|
||||
}
|
||||
// Test if we are dealing with a MIFARE compatible tag
|
||||
|
||||
if (nt.nti.nai.abtAtqa[1] != 0x44) {
|
||||
ERR ("tag is not a MIFARE Ultralight card\n");
|
||||
nfc_close (pnd);
|
||||
nfc_exit (NULL);
|
||||
ERR("tag is not a MIFARE Ultralight card\n");
|
||||
nfc_close(pnd);
|
||||
nfc_exit(NULL);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// Get the info from the current tag
|
||||
printf ("Found MIFARE Ultralight card with UID: ");
|
||||
printf("Found MIFARE Ultralight card with UID: ");
|
||||
size_t szPos;
|
||||
for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) {
|
||||
printf ("%02x", nt.nti.nai.abtUid[szPos]);
|
||||
printf("%02x", nt.nti.nai.abtUid[szPos]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
if (bReadAction) {
|
||||
if (read_card ()) {
|
||||
printf ("Writing data to file: %s ... ", argv[2]);
|
||||
fflush (stdout);
|
||||
pfDump = fopen (argv[2], "wb");
|
||||
if (read_card()) {
|
||||
printf("Writing data to file: %s ... ", argv[2]);
|
||||
fflush(stdout);
|
||||
pfDump = fopen(argv[2], "wb");
|
||||
if (pfDump == NULL) {
|
||||
printf ("Could not open file: %s\n", argv[2]);
|
||||
printf("Could not open file: %s\n", argv[2]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
||||
printf ("Could not write to file: %s\n", argv[2]);
|
||||
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
|
||||
printf("Could not write to file: %s\n", argv[2]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fclose (pfDump);
|
||||
printf ("Done.\n");
|
||||
fclose(pfDump);
|
||||
printf("Done.\n");
|
||||
}
|
||||
} else {
|
||||
write_card ();
|
||||
write_card();
|
||||
}
|
||||
|
||||
nfc_close (pnd);
|
||||
nfc_exit (NULL);
|
||||
nfc_close(pnd);
|
||||
nfc_exit(NULL);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,61 +60,61 @@
|
|||
static nfc_device *pnd;
|
||||
|
||||
static void
|
||||
print_usage (const char* progname)
|
||||
print_usage(const char* progname)
|
||||
{
|
||||
printf ("usage: %s [-v]\n", progname);
|
||||
printf (" -v\t verbose display\n");
|
||||
printf("usage: %s [-v]\n", progname);
|
||||
printf(" -v\t verbose display\n");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, const char *argv[])
|
||||
main(int argc, const char *argv[])
|
||||
{
|
||||
(void) argc;
|
||||
const char *acLibnfcVersion;
|
||||
size_t i;
|
||||
bool verbose = false;
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
// Display libnfc version
|
||||
acLibnfcVersion = nfc_version ();
|
||||
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
acLibnfcVersion = nfc_version();
|
||||
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
if (argc != 1) {
|
||||
if ((argc == 2) && (0 == strcmp ("-v", argv[1]))) {
|
||||
if ((argc == 2) && (0 == strcmp("-v", argv[1]))) {
|
||||
verbose = true;
|
||||
} else {
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBUSB
|
||||
# ifdef DEBUG
|
||||
usb_set_debug (4);
|
||||
usb_set_debug(4);
|
||||
# endif
|
||||
#endif
|
||||
|
||||
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
||||
size_t szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
|
||||
size_t szDeviceFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT);
|
||||
|
||||
int res = EXIT_FAILURE;
|
||||
if (szDeviceFound == 0) {
|
||||
printf ("No NFC device found.\n");
|
||||
printf("No NFC device found.\n");
|
||||
goto bye;
|
||||
}
|
||||
|
||||
printf ("%d NFC device(s) found:\n", (int)szDeviceFound);
|
||||
printf("%d NFC device(s) found:\n", (int)szDeviceFound);
|
||||
char strinfo[1024];
|
||||
for (i = 0; i < szDeviceFound; i++) {
|
||||
pnd = nfc_open (NULL, connstrings[i]);
|
||||
pnd = nfc_open(NULL, connstrings[i]);
|
||||
if (pnd != NULL) {
|
||||
printf ("- %s:\n %s\n", nfc_device_get_name (pnd), nfc_device_get_connstring (pnd));
|
||||
printf("- %s:\n %s\n", nfc_device_get_name(pnd), nfc_device_get_connstring(pnd));
|
||||
if (verbose) {
|
||||
if (nfc_device_get_information_about (pnd, strinfo, sizeof(strinfo)) >= 0) {
|
||||
printf ("%s", strinfo);
|
||||
if (nfc_device_get_information_about(pnd, strinfo, sizeof(strinfo)) >= 0) {
|
||||
printf("%s", strinfo);
|
||||
}
|
||||
}
|
||||
nfc_close (pnd);
|
||||
nfc_close(pnd);
|
||||
} else {
|
||||
printf("nfc_open failed for %s\n", connstrings[i]);
|
||||
}
|
||||
|
|
@ -122,6 +122,6 @@ main (int argc, const char *argv[])
|
|||
res = EXIT_SUCCESS;
|
||||
|
||||
bye:
|
||||
nfc_exit (NULL);
|
||||
nfc_exit(NULL);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,18 +60,18 @@ static nfc_device *pnd;
|
|||
static void
|
||||
print_usage(char *progname)
|
||||
{
|
||||
fprintf (stderr, "usage: %s -o FILE\n", progname);
|
||||
fprintf (stderr, "\nOptions:\n");
|
||||
fprintf (stderr, " -o Extract NDEF message if available in FILE\n");
|
||||
fprintf(stderr, "usage: %s -o FILE\n", progname);
|
||||
fprintf(stderr, "\nOptions:\n");
|
||||
fprintf(stderr, " -o Extract NDEF message if available in FILE\n");
|
||||
}
|
||||
|
||||
static void stop_select (int sig)
|
||||
static void stop_select(int sig)
|
||||
{
|
||||
(void) sig;
|
||||
if (pnd)
|
||||
nfc_abort_command (pnd);
|
||||
nfc_abort_command(pnd);
|
||||
else
|
||||
exit (EXIT_FAILURE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -80,13 +80,13 @@ build_felica_frame(const nfc_felica_info nfi, const uint8_t command, const uint8
|
|||
frame[0] = 1 + 1 + 8 + payload_len;
|
||||
*frame_len = frame[0];
|
||||
frame[1] = command;
|
||||
memcpy (frame + 2, nfi.abtId, 8);
|
||||
memcpy (frame + 10, payload, payload_len);
|
||||
memcpy(frame + 2, nfi.abtId, 8);
|
||||
memcpy(frame + 10, payload, payload_len);
|
||||
}
|
||||
|
||||
#define CHECK 0x06
|
||||
static int
|
||||
nfc_forum_tag_type3_check (nfc_device *dev, const nfc_target nt, const uint16_t block, const uint8_t block_count, uint8_t *data, size_t *data_len)
|
||||
nfc_forum_tag_type3_check(nfc_device *dev, const nfc_target nt, const uint16_t block, const uint8_t block_count, uint8_t *data, size_t *data_len)
|
||||
{
|
||||
uint8_t payload[1024] = {
|
||||
1, // Services
|
||||
|
|
@ -109,11 +109,11 @@ nfc_forum_tag_type3_check (nfc_device *dev, const nfc_target nt, const uint16_t
|
|||
|
||||
uint8_t frame[1024];
|
||||
size_t frame_len = sizeof(frame);
|
||||
build_felica_frame (nt.nti.nfi, CHECK, payload, payload_len, frame, &frame_len);
|
||||
build_felica_frame(nt.nti.nfi, CHECK, payload, payload_len, frame, &frame_len);
|
||||
|
||||
uint8_t rx[1024];
|
||||
int res;
|
||||
if ((res = nfc_initiator_transceive_bytes (dev, frame, frame_len, rx, sizeof(rx), 0)) < 0) {
|
||||
if ((res = nfc_initiator_transceive_bytes(dev, frame, frame_len, rx, sizeof(rx), 0)) < 0) {
|
||||
return res;
|
||||
}
|
||||
const int res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS
|
||||
|
|
@ -130,7 +130,7 @@ nfc_forum_tag_type3_check (nfc_device *dev, const nfc_target nt, const uint16_t
|
|||
// Command return does not match
|
||||
return -1;
|
||||
}
|
||||
if (0 != memcmp (&rx[2], nt.nti.nfi.abtId, 8)) {
|
||||
if (0 != memcmp(&rx[2], nt.nti.nfi.abtId, 8)) {
|
||||
// NFCID2 does not match
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -138,12 +138,12 @@ nfc_forum_tag_type3_check (nfc_device *dev, const nfc_target nt, const uint16_t
|
|||
const uint8_t status_flag2 = rx[11];
|
||||
if ((status_flag1) || (status_flag2)) {
|
||||
// Felica card's error
|
||||
fprintf (stderr, "Status bytes: %02x, %02x\n", status_flag1, status_flag2);
|
||||
fprintf(stderr, "Status bytes: %02x, %02x\n", status_flag1, status_flag2);
|
||||
return -1;
|
||||
}
|
||||
// const uint8_t res_block_count = res[12];
|
||||
*data_len = res - res_overhead + 1; // +1 => block count is stored on 1 byte
|
||||
memcpy (data, &rx[res_overhead + 1], *data_len);
|
||||
memcpy(data, &rx[res_overhead + 1], *data_len);
|
||||
return *data_len;
|
||||
}
|
||||
|
||||
|
|
@ -155,91 +155,91 @@ main(int argc, char *argv[])
|
|||
|
||||
int ch;
|
||||
char *ndef_output = NULL;
|
||||
while ((ch = getopt (argc, argv, "ho:")) != -1) {
|
||||
while ((ch = getopt(argc, argv, "ho:")) != -1) {
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
print_usage(argv[0]);
|
||||
exit (EXIT_SUCCESS);
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
case 'o':
|
||||
ndef_output = optarg;
|
||||
break;
|
||||
case '?':
|
||||
if (optopt == 'o')
|
||||
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
|
||||
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
|
||||
default:
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (ndef_output == NULL) {
|
||||
print_usage (argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
print_usage(argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
FILE* message_stream = NULL;
|
||||
FILE* ndef_stream = NULL;
|
||||
|
||||
if ((strlen (ndef_output) == 1) && (ndef_output[0] == '-')) {
|
||||
if ((strlen(ndef_output) == 1) && (ndef_output[0] == '-')) {
|
||||
message_stream = stderr;
|
||||
ndef_stream = stdout;
|
||||
} else {
|
||||
message_stream = stdout;
|
||||
ndef_stream = fopen(ndef_output, "wb");
|
||||
if (!ndef_stream) {
|
||||
fprintf (stderr, "Could not open file %s.\n", ndef_output);
|
||||
exit (EXIT_FAILURE);
|
||||
fprintf(stderr, "Could not open file %s.\n", ndef_output);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
pnd = nfc_open (NULL, NULL);
|
||||
pnd = nfc_open(NULL, NULL);
|
||||
|
||||
if (pnd == NULL) {
|
||||
ERR("Unable to open NFC device");
|
||||
exit (EXIT_FAILURE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fprintf (message_stream, "NFC device: %s opened\n", nfc_device_get_name (pnd));
|
||||
fprintf(message_stream, "NFC device: %s opened\n", nfc_device_get_name(pnd));
|
||||
|
||||
nfc_modulation nm = {
|
||||
.nmt = NMT_FELICA,
|
||||
.nbr = NBR_212,
|
||||
};
|
||||
|
||||
signal (SIGINT, stop_select);
|
||||
signal(SIGINT, stop_select);
|
||||
|
||||
nfc_target nt;
|
||||
|
||||
if (nfc_initiator_init (pnd) < 0) {
|
||||
nfc_perror (pnd, "nfc_initiator_init");
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_init(pnd) < 0) {
|
||||
nfc_perror(pnd, "nfc_initiator_init");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fprintf (message_stream, "Place your NFC Forum Tag Type 3 in the field...\n");
|
||||
fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the field...\n");
|
||||
|
||||
int error = EXIT_SUCCESS;
|
||||
// Polling payload (SENSF_REQ) must be present (see NFC Digital Protol)
|
||||
const uint8_t *pbtSensfReq = (uint8_t*)"\x00\xff\xff\x01\x00";
|
||||
if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) < 0) {
|
||||
nfc_perror (pnd, "nfc_initiator_select_passive_target");
|
||||
nfc_perror(pnd, "nfc_initiator_select_passive_target");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Check if System Code equals 0x12fc
|
||||
const uint8_t abtNfcForumSysCode[] = { 0x12, 0xfc };
|
||||
if (0 != memcmp (nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
|
||||
if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
|
||||
// Retry with special polling
|
||||
const uint8_t *pbtSensfReqNfcForum = (uint8_t*)"\x00\x12\xfc\x01\x00";
|
||||
if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) < 0) {
|
||||
nfc_perror (pnd, "nfc_initiator_select_passive_target");
|
||||
nfc_perror(pnd, "nfc_initiator_select_passive_target");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
// Check again if System Code equals 0x12fc
|
||||
if (0 != memcmp (nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
|
||||
fprintf (stderr, "Tag is not NFC Forum Tag Type 3 compliant.\n");
|
||||
if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
|
||||
fprintf(stderr, "Tag is not NFC Forum Tag Type 3 compliant.\n");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -247,8 +247,8 @@ main(int argc, char *argv[])
|
|||
|
||||
//print_nfc_felica_info(nt.nti.nfi, true);
|
||||
|
||||
if ((nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false) < 0)) {
|
||||
nfc_perror (pnd, "nfc_device_set_property_bool");
|
||||
if ((nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0)) {
|
||||
nfc_perror(pnd, "nfc_device_set_property_bool");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -257,21 +257,21 @@ main(int argc, char *argv[])
|
|||
size_t data_len = sizeof(data);
|
||||
int len;
|
||||
|
||||
if (0 >= (len = nfc_forum_tag_type3_check (pnd, nt, 0, 1, data, &data_len))) {
|
||||
nfc_perror (pnd, "nfc_forum_tag_type3_check");
|
||||
if (0 >= (len = nfc_forum_tag_type3_check(pnd, nt, 0, 1, data, &data_len))) {
|
||||
nfc_perror(pnd, "nfc_forum_tag_type3_check");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
const int ndef_major_version = (data[0] & 0xf0) >> 4;
|
||||
const int ndef_minor_version = (data[0] & 0x0f);
|
||||
fprintf (message_stream, "NDEF Mapping version: %d.%d\n", ndef_major_version, ndef_minor_version);
|
||||
fprintf(message_stream, "NDEF Mapping version: %d.%d\n", ndef_major_version, ndef_minor_version);
|
||||
|
||||
const int available_block_count = (data[3] << 8) + data[4];
|
||||
fprintf (message_stream, "NFC Forum Tag Type 3 capacity: %d bytes\n", available_block_count * 16);
|
||||
fprintf(message_stream, "NFC Forum Tag Type 3 capacity: %d bytes\n", available_block_count * 16);
|
||||
|
||||
uint32_t ndef_data_len = (data[11] << 16) + (data[12] << 8) + data[13];
|
||||
fprintf (message_stream, "NDEF data length: %d bytes\n", ndef_data_len);
|
||||
fprintf(message_stream, "NDEF data length: %d bytes\n", ndef_data_len);
|
||||
|
||||
uint16_t ndef_calculated_checksum = 0;
|
||||
for (size_t n = 0; n < 14; n++)
|
||||
|
|
@ -279,13 +279,13 @@ main(int argc, char *argv[])
|
|||
|
||||
const uint16_t ndef_checksum = (data[14] << 8) + data[15];
|
||||
if (ndef_calculated_checksum != ndef_checksum) {
|
||||
fprintf (stderr, "NDEF CRC does not match with calculated one\n");
|
||||
fprintf(stderr, "NDEF CRC does not match with calculated one\n");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!ndef_data_len) {
|
||||
fprintf (stderr, "Empty NFC Forum Tag Type 3\n");
|
||||
fprintf(stderr, "Empty NFC Forum Tag Type 3\n");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -296,24 +296,24 @@ main(int argc, char *argv[])
|
|||
data_len = 0;
|
||||
for (uint16_t b = 0; b < (block_count_to_check / block_max_per_check); b += block_max_per_check) {
|
||||
size_t size = sizeof(data) - data_len;
|
||||
if(!nfc_forum_tag_type3_check (pnd, nt, 1 + b, MIN(block_max_per_check, (block_count_to_check - (b * block_max_per_check))), data + data_len, &size)) {
|
||||
nfc_perror (pnd, "nfc_forum_tag_type3_check");
|
||||
if (!nfc_forum_tag_type3_check(pnd, nt, 1 + b, MIN(block_max_per_check, (block_count_to_check - (b * block_max_per_check))), data + data_len, &size)) {
|
||||
nfc_perror(pnd, "nfc_forum_tag_type3_check");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
data_len += size;
|
||||
}
|
||||
if (fwrite (data, 1, data_len, ndef_stream) != data_len) {
|
||||
fprintf (stderr, "Could not write to file.\n");
|
||||
if (fwrite(data, 1, data_len, ndef_stream) != data_len) {
|
||||
fprintf(stderr, "Could not write to file.\n");
|
||||
error = EXIT_FAILURE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
error:
|
||||
fclose (ndef_stream);
|
||||
fclose(ndef_stream);
|
||||
if (pnd) {
|
||||
nfc_close (pnd);
|
||||
nfc_close(pnd);
|
||||
}
|
||||
nfc_exit (NULL);
|
||||
exit (error);
|
||||
nfc_exit(NULL);
|
||||
exit(error);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,50 +72,50 @@ FILE * fd3;
|
|||
FILE * fd4;
|
||||
|
||||
static void
|
||||
intr_hdlr (int sig)
|
||||
intr_hdlr(int sig)
|
||||
{
|
||||
(void) sig;
|
||||
printf ("\nQuitting...\n");
|
||||
printf ("Please send a last command to the emulator to quit properly.\n");
|
||||
printf("\nQuitting...\n");
|
||||
printf("Please send a last command to the emulator to quit properly.\n");
|
||||
quitting = true;
|
||||
return;
|
||||
}
|
||||
|
||||
static void
|
||||
print_usage (char *argv[])
|
||||
print_usage(char *argv[])
|
||||
{
|
||||
printf ("Usage: %s [OPTIONS]\n", argv[0]);
|
||||
printf ("Options:\n");
|
||||
printf ("\t-h\tHelp. Print this message.\n");
|
||||
printf ("\t-q\tQuiet mode. Suppress printing of relayed data (improves timing).\n");
|
||||
printf ("\t-t\tTarget mode only (the one on reader side). Data expected from FD3 to FD4.\n");
|
||||
printf ("\t-i\tInitiator mode only (the one on tag side). Data expected from FD3 to FD4.\n");
|
||||
printf ("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n");
|
||||
printf("Usage: %s [OPTIONS]\n", argv[0]);
|
||||
printf("Options:\n");
|
||||
printf("\t-h\tHelp. Print this message.\n");
|
||||
printf("\t-q\tQuiet mode. Suppress printing of relayed data (improves timing).\n");
|
||||
printf("\t-t\tTarget mode only (the one on reader side). Data expected from FD3 to FD4.\n");
|
||||
printf("\t-i\tInitiator mode only (the one on tag side). Data expected from FD3 to FD4.\n");
|
||||
printf("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n");
|
||||
}
|
||||
|
||||
static bool print_hex_fd4 (const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix)
|
||||
static bool print_hex_fd4(const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix)
|
||||
{
|
||||
size_t szPos;
|
||||
if (szBytes > MAX_FRAME_LEN) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (fprintf (fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) {
|
||||
if (fprintf(fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (szPos = 0; szPos < szBytes; szPos++) {
|
||||
if (fprintf (fd4, "%02x ", pbtData[szPos]) < 0) {
|
||||
if (fprintf(fd4, "%02x ", pbtData[szPos]) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
if (fprintf (fd4, "\n") < 0) {
|
||||
if (fprintf(fd4, "\n") < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fflush(fd4);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix)
|
||||
static bool scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix)
|
||||
{
|
||||
size_t szPos;
|
||||
unsigned int uiBytes;
|
||||
|
|
@ -123,14 +123,14 @@ static bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPre
|
|||
char pchScan[256];
|
||||
int c;
|
||||
// Look for our next sync marker
|
||||
while ( (c = fgetc(fd3)) != '#') {
|
||||
while ((c = fgetc(fd3)) != '#') {
|
||||
if (c == EOF) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
strncpy(pchScan, pchPrefix, 250);
|
||||
strcat(pchScan, " %04x:");
|
||||
if (fscanf (fd3, pchScan, &uiBytes) < 1) {
|
||||
if (fscanf(fd3, pchScan, &uiBytes) < 1) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
*pszBytes = uiBytes;
|
||||
|
|
@ -138,7 +138,7 @@ static bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPre
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
for (szPos = 0; szPos < *pszBytes; szPos++) {
|
||||
if (fscanf (fd3, "%02x", &uiData) < 1) {
|
||||
if (fscanf(fd3, "%02x", &uiData) < 1) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
pbtData[szPos] = uiData;
|
||||
|
|
@ -147,66 +147,66 @@ static bool scan_hex_fd3 (uint8_t *pbtData, size_t *pszBytes, const char *pchPre
|
|||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int arg;
|
||||
const char *acLibnfcVersion = nfc_version ();
|
||||
const char *acLibnfcVersion = nfc_version();
|
||||
nfc_target ntRealTarget;
|
||||
|
||||
// Get commandline options
|
||||
for (arg = 1; arg < argc; arg++) {
|
||||
if (0 == strcmp (argv[arg], "-h")) {
|
||||
print_usage (argv);
|
||||
if (0 == strcmp(argv[arg], "-h")) {
|
||||
print_usage(argv);
|
||||
return EXIT_SUCCESS;
|
||||
} else if (0 == strcmp (argv[arg], "-q")) {
|
||||
} else if (0 == strcmp(argv[arg], "-q")) {
|
||||
quiet_output = true;
|
||||
} else if (0 == strcmp (argv[arg], "-t")) {
|
||||
printf ("INFO: %s\n", "Target mode only.");
|
||||
} else if (0 == strcmp(argv[arg], "-t")) {
|
||||
printf("INFO: %s\n", "Target mode only.");
|
||||
initiator_only_mode = false;
|
||||
target_only_mode = true;
|
||||
} else if (0 == strcmp (argv[arg], "-i")) {
|
||||
printf ("INFO: %s\n", "Initiator mode only.");
|
||||
} else if (0 == strcmp(argv[arg], "-i")) {
|
||||
printf("INFO: %s\n", "Initiator mode only.");
|
||||
initiator_only_mode = true;
|
||||
target_only_mode = false;
|
||||
} else if (0 == strcmp (argv[arg], "-n")) {
|
||||
} else if (0 == strcmp(argv[arg], "-n")) {
|
||||
if (++arg == argc || (sscanf(argv[arg], "%i", &waiting_time) < 1)) {
|
||||
ERR ("Missing or wrong waiting time value: %s.", argv[arg]);
|
||||
print_usage (argv);
|
||||
ERR("Missing or wrong waiting time value: %s.", argv[arg]);
|
||||
print_usage(argv);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf ("Waiting time: %i secs.\n", waiting_time);
|
||||
printf("Waiting time: %i secs.\n", waiting_time);
|
||||
} else {
|
||||
ERR ("%s is not supported option.", argv[arg]);
|
||||
print_usage (argv);
|
||||
ERR("%s is not supported option.", argv[arg]);
|
||||
print_usage(argv);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
// Display libnfc version
|
||||
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
||||
|
||||
#ifdef WIN32
|
||||
signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr);
|
||||
signal(SIGINT, (void (__cdecl *)(int)) intr_hdlr);
|
||||
#else
|
||||
signal (SIGINT, intr_hdlr);
|
||||
signal(SIGINT, intr_hdlr);
|
||||
#endif
|
||||
|
||||
nfc_init (NULL);
|
||||
nfc_init(NULL);
|
||||
|
||||
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
||||
// List available devices
|
||||
size_t szFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT);
|
||||
size_t szFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT);
|
||||
|
||||
if (initiator_only_mode || target_only_mode) {
|
||||
if (szFound < 1) {
|
||||
ERR ("No device found");
|
||||
ERR("No device found");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fd3 = fdopen(3, "r");
|
||||
fd4 = fdopen(4, "w");
|
||||
} else {
|
||||
if (szFound < 2) {
|
||||
ERR ("%zd device found but two opened devices are needed to relay NFC.", szFound);
|
||||
ERR("%zd device found but two opened devices are needed to relay NFC.", szFound);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
|
@ -218,23 +218,23 @@ main (int argc, char *argv[])
|
|||
// if there is more than one readers opened we open the second reader
|
||||
// (we hope they're always detected in the same order)
|
||||
if (szFound == 1) {
|
||||
pndInitiator = nfc_open (NULL, connstrings[0]);
|
||||
pndInitiator = nfc_open(NULL, connstrings[0]);
|
||||
} else {
|
||||
pndInitiator = nfc_open (NULL, connstrings[1]);
|
||||
pndInitiator = nfc_open(NULL, connstrings[1]);
|
||||
}
|
||||
|
||||
if (!pndInitiator) {
|
||||
printf ("Error opening NFC reader\n");
|
||||
printf("Error opening NFC reader\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf ("NFC reader device: %s opened\n", nfc_device_get_name (pndInitiator));
|
||||
printf("NFC reader device: %s opened\n", nfc_device_get_name(pndInitiator));
|
||||
|
||||
if (nfc_initiator_init (pndInitiator) < 0) {
|
||||
printf ("Error: fail initializing initiator\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_init(pndInitiator) < 0) {
|
||||
printf("Error: fail initializing initiator\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Try to find a ISO 14443-4A tag
|
||||
|
|
@ -242,48 +242,48 @@ main (int argc, char *argv[])
|
|||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106,
|
||||
};
|
||||
if (nfc_initiator_select_passive_target (pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) {
|
||||
printf ("Error: no tag was found\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
exit (EXIT_FAILURE);
|
||||
if (nfc_initiator_select_passive_target(pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) {
|
||||
printf("Error: no tag was found\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Found tag:\n");
|
||||
print_nfc_iso14443a_info (ntRealTarget.nti.nai, false);
|
||||
print_nfc_iso14443a_info(ntRealTarget.nti.nai, false);
|
||||
if (initiator_only_mode) {
|
||||
if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while printing UID to FD4\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while printing UID to FD4\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while printing ATQA to FD4\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while printing ATQA to FD4\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while printing SAK to FD4\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while printing SAK to FD4\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while printing ATS to FD4\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while printing ATS to FD4\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (initiator_only_mode) {
|
||||
printf ("Hint: tag <---> *INITIATOR* (relay) <-FD3/FD4-> target (relay) <---> original reader\n\n");
|
||||
printf("Hint: tag <---> *INITIATOR* (relay) <-FD3/FD4-> target (relay) <---> original reader\n\n");
|
||||
} else if (target_only_mode) {
|
||||
printf ("Hint: tag <---> initiator (relay) <-FD3/FD4-> *TARGET* (relay) <---> original reader\n\n");
|
||||
printf("Hint: tag <---> initiator (relay) <-FD3/FD4-> *TARGET* (relay) <---> original reader\n\n");
|
||||
} else {
|
||||
printf ("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
|
||||
printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
|
||||
}
|
||||
if (!initiator_only_mode) {
|
||||
nfc_target ntEmulatedTarget = {
|
||||
|
|
@ -295,26 +295,26 @@ main (int argc, char *argv[])
|
|||
if (target_only_mode) {
|
||||
size_t foo;
|
||||
if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while scanning UID from FD3\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while scanning UID from FD3\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while scanning ATQA from FD3\n");
|
||||
nfc_close (pndInitiator);
|
||||
fprintf(stderr, "Error while scanning ATQA from FD3\n");
|
||||
nfc_close(pndInitiator);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while scanning SAK from FD3\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while scanning SAK from FD3\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while scanning ATS from FD3\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while scanning ATS from FD3\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -338,7 +338,7 @@ main (int argc, char *argv[])
|
|||
// Creates ATS and copy max 48 bytes of Tk:
|
||||
uint8_t * pbtTk;
|
||||
size_t szTk;
|
||||
pbtTk = iso14443a_locate_historical_bytes (ntEmulatedTarget.nti.nai.abtAts, ntEmulatedTarget.nti.nai.szAtsLen, &szTk);
|
||||
pbtTk = iso14443a_locate_historical_bytes(ntEmulatedTarget.nti.nai.abtAts, ntEmulatedTarget.nti.nai.szAtsLen, &szTk);
|
||||
szTk = (szTk > 48) ? 48 : szTk;
|
||||
uint8_t pbtTkt[48];
|
||||
memcpy(pbtTkt, pbtTk, szTk);
|
||||
|
|
@ -350,31 +350,31 @@ main (int argc, char *argv[])
|
|||
memcpy(&(ntEmulatedTarget.nti.nai.abtAts[4]), pbtTkt, szTk);
|
||||
|
||||
printf("We will emulate:\n");
|
||||
print_nfc_iso14443a_info (ntEmulatedTarget.nti.nai, false);
|
||||
print_nfc_iso14443a_info(ntEmulatedTarget.nti.nai, false);
|
||||
|
||||
// Try to open the NFC emulator device
|
||||
pndTarget = nfc_open (NULL, connstrings[0]);
|
||||
pndTarget = nfc_open(NULL, connstrings[0]);
|
||||
if (pndTarget == NULL) {
|
||||
printf ("Error opening NFC emulator device\n");
|
||||
printf("Error opening NFC emulator device\n");
|
||||
if (!target_only_mode) {
|
||||
nfc_close (pndInitiator);
|
||||
nfc_close(pndInitiator);
|
||||
}
|
||||
nfc_exit (NULL);
|
||||
nfc_exit(NULL);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf ("NFC emulator device: %s opened\n", nfc_device_get_name (pndTarget));
|
||||
printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTarget));
|
||||
int res;
|
||||
if ((res = nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, sizeof(abtCapdu), 0)) < 0) {
|
||||
ERR ("%s", "Initialization of NFC emulator failed");
|
||||
if ((res = nfc_target_init(pndTarget, &ntEmulatedTarget, abtCapdu, sizeof(abtCapdu), 0)) < 0) {
|
||||
ERR("%s", "Initialization of NFC emulator failed");
|
||||
if (!target_only_mode) {
|
||||
nfc_close (pndInitiator);
|
||||
nfc_close(pndInitiator);
|
||||
}
|
||||
nfc_close (pndTarget);
|
||||
nfc_exit (NULL);
|
||||
nfc_close(pndTarget);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf ("%s\n", "Done, relaying frames now!");
|
||||
printf("%s\n", "Done, relaying frames now!");
|
||||
}
|
||||
|
||||
while (!quitting) {
|
||||
|
|
@ -382,41 +382,41 @@ main (int argc, char *argv[])
|
|||
int res = 0;
|
||||
if (!initiator_only_mode) {
|
||||
// Receive external reader command through target
|
||||
if ((res = nfc_target_receive_bytes(pndTarget, abtCapdu, sizeof (abtCapdu), 0)) < 0) {
|
||||
nfc_perror (pndTarget, "nfc_target_receive_bytes");
|
||||
if ((res = nfc_target_receive_bytes(pndTarget, abtCapdu, sizeof(abtCapdu), 0)) < 0) {
|
||||
nfc_perror(pndTarget, "nfc_target_receive_bytes");
|
||||
if (!target_only_mode) {
|
||||
nfc_close (pndInitiator);
|
||||
nfc_close(pndInitiator);
|
||||
}
|
||||
nfc_close (pndTarget);
|
||||
nfc_exit (NULL);
|
||||
nfc_close(pndTarget);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
szCapduLen = (size_t) res;
|
||||
if (target_only_mode) {
|
||||
if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while printing C-APDU to FD4\n");
|
||||
nfc_close (pndTarget);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while printing C-APDU to FD4\n");
|
||||
nfc_close(pndTarget);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while scanning C-APDU from FD3\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while scanning C-APDU from FD3\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
// Show transmitted response
|
||||
if (!quiet_output) {
|
||||
printf ("Forwarding C-APDU: ");
|
||||
print_hex (abtCapdu, szCapduLen);
|
||||
printf("Forwarding C-APDU: ");
|
||||
print_hex(abtCapdu, szCapduLen);
|
||||
}
|
||||
|
||||
if (!target_only_mode) {
|
||||
// Forward the frame to the original tag
|
||||
if ((res = nfc_initiator_transceive_bytes (pndInitiator, abtCapdu, szCapduLen, abtRapdu, sizeof(abtRapdu), -1) < 0)) {
|
||||
if ((res = nfc_initiator_transceive_bytes(pndInitiator, abtCapdu, szCapduLen, abtRapdu, sizeof(abtRapdu), -1) < 0)) {
|
||||
ret = false;
|
||||
} else {
|
||||
szCapduLen = (size_t) res;
|
||||
|
|
@ -424,9 +424,9 @@ main (int argc, char *argv[])
|
|||
}
|
||||
} else {
|
||||
if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while scanning R-APDU from FD3\n");
|
||||
nfc_close (pndTarget);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while scanning R-APDU from FD3\n");
|
||||
nfc_close(pndTarget);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ret = true;
|
||||
|
|
@ -435,34 +435,34 @@ main (int argc, char *argv[])
|
|||
// Redirect the answer back to the external reader
|
||||
if (waiting_time > 0) {
|
||||
if (!quiet_output) {
|
||||
printf ("Waiting %is to simulate longer relay...\n", waiting_time);
|
||||
printf("Waiting %is to simulate longer relay...\n", waiting_time);
|
||||
}
|
||||
sleep(waiting_time);
|
||||
}
|
||||
// Show transmitted response
|
||||
if (!quiet_output) {
|
||||
printf ("Forwarding R-APDU: ");
|
||||
print_hex (abtRapdu, szRapduLen);
|
||||
printf("Forwarding R-APDU: ");
|
||||
print_hex(abtRapdu, szRapduLen);
|
||||
}
|
||||
if (!initiator_only_mode) {
|
||||
// Transmit the response bytes
|
||||
if (nfc_target_send_bytes(pndTarget, abtRapdu, szRapduLen, 0) < 0) {
|
||||
nfc_perror (pndTarget, "nfc_target_send_bytes");
|
||||
nfc_perror(pndTarget, "nfc_target_send_bytes");
|
||||
if (!target_only_mode) {
|
||||
nfc_close (pndInitiator);
|
||||
nfc_close(pndInitiator);
|
||||
}
|
||||
if (!initiator_only_mode) {
|
||||
nfc_close (pndTarget);
|
||||
nfc_exit (NULL);
|
||||
nfc_close(pndTarget);
|
||||
nfc_exit(NULL);
|
||||
}
|
||||
nfc_exit (NULL);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) {
|
||||
fprintf (stderr, "Error while printing R-APDU to FD4\n");
|
||||
nfc_close (pndInitiator);
|
||||
nfc_exit (NULL);
|
||||
fprintf(stderr, "Error while printing R-APDU to FD4\n");
|
||||
nfc_close(pndInitiator);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
|
@ -470,12 +470,12 @@ main (int argc, char *argv[])
|
|||
}
|
||||
|
||||
if (!target_only_mode) {
|
||||
nfc_close (pndInitiator);
|
||||
nfc_close(pndInitiator);
|
||||
}
|
||||
if (!initiator_only_mode) {
|
||||
nfc_close (pndTarget);
|
||||
nfc_close(pndTarget);
|
||||
}
|
||||
nfc_exit (NULL);
|
||||
exit (EXIT_SUCCESS);
|
||||
nfc_exit(NULL);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -126,14 +126,14 @@ struct card_sak const_cs[] = {
|
|||
};
|
||||
|
||||
uint8_t
|
||||
oddparity (const uint8_t bt)
|
||||
oddparity(const uint8_t bt)
|
||||
{
|
||||
// cf http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
|
||||
return (0x9669 >> ((bt ^ (bt >> 4)) & 0xF)) & 1;
|
||||
return (0x9669 >> ((bt ^(bt >> 4)) & 0xF)) & 1;
|
||||
}
|
||||
|
||||
void
|
||||
oddparity_bytes_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
|
||||
oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
|
||||
{
|
||||
size_t szByteNr;
|
||||
// Calculate the parity bits for the command
|
||||
|
|
@ -143,51 +143,51 @@ oddparity_bytes_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
|
|||
}
|
||||
|
||||
void
|
||||
print_hex (const uint8_t *pbtData, const size_t szBytes)
|
||||
print_hex(const uint8_t *pbtData, const size_t szBytes)
|
||||
{
|
||||
size_t szPos;
|
||||
|
||||
for (szPos = 0; szPos < szBytes; szPos++) {
|
||||
printf ("%02x ", pbtData[szPos]);
|
||||
printf("%02x ", pbtData[szPos]);
|
||||
}
|
||||
printf ("\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void
|
||||
print_hex_bits (const uint8_t *pbtData, const size_t szBits)
|
||||
print_hex_bits(const uint8_t *pbtData, const size_t szBits)
|
||||
{
|
||||
uint8_t uRemainder;
|
||||
size_t szPos;
|
||||
size_t szBytes = szBits / 8;
|
||||
|
||||
for (szPos = 0; szPos < szBytes; szPos++) {
|
||||
printf ("%02x ", pbtData[szPos]);
|
||||
printf("%02x ", pbtData[szPos]);
|
||||
}
|
||||
|
||||
uRemainder = szBits % 8;
|
||||
// Print the rest bits
|
||||
if (uRemainder != 0) {
|
||||
if (uRemainder < 5)
|
||||
printf ("%01x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
else
|
||||
printf ("%02x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
}
|
||||
printf ("\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void
|
||||
print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
|
||||
print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
|
||||
{
|
||||
uint8_t uRemainder;
|
||||
size_t szPos;
|
||||
size_t szBytes = szBits / 8;
|
||||
|
||||
for (szPos = 0; szPos < szBytes; szPos++) {
|
||||
printf ("%02x", pbtData[szPos]);
|
||||
printf("%02x", pbtData[szPos]);
|
||||
if (oddparity(pbtData[szPos]) != pbtDataPar[szPos]) {
|
||||
printf ("! ");
|
||||
printf("! ");
|
||||
} else {
|
||||
printf (" ");
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -195,11 +195,11 @@ print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDa
|
|||
// Print the rest bits, these cannot have parity bit
|
||||
if (uRemainder != 0) {
|
||||
if (uRemainder < 5)
|
||||
printf ("%01x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
printf("%01x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
else
|
||||
printf ("%02x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
printf("%02x (%d bits)", pbtData[szBytes], uRemainder);
|
||||
}
|
||||
printf ("\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#define SAK_UID_NOT_COMPLETE 0x04
|
||||
|
|
@ -207,10 +207,10 @@ print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDa
|
|||
#define SAK_ISO18092_COMPLIANT 0x40
|
||||
|
||||
void
|
||||
print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose)
|
||||
print_nfc_iso14443a_info(const nfc_iso14443a_info nai, bool verbose)
|
||||
{
|
||||
printf (" ATQA (SENS_RES): ");
|
||||
print_hex (nai.abtAtqa, 2);
|
||||
printf(" ATQA (SENS_RES): ");
|
||||
print_hex(nai.abtAtqa, 2);
|
||||
if (verbose) {
|
||||
printf("* UID size: ");
|
||||
switch ((nai.abtAtqa[1] & 0xc0) >> 6) {
|
||||
|
|
@ -241,80 +241,80 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose)
|
|||
break;
|
||||
}
|
||||
}
|
||||
printf (" UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1'));
|
||||
print_hex (nai.abtUid, nai.szUidLen);
|
||||
printf(" UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1'));
|
||||
print_hex(nai.abtUid, nai.szUidLen);
|
||||
if (verbose) {
|
||||
if (nai.abtUid[0] == 0x08) {
|
||||
printf ("* Random UID\n");
|
||||
printf("* Random UID\n");
|
||||
}
|
||||
}
|
||||
printf (" SAK (SEL_RES): ");
|
||||
print_hex (&nai.btSak, 1);
|
||||
printf(" SAK (SEL_RES): ");
|
||||
print_hex(&nai.btSak, 1);
|
||||
if (verbose) {
|
||||
if (nai.btSak & SAK_UID_NOT_COMPLETE) {
|
||||
printf ("* Warning! Cascade bit set: UID not complete\n");
|
||||
printf("* Warning! Cascade bit set: UID not complete\n");
|
||||
}
|
||||
if (nai.btSak & SAK_ISO14443_4_COMPLIANT) {
|
||||
printf ("* Compliant with ISO/IEC 14443-4\n");
|
||||
printf("* Compliant with ISO/IEC 14443-4\n");
|
||||
} else {
|
||||
printf ("* Not compliant with ISO/IEC 14443-4\n");
|
||||
printf("* Not compliant with ISO/IEC 14443-4\n");
|
||||
}
|
||||
if (nai.btSak & SAK_ISO18092_COMPLIANT) {
|
||||
printf ("* Compliant with ISO/IEC 18092\n");
|
||||
printf("* Compliant with ISO/IEC 18092\n");
|
||||
} else {
|
||||
printf ("* Not compliant with ISO/IEC 18092\n");
|
||||
printf("* Not compliant with ISO/IEC 18092\n");
|
||||
}
|
||||
}
|
||||
if (nai.szAtsLen) {
|
||||
printf (" ATS: ");
|
||||
print_hex (nai.abtAts, nai.szAtsLen);
|
||||
printf(" ATS: ");
|
||||
print_hex(nai.abtAts, nai.szAtsLen);
|
||||
}
|
||||
if (nai.szAtsLen && verbose) {
|
||||
// Decode ATS according to ISO/IEC 14443-4 (5.2 Answer to select)
|
||||
const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };
|
||||
printf ("* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[nai.abtAts[0] & 0x0F]);
|
||||
printf("* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[nai.abtAts[0] & 0x0F]);
|
||||
|
||||
size_t offset = 1;
|
||||
if (nai.abtAts[0] & 0x10) { // TA(1) present
|
||||
uint8_t TA = nai.abtAts[offset];
|
||||
offset++;
|
||||
printf ("* Bit Rate Capability:\n");
|
||||
printf("* Bit Rate Capability:\n");
|
||||
if (TA == 0) {
|
||||
printf (" * PICC supports only 106 kbits/s in both directions\n");
|
||||
printf(" * PICC supports only 106 kbits/s in both directions\n");
|
||||
}
|
||||
if (TA & 1 << 7) {
|
||||
printf (" * Same bitrate in both directions mandatory\n");
|
||||
printf(" * Same bitrate in both directions mandatory\n");
|
||||
}
|
||||
if (TA & 1 << 4) {
|
||||
printf (" * PICC to PCD, DS=2, bitrate 212 kbits/s supported\n");
|
||||
printf(" * PICC to PCD, DS=2, bitrate 212 kbits/s supported\n");
|
||||
}
|
||||
if (TA & 1 << 5) {
|
||||
printf (" * PICC to PCD, DS=4, bitrate 424 kbits/s supported\n");
|
||||
printf(" * PICC to PCD, DS=4, bitrate 424 kbits/s supported\n");
|
||||
}
|
||||
if (TA & 1 << 6) {
|
||||
printf (" * PICC to PCD, DS=8, bitrate 847 kbits/s supported\n");
|
||||
printf(" * PICC to PCD, DS=8, bitrate 847 kbits/s supported\n");
|
||||
}
|
||||
if (TA & 1 << 0) {
|
||||
printf (" * PCD to PICC, DR=2, bitrate 212 kbits/s supported\n");
|
||||
printf(" * PCD to PICC, DR=2, bitrate 212 kbits/s supported\n");
|
||||
}
|
||||
if (TA & 1 << 1) {
|
||||
printf (" * PCD to PICC, DR=4, bitrate 424 kbits/s supported\n");
|
||||
printf(" * PCD to PICC, DR=4, bitrate 424 kbits/s supported\n");
|
||||
}
|
||||
if (TA & 1 << 2) {
|
||||
printf (" * PCD to PICC, DR=8, bitrate 847 kbits/s supported\n");
|
||||
printf(" * PCD to PICC, DR=8, bitrate 847 kbits/s supported\n");
|
||||
}
|
||||
if (TA & 1 << 3) {
|
||||
printf (" * ERROR unknown value\n");
|
||||
printf(" * ERROR unknown value\n");
|
||||
}
|
||||
}
|
||||
if (nai.abtAts[0] & 0x20) { // TB(1) present
|
||||
uint8_t TB = nai.abtAts[offset];
|
||||
offset++;
|
||||
printf ("* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((TB & 0xf0) >> 4)) / 13560.0);
|
||||
printf("* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((TB & 0xf0) >> 4)) / 13560.0);
|
||||
if ((TB & 0x0f) == 0) {
|
||||
printf ("* No Start-up Frame Guard Time required\n");
|
||||
printf("* No Start-up Frame Guard Time required\n");
|
||||
} else {
|
||||
printf ("* Start-up Frame Guard Time: %.4g ms\n", 256.0 * 16.0 * (1 << (TB & 0x0f)) / 13560.0);
|
||||
printf("* Start-up Frame Guard Time: %.4g ms\n", 256.0 * 16.0 * (1 << (TB & 0x0f)) / 13560.0);
|
||||
}
|
||||
}
|
||||
if (nai.abtAts[0] & 0x40) { // TC(1) present
|
||||
|
|
@ -332,8 +332,8 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose)
|
|||
}
|
||||
}
|
||||
if (nai.szAtsLen > offset) {
|
||||
printf ("* Historical bytes Tk: " );
|
||||
print_hex (nai.abtAts + offset, (nai.szAtsLen - offset));
|
||||
printf("* Historical bytes Tk: ");
|
||||
print_hex(nai.abtAts + offset, (nai.szAtsLen - offset));
|
||||
uint8_t CIB = nai.abtAts[offset];
|
||||
offset++;
|
||||
if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) {
|
||||
|
|
@ -542,174 +542,174 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose)
|
|||
}
|
||||
|
||||
void
|
||||
print_nfc_felica_info (const nfc_felica_info nfi, bool verbose)
|
||||
print_nfc_felica_info(const nfc_felica_info nfi, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" ID (NFCID2): ");
|
||||
print_hex (nfi.abtId, 8);
|
||||
printf (" Parameter (PAD): ");
|
||||
print_hex (nfi.abtPad, 8);
|
||||
printf (" System Code (SC): ");
|
||||
print_hex (nfi.abtSysCode, 2);
|
||||
printf(" ID (NFCID2): ");
|
||||
print_hex(nfi.abtId, 8);
|
||||
printf(" Parameter (PAD): ");
|
||||
print_hex(nfi.abtPad, 8);
|
||||
printf(" System Code (SC): ");
|
||||
print_hex(nfi.abtSysCode, 2);
|
||||
}
|
||||
|
||||
void
|
||||
print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose)
|
||||
print_nfc_jewel_info(const nfc_jewel_info nji, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" ATQA (SENS_RES): ");
|
||||
print_hex (nji.btSensRes, 2);
|
||||
printf (" 4-LSB JEWELID: ");
|
||||
print_hex (nji.btId, 4);
|
||||
printf(" ATQA (SENS_RES): ");
|
||||
print_hex(nji.btSensRes, 2);
|
||||
printf(" 4-LSB JEWELID: ");
|
||||
print_hex(nji.btId, 4);
|
||||
}
|
||||
|
||||
#define PI_ISO14443_4_SUPPORTED 0x01
|
||||
#define PI_NAD_SUPPORTED 0x01
|
||||
#define PI_CID_SUPPORTED 0x02
|
||||
void
|
||||
print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose)
|
||||
print_nfc_iso14443b_info(const nfc_iso14443b_info nbi, bool verbose)
|
||||
{
|
||||
const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };
|
||||
printf (" PUPI: ");
|
||||
print_hex (nbi.abtPupi, 4);
|
||||
printf (" Application Data: ");
|
||||
print_hex (nbi.abtApplicationData, 4);
|
||||
printf (" Protocol Info: ");
|
||||
print_hex (nbi.abtProtocolInfo, 3);
|
||||
printf(" PUPI: ");
|
||||
print_hex(nbi.abtPupi, 4);
|
||||
printf(" Application Data: ");
|
||||
print_hex(nbi.abtApplicationData, 4);
|
||||
printf(" Protocol Info: ");
|
||||
print_hex(nbi.abtProtocolInfo, 3);
|
||||
if (verbose) {
|
||||
printf ("* Bit Rate Capability:\n");
|
||||
printf("* Bit Rate Capability:\n");
|
||||
if (nbi.abtProtocolInfo[0] == 0) {
|
||||
printf (" * PICC supports only 106 kbits/s in both directions\n");
|
||||
printf(" * PICC supports only 106 kbits/s in both directions\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 7) {
|
||||
printf (" * Same bitrate in both directions mandatory\n");
|
||||
printf(" * Same bitrate in both directions mandatory\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 4) {
|
||||
printf (" * PICC to PCD, 1etu=64/fc, bitrate 212 kbits/s supported\n");
|
||||
printf(" * PICC to PCD, 1etu=64/fc, bitrate 212 kbits/s supported\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 5) {
|
||||
printf (" * PICC to PCD, 1etu=32/fc, bitrate 424 kbits/s supported\n");
|
||||
printf(" * PICC to PCD, 1etu=32/fc, bitrate 424 kbits/s supported\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 6) {
|
||||
printf (" * PICC to PCD, 1etu=16/fc, bitrate 847 kbits/s supported\n");
|
||||
printf(" * PICC to PCD, 1etu=16/fc, bitrate 847 kbits/s supported\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 0) {
|
||||
printf (" * PCD to PICC, 1etu=64/fc, bitrate 212 kbits/s supported\n");
|
||||
printf(" * PCD to PICC, 1etu=64/fc, bitrate 212 kbits/s supported\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 1) {
|
||||
printf (" * PCD to PICC, 1etu=32/fc, bitrate 424 kbits/s supported\n");
|
||||
printf(" * PCD to PICC, 1etu=32/fc, bitrate 424 kbits/s supported\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 2) {
|
||||
printf (" * PCD to PICC, 1etu=16/fc, bitrate 847 kbits/s supported\n");
|
||||
printf(" * PCD to PICC, 1etu=16/fc, bitrate 847 kbits/s supported\n");
|
||||
}
|
||||
if (nbi.abtProtocolInfo[0] & 1 << 3) {
|
||||
printf (" * ERROR unknown value\n");
|
||||
printf(" * ERROR unknown value\n");
|
||||
}
|
||||
if( (nbi.abtProtocolInfo[1] & 0xf0) <= 0x80 ) {
|
||||
printf ("* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]);
|
||||
if ((nbi.abtProtocolInfo[1] & 0xf0) <= 0x80) {
|
||||
printf("* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]);
|
||||
}
|
||||
if((nbi.abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) {
|
||||
printf ("* Protocol types supported: ISO/IEC 14443-4\n");
|
||||
if ((nbi.abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) {
|
||||
printf("* Protocol types supported: ISO/IEC 14443-4\n");
|
||||
}
|
||||
printf ("* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((nbi.abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0);
|
||||
if((nbi.abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) {
|
||||
printf ("* Frame options supported: ");
|
||||
if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) printf ("NAD ");
|
||||
if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) printf ("CID ");
|
||||
printf("* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((nbi.abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0);
|
||||
if ((nbi.abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) {
|
||||
printf("* Frame options supported: ");
|
||||
if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) printf("NAD ");
|
||||
if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) printf("CID ");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose)
|
||||
print_nfc_iso14443bi_info(const nfc_iso14443bi_info nii, bool verbose)
|
||||
{
|
||||
printf (" DIV: ");
|
||||
print_hex (nii.abtDIV, 4);
|
||||
printf(" DIV: ");
|
||||
print_hex(nii.abtDIV, 4);
|
||||
if (verbose) {
|
||||
int version = (nii.btVerLog & 0x1e) >> 1;
|
||||
printf (" Software Version: ");
|
||||
printf(" Software Version: ");
|
||||
if (version == 15) {
|
||||
printf ("Undefined\n");
|
||||
printf("Undefined\n");
|
||||
} else {
|
||||
printf ("%i\n", version);
|
||||
printf("%i\n", version);
|
||||
}
|
||||
|
||||
if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x80)) {
|
||||
printf (" Wait Enable: yes");
|
||||
printf(" Wait Enable: yes");
|
||||
}
|
||||
}
|
||||
if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x40)) {
|
||||
printf (" ATS: ");
|
||||
print_hex (nii.abtAtr, nii.szAtrLen);
|
||||
printf(" ATS: ");
|
||||
print_hex(nii.abtAtr, nii.szAtrLen);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose)
|
||||
print_nfc_iso14443b2sr_info(const nfc_iso14443b2sr_info nsi, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" UID: ");
|
||||
print_hex (nsi.abtUID, 8);
|
||||
printf(" UID: ");
|
||||
print_hex(nsi.abtUID, 8);
|
||||
}
|
||||
|
||||
void
|
||||
print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose)
|
||||
print_nfc_iso14443b2ct_info(const nfc_iso14443b2ct_info nci, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
uint32_t uid;
|
||||
uid = (nci.abtUID[3] << 24) + (nci.abtUID[2] << 16) + (nci.abtUID[1] << 8) + nci.abtUID[0];
|
||||
printf (" UID: ");
|
||||
print_hex (nci.abtUID, sizeof(nci.abtUID));
|
||||
printf (" UID (decimal): %010u\n", uid);
|
||||
printf (" Product Code: %02X\n", nci.btProdCode);
|
||||
printf (" Fab Code: %02X\n", nci.btFabCode);
|
||||
printf(" UID: ");
|
||||
print_hex(nci.abtUID, sizeof(nci.abtUID));
|
||||
printf(" UID (decimal): %010u\n", uid);
|
||||
printf(" Product Code: %02X\n", nci.btProdCode);
|
||||
printf(" Fab Code: %02X\n", nci.btFabCode);
|
||||
}
|
||||
|
||||
void
|
||||
print_nfc_dep_info (const nfc_dep_info ndi, bool verbose)
|
||||
print_nfc_dep_info(const nfc_dep_info ndi, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" NFCID3: ");
|
||||
print_hex (ndi.abtNFCID3, 10);
|
||||
printf (" BS: %02x\n", ndi.btBS);
|
||||
printf (" BR: %02x\n", ndi.btBR);
|
||||
printf (" TO: %02x\n", ndi.btTO);
|
||||
printf (" PP: %02x\n", ndi.btPP);
|
||||
printf(" NFCID3: ");
|
||||
print_hex(ndi.abtNFCID3, 10);
|
||||
printf(" BS: %02x\n", ndi.btBS);
|
||||
printf(" BR: %02x\n", ndi.btBR);
|
||||
printf(" TO: %02x\n", ndi.btTO);
|
||||
printf(" PP: %02x\n", ndi.btPP);
|
||||
if (ndi.szGB) {
|
||||
printf ("General Bytes: ");
|
||||
print_hex (ndi.abtGB, ndi.szGB);
|
||||
printf("General Bytes: ");
|
||||
print_hex(ndi.abtGB, ndi.szGB);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_nfc_target (const nfc_target nt, bool verbose)
|
||||
print_nfc_target(const nfc_target nt, bool verbose)
|
||||
{
|
||||
printf ("%s (%s%s) target:\n", str_nfc_modulation_type(nt.nm.nmt), str_nfc_baud_rate(nt.nm.nbr), (nt.nm.nmt != NMT_DEP) ? "" : (nt.nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode");
|
||||
switch(nt.nm.nmt) {
|
||||
printf("%s (%s%s) target:\n", str_nfc_modulation_type(nt.nm.nmt), str_nfc_baud_rate(nt.nm.nbr), (nt.nm.nmt != NMT_DEP) ? "" : (nt.nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode");
|
||||
switch (nt.nm.nmt) {
|
||||
case NMT_ISO14443A:
|
||||
print_nfc_iso14443a_info (nt.nti.nai, verbose);
|
||||
print_nfc_iso14443a_info(nt.nti.nai, verbose);
|
||||
break;
|
||||
case NMT_JEWEL:
|
||||
print_nfc_jewel_info (nt.nti.nji, verbose);
|
||||
print_nfc_jewel_info(nt.nti.nji, verbose);
|
||||
break;
|
||||
case NMT_FELICA:
|
||||
print_nfc_felica_info (nt.nti.nfi, verbose);
|
||||
print_nfc_felica_info(nt.nti.nfi, verbose);
|
||||
break;
|
||||
case NMT_ISO14443B:
|
||||
print_nfc_iso14443b_info (nt.nti.nbi, verbose);
|
||||
print_nfc_iso14443b_info(nt.nti.nbi, verbose);
|
||||
break;
|
||||
case NMT_ISO14443BI:
|
||||
print_nfc_iso14443bi_info (nt.nti.nii, verbose);
|
||||
print_nfc_iso14443bi_info(nt.nti.nii, verbose);
|
||||
break;
|
||||
case NMT_ISO14443B2SR:
|
||||
print_nfc_iso14443b2sr_info (nt.nti.nsi, verbose);
|
||||
print_nfc_iso14443b2sr_info(nt.nti.nsi, verbose);
|
||||
break;
|
||||
case NMT_ISO14443B2CT:
|
||||
print_nfc_iso14443b2ct_info (nt.nti.nci, verbose);
|
||||
print_nfc_iso14443b2ct_info(nt.nti.nci, verbose);
|
||||
break;
|
||||
case NMT_DEP:
|
||||
print_nfc_dep_info (nt.nti.ndi, verbose);
|
||||
print_nfc_dep_info(nt.nti.ndi, verbose);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,22 +86,22 @@
|
|||
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
uint8_t oddparity (const uint8_t bt);
|
||||
void oddparity_bytes_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar);
|
||||
uint8_t oddparity(const uint8_t bt);
|
||||
void oddparity_bytes_ts(const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar);
|
||||
|
||||
void print_hex (const uint8_t *pbtData, const size_t szLen);
|
||||
void print_hex_bits (const uint8_t *pbtData, const size_t szBits);
|
||||
void print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar);
|
||||
void print_hex(const uint8_t *pbtData, const size_t szLen);
|
||||
void print_hex_bits(const uint8_t *pbtData, const size_t szBits);
|
||||
void print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar);
|
||||
|
||||
void print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose);
|
||||
void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose);
|
||||
void print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose);
|
||||
void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose);
|
||||
void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose);
|
||||
void print_nfc_felica_info (const nfc_felica_info nfi, bool verbose);
|
||||
void print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose);
|
||||
void print_nfc_dep_info (const nfc_dep_info ndi, bool verbose);
|
||||
void print_nfc_iso14443a_info(const nfc_iso14443a_info nai, bool verbose);
|
||||
void print_nfc_iso14443b_info(const nfc_iso14443b_info nbi, bool verbose);
|
||||
void print_nfc_iso14443bi_info(const nfc_iso14443bi_info nii, bool verbose);
|
||||
void print_nfc_iso14443b2sr_info(const nfc_iso14443b2sr_info nsi, bool verbose);
|
||||
void print_nfc_iso14443b2ct_info(const nfc_iso14443b2ct_info nci, bool verbose);
|
||||
void print_nfc_felica_info(const nfc_felica_info nfi, bool verbose);
|
||||
void print_nfc_jewel_info(const nfc_jewel_info nji, bool verbose);
|
||||
void print_nfc_dep_info(const nfc_dep_info ndi, bool verbose);
|
||||
|
||||
void print_nfc_target (const nfc_target nt, bool verbose);
|
||||
void print_nfc_target(const nfc_target nt, bool verbose);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue