Esempio n. 1
0
        private byte[] SendCommand(byte[] command)
        {
            if (Port == null)
            {
                return(null);
            }

            Pm3UsbCommand cmd = new Pm3UsbCommand(0x313, (byte)command.Length, 1, 1, command);

            LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: Send " + BitConverter.ToString(command).Replace("-", ""));
            cmd.Write(Port);

            while (true)
            {
                Pm3UsbResponse response = new Pm3UsbResponse(Port);

                switch (response.Cmd)
                {
                case Pm3UsbResponse.eResponseType.DebugString:
                {
                    string debugStr = Encoding.UTF8.GetString(response.data.d, 0, response.data.dataLen);
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: DebugMessage '" + debugStr + "'");
                    break;
                }

                case Pm3UsbResponse.eResponseType.NoData:
                case Pm3UsbResponse.eResponseType.Timeout:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: timeout, returning");
                    return(null);

                case Pm3UsbResponse.eResponseType.ACK:
                    if (response.data.arg[0] > 0 && response.data.arg[0] < response.data.d.Length)
                    {
                        byte[] ret = new byte[response.data.arg[0]];
                        Array.Copy(response.data.d, ret, response.data.arg[0]);

                        if (!ISO15693.CheckChecksum(ret))
                        {
                            LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: ACK, but Checksum failed");
                            return(null);
                        }

                        LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: ACK, returning data");
                        return(ret);
                    }
                    else
                    {
                        LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: no tag answered, returning");
                        return(null);
                    }

                default:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: Unhandled: " + response.Cmd);
                    break;
                }
            }
        }
Esempio n. 2
0
        private bool ReadAck()
        {
            Pm3UsbResponse res = new Pm3UsbResponse(Port);

            if (res.Cmd != Pm3UsbResponse.eResponseType.ACK)
            {
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] ReadAck: did not reply with ACK");
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        private bool UnlockTag(uint pass, ref int reason)
        {
            if (Port == null)
            {
                return(false);
            }
            if (!UnlockSupported)
            {
                return(false);
            }
            Pm3UsbCommand cmd = new Pm3UsbCommand(0x319, pass);

            LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag: Send request for pass 0x" + pass.ToString("X8"));
            cmd.Write(Port);

            while (true)
            {
                Pm3UsbResponse response = new Pm3UsbResponse(Port);

                switch (response.Cmd)
                {
                case Pm3UsbResponse.eResponseType.DebugString:
                {
                    string debugStr = Encoding.UTF8.GetString(response.data.d, 0, response.data.dataLen);
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag:   String '" + debugStr + "'");
                    break;
                }

                case Pm3UsbResponse.eResponseType.ACK:
                    reason = 0;
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag: ACK");
                    return(true);

                case Pm3UsbResponse.eResponseType.NACK:
                    reason = 1 + (int)response.data.arg[0];
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag: NACK (reason: " + reason + ")");
                    return(false);

                case Pm3UsbResponse.eResponseType.NoData:
                case Pm3UsbResponse.eResponseType.Timeout:
                    reason = -1;
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag: timeout, returning");
                    return(false);

                default:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag: Unhandled: " + response.Cmd);
                    break;
                }
            }
        }
Esempio n. 4
0
        private bool MeasureAntennaInternal(MeasurementResult result, eMeasurementType type = eMeasurementType.Both)
        {
            if (Port == null)
            {
                return(false);
            }
            Pm3UsbCommand cmd = new Pm3UsbCommand(0x400, (ulong)type);

            LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] MeasureAntenna: Start");
            cmd.Write(Port);

            while (true)
            {
                Pm3UsbResponse response = new Pm3UsbResponse(Port);

                switch (response.Cmd)
                {
                case Pm3UsbResponse.eResponseType.DebugString:
                {
                    string debugStr = Encoding.UTF8.GetString(response.data.d, 0, response.data.dataLen);
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] MeasureAntenna: DebugMessage '" + debugStr + "'");
                    break;
                }

                case Pm3UsbResponse.eResponseType.NoData:
                case Pm3UsbResponse.eResponseType.Timeout:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] MeasureAntenna: timeout");
                    continue;

                case Pm3UsbResponse.eResponseType.MeasuredAntennaTuning:
                    result.vLF125 = (response.data.arg[0] & 0xFFFF) * 0.002f;
                    result.vLF134 = ((response.data.arg[0] >> 16) & 0xFFFF) * 0.002f;
                    result.vHF    = (response.data.arg[1] & 0xFFFF) / 1000.0f;
                    result.peakF  = (response.data.arg[2] & 0xFFFF);
                    result.peakV  = ((response.data.arg[0] >> 16) & 0xFFFF) * 0.002f;
                    Array.Copy(response.data.d, result.amplitudes, 256);

                    return(true);

                default:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] MeasureAntenna: Unhandled: " + response.Cmd);
                    break;
                }
            }
        }
Esempio n. 5
0
        private void EmulateTagInternal(byte[] data)
        {
            if (Port == null)
            {
                return;
            }
            Pm3UsbCommand cmd = new Pm3UsbCommand(0x311, 0, 0, 0, data);

            LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Emulate: Start");
            cmd.Write(Port);

            while (true)
            {
                Pm3UsbResponse response = new Pm3UsbResponse(Port);

                switch (response.Cmd)
                {
                case Pm3UsbResponse.eResponseType.DebugString:
                {
                    string debugStr = Encoding.UTF8.GetString(response.data.d, 0, response.data.dataLen);
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Emulate: DebugMessage '" + debugStr + "'");
                    break;
                }

                case Pm3UsbResponse.eResponseType.NoData:
                case Pm3UsbResponse.eResponseType.Timeout:
                    //LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Emulate: timeout, returning");
                    //return;
                    continue;

                case Pm3UsbResponse.eResponseType.ACK:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Emulate: Done");
                    return;

                default:
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Emulate: Unhandled: " + response.Cmd);
                    break;
                }
            }
        }
Esempio n. 6
0
        private bool TryOpen(string port)
        {
            SerialPort p = null;

            try
            {
                p             = new SerialPort(port, 115200);
                p.ReadTimeout = 500;

                p.Open();
                Flush(p);

                /* read version */
                Pm3UsbCommand cmdDevInfo = new Pm3UsbCommand(0);
                cmdDevInfo.Write(p);
                Pm3UsbResponse resDevInfo = new Pm3UsbResponse(p);
                if (resDevInfo.Cmd != Pm3UsbResponse.eResponseType.DeviceInfo)
                {
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] TryOpen: " + port + " did not reply to a device info");
                    p.Close();
                    return(false);
                }

                DeviceInfo = (eDeviceInfo)resDevInfo.data.arg[0];

                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Device info: ");
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3]   BOOTROM_PRESENT         " + ((DeviceInfo & eDeviceInfo.BootromPresent) != 0 ? "Y" : "N"));
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3]   OSIMAGE_PRESENT         " + ((DeviceInfo & eDeviceInfo.OsImagePresent) != 0 ? "Y" : "N"));
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3]   MODE_BOOTROM            " + ((DeviceInfo & eDeviceInfo.ModeBootrom) != 0 ? "Y" : "N"));
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3]   MODE_OS                 " + ((DeviceInfo & eDeviceInfo.ModeOs) != 0 ? "Y" : "N"));
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3]   UNDERSTANDS_START_FLASH " + ((DeviceInfo & eDeviceInfo.UnderstandStartFlash) != 0 ? "Y" : "N"));

                if ((DeviceInfo & eDeviceInfo.ModeBootrom) != 0 && (DeviceInfo & eDeviceInfo.ModeBootrom) != 0)
                {
                    Pm3UsbCommand cmdReset = new Pm3UsbCommand((ulong)Pm3UsbCommand.eCommandType.HardwareReset);
                    CurrentPort = port;
                    Port        = p;

                    if (!File.Exists(Flashfile))
                    {
                        LogWindow.Log(LogWindow.eLogLevel.Error, "[PM3] Device started in bootloader mode, but I cannot find flash file " + Flashfile + ". Reset device.");
                    }
                    else
                    {
                        bool bootloader = false;
                        var  segs       = ReadFlash(Flashfile, out bootloader);

                        FlashRequestContext ctx = new FlashRequestContext();

                        ctx.Bootloader = bootloader;
                        ctx.FlashFile  = Flashfile;
                        ctx.Proceed    = false;

                        FlashRequest?.Invoke(this, ctx);

                        if (ctx.Proceed)
                        {
                            bool success = Flash(segs, bootloader);
                            FlashResult?.Invoke(this, success);
                        }
                    }

                    cmdReset.Write(p);
                    p.Close();

                    return(false);
                }

                Pm3UsbCommand cmdPing = new Pm3UsbCommand(0x109);
                cmdPing.Write(p);
                Pm3UsbResponse resPing = new Pm3UsbResponse(p);

                if (resPing.Cmd != Pm3UsbResponse.eResponseType.ACK)
                {
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] TryOpen: " + port + " did not reply to a ping");
                    p.Close();
                    return(false);
                }

                /* read version */
                Pm3UsbCommand cmdVers = new Pm3UsbCommand(0x107);
                cmdVers.Write(p);
                Pm3UsbResponse resVers = new Pm3UsbResponse(p);
                if (resVers.Cmd != Pm3UsbResponse.eResponseType.ACK)
                {
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] TryOpen: " + port + " did not reply to a version info");
                    p.Close();
                    return(false);
                }
                Thread.Sleep(500);
                Flush(p);

                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Chip ID: 0x" + resVers.data.arg[0].ToString("X8"));
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Flash:   0x" + resVers.data.arg[1].ToString("X8"));
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Caps:    0x" + resVers.data.arg[2].ToString("X8"));
                string versionString = Encoding.UTF8.GetString(resVers.data.d, 0, resVers.data.dataLen - 1);
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Version:");
                foreach (string line in versionString.Split('\n'))
                {
                    LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3]     " + line);
                }

                CurrentPort = port;
                Port        = p;

                /* does it support the unlock command? */
                int reason = 0;
                UnlockSupported = true;
                UnlockTag(0xDEADBEEF, ref reason);

                /* if not supported, the command will simply time out */
                UnlockSupported = reason > 0;
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] Device does " + (UnlockSupported ? "" : "*NOT*") + " support SLIX-L unlock command");

                DeviceFound?.Invoke(this, CurrentPort);
                LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] TryOpen: " + port + " successfully opened");


                Connected = true;
                return(true);
            }
            catch (Exception ex)
            {
                try
                {
                    if (p != null && p.IsOpen)
                    {
                        p.Close();
                    }
                }
                catch (Exception e)
                {
                }
            }

            return(false);
        }