diff --git a/script/pn532_cli_unit.py b/script/pn532_cli_unit.py index 2145672..0cb5b02 100644 --- a/script/pn532_cli_unit.py +++ b/script/pn532_cli_unit.py @@ -1374,7 +1374,8 @@ def args_parser(self) -> ArgumentParserNoExit: examples: hf mf setuid -u 11223344 hf mf setuid -u 11223344 -g 2 - hf mf setuid --blk0 1122334444080400aabbccddeeff1122 -g 3 + hf mf setuid -u 11223344556677 -g 3 + hf mf setuid -u 11223344556677 --blk0 11223344556677084400120111003912 -g 3 hf mf setuid --blk0 1122334444080400aabbccddeeff1122 -g 4 --pwd 00000000 """ return parser @@ -1407,13 +1408,13 @@ def get_block0(self, uid, args): print(f"{CR}Block0 needs to be 16 bytes{C0}") return - uid = str_to_bytes(block0[0:8]) - bcc = 0 - bcc = uid[0] ^ uid[1] ^ uid[2] ^ uid[3] - # check if bcc is valid on the block0 - if block0[8:10] != format(bcc, "02x"): - print(f"{CR}Invalid BCC{C0}") - return + if not (block0[16:20].lower() == "4400" or block0[16:20].lower() == "4200"): + uid = str_to_bytes(block0[0:8]) + bcc = uid[0] ^ uid[1] ^ uid[2] ^ uid[3] + # check if bcc is valid on the block0 + if block0[8:10] != format(bcc, "02x"): + print(f"{CR}Invalid BCC{C0}") + return return str_to_bytes(block0) def gen1a_set_block0(self, block0: bytes): @@ -1476,10 +1477,11 @@ def gen2_set_block0(self, block0: bytes, key: bytes, use_key_b: bool = False): print(f" - {CR}Write failed.{C0}") def gen3_set_block0(self, uid: bytes, block0: bytes, lock: bool = False): - selectTag = self.cmd.selectTag() - if not selectTag: - print(f"{CR}Select tag failed{C0}") + isGen3 = self.cmd.isGen3() + if not isGen3: + print(f"{CR}Tag is not Gen3{C0}") return + print("Found Gen3 Tag") resp1 = self.cmd.setGen3Uid(uid) print( f"Set UID to {uid.hex().upper()}: {CG}Success{C0}" @@ -1551,6 +1553,7 @@ def on_exec(self, args: argparse.Namespace): block0 = self.get_block0(uid, args) if block0 == None: return + self.device_com.set_normal_mode() gen = args.g if gen == 1: self.gen1a_set_block0(block0) diff --git a/script/pn532_cmd.py b/script/pn532_cmd.py index 9ed7f3d..db024fb 100644 --- a/script/pn532_cmd.py +++ b/script/pn532_cmd.py @@ -342,6 +342,10 @@ def isGen3(self): return False def setGen3Uid(self, uid: bytes): + selected_tag = self.selectTag() + if selected_tag is None: + print(f"{CR}Select tag failed{C0}") + return options = { "activate_rf_field": 0, "wait_response": 1, @@ -352,7 +356,7 @@ def setGen3Uid(self, uid: bytes): } command = "90FBCCCC07" + uid.hex() resp = self.hf14a_raw( - options=options, resp_timeout_ms=1000, data=bytes.fromhex(command) + options=options, resp_timeout_ms=2000, data=bytes.fromhex(command) ) if resp[0] == 0x00: return True diff --git a/script/pn532_tag_scanner.py b/script/pn532_tag_scanner.py index 979a7d5..a462841 100644 --- a/script/pn532_tag_scanner.py +++ b/script/pn532_tag_scanner.py @@ -1,27 +1,8 @@ -import struct -import re -import ctypes -from typing import Union -import threading - import pn532_com -from unit.calc import crc16A, crc16Ccitt -from pn532_com import Response, DEBUG -from pn532_utils import expect_response -from pn532_enum import Command, MifareCommand, ApduCommand, TagFile, NdefCommand, Status -from pn532_enum import Pn532KillerCommand from pn532_cmd import Pn532CMD -from pn532_enum import ButtonPressFunction, ButtonType, MifareClassicDarksideStatus -from pn532_enum import MfcKeyType, MfcValueBlockOperator -from time import sleep -from pn532_utils import CC, CB, CG, C0, CY, CR import os import subprocess -import ndef -from multiprocessing import Pool, cpu_count -from typing import Union -from pathlib import Path from platform import uname import sys import select @@ -32,7 +13,6 @@ import tkinter as tk def test_fn(): - # connect to pn532 dev = pn532_com.Pn532Com() platform_name = uname().release if "Microsoft" in platform_name: @@ -102,6 +82,5 @@ def update_uid(): print("Error:", e) dev.close() - if __name__ == "__main__": test_fn()