2010-02-19 11:43:06 +01:00
|
|
|
/*-
|
|
|
|
* Copyright (C) 2010, Romain Tartiere, Romuald Conty.
|
|
|
|
*
|
|
|
|
* 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/>
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2010-01-10 15:24:26 +01:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2010-01-07 00:44:17 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nfc/nfc.h>
|
|
|
|
|
2010-01-08 14:40:39 +01:00
|
|
|
#include <freefare.h>
|
2010-01-07 00:44:17 +01:00
|
|
|
|
2010-06-26 13:57:25 +02:00
|
|
|
#define START_FORMAT_N "Formatting %d blocks"
|
|
|
|
#define DONE_FORMAT " done.\n"
|
|
|
|
|
2010-01-10 15:24:26 +01:00
|
|
|
MifareClassicKey default_keys[] = {
|
|
|
|
{ 0xff,0xff,0xff,0xff,0xff,0xff },
|
|
|
|
{ 0xd3,0xf7,0xd3,0xf7,0xd3,0xf7 },
|
|
|
|
{ 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5 },
|
|
|
|
{ 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5 },
|
|
|
|
{ 0x4d,0x3a,0x99,0xc3,0x51,0xdd },
|
|
|
|
{ 0x1a,0x98,0x2c,0x7e,0x45,0x9a },
|
|
|
|
{ 0xaa,0xbb,0xcc,0xdd,0xee,0xff },
|
|
|
|
{ 0x00,0x00,0x00,0x00,0x00,0x00 }
|
|
|
|
};
|
2010-02-23 03:12:18 +01:00
|
|
|
int format_mifare_classic_1k (MifareTag tag);
|
|
|
|
int format_mifare_classic_4k (MifareTag tag);
|
|
|
|
int try_format_sector (MifareTag tag, MifareClassicBlockNumber block);
|
|
|
|
|
2010-06-26 13:57:25 +02:00
|
|
|
int at_block = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
display_progress ()
|
|
|
|
{
|
|
|
|
at_block++;
|
|
|
|
if (0 == (at_block % 10)) {
|
|
|
|
printf ("%d", at_block);
|
|
|
|
fflush (stdout);
|
|
|
|
} else if (0 == (at_block % 2)) {
|
|
|
|
printf (".");
|
|
|
|
fflush (stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-23 03:12:18 +01:00
|
|
|
int
|
|
|
|
format_mifare_classic_1k (MifareTag tag)
|
|
|
|
{
|
2010-06-26 13:57:25 +02:00
|
|
|
printf (START_FORMAT_N, 16);
|
2010-02-23 03:12:18 +01:00
|
|
|
for (int sector = 0; sector < 16; sector++) {
|
|
|
|
if (!try_format_sector (tag, sector * 4))
|
|
|
|
return 0;
|
|
|
|
}
|
2010-06-26 13:57:25 +02:00
|
|
|
printf (DONE_FORMAT);
|
2010-02-23 03:12:18 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
format_mifare_classic_4k (MifareTag tag)
|
|
|
|
{
|
2010-06-26 13:57:25 +02:00
|
|
|
printf (START_FORMAT_N, 32 + 8);
|
2010-02-23 03:12:18 +01:00
|
|
|
for (int sector = 0; sector < 32; sector++) {
|
|
|
|
if (!try_format_sector (tag, sector * 4))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int sector = 0; sector < 8; sector++) {
|
|
|
|
if (!try_format_sector (tag, 128 + sector * 16))
|
|
|
|
return 0;
|
|
|
|
}
|
2010-06-26 13:57:25 +02:00
|
|
|
printf (DONE_FORMAT);
|
2010-02-23 03:12:18 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-01-07 00:44:17 +01:00
|
|
|
|
|
|
|
int
|
2010-02-23 03:12:18 +01:00
|
|
|
try_format_sector (MifareTag tag, MifareClassicBlockNumber block)
|
2010-01-07 00:44:17 +01:00
|
|
|
{
|
2010-06-26 13:57:25 +02:00
|
|
|
display_progress ();
|
2010-01-10 15:24:26 +01:00
|
|
|
for (int i = 0; i < (sizeof (default_keys) / sizeof (MifareClassicKey)); i++) {
|
2010-02-23 03:12:18 +01:00
|
|
|
if ((0 == mifare_classic_connect (tag)) && (0 == mifare_classic_authenticate (tag, block, default_keys[i], MFC_KEY_A))) {
|
|
|
|
if (0 == mifare_classic_format_sector (tag, block)) {
|
2010-01-10 15:24:26 +01:00
|
|
|
mifare_classic_disconnect (tag);
|
2010-02-23 03:12:18 +01:00
|
|
|
return 1;
|
2010-01-10 15:24:26 +01:00
|
|
|
} else if (EIO == errno) {
|
2010-02-23 03:12:18 +01:00
|
|
|
err (EXIT_FAILURE, "block %d", block);
|
2010-01-10 15:24:26 +01:00
|
|
|
}
|
|
|
|
mifare_classic_disconnect (tag);
|
2010-01-07 00:44:17 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 03:12:18 +01:00
|
|
|
if ((0 == mifare_classic_connect (tag)) && (0 == mifare_classic_authenticate (tag, block, default_keys[i], MFC_KEY_B))) {
|
|
|
|
if (0 == mifare_classic_format_sector (tag, block)) {
|
2010-01-10 15:24:26 +01:00
|
|
|
mifare_classic_disconnect (tag);
|
2010-02-23 03:12:18 +01:00
|
|
|
return 1;
|
2010-01-10 15:24:26 +01:00
|
|
|
} else if (EIO == errno) {
|
2010-02-23 03:12:18 +01:00
|
|
|
err (EXIT_FAILURE, "block %d", block);
|
2010-01-10 15:24:26 +01:00
|
|
|
}
|
|
|
|
mifare_classic_disconnect (tag);
|
2010-01-07 00:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-23 03:12:18 +01:00
|
|
|
warnx ("No known authentication key for block %d", block);
|
|
|
|
return 0;
|
2010-01-07 00:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-01-10 15:24:26 +01:00
|
|
|
main(int argc, char *argv[])
|
2010-01-07 00:44:17 +01:00
|
|
|
{
|
2010-01-10 15:24:26 +01:00
|
|
|
int error = 0;
|
|
|
|
nfc_device_t *device = NULL;
|
2010-02-19 15:50:18 +01:00
|
|
|
MifareTag *tags = NULL;
|
2010-01-07 00:44:17 +01:00
|
|
|
|
2010-01-10 15:24:26 +01:00
|
|
|
device = nfc_connect (NULL);
|
|
|
|
if (!device)
|
|
|
|
errx (EXIT_FAILURE, "No NFC device found.");
|
2010-01-07 00:44:17 +01:00
|
|
|
|
2010-02-19 15:50:18 +01:00
|
|
|
tags = freefare_get_tags (device);
|
2010-01-10 15:24:26 +01:00
|
|
|
if (!tags) {
|
|
|
|
nfc_disconnect (device);
|
|
|
|
errx (EXIT_FAILURE, "Error listing MIFARE classic tag.");
|
2010-01-07 00:44:17 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 03:12:18 +01:00
|
|
|
for (int i = 0; (!error) && tags[i]; i++) {
|
|
|
|
switch (freefare_get_tag_type (tags[i])) {
|
|
|
|
case CLASSIC_1K:
|
|
|
|
case CLASSIC_4K:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-07 00:44:17 +01:00
|
|
|
|
2010-03-30 18:24:37 +02:00
|
|
|
char *tag_uid = freefare_get_tag_uid (tags[i]);
|
2010-02-23 03:12:18 +01:00
|
|
|
char buffer[BUFSIZ];
|
|
|
|
|
2010-03-01 15:04:47 +01:00
|
|
|
printf ("Found %s with UID %s. Format [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid);
|
2010-02-23 03:12:18 +01:00
|
|
|
fgets (buffer, BUFSIZ, stdin);
|
|
|
|
bool format = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
|
|
|
|
|
|
|
|
if (format) {
|
2010-06-26 13:57:25 +02:00
|
|
|
at_block = 0;
|
2010-02-23 03:12:18 +01:00
|
|
|
switch (freefare_get_tag_type (tags[i])) {
|
|
|
|
case CLASSIC_1K:
|
|
|
|
if (!format_mifare_classic_1k (tags[i]))
|
|
|
|
error = 1;
|
|
|
|
break;
|
|
|
|
case CLASSIC_4K:
|
|
|
|
if (!format_mifare_classic_4k (tags[i]))
|
|
|
|
error = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Keep compiler quiet */
|
|
|
|
break;
|
2010-01-10 15:24:26 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-07 00:44:17 +01:00
|
|
|
|
2010-02-23 03:12:18 +01:00
|
|
|
free (tag_uid);
|
2010-01-07 00:44:17 +01:00
|
|
|
}
|
|
|
|
|
2010-02-19 15:50:18 +01:00
|
|
|
freefare_free_tags (tags);
|
2010-01-10 15:24:26 +01:00
|
|
|
nfc_disconnect (device);
|
2010-01-07 00:44:17 +01:00
|
|
|
|
2010-01-10 15:24:26 +01:00
|
|
|
exit (error);
|
2010-01-07 00:44:17 +01:00
|
|
|
}
|