nfc-mfultralight: Fix read and write counters. Code cleanup.

This commit is contained in:
Romuald Conty 2010-06-24 10:16:09 +00:00
parent 2e1fa09166
commit 470dd8be05

View file

@ -19,7 +19,7 @@
/** /**
* @file nfc-mfultool.c * @file nfc-mfultool.c
* @brief * @brief MIFARE Ultralight dump tool
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -36,6 +36,7 @@
#include <ctype.h> #include <ctype.h>
#include <nfc/nfc.h> #include <nfc/nfc.h>
#include <nfc/nfc-messages.h>
#include "mifare.h" #include "mifare.h"
#include "nfc-utils.h" #include "nfc-utils.h"
@ -46,32 +47,23 @@ static mifare_param mp;
static mifareul_tag mtDump; static mifareul_tag mtDump;
static uint32_t uiBlocks = 0xF; static uint32_t uiBlocks = 0xF;
static void print_success_or_failure(bool bFailure, uint32_t* uiBlockCounter) static void print_success_or_failure(bool bFailure, uint32_t* uiCounter)
{ {
printf("%c",(bFailure)?'x':'.'); printf("%c",(bFailure)?'x':'.');
if (uiBlockCounter) if (uiCounter)
*uiBlockCounter += (bFailure)?0:1; *uiCounter += (bFailure)?0:1;
} }
static bool read_card(void) static bool read_card(void)
{ {
uint32_t page; uint32_t page;
bool bFailure = false; bool bFailure = false;
uint32_t uiReadBlocks = 0; uint32_t uiReadedPages = 0;
printf("Reading out %d blocks |",uiBlocks+1); printf("Reading out %d blocks |",uiBlocks+1);
for (page = 0; page <= uiBlocks; page += 4) for (page = 0; page <= uiBlocks; page += 4)
{ {
// Skip this the first time, bFailure it means nothing (yet)
if (page != 0)
{
print_success_or_failure(bFailure, &uiReadBlocks);
print_success_or_failure(bFailure, &uiReadBlocks);
print_success_or_failure(bFailure, &uiReadBlocks);
print_success_or_failure(bFailure, &uiReadBlocks);
}
// Try to read out the data block // Try to read out the data block
if (nfc_initiator_mifare_cmd(pnd,MC_READ,page,&mp)) if (nfc_initiator_mifare_cmd(pnd,MC_READ,page,&mp))
{ {
@ -80,11 +72,14 @@ static bool read_card(void)
bFailure = true; bFailure = true;
break; 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);
} }
//return bSuccess;
print_success_or_failure(bFailure, &uiReadBlocks);
printf("|\n"); printf("|\n");
printf("Done, %d of %d blocks read.\n", uiReadBlocks, uiBlocks+1); printf("Done, %d of %d blocks read.\n", uiReadedPages, uiBlocks+1);
fflush(stdout); fflush(stdout);
return (!bFailure); return (!bFailure);
@ -95,26 +90,25 @@ static bool write_card(void)
uint32_t uiBlock = 0; uint32_t uiBlock = 0;
int page; int page;
bool bFailure = false; bool bFailure = false;
uint32_t uiWriteBlocks = 0; uint32_t uiWritenPages = 0;
for (page = 0x4; page <= 0xF; page++) { printf("Writing out %d blocks |",uiBlocks+1);
/* We are writting only data pages, so we need to skip 4 pages. */
printf("ssss");
for (page = 0x4; page <= 0xF; page ++) {
// Show if the readout went well // Show if the readout went well
if (bFailure) if (bFailure)
{ {
printf("x"); // printf("x");
// When a failure occured we need to redo the anti-collision // When a failure occured we need to redo the anti-collision
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti)) if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti))
{ {
printf("!\nError: tag was removed\n"); ERR("!\nError: tag was removed\n");
return false; return false;
} }
bFailure = false; bFailure = false;
} } else {
fflush(stdout);
// Make sure a earlier write did not fail
if (!bFailure)
{
// For the Mifare Ultralight, this write command can be used // For the Mifare Ultralight, this write command can be used
// in compatibility mode, which only actually writes the first // in compatibility mode, which only actually writes the first
// page (4 bytes). The Ultralight-specific Write command only // page (4 bytes). The Ultralight-specific Write command only
@ -123,10 +117,10 @@ static bool write_card(void)
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16); 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; if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp)) bFailure = true;
} }
print_success_or_failure(bFailure, &uiWritenPages);
} }
print_success_or_failure(bFailure, &uiWriteBlocks);
printf("|\n"); printf("|\n");
printf("Done, %d of %d blocks written.\n", uiWriteBlocks, uiBlocks+1); printf("Done, %d of %d blocks written (4 first pages are skipped).\n", uiWritenPages, uiBlocks+1);
fflush(stdout); fflush(stdout);
return true; return true;
@ -149,7 +143,7 @@ int main(int argc, const char* argv[])
return 1; return 1;
} }
printf("\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');
@ -161,25 +155,25 @@ int main(int argc, const char* argv[])
if (pfDump == NULL) if (pfDump == NULL)
{ {
printf("Could not open dump file: %s\n",argv[2]); ERR("Could not open dump file: %s\n",argv[2]);
return 1; return 1;
} }
if (fread(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump)) if (fread(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump))
{ {
printf("Could not read from dump file: %s\n",argv[2]); ERR("Could not read from dump file: %s\n",argv[2]);
fclose(pfDump); fclose(pfDump);
return 1; return 1;
} }
fclose(pfDump); fclose(pfDump);
} }
printf("Succesful opened the dump file\n"); DBG("Successfully opened the dump file\n");
// Try to open the NFC reader // Try to open the NFC reader
pnd = nfc_connect(NULL); pnd = nfc_connect(NULL);
if (pnd == NULL) if (pnd == NULL)
{ {
printf("Error connecting NFC reader\n"); ERR("Error connecting NFC reader\n");
return 1; return 1;
} }
@ -201,7 +195,7 @@ int main(int argc, const char* argv[])
// Try to find a MIFARE Ultralight tag // Try to find a MIFARE Ultralight tag
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti)) if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti))
{ {
printf("Error: no tag was found\n"); ERR("no tag was found\n");
nfc_disconnect(pnd); nfc_disconnect(pnd);
return 1; return 1;
} }
@ -209,15 +203,15 @@ int main(int argc, const char* argv[])
// Test if we are dealing with a MIFARE compatible tag // Test if we are dealing with a MIFARE compatible tag
if (nti.nai.abtAtqa[1] != 0x44){ if (nti.nai.abtAtqa[1] != 0x44){
printf("Error: tag is not a MIFARE Ultralight card\n"); ERR("tag is not a MIFARE Ultralight card\n");
nfc_disconnect(pnd); nfc_disconnect(pnd);
return 1; return EXIT_FAILURE;
} }
// Get the info from the current tag (UID is stored little-endian) // Get the info from the current tag (UID is stored little-endian)
pbtUID = nti.nai.abtUid; pbtUID = nti.nai.abtUid;
printf("Found MIFARE Ultralight card with uid: %02x%02x%02x%02x\n", pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]); printf("Found MIFARE Ultralight card with UID: %02x%02x%02x%02x\n", pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]);
if (bReadAction) if (bReadAction)
{ {
@ -229,12 +223,12 @@ int main(int argc, const char* argv[])
if (pfDump == NULL) if (pfDump == NULL)
{ {
printf("Could not open file: %s\n",argv[2]); printf("Could not open file: %s\n",argv[2]);
return 1; return EXIT_FAILURE;
} }
if (fwrite(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump)) if (fwrite(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump))
{ {
printf("Could not write to file: %s\n",argv[2]); printf("Could not write to file: %s\n",argv[2]);
return 1; return EXIT_FAILURE;
} }
fclose(pfDump); fclose(pfDump);
printf("Done.\n"); printf("Done.\n");
@ -245,5 +239,5 @@ int main(int argc, const char* argv[])
nfc_disconnect(pnd); nfc_disconnect(pnd);
return 0; return EXIT_SUCCESS;
} }