2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2009-10-12 16:52:26 +02:00
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
2010-06-30 17:06:59 +02:00
|
|
|
* Copyright (C) 2009, Roel Verdult, 2010, Romuald Conty
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2010-01-15 11:18:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-10-12 16:52:26 +02:00
|
|
|
* @file nfc-mfultool.c
|
2010-06-24 12:16:09 +02:00
|
|
|
* @brief MIFARE Ultralight dump tool
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-06-24 12:19:01 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-09-02 22:15:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2010-06-24 12:16:09 +02:00
|
|
|
#include <nfc/nfc-messages.h>
|
2009-11-02 12:34:58 +01:00
|
|
|
|
2010-06-15 17:33:22 +02:00
|
|
|
#include "mifare.h"
|
2010-04-16 18:38:57 +02:00
|
|
|
#include "nfc-utils.h"
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
static nfc_device_t *pnd;
|
2009-11-18 12:52:18 +01:00
|
|
|
static nfc_target_info_t nti;
|
2009-09-02 22:15:21 +02:00
|
|
|
static mifare_param mp;
|
|
|
|
static mifareul_tag mtDump;
|
2009-11-27 18:02:53 +01:00
|
|
|
static uint32_t uiBlocks = 0xF;
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
static void
|
|
|
|
print_success_or_failure (bool bFailure, uint32_t * uiCounter)
|
2009-11-27 18:02:53 +01:00
|
|
|
{
|
2010-06-24 12:19:01 +02:00
|
|
|
printf ("%c", (bFailure) ? 'x' : '.');
|
2010-06-24 12:16:09 +02:00
|
|
|
if (uiCounter)
|
2010-06-24 12:19:01 +02:00
|
|
|
*uiCounter += (bFailure) ? 0 : 1;
|
2009-11-27 18:02:53 +01:00
|
|
|
}
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
static bool
|
|
|
|
read_card (void)
|
2009-09-02 22:15:21 +02:00
|
|
|
{
|
2009-11-27 18:02:53 +01:00
|
|
|
uint32_t page;
|
|
|
|
bool bFailure = false;
|
2010-06-24 12:16:09 +02:00
|
|
|
uint32_t uiReadedPages = 0;
|
2009-11-27 18:02:53 +01:00
|
|
|
|
2010-06-24 13:04:05 +02:00
|
|
|
printf ("Reading %d pages |", uiBlocks + 1);
|
2009-11-27 18:02:53 +01:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
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);
|
|
|
|
} 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);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
printf ("|\n");
|
2010-06-24 13:04:05 +02:00
|
|
|
printf ("Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
|
2010-06-24 12:19:01 +02:00
|
|
|
fflush (stdout);
|
2009-11-27 18:02:53 +01:00
|
|
|
|
|
|
|
return (!bFailure);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
static bool
|
|
|
|
write_card (void)
|
2009-09-02 22:15:21 +02:00
|
|
|
{
|
|
|
|
uint32_t uiBlock = 0;
|
2010-06-30 17:06:59 +02:00
|
|
|
int page = 0x4;
|
2009-09-02 22:15:21 +02:00
|
|
|
bool bFailure = false;
|
2010-06-24 12:16:09 +02:00
|
|
|
uint32_t uiWritenPages = 0;
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-30 17:06:59 +02:00
|
|
|
char buffer[BUFSIZ];
|
|
|
|
printf ("Write OTP bytes ? [yN] ");
|
|
|
|
fgets (buffer, BUFSIZ, stdin);
|
|
|
|
bool write_otp = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
|
|
|
|
|
|
|
/* We need to skip 3 first pages. */
|
2010-06-24 13:04:05 +02:00
|
|
|
printf ("Writing %d pages |", uiBlocks + 1);
|
2010-06-30 17:06:59 +02:00
|
|
|
printf ("sss");
|
|
|
|
|
|
|
|
if(write_otp) {
|
|
|
|
page = 0x3;
|
|
|
|
} else {
|
|
|
|
/* If user don't want to write OTP, we skip 1 page more. */
|
|
|
|
printf("s");
|
|
|
|
page = 0x4;
|
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
|
2010-07-24 20:54:50 +02:00
|
|
|
for (; page <= 0xF; page++) {
|
2010-06-24 12:19:01 +02:00
|
|
|
// Show if the readout went well
|
|
|
|
if (bFailure) {
|
|
|
|
// When a failure occured we need to redo the anti-collision
|
2010-07-21 12:37:37 +02:00
|
|
|
if (!nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
2010-06-24 13:04:05 +02:00
|
|
|
ERR ("tag was removed");
|
2010-06-24 12:19:01 +02:00
|
|
|
return false;
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
bFailure = false;
|
|
|
|
}
|
2010-06-30 16:54:34 +02:00
|
|
|
|
|
|
|
// For the Mifare Ultralight, this write command can be used
|
|
|
|
// in compatibility mode, which only actually writes the first
|
|
|
|
// 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))
|
|
|
|
bFailure = true;
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
print_success_or_failure (bFailure, &uiWritenPages);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
printf ("|\n");
|
2010-06-30 17:06:59 +02:00
|
|
|
printf ("Done, %d of %d pages written (%d first pages are skipped).\n", uiWritenPages, uiBlocks + 1, write_otp?3:4);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-09-02 22:15:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
int
|
|
|
|
main (int argc, const char *argv[])
|
|
|
|
{
|
2009-09-02 22:15:21 +02:00
|
|
|
bool bReadAction;
|
2010-06-24 12:19:01 +02:00
|
|
|
byte_t *pbtUID;
|
|
|
|
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");
|
2009-09-02 22:15:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
DBG ("\nChecking arguments and settings\n");
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
bReadAction = tolower ((int) ((unsigned char) *(argv[1])) == 'r');
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
if (bReadAction) {
|
|
|
|
memset (&mtDump, 0x00, sizeof (mtDump));
|
2009-09-02 22:15:21 +02:00
|
|
|
} else {
|
2010-06-24 12:19:01 +02:00
|
|
|
pfDump = fopen (argv[2], "rb");
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
if (pfDump == NULL) {
|
|
|
|
ERR ("Could not open dump file: %s\n", argv[2]);
|
2009-09-02 22:15:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
|
|
|
ERR ("Could not read from dump file: %s\n", argv[2]);
|
|
|
|
fclose (pfDump);
|
2009-09-02 22:15:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
fclose (pfDump);
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
DBG ("Successfully opened the dump file\n");
|
2009-09-02 22:15:21 +02:00
|
|
|
|
|
|
|
// Try to open the NFC reader
|
2010-06-24 12:19:01 +02:00
|
|
|
pnd = nfc_connect (NULL);
|
|
|
|
if (pnd == NULL) {
|
|
|
|
ERR ("Error connecting NFC reader\n");
|
2009-09-02 22:15:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
nfc_initiator_init (pnd);
|
2009-09-02 22:15:21 +02:00
|
|
|
|
|
|
|
// Drop the field for a while
|
2010-06-24 12:19:01 +02:00
|
|
|
nfc_configure (pnd, NDO_ACTIVATE_FIELD, false);
|
2009-10-02 11:52:02 +02:00
|
|
|
|
2009-09-02 22:15:21 +02:00
|
|
|
// Let the reader only try once to find a tag
|
2010-06-24 12:19:01 +02:00
|
|
|
nfc_configure (pnd, NDO_INFINITE_SELECT, false);
|
|
|
|
nfc_configure (pnd, NDO_HANDLE_CRC, true);
|
|
|
|
nfc_configure (pnd, NDO_HANDLE_PARITY, true);
|
2009-09-02 22:15:21 +02:00
|
|
|
|
|
|
|
// Enable field so more power consuming cards can power themselves up
|
2010-06-24 12:19:01 +02:00
|
|
|
nfc_configure (pnd, NDO_ACTIVATE_FIELD, true);
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
printf ("Connected to NFC reader: %s\n", pnd->acName);
|
2009-09-02 22:15:21 +02:00
|
|
|
|
|
|
|
// Try to find a MIFARE Ultralight tag
|
2010-07-21 12:37:37 +02:00
|
|
|
if (!nfc_initiator_select_passive_target (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
2010-06-24 12:19:01 +02:00
|
|
|
ERR ("no tag was found\n");
|
|
|
|
nfc_disconnect (pnd);
|
2009-09-02 22:15:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Test if we are dealing with a MIFARE compatible tag
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
if (nti.nai.abtAtqa[1] != 0x44) {
|
|
|
|
ERR ("tag is not a MIFARE Ultralight card\n");
|
|
|
|
nfc_disconnect (pnd);
|
|
|
|
return EXIT_FAILURE;
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-04-17 00:38:45 +02:00
|
|
|
// Get the info from the current tag (UID is stored little-endian)
|
2009-11-18 12:52:18 +01:00
|
|
|
pbtUID = nti.nai.abtUid;
|
2010-06-24 12:19:01 +02:00
|
|
|
printf ("Found MIFARE Ultralight card with UID: %02x%02x%02x%02x\n", pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]);
|
|
|
|
|
|
|
|
if (bReadAction) {
|
|
|
|
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]);
|
2010-06-24 12:16:09 +02:00
|
|
|
return EXIT_FAILURE;
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
|
|
|
printf ("Could not write to file: %s\n", argv[2]);
|
2010-06-24 12:16:09 +02:00
|
|
|
return EXIT_FAILURE;
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
2010-06-24 12:19:01 +02:00
|
|
|
fclose (pfDump);
|
|
|
|
printf ("Done.\n");
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
2010-06-24 12:19:01 +02:00
|
|
|
write_card ();
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 12:19:01 +02:00
|
|
|
nfc_disconnect (pnd);
|
2009-09-02 22:15:21 +02:00
|
|
|
|
2010-06-24 12:16:09 +02:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-02 22:15:21 +02:00
|
|
|
}
|