- if (strategy == 2) {
- // test with additional halt command
- halt_time = 0;
- int len = mifare_sendcmd_short(NULL, false, 0x50, 0x00, receivedAnswer, receivedAnswerPar, &halt_time);
-
- if (len && MF_DBGLEVEL >= 3)
- Dbprintf("Unexpected response of %d bytes to halt command.", len);
- }
-
- if (strategy == 3) {
- // test with FPGA power off/on
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- SpinDelay(200);
- iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
- SpinDelay(100);
- sync_time = GetCountSspClk() & 0xfffffff8;
- WDT_HIT();
- }
-
- if (!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {
- if (MF_DBGLEVEL >= 2) Dbprintf("Mifare: Can't select card\n");
- continue;
+ // this part is from Piwi's faster nonce collecting part in Hardnested.
+ if (!have_uid) { // need a full select cycle to get the uid first
+ iso14a_card_select_t card_info;
+ if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {
+ if (MF_DBGLEVEL >= 4) Dbprintf("Mifare: Can't select card (ALL)");
+ break;
+ }
+ switch (card_info.uidlen) {
+ case 4 : cascade_levels = 1; break;
+ case 7 : cascade_levels = 2; break;
+ case 10: cascade_levels = 3; break;
+ default: break;
+ }
+ have_uid = TRUE;
+ } else { // no need for anticollision. We can directly select the card
+ if(!iso14443a_select_card(uid, NULL, &cuid, false, cascade_levels)) {
+ if (MF_DBGLEVEL >= 4) Dbprintf("Mifare: Can't select card (UID)");
+ continue;
+ }