Problem with RFID/NFC shield write/read

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
rabernat
 
Posts: 4
Joined: Mon Oct 22, 2012 10:13 pm

Problem with RFID/NFC shield write/read

Post by rabernat »

I would really appreciate your help. I'm trying to write and read data on MiFare Classic 1K cards using the Arduino RFID/NFC shield, Arduino Uno, and the 12C library. The problem is that once I've written data to a card, I can no longer read the data.

When I run the mifareclassic_formatndef sketch on my card (without making any changes, so it's supposed to be writing the "adafruit.com" url), here's the output:

Code: Select all

Looking for PN532...
Found chip PN532
Firmware ver. 1.6

Place your Mifare Classic card on the reader to format with NDEF
and press any key to continue ...
Found 1 tags
ATQA: 0x4
SAK: 0x8
UID: 0x2 0xC2 0x32 0xAF
Found an ISO14443A card
  UID Length: 4 bytes
  UID Value: 0x02 0xC2 0x32 0xAF

Seems to be a Mifare Classic card (4 byte UID)
Trying to authenticate card 0x02 0xC2 0x32 0xAF
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Trying to write 16 bytes to block 1
Trying to write 16 bytes to block 2
Trying to write 16 bytes to block 3
Card has been formatted for NDEF data using MAD1
Trying to authenticate card 0x02 0xC2 0x32 0xAF
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Writing URI to sector 1 as an NDEF Message
Trying to write 16 bytes to block 4
Trying to write 16 bytes to block 5
Trying to write 16 bytes to block 6
Trying to write 16 bytes to block 7
NDEF URI Record written to sector 1
So that seems fine. But then if I run the mifareclassic_memdump sketch on the same card, it can't read any of the blocks.

Code: Select all

Looking for PN532...
Found chip PN532
Firmware ver. 1.6
Waiting for an ISO14443A Card ...
Found 1 tags
ATQA: 0x4
SAK: 0x8
UID: 0x2 0xC2 0x32 0xAF
Found an ISO14443A card
  UID Length: 4 bytes
  UID Value: 0x02 0xC2 0x32 0xAF

Seems to be a Mifare Classic card (4 byte UID)
------------------------Sector 0-------------------------
Trying to authenticate card 0x02 0xC2 0x32 0xAF
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Trying to read 16 bytes from block 0
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 0 unable to read this block
Trying to read 16 bytes from block 1
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 1 unable to read this block
Trying to read 16 bytes from block 2
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 2 unable to read this block
Trying to read 16 bytes from block 3
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 3 unable to read this block
------------------------Sector 1-------------------------
Trying to authenticate card 0x02 0xC2 0x32 0xAF
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Trying to read 16 bytes from block 4
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 4 unable to read this block
Trying to read 16 bytes from block 5
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 5 unable to read this block
Trying to read 16 bytes from block 6
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 6 unable to read this block
Trying to read 16 bytes from block 7
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 7 unable to read this block
------------------------Sector 2-------------------------
Trying to authenticate card 0x02 0xC2 0x32 0xAF
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Trying to read 16 bytes from block 8
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 8 unable to read this block
Trying to read 16 bytes from block 9
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 9 unable to read this block
Trying to read 16 bytes from block 10
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 10 unable to read this block
Trying to read 16 bytes from block 11
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 11 unable to read this block
------------------------Sector 3-------------------------
Trying to authenticate card 0x02 0xC2 0x32 0xAF
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Trying to read 16 bytes from block 12
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 12 unable to read this block
Trying to read 16 bytes from block 13
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 13 unable to read this block
Trying to read 16 bytes from block 14
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 14 unable to read this block
Trying to read 16 bytes from block 15
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Block 15 unable to read this block
...
By the way, the readMifare sketch also can't read from the corrupted card. But I was able to read the (empty) data from the card before I tried to write to it. Also, I've tried this with multiple cards, with the same effect.

Since I haven't changed the examples at all, why doesn't this work? And what can I do to fix it? I'm stumped - but I'd love to get this working (since it's for a class project) so I appreciate your help.

User avatar
adafruit_support_bill
 
Posts: 88041
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem with RFID/NFC shield write/read

Post by adafruit_support_bill »

By the way, the readMifare sketch also can't read from the corrupted card.
Is it coming up with the same "80 80 80..."?

I'll see if I can reproduce this here later today.

rabernat
 
Posts: 4
Joined: Mon Oct 22, 2012 10:13 pm

Re: Problem with RFID/NFC shield write/read

Post by rabernat »

Yeah, here's the readMifare result:

Code: Select all

Hello!
Found chip PN532
Firmware ver. 1.6
Waiting for an ISO14443A Card ...
Found 1 tags
ATQA: 0x4
SAK: 0x8
UID: 0x4A 0x1 0xDA 0x65
Found an ISO14443A card
  UID Length: 4 bytes
  UID Value: 0x4A 0x01 0xDA 0x65

Seems to be a Mifare Classic card (4 byte UID)
Trying to authenticate block 4 with default KEYA value
Trying to authenticate card 0x4A 0x01 0xDA 0x65
Using authentication KEY A: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
Sector 1 (Blocks 4..7) has been authenticated
Trying to read 16 bytes from block 4
Unexpected response
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80  €€€€€€€€€€€€€€€€€€€€€€€€€€
Ooops ... unable to read the requested block.  Try another key?
If it helps, you should know that I've been working with a friend whose project also involves reading/writing RFID tags, and she got the same errors when she tried to write and read tags with her Arduino, shield, and code installation.

User avatar
adafruit_support_bill
 
Posts: 88041
Joined: Sat Feb 07, 2009 10:11 am

Re: Problem with RFID/NFC shield write/read

Post by adafruit_support_bill »

I spoke with the designer of the shield and he suspects that there is a bug in the latest version of the library. Hold on and we'll try to get this fixed as soon as we can.

rabernat
 
Posts: 4
Joined: Mon Oct 22, 2012 10:13 pm

Re: Problem with RFID/NFC shield write/read

Post by rabernat »

Thank very much!

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: Problem with RFID/NFC shield write/read

Post by ktownsend »

If you pull the latest code from github (https://github.com/adafruit/Adafruit_NFCShield_I2C) I pushed some changes. When you format a card for NDEF records the authentication keys also need to be changed (with the result you can no longer treat the cards as unformatted basic Mifare Classic cards anymore since you have changed the keys in sector 0, etc.

I've added this define to the memdump example ... set it to 1 or 0 depending on if this is an NDEF card or not:

Code: Select all

// ***********************
// TODO: !!! IMPORTANT !!!
// ***********************
// Set this field depending on if this is an NDEF formatted card.  
// For a non-NDEF card (for example an unformatted Mifare Classic card), 
// set this to 0.  
//
// NDEF formatted cards use specific keys for authentication, and the
// authentication requests will fail if you use the default Mifare keys
// (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for both key a and key B).
//
// Note that you will no longer be able to authenticate non-NDEF blocks
// when using NDEF keys since they will not match what is being provided.
// Also, because key a changes in block 0 (the MAD Sector for NDEF
// records), NDEF formatted cards will no longer work in normal
// Mifare mode.  Cards should be treated as NDEF or not, and it's best
// not to mix them because key managed becomes complicated.
//
// Possible values:
//
// READ_AS_NDEF = 0    No NDEF Sectors ... use default Mifare Classic keys
// READ_AS_NDEF = 1    NDEF Formatted Card ... use NDEF keys (only NDEF blocks will authenticate!)
//
// For more information see: 
//
// http://www.ladyada.net/products/rfidnfc/mifare.html
// http://www.ladyada.net/products/rfidnfc/ndef.html

#define READ_AS_NDEF (0)
I've also added a warning when formatting NDEF cards that they can no longer be read using the default keys.

rabernat
 
Posts: 4
Joined: Mon Oct 22, 2012 10:13 pm

Re: Problem with RFID/NFC shield write/read

Post by rabernat »

Thanks for the prompt reply. But I'm still having some trouble. Reading cards that I formatted earlier seems to work - I can see the "adafruit.com" that I wrote before - so that's great! But I haven't been able to rewrite the data. When I try to run the new formatndef program, it gets seems to get stuck trying to authenticate the card:

Code: Select all

Looking for PN532...
Found chip PN532
Firmware ver. 1.6

PLEASE NOTE: Formatting your card for NDEF records will change the
authentication keys and you will no longer be able to read the
card as a normal Mifare card without resetting all keys.  Try to keep
seperate cards for NDEF and non-NDEF purposes.

Place your Mifare Classic card on the reader to format with NDEF
and press any key to continue ...
Found 1 tags
ATQA: 0x4
SAK: 0x8
UID: 0x9A 0x8D 0xD4 0x65
Found an ISO14443A card
  UID Length: 4 bytes
  UID Value: 0x9A 0x8D 0xD4 0x65

Seems to be a Mifare Classic card (4 byte UID)
Trying to authenticate card 0x9
And that's the end of output. The final 0x9 is the beginning of the UID - the output from another card ends with 0x6, and so on. This produces no difference when I read the card again either.

These are cards that have already been formatted as NDEF - do I need to change the keya in the formatndef folder? Or is there some way to wipe the card and start over?

User avatar
mattsvensson
 
Posts: 1
Joined: Mon Oct 29, 2012 10:45 pm

Re: Problem with RFID/NFC shield write/read

Post by mattsvensson »

rabernat,

I have been have the same issue but found a workaround for now. I believe the issues is with AuthenticateBlock and FormatNDEF because when you comment out the below block of code from the mifareclassic_formatndef file, you can change the data that you had previously written. I know this isn't a PERFECT solution but it will work to re-write that sector with a new email/phone number/website.

I've still VERY new (in days...) to NFC and Arduinos so haven't had much time to do research, but if you find a way to do a good, full format of a card I'd love to see the code. So far I've just pieced together one file to save NFC data to an SD card so its nothing fancy...

Lines to comment out:
// Try to format the card for NDEF data
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya);
if (!success)
{
Serial.println("Unable to authenticate block 0 to enable card formatting!");
return;
}
success = nfc.mifareclassic_FormatNDEF();
if (!success)
{
Serial.println("Unable to format the card for NDEF");
return;
}
Serial.println("Card has been formatted for NDEF data using MAD1");

AntonioHolguin
 
Posts: 12
Joined: Thu Aug 25, 2011 4:12 pm

Re: Problem with RFID/NFC shield write/read

Post by AntonioHolguin »

These are cards that have already been formatted as NDEF - do I need to change the keya in the formatndef folder? Or is there some way to wipe the card and start over?
Has there been any further information on this?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Problem with RFID/NFC shield write/read

Post by adafruit_support_rick »

The library and example sketches have been updated recently.

Note additional comments regarding NDEF and Mifare keys:
PLEASE NOTE: Formatting your card for NDEF records will change the
authentication keys and you will no longer be able to read the
card as a normal Mifare card without resetting all keys. Try to keep
separate cards for NDEF and non-NDEF purposes.

nomnom
 
Posts: 14
Joined: Wed Nov 14, 2012 12:46 am

Re: Problem with RFID/NFC shield write/read

Post by nomnom »

@driverblock
how do you reset all the keys? is there another way to un-format an ndef card?

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: Problem with RFID/NFC shield write/read

Post by ktownsend »

You'll really have to dig into the datasheets for this. You'll need to write new default Mifare keys to the card (FF FF FF FF FF FF), using the previous NDEF keys, and reset each of the blocks back to their original state including the sector control bits in the last block of every sector.

As is, NDEF is a one-way street and you should really keep seperate cards for NDEF and normal Mifare Classic.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”