38 #endif // HAVE_CONFIG_H
56 static mifare_param mp;
57 static mifareul_tag mtDump;
58 static uint32_t uiBlocks = 0xF;
66 print_success_or_failure (
bool bFailure, uint32_t * uiCounter)
68 printf (
"%c", (bFailure) ?
'x' :
'.');
70 *uiCounter += (bFailure) ? 0 : 1;
77 bool bFailure =
false;
78 uint32_t uiReadedPages = 0;
80 printf (
"Reading %d pages |", uiBlocks + 1);
82 for (page = 0; page <= uiBlocks; page += 4) {
84 if (nfc_initiator_mifare_cmd (pnd, MC_READ, page, &mp)) {
85 memcpy (mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
91 print_success_or_failure (bFailure, &uiReadedPages);
92 print_success_or_failure (bFailure, &uiReadedPages);
93 print_success_or_failure (bFailure, &uiReadedPages);
94 print_success_or_failure (bFailure, &uiReadedPages);
97 printf (
"Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
106 uint32_t uiBlock = 0;
107 bool bFailure =
false;
108 uint32_t uiWritenPages = 0;
109 uint32_t uiSkippedPages;
115 printf (
"Write OTP bytes ? [yN] ");
116 if (!fgets (buffer, BUFSIZ, stdin)) {
117 ERR (
"Unable to read standard input.");
119 write_otp = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
120 printf (
"Write Lock bytes ? [yN] ");
121 if (!fgets (buffer, BUFSIZ, stdin)) {
122 ERR (
"Unable to read standard input.");
124 write_lock = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
126 printf (
"Writing %d pages |", uiBlocks + 1);
131 for (
int page = 0x2; page <= 0xF; page++) {
132 if ((page==0x2) && (!write_lock)) {
137 if ((page==0x3) && (!write_otp)) {
146 ERR (
"tag was removed");
156 memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
157 if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, page, &mp))
160 print_success_or_failure (bFailure, &uiWritenPages);
163 printf (
"Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages);
169 main (
int argc,
const char *argv[])
176 printf (
"%s r|w <dump.mfd>\n", argv[0]);
178 printf (
"r|w - Perform read from or write to card\n");
179 printf (
"<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
184 DBG (
"\nChecking arguments and settings\n");
186 bReadAction = tolower ((
int) ((
unsigned char) *(argv[1])) ==
'r');
189 memset (&mtDump, 0x00,
sizeof (mtDump));
191 pfDump = fopen (argv[2],
"rb");
193 if (pfDump == NULL) {
194 ERR (
"Could not open dump file: %s\n", argv[2]);
198 if (fread (&mtDump, 1,
sizeof (mtDump), pfDump) !=
sizeof (mtDump)) {
199 ERR (
"Could not read from dump file: %s\n", argv[2]);
205 DBG (
"Successfully opened the dump file\n");
210 ERR (
"Error connecting NFC device\n");
240 printf (
"Connected to NFC device: %s\n", pnd->
acName);
244 ERR (
"no tag was found\n");
250 if (nt.nti.nai.abtAtqa[1] != 0x44) {
251 ERR (
"tag is not a MIFARE Ultralight card\n");
256 printf (
"Found MIFARE Ultralight card with UID: ");
258 for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) {
259 printf (
"%02x", nt.nti.nai.abtUid[szPos]);
265 printf (
"Writing data to file: %s ... ", argv[2]);
267 pfDump = fopen (argv[2],
"wb");
268 if (pfDump == NULL) {
269 printf (
"Could not open file: %s\n", argv[2]);
272 if (fwrite (&mtDump, 1,
sizeof (mtDump), pfDump) !=
sizeof (mtDump)) {
273 printf (
"Could not write to file: %s\n", argv[2]);