Re-ident examples/nfc-mfultralight.c using "indent -br -ce --line-length120 -nut -i2 -ppi 2" command line.

This commit is contained in:
Romuald Conty 2010-06-24 10:19:01 +00:00
parent 470dd8be05
commit 688ebe2324

View file

@ -23,7 +23,7 @@
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" # include "config.h"
#endif // HAVE_CONFIG_H #endif // HAVE_CONFIG_H
#include <stdio.h> #include <stdio.h>
@ -41,203 +41,192 @@
#include "mifare.h" #include "mifare.h"
#include "nfc-utils.h" #include "nfc-utils.h"
static nfc_device_t* pnd; static nfc_device_t *pnd;
static nfc_target_info_t nti; static nfc_target_info_t nti;
static mifare_param mp; 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* uiCounter) static void
print_success_or_failure (bool bFailure, uint32_t * uiCounter)
{ {
printf("%c",(bFailure)?'x':'.'); printf ("%c", (bFailure) ? 'x' : '.');
if (uiCounter) if (uiCounter)
*uiCounter += (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 uiReadedPages = 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) {
{ // 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)) memcpy (mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
{ } else {
memcpy(mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16); bFailure = true;
} else { break;
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 ("|\n");
printf("Done, %d of %d blocks read.\n", uiReadedPages, uiBlocks+1); printf ("Done, %d of %d blocks read.\n", uiReadedPages, uiBlocks + 1);
fflush(stdout); fflush (stdout);
return (!bFailure); return (!bFailure);
} }
static bool write_card(void) 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 uiWritenPages = 0; uint32_t uiWritenPages = 0;
printf("Writing out %d blocks |",uiBlocks+1); printf ("Writing out %d blocks |", uiBlocks + 1);
/* We are writting only data pages, so we need to skip 4 pages. */ /* We are writting only data pages, so we need to skip 4 pages. */
printf("ssss"); printf ("ssss");
for (page = 0x4; page <= 0xF; page ++) { 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)) {
{ ERR ("!\nError: tag was removed\n");
ERR("!\nError: tag was removed\n"); return false;
return false;
}
bFailure = false;
} else {
// 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;
} }
print_success_or_failure(bFailure, &uiWritenPages); bFailure = false;
} else {
// 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;
}
print_success_or_failure (bFailure, &uiWritenPages);
} }
printf("|\n"); printf ("|\n");
printf("Done, %d of %d blocks written (4 first pages are skipped).\n", uiWritenPages, 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;
} }
int main(int argc, const char* argv[]) int
{ main (int argc, const char *argv[])
{
bool bReadAction; bool bReadAction;
byte_t* pbtUID; byte_t *pbtUID;
FILE* pfDump; FILE *pfDump;
if (argc < 3) if (argc < 3) {
{ printf ("\n");
printf("\n"); printf ("%s r|w <dump.mfd>\n", argv[0]);
printf("%s r|w <dump.mfd>\n", argv[0]); printf ("\n");
printf("\n"); printf ("r|w - Perform read from or write to card\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("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n"); printf ("\n");
printf("\n");
return 1; 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) if (bReadAction) {
{ memset (&mtDump, 0x00, sizeof (mtDump));
memset(&mtDump,0x00,sizeof(mtDump));
} else { } else {
pfDump = fopen(argv[2],"rb"); pfDump = fopen (argv[2], "rb");
if (pfDump == NULL) 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; return 1;
} }
if (fread(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump)) if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
{ ERR ("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);
} }
DBG("Successfully 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) {
{ ERR ("Error connecting NFC reader\n");
ERR("Error connecting NFC reader\n");
return 1; return 1;
} }
nfc_initiator_init(pnd); nfc_initiator_init (pnd);
// Drop the field for a while // Drop the field for a while
nfc_configure(pnd,NDO_ACTIVATE_FIELD,false); nfc_configure (pnd, NDO_ACTIVATE_FIELD, false);
// Let the reader only try once to find a tag // Let the reader only try once to find a tag
nfc_configure(pnd,NDO_INFINITE_SELECT,false); nfc_configure (pnd, NDO_INFINITE_SELECT, false);
nfc_configure(pnd,NDO_HANDLE_CRC,true); nfc_configure (pnd, NDO_HANDLE_CRC, true);
nfc_configure(pnd,NDO_HANDLE_PARITY,true); nfc_configure (pnd, NDO_HANDLE_PARITY, true);
// Enable field so more power consuming cards can power themselves up // Enable field so more power consuming cards can power themselves up
nfc_configure(pnd,NDO_ACTIVATE_FIELD,true); nfc_configure (pnd, NDO_ACTIVATE_FIELD, true);
printf("Connected to NFC reader: %s\n",pnd->acName); printf ("Connected to NFC reader: %s\n", pnd->acName);
// 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)) {
{ ERR ("no tag was found\n");
ERR("no tag was found\n"); nfc_disconnect (pnd);
nfc_disconnect(pnd);
return 1; return 1;
} }
// 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) {
ERR("tag is not a MIFARE Ultralight card\n"); ERR ("tag is not a MIFARE Ultralight card\n");
nfc_disconnect(pnd); nfc_disconnect (pnd);
return EXIT_FAILURE; 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) {
{ if (read_card ()) {
if (read_card()) printf ("Writing data to file: %s ... ", argv[2]);
{ fflush (stdout);
printf("Writing data to file: %s ... ",argv[2]); pfDump = fopen (argv[2], "wb");
fflush(stdout); if (pfDump == NULL) {
pfDump = fopen(argv[2],"wb"); printf ("Could not open file: %s\n", argv[2]);
if (pfDump == NULL)
{
printf("Could not open file: %s\n",argv[2]);
return EXIT_FAILURE; 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 EXIT_FAILURE; return EXIT_FAILURE;
} }
fclose(pfDump); fclose (pfDump);
printf("Done.\n"); printf ("Done.\n");
} }
} else { } else {
write_card(); write_card ();
} }
nfc_disconnect(pnd); nfc_disconnect (pnd);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }