Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write operation failed: Status code: 0x6300 #25

Closed
bdsomer opened this issue Sep 9, 2017 · 5 comments
Closed

Write operation failed: Status code: 0x6300 #25

bdsomer opened this issue Sep 9, 2017 · 5 comments
Assignees
Labels

Comments

@bdsomer
Copy link

bdsomer commented Sep 9, 2017

A very simple program that just writes to a card:

const { NFC } = require('nfc-pcsc');
const nfc = new NFC();

nfc.on('reader', (reader) => {
	reader.on('card', async (card) => {
		try {
			const data = Buffer.allocUnsafe(16);
			data.fill(0);
			const randomNumber = Math.round(Math.random() * 1000);
			data.writeInt16BE(randomNumber);

			await reader.write(4, data);
		} catch (err) {
			console.error(err)
		}
	})
})

When a card is touched to the reader, the error occurs:

{ Error: Write operation failed: Status code: 0x6300
    at /.../node_modules/nfc-pcsc/dist/Reader.js:578:11
    at Generator.next (<anonymous>)
    at step (/.../node_modules/nfc-pcsc/dist/Reader.js:18:191)
    at /.../node_modules/nfc-pcsc/dist/Reader.js:18:361
    at <anonymous> name: 'WriteError', code: 'operation_failed' }

Before writing, I've tried adding:

await reader.authenticate(4, KEY_TYPE_A, 'ffffffffffff')

and

await reader.loadAuthenticationKey(0, 'ffffffffffff')
await reader.authenticate(4, KEY_TYPE_A, 'ffffffffffff')

without success.

What's causing this? Is this a bug?

I was getting the same status code at a lower level, as well.

@pokusew
Copy link
Owner

pokusew commented Sep 9, 2017

Hi @bdsomer,

it is certainly not a bug (hopefully). 😄

Firstly, what card you you use? (Mifare Classic?)

In case, that you use Mifare Classic, you need to authenticate every block, that you want to access (write to). That you do with the following:

await reader.authenticate(4, KEY_TYPE_A, 'ffffffffffff');

Note: reader.loadAuthenticationKey is internal API, you don't need to use it. It is called automatically and in highly efficient manner, when you call reader.authenticate.

Further, if you use Mifare Classic, you need to pass the third parameter (blockSize) to reader.write. Mifare Classic cards have block size 16. So you can write data like this:

await reader.write(4, data, 16);

Please also be ware of the memory organization (structure) of Mifare Classic card – see my comment explaining it.
You should to be cautious, when writing more than 16 bytes of data with single reader.write call. For example if you want to write 64 bytes of data starting in block number 4 using block size 16 (needed for Mifare Classic) and you call reader.write(4, data, 16), under the hood it will result in four commands (reader.write(4, data1, 16), reader.write(5, data2, 16), reader.write(6, data3, 16), reader.write(7, data4, 16)) (e.g. 64 / 16 = 4). The problem is the memory organization of Mifare Classic card. Last block in each sector is "sector trailer" (in this case 7) which contains access conditions for the other 3 data blocks (for 4, 5, 6) in the sector.

Finally, take a look at the main example, where everything is explained in comments (note that you need to uncomment some code to make it work with Mifare Classic). Just read the comments I you will be okay. 🙂

That should be all. Please let me know if it solves your problems.

Hope it helps.

@pokusew pokusew self-assigned this Sep 9, 2017
@bdsomer
Copy link
Author

bdsomer commented Sep 9, 2017

@pokusew Awesome, thanks! This solved my problem.

To fix, I followed your steps:

  • Remove await reader.loadAuthenticationKey(0, 'ffffffffffff')
  • Pass the third parameter to the reader#read and reader#write methods

Thanks, again. 👍

@bdsomer bdsomer closed this as completed Sep 9, 2017
@Philip-Nunoo
Copy link

@bdsomer could you give a full descripton and better summary. I'm facing similar issues here as well.

thanks.

@pokusew
Copy link
Owner

pokusew commented Sep 13, 2017

Hi @Philip-Nunoo,

firstly, I suppose you use Mifare Classic card, right?

I think that everything is summed up in my previous comment #25 (comment). There is a link to the example, where everything is explained with comments and code (you need to uncomment some code for Mifare Classic as there is described) 🙂

Just a small recapitulation – how to read/write data on Mifare Classic card (just for your convenience, but be sure to read my previous comment, links and example):

  1. You need to authenticate every block, you want to access (read or write), using reader.authenticate as shown here (remove Promise.all if you don't need to authenticate more blocks). Ensure, that you use the correct key (default is ffffffffffff , as far as I know) and the correct key type.

  2. Then don't forget to pass the third parameter – block size 16 (number) to the reader.read and reader.write calls.

  3. Lastly be aware of the memory organization (structure) of Mifare Classic card. See my previous comment.

Hope it helps. 🙂


PS Don't forget to star ⭐️ my library, if you find it useful. 😃 Thanks.

@Philip-Nunoo
Copy link

@pokusew nice. Was able to figure it out and thanks for the prompt reply as well.

I'd sure ⭐ it ayt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants