|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #include "MFRC522Hack.h"
-
-
- bool MFRC522Hack::MIFARE_OpenUidBackdoor(const bool logErrors) const {
-
-
-
-
-
-
-
-
- _device->PICC_HaltA();
-
- byte cmd = 0x40;
- byte validBits = 7;
-
- byte response[32];
- byte received;
- MFRC522::StatusCode status = _device->PCD_TransceiveData(&cmd, (byte) 1, response, &received, &validBits, (byte) 0,
- false);
- if (status != MFRC522::STATUS_OK) {
- if (logErrors) {
- Serial.println(
- F("Card did not respond to 0x40 after HALT command. Are you sure it is a UID changeable one?"));
- Serial.print(F("Error name: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- }
- return false;
- }
- if (received != 1 || response[0] != 0x0A) {
- if (logErrors) {
- Serial.print(F("Got bad response on backdoor 0x40 command: "));
- Serial.print(response[0], HEX);
- Serial.print(F(" ("));
- Serial.print(validBits);
- Serial.print(F(" valid bits)\r\n"));
- }
- return false;
- }
-
- cmd = 0x43;
- validBits = 8;
- status = _device->PCD_TransceiveData(&cmd, (byte) 1, response, &received, &validBits, (byte) 0, false);
- if (status != MFRC522::STATUS_OK) {
- if (logErrors) {
- Serial.println(F("Error in communication at command 0x43, after successfully executing 0x40"));
- Serial.print(F("Error name: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- }
- return false;
- }
- if (received != 1 || response[0] != 0x0A) {
- if (logErrors) {
- Serial.print(F("Got bad response on backdoor 0x43 command: "));
- Serial.print(response[0], HEX);
- Serial.print(F(" ("));
- Serial.print(validBits);
- Serial.print(F(" valid bits)\r\n"));
- }
- return false;
- }
-
-
- return true;
- }
-
-
- bool MFRC522Hack::MIFARE_SetUid(const byte *newUid, const byte uidSize, const bool logErrors) const {
-
-
- if (!newUid || !uidSize || uidSize > 15) {
- if (logErrors) {
- Serial.println(F("New UID buffer empty, size 0, or size > 15 given"));
- }
- return false;
- }
-
-
- MFRC522::MIFARE_Key key = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- MFRC522::StatusCode status = _device->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte) 1, &key, &(_device->uid));
- if (status != MFRC522::STATUS_OK) {
-
- if (status == MFRC522::STATUS_TIMEOUT) {
-
-
-
-
-
-
-
- if (!_device->PICC_IsNewCardPresent() || !_device->PICC_ReadCardSerial()) {
- Serial.println(F("No card was previously selected, and none are available. Failed to set UID."));
- return false;
- }
-
- status = _device->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, (byte) 1, &key, &(_device->uid));
- if (status != MFRC522::STATUS_OK) {
-
- if (logErrors) {
- Serial.println(F("Failed to authenticate to card for reading, could not set UID: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- }
- return false;
- }
- } else {
- if (logErrors) {
- Serial.print(F("PCD_Authenticate() failed: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- }
- return false;
- }
- }
-
-
- byte block0_buffer[18];
- byte byteCount = sizeof(block0_buffer);
- status = _device->MIFARE_Read((byte) 0, block0_buffer, &byteCount);
- if (status != MFRC522::STATUS_OK) {
- if (logErrors) {
- Serial.print(F("MIFARE_Read() failed: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- Serial.println(F("Are you sure your KEY A for sector 0 is 0xFFFFFFFFFFFF?"));
- }
- return false;
- }
-
-
- byte bcc = 0;
- for (uint8_t i = 0; i < uidSize; i++) {
- block0_buffer[i] = newUid[i];
- bcc ^= newUid[i];
- }
-
-
- block0_buffer[uidSize] = bcc;
-
-
- _device->PCD_StopCrypto1();
-
-
- if (!MIFARE_OpenUidBackdoor(logErrors)) {
- if (logErrors) {
- Serial.println(F("Activating the UID backdoor failed."));
- }
- return false;
- }
-
-
- status = _device->MIFARE_Write((byte) 0, block0_buffer, (byte) 16);
- if (status != MFRC522::STATUS_OK) {
- if (logErrors) {
- Serial.print(F("MIFARE_Write() failed: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- }
- return false;
- }
-
-
- byte atqa_answer[2];
- byte atqa_size = 2;
- _device->PICC_WakeupA(atqa_answer, &atqa_size);
-
- return true;
- }
-
-
- bool MFRC522Hack::MIFARE_UnbrickUidSector(const bool logErrors) const {
- MIFARE_OpenUidBackdoor(logErrors);
-
- byte block0_buffer[] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00};
-
-
- MFRC522::StatusCode status = _device->MIFARE_Write((byte) 0, block0_buffer, (byte) 16);
- if (status != MFRC522::STATUS_OK) {
- if (logErrors) {
- Serial.print(F("MIFARE_Write() failed: "));
- Serial.println(MFRC522Debug::GetStatusCodeName(status));
- }
- return false;
- }
- return true;
- }
|