45 #endif // HAVE_CONFIG_H
63 static mifare_param mp;
64 static mifareul_tag mtDump;
65 static uint32_t uiBlocks = 0xF;
73 print_success_or_failure(
bool bFailure, uint32_t *uiCounter)
75 printf(
"%c", (bFailure) ?
'x' :
'.');
77 *uiCounter += (bFailure) ? 0 : 1;
84 bool bFailure =
false;
85 uint32_t uiReadedPages = 0;
87 printf(
"Reading %d pages |", uiBlocks + 1);
89 for (page = 0; page <= uiBlocks; page += 4) {
92 memcpy(mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
98 print_success_or_failure(bFailure, &uiReadedPages);
99 print_success_or_failure(bFailure, &uiReadedPages);
100 print_success_or_failure(bFailure, &uiReadedPages);
101 print_success_or_failure(bFailure, &uiReadedPages);
104 printf(
"Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
113 uint32_t uiBlock = 0;
114 bool bFailure =
false;
115 uint32_t uiWritenPages = 0;
116 uint32_t uiSkippedPages = 0;
123 printf(
"Write OTP bytes ? [yN] ");
124 if (!fgets(buffer, BUFSIZ, stdin)) {
125 ERR(
"Unable to read standard input.");
127 write_otp = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
128 printf(
"Write Lock bytes ? [yN] ");
129 if (!fgets(buffer, BUFSIZ, stdin)) {
130 ERR(
"Unable to read standard input.");
132 write_lock = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
133 printf(
"Write UID bytes (only for special writeable UID cards) ? [yN] ");
134 if (!fgets(buffer, BUFSIZ, stdin)) {
135 ERR(
"Unable to read standard input.");
137 write_uid = ((buffer[0] ==
'y') || (buffer[0] ==
'Y'));
139 printf(
"Writing %d pages |", uiBlocks + 1);
146 for (
int page = uiSkippedPages; page <= 0xF; page++) {
147 if ((page == 0x2) && (!write_lock)) {
152 if ((page == 0x3) && (!write_otp)) {
161 ERR(
"tag was removed");
171 memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
175 print_success_or_failure(bFailure, &uiWritenPages);
178 printf(
"Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages);
184 main(
int argc,
const char *argv[])
191 printf(
"%s r|w <dump.mfd>\n", argv[0]);
193 printf(
"r|w - Perform read from or write to card\n");
194 printf(
"<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
199 DBG(
"\nChecking arguments and settings\n");
201 bReadAction = tolower((
int)((
unsigned char) * (argv[1])) ==
'r');
204 memset(&mtDump, 0x00,
sizeof(mtDump));
206 pfDump = fopen(argv[2],
"rb");
208 if (pfDump == NULL) {
209 ERR(
"Could not open dump file: %s\n", argv[2]);
213 if (fread(&mtDump, 1,
sizeof(mtDump), pfDump) !=
sizeof(mtDump)) {
214 ERR(
"Could not read from dump file: %s\n", argv[2]);
220 DBG(
"Successfully opened the dump file\n");
224 if (context == NULL) {
225 ERR(
"Unable to init libnfc (malloc)");
232 ERR(
"Error opening NFC device");
246 nfc_perror(pnd,
"nfc_device_set_property_bool");
256 ERR(
"no tag was found\n");
263 if (nt.nti.nai.abtAtqa[1] != 0x44) {
264 ERR(
"tag is not a MIFARE Ultralight card\n");
270 printf(
"Found MIFARE Ultralight card with UID: ");
272 for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) {
273 printf(
"%02x", nt.nti.nai.abtUid[szPos]);
279 printf(
"Writing data to file: %s ... ", argv[2]);
281 pfDump = fopen(argv[2],
"wb");
282 if (pfDump == NULL) {
283 printf(
"Could not open file: %s\n", argv[2]);
288 if (fwrite(&mtDump, 1,
sizeof(mtDump), pfDump) !=
sizeof(mtDump)) {
289 printf(
"Could not write to file: %s\n", argv[2]);