| 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 Presco tag commands |
| 8 | //----------------------------------------------------------------------------- |
| 9 | |
| 10 | #include "cmdlfjablotron.h" |
| 11 | |
| 12 | static int CmdHelp(const char *Cmd); |
| 13 | |
| 14 | int usage_lf_jablotron_clone(void){ |
| 15 | PrintAndLog("clone a Jablotron tag to a T55x7 tag."); |
| 16 | PrintAndLog("Usage: lf jablotron clone [h] <card ID> <Q5>"); |
| 17 | PrintAndLog("Options:"); |
| 18 | PrintAndLog(" h : This help"); |
| 19 | PrintAndLog(" <card ID> : jablotron card ID"); |
| 20 | PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)"); |
| 21 | PrintAndLog(""); |
| 22 | PrintAndLog("Sample: lf jablotron clone 112233"); |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | int usage_lf_jablotron_sim(void) { |
| 27 | PrintAndLog("Enables simulation of jablotron card with specified card number."); |
| 28 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); |
| 29 | PrintAndLog(""); |
| 30 | PrintAndLog("Usage: lf jablotron sim [h] <card ID>"); |
| 31 | PrintAndLog("Options:"); |
| 32 | PrintAndLog(" h : This help"); |
| 33 | PrintAndLog(" <card ID> : jablotron card ID"); |
| 34 | PrintAndLog(""); |
| 35 | PrintAndLog("Sample: lf jablotron sim 112233"); |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static uint8_t jablontron_chksum(uint8_t *bits){ |
| 40 | uint8_t chksum = 0; |
| 41 | for (int i=16; i < 56; i += 8) { |
| 42 | chksum += bytebits_to_byte(bits+i,8); |
| 43 | } |
| 44 | chksum ^= 0x3A; |
| 45 | return chksum; |
| 46 | } |
| 47 | |
| 48 | int getJablotronBits(uint64_t fullcode, uint8_t *bits) { |
| 49 | //preamp |
| 50 | num_to_bytebits(0xFFFF, 16, bits); |
| 51 | |
| 52 | //fullcode |
| 53 | num_to_bytebits(fullcode, 40, bits+16); |
| 54 | |
| 55 | //chksum byte |
| 56 | uint8_t chksum = jablontron_chksum(bits); |
| 57 | num_to_bytebits(chksum, 8, bits+56); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | static uint64_t getJablontronCardId( uint64_t rawcode ){ |
| 62 | uint64_t id = 0; |
| 63 | uint8_t bytes[] = {0,0,0,0,0}; |
| 64 | num_to_bytes(rawcode, 5, bytes); |
| 65 | for ( int i = 4, j = 0; i > -1; --i, j += 2 ) { |
| 66 | id += NIBBLE_LOW( bytes[i] ) * (int)pow(10,j); |
| 67 | id += NIBBLE_HIGH( bytes[i] ) * (int)pow(10,j+1); |
| 68 | } |
| 69 | return id; |
| 70 | } |
| 71 | |
| 72 | //see ASKDemod for what args are accepted |
| 73 | int CmdJablotronDemod(const char *Cmd) { |
| 74 | |
| 75 | //Differential Biphase / di-phase (inverted biphase) |
| 76 | //get binary from ask wave |
| 77 | if (!ASKbiphaseDemod("0 64 1 0", FALSE)) { |
| 78 | if (g_debugMode) PrintAndLog("DEBUG: Error - Jablotron ASKbiphaseDemod failed"); |
| 79 | return 0; |
| 80 | } |
| 81 | size_t size = DemodBufferLen; |
| 82 | int ans = JablotronDemod(DemodBuffer, &size); |
| 83 | if (ans < 0){ |
| 84 | if (g_debugMode){ |
| 85 | if (ans == -1) |
| 86 | PrintAndLog("DEBUG: Error - Jablotron too few bits found"); |
| 87 | else if (ans == -2) |
| 88 | PrintAndLog("DEBUG: Error - Jablotron preamble not found"); |
| 89 | else if (ans == -3) |
| 90 | PrintAndLog("DEBUG: Error - Jablotron size not correct: %d", size); |
| 91 | else if (ans == -5) |
| 92 | PrintAndLog("DEBUG: Error - Jablotron checksum failed"); |
| 93 | else |
| 94 | PrintAndLog("DEBUG: Error - Jablotron ans: %d", ans); |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | setDemodBuf(DemodBuffer+ans, 64, 0); |
| 100 | |
| 101 | //got a good demod |
| 102 | uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32); |
| 103 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32); |
| 104 | |
| 105 | uint64_t rawid = bytebits_to_byte(DemodBuffer+16, 40); |
| 106 | uint64_t id = getJablontronCardId(rawid); |
| 107 | |
| 108 | PrintAndLog("Jablotron Tag Found: Card ID %u", id); |
| 109 | PrintAndLog("Raw: %08X%08X", raw1 ,raw2); |
| 110 | |
| 111 | uint8_t chksum = raw2 & 0xFF; |
| 112 | PrintAndLog("Checksum: %02X [%s]", |
| 113 | chksum, |
| 114 | (chksum == jablontron_chksum(DemodBuffer)) ? "OK":"FAIL" |
| 115 | ); |
| 116 | |
| 117 | id = DEC2BCD(id); |
| 118 | // Printed format: 1410-nn-nnnn-nnnn |
| 119 | PrintAndLog("Printed: 1410-%02X-%04X-%04X", |
| 120 | (uint8_t)(id >> 32) & 0xFF, |
| 121 | (uint16_t)(id >> 16) & 0xFFFF, |
| 122 | (uint16_t)id & 0xFFFF |
| 123 | ); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | int CmdJablotronRead(const char *Cmd) { |
| 128 | CmdLFRead("s"); |
| 129 | getSamples("12000", TRUE); |
| 130 | return CmdJablotronDemod(Cmd); |
| 131 | } |
| 132 | |
| 133 | int CmdJablotronClone(const char *Cmd) { |
| 134 | |
| 135 | uint64_t fullcode = 0; |
| 136 | uint32_t blocks[3] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_64 | 2<<T55x7_MAXBLOCK_SHIFT, 0, 0}; |
| 137 | |
| 138 | uint8_t bits[64]; |
| 139 | uint8_t *bs = bits; |
| 140 | memset(bs, 0, sizeof(bits)); |
| 141 | |
| 142 | char cmdp = param_getchar(Cmd, 0); |
| 143 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_clone(); |
| 144 | |
| 145 | fullcode = param_get64ex(Cmd, 0, 0, 16); |
| 146 | |
| 147 | //Q5 |
| 148 | if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') { |
| 149 | //t5555 (Q5) BITRATE = (RF-2)/2 (iceman) |
| 150 | blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | 64<<T5555_BITRATE_SHIFT | 2<<T5555_MAXBLOCK_SHIFT; |
| 151 | } |
| 152 | |
| 153 | // clearing the topbit needed for the preambl detection. |
| 154 | if ((fullcode & 0x7FFFFFFFFF) != fullcode) { |
| 155 | fullcode &= 0x7FFFFFFFFF; |
| 156 | PrintAndLog("Card Number Truncated to 39bits: %"PRIx64, fullcode); |
| 157 | } |
| 158 | |
| 159 | if ( !getJablotronBits(fullcode, bs)) { |
| 160 | PrintAndLog("Error with tag bitstream generation."); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | // |
| 165 | blocks[1] = bytebits_to_byte(bs,32); |
| 166 | blocks[2] = bytebits_to_byte(bs+32,32); |
| 167 | |
| 168 | PrintAndLog("Preparing to clone Jablotron to T55x7 with FullCode: %"PRIx64, fullcode); |
| 169 | PrintAndLog("Blk | Data "); |
| 170 | PrintAndLog("----+------------"); |
| 171 | PrintAndLog(" 00 | 0x%08x", blocks[0]); |
| 172 | PrintAndLog(" 01 | 0x%08x", blocks[1]); |
| 173 | PrintAndLog(" 02 | 0x%08x", blocks[2]); |
| 174 | |
| 175 | UsbCommand resp; |
| 176 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; |
| 177 | |
| 178 | for (int i = 2; i >= 0; --i) { |
| 179 | c.arg[0] = blocks[i]; |
| 180 | c.arg[1] = i; |
| 181 | clearCommandBuffer(); |
| 182 | SendCommand(&c); |
| 183 | if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){ |
| 184 | PrintAndLog("Error occurred, device did not respond during write operation."); |
| 185 | return -1; |
| 186 | } |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | int CmdJablotronSim(const char *Cmd) { |
| 192 | uint64_t fullcode = 0; |
| 193 | |
| 194 | char cmdp = param_getchar(Cmd, 0); |
| 195 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_sim(); |
| 196 | |
| 197 | fullcode = param_get64ex(Cmd, 0, 0, 16); |
| 198 | |
| 199 | // clearing the topbit needed for the preambl detection. |
| 200 | if ((fullcode & 0x7FFFFFFFFF) != fullcode) { |
| 201 | fullcode &= 0x7FFFFFFFFF; |
| 202 | PrintAndLog("Card Number Truncated to 39bits: %"PRIx64, fullcode); |
| 203 | } |
| 204 | |
| 205 | uint8_t clk = 64, encoding = 2, separator = 0, invert = 1; |
| 206 | uint16_t arg1, arg2; |
| 207 | size_t size = 64; |
| 208 | arg1 = clk << 8 | encoding; |
| 209 | arg2 = invert << 8 | separator; |
| 210 | |
| 211 | PrintAndLog("Simulating Jablotron - FullCode: %"PRIx64, fullcode); |
| 212 | |
| 213 | UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; |
| 214 | getJablotronBits(fullcode, c.d.asBytes); |
| 215 | clearCommandBuffer(); |
| 216 | SendCommand(&c); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static command_t CommandTable[] = { |
| 221 | {"help", CmdHelp, 1, "This help"}, |
| 222 | {"read", CmdJablotronRead, 0, "Attempt to read and extract tag data"}, |
| 223 | {"clone", CmdJablotronClone, 0, "clone jablotron tag"}, |
| 224 | {"sim", CmdJablotronSim, 0, "simulate jablotron tag"}, |
| 225 | {NULL, NULL, 0, NULL} |
| 226 | }; |
| 227 | |
| 228 | int CmdLFJablotron(const char *Cmd) { |
| 229 | clearCommandBuffer(); |
| 230 | CmdsParse(CommandTable, Cmd); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int CmdHelp(const char *Cmd) { |
| 235 | CmdsHelp(CommandTable); |
| 236 | return 0; |
| 237 | } |