| 1 | //----------------------------------------------------------------------------- |
| 2 | // |
| 3 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
| 4 | // at your option, any later version. See the LICENSE.txt file for the text of |
| 5 | // the license. |
| 6 | //----------------------------------------------------------------------------- |
| 7 | // Low frequency Stanley/PAC tag commands |
| 8 | // NRZ, RF/32, 128 bits long (unknown cs) |
| 9 | //----------------------------------------------------------------------------- |
| 10 | |
| 11 | #include "cmdlfpac.h" |
| 12 | |
| 13 | #include <string.h> |
| 14 | #include <inttypes.h> |
| 15 | #include "comms.h" |
| 16 | #include "ui.h" |
| 17 | #include "util.h" |
| 18 | #include "graph.h" |
| 19 | #include "cmdparser.h" |
| 20 | #include "cmddata.h" |
| 21 | #include "cmdmain.h" |
| 22 | #include "cmdlf.h" |
| 23 | #include "lfdemod.h" // preamble test |
| 24 | |
| 25 | static int CmdHelp(const char *Cmd); |
| 26 | |
| 27 | // by marshmellow |
| 28 | // find PAC preamble in already demoded data |
| 29 | int PacFind(uint8_t *dest, size_t *size) { |
| 30 | if (*size < 128) return -1; //make sure buffer has data |
| 31 | size_t startIdx = 0; |
| 32 | uint8_t preamble[] = {1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0}; |
| 33 | if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx)) |
| 34 | return -2; //preamble not found |
| 35 | if (*size != 128) return -3; //wrong demoded size |
| 36 | //return start position |
| 37 | return (int)startIdx; |
| 38 | } |
| 39 | |
| 40 | //see NRZDemod for what args are accepted |
| 41 | int CmdPacDemod(const char *Cmd) { |
| 42 | |
| 43 | //NRZ |
| 44 | if (!NRZrawDemod(Cmd, false)) { |
| 45 | if (g_debugMode) PrintAndLog("DEBUG: Error - PAC: NRZ Demod failed"); |
| 46 | return 0; |
| 47 | } |
| 48 | size_t size = DemodBufferLen; |
| 49 | int ans = PacFind(DemodBuffer, &size); |
| 50 | if (ans < 0) { |
| 51 | if (g_debugMode) { |
| 52 | if (ans == -1) |
| 53 | PrintAndLog("DEBUG: Error - PAC: too few bits found"); |
| 54 | else if (ans == -2) |
| 55 | PrintAndLog("DEBUG: Error - PAC: preamble not found"); |
| 56 | else if (ans == -3) |
| 57 | PrintAndLog("DEBUG: Error - PAC: Size not correct: %d", size); |
| 58 | else |
| 59 | PrintAndLog("DEBUG: Error - PAC: ans: %d", ans); |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | setDemodBuf(DemodBuffer, 128, ans); |
| 64 | setClockGrid(g_DemodClock, g_DemodStartIdx + (ans*g_DemodClock)); |
| 65 | |
| 66 | //got a good demod |
| 67 | uint32_t raw1 = bytebits_to_byte(DemodBuffer , 32); |
| 68 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32); |
| 69 | uint32_t raw3 = bytebits_to_byte(DemodBuffer+64, 32); |
| 70 | uint32_t raw4 = bytebits_to_byte(DemodBuffer+96, 32); |
| 71 | |
| 72 | // preamble then appears to have marker bits of "10" CS? |
| 73 | // 11111111001000000 10 01001100 10 00001101 10 00001101 10 00001101 10 00001101 10 00001101 10 00001101 10 00001101 10 00001101 10 10001100 10 100000001 |
| 74 | // unknown checksum 9 bits at the end |
| 75 | |
| 76 | PrintAndLog("PAC/Stanley Tag Found -- Raw: %08X%08X%08X%08X", raw1 ,raw2, raw3, raw4); |
| 77 | PrintAndLog("\nHow the Raw ID is translated by the reader is unknown"); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | int CmdPacRead(const char *Cmd) { |
| 82 | lf_read(true, 4096*2 + 20); |
| 83 | return CmdPacDemod(Cmd); |
| 84 | } |
| 85 | |
| 86 | static command_t CommandTable[] = { |
| 87 | {"help", CmdHelp, 1, "This help"}, |
| 88 | {"demod", CmdPacDemod,1, "Attempt to read and extract tag data from the GraphBuffer"}, |
| 89 | {"read", CmdPacRead, 0, "Attempt to read and extract tag data from the antenna"}, |
| 90 | {NULL, NULL, 0, NULL} |
| 91 | }; |
| 92 | |
| 93 | int CmdLFPac(const char *Cmd) { |
| 94 | clearCommandBuffer(); |
| 95 | CmdsParse(CommandTable, Cmd); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int CmdHelp(const char *Cmd) { |
| 100 | CmdsHelp(CommandTable); |
| 101 | return 0; |
| 102 | } |