1 //-----------------------------------------------------------------------------
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
6 //-----------------------------------------------------------------------------
7 // Low frequency Presco tag commands
8 //-----------------------------------------------------------------------------
10 #include "cmdlfvisa2000.h"
12 #define BL0CK1 0x56495332
14 static int CmdHelp(const char *Cmd
);
16 int usage_lf_visa2k_clone(void){
17 PrintAndLog("clone a Visa2000 tag to a T55x7 tag.");
18 PrintAndLog("Usage: lf visa2k clone [h] <card ID> <Q5>");
19 PrintAndLog("Options:");
20 PrintAndLog(" h : This help");
21 PrintAndLog(" <card ID> : Visa2k card ID");
22 PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)");
24 PrintAndLog("Sample: lf visa2k clone 112233");
28 int usage_lf_visa2k_sim(void) {
29 PrintAndLog("Enables simulation of visa2k card with specified card number.");
30 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
32 PrintAndLog("Usage: lf visa2k sim [h] <card ID>");
33 PrintAndLog("Options:");
34 PrintAndLog(" h : This help");
35 PrintAndLog(" <card ID> : Visa2k card ID");
37 PrintAndLog("Sample: lf visa2k sim 112233");
41 static uint8_t visa_chksum( uint32_t id
) {
43 for (uint8_t i
= 0; i
< 32; i
+= 4)
44 sum
^= (id
>> i
) & 0xF;
48 static uint8_t visa_parity( uint32_t id
) {
57 par
|= par_lut
[ NIBBLE_HIGH( (id
>> 8) & 0xFF) ] << 3;
58 par
|= par_lut
[ NIBBLE_LOW( (id
>> 8) & 0xFF) ] << 2;
59 par
|= par_lut
[ NIBBLE_HIGH( id
& 0xFF ) ] << 1;
60 par
|= par_lut
[ NIBBLE_LOW( id
& 0xFF) ];
67 * 56495332 00096ebd 00000077 —> tag id 618173
68 * aaaaaaaa iiiiiiii -----..c
70 * a = fixed value ascii 'VIS2'
72 * c = checksum (xor of card id)
76 //see ASKDemod for what args are accepted
77 int CmdVisa2kDemod(const char *Cmd
) {
79 // save GraphBuffer - to restore it later
82 //sCmdAskEdgeDetect("");
86 if (!ASKDemod_ext("64 0 0", FALSE
, FALSE
, 1, &st
)) {
87 if (g_debugMode
) PrintAndLog("DEBUG: Error - Visa2k: ASK/Manchester Demod failed");
91 size_t size
= DemodBufferLen
;
92 int ans
= Visa2kDemod_AM(DemodBuffer
, &size
);
96 PrintAndLog("DEBUG: Error - Visa2k: too few bits found");
98 PrintAndLog("DEBUG: Error - Visa2k: preamble not found");
100 PrintAndLog("DEBUG: Error - Visa2k: Size not correct: %d", size
);
102 PrintAndLog("DEBUG: Error - Visa2k: ans: %d", ans
);
107 setDemodBuf(DemodBuffer
, 96, ans
);
111 uint32_t raw1
= bytebits_to_byte(DemodBuffer
, 32);
112 uint32_t raw2
= bytebits_to_byte(DemodBuffer
+32, 32);
113 uint32_t raw3
= bytebits_to_byte(DemodBuffer
+64, 32);
116 uint8_t calc
= visa_chksum(raw2
);
117 uint8_t chk
= raw3
& 0xF;
121 printf("DEBUG: error: Visa2000 checksum failed %x - %x\n", chk
, calc
);
126 uint8_t calc_par
= visa_parity(raw2
);
127 uint8_t chk_par
= (raw3
& 0xF0) >> 4;
128 if ( calc_par
!= chk_par
) {
129 printf("DEBUG: error: Visa2000 parity failed %x - %x\n", chk_par
, calc_par
);
133 PrintAndLog("Visa2000 Tag Found: Card ID %u, Raw: %08X%08X%08X", raw2
, raw1
,raw2
, raw3
);
138 int CmdVisa2kRead(const char *Cmd
) {
140 getSamples("12000",TRUE
);
141 return CmdVisa2kDemod(Cmd
);
144 int CmdVisa2kClone(const char *Cmd
) {
147 uint32_t blocks
[4] = {T55x7_MODULATION_MANCHESTER
| T55x7_BITRATE_RF_64
| T55x7_ST_TERMINATOR
| 3 << T55x7_MAXBLOCK_SHIFT
, BL0CK1
, 0};
149 char cmdp
= param_getchar(Cmd
, 0);
150 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_visa2k_clone();
152 id
= param_get32ex(Cmd
, 0, 0, 10);
155 if (param_getchar(Cmd
, 1) == 'Q' || param_getchar(Cmd
, 1) == 'q') {
156 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
157 blocks
[0] = T5555_MODULATION_MANCHESTER
| ((64-2)>>1) << T5555_BITRATE_SHIFT
| T5555_ST_TERMINATOR
| 3 << T5555_MAXBLOCK_SHIFT
;
162 blocks
[3] = (visa_parity(id
) << 4) | visa_chksum(id
);
164 PrintAndLog("Preparing to clone Visa2000 to T55x7 with CardId: %u", id
);
165 PrintAndLog("Blk | Data ");
166 PrintAndLog("----+------------");
167 for(int i
= 0; i
<4; ++i
)
168 PrintAndLog(" %02d | 0x%08x", i
, blocks
[i
]);
171 UsbCommand c
= {CMD_T55XX_WRITE_BLOCK
, {0,0,0}};
173 for (int i
= 3; i
>= 0; --i
) {
174 c
.arg
[0] = blocks
[i
];
176 clearCommandBuffer();
178 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, T55XX_WRITE_TIMEOUT
)){
179 PrintAndLog("Error occurred, device did not respond during write operation.");
186 int CmdVisa2kSim(const char *Cmd
) {
189 char cmdp
= param_getchar(Cmd
, 0);
190 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_visa2k_sim();
192 id
= param_get32ex(Cmd
, 0, 0, 10);
194 uint8_t clk
= 64, encoding
= 1, separator
= 1, invert
= 0;
197 arg1
= clk
<< 8 | encoding
;
198 arg2
= invert
<< 8 | separator
;
200 PrintAndLog("Simulating Visa2000 - CardId: %u", id
);
202 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
204 uint32_t blocks
[3] = { BL0CK1
, id
, (visa_parity(id
) << 4) | visa_chksum(id
) };
206 for(int i
=0; i
<3; ++i
)
207 num_to_bytebits(blocks
[i
], 32, c
.d
.asBytes
+ i
*32);
209 clearCommandBuffer();
214 static command_t CommandTable
[] = {
215 {"help", CmdHelp
, 1, "This help"},
216 {"read", CmdVisa2kRead
, 0, "Attempt to read and extract tag data"},
217 {"clone", CmdVisa2kClone
, 0, "clone Visa2000 tag"},
218 {"sim", CmdVisa2kSim
, 0, "simulate Visa2000 tag"},
219 {NULL
, NULL
, 0, NULL
}
222 int CmdLFVisa2k(const char *Cmd
) {
223 clearCommandBuffer();
224 CmdsParse(CommandTable
, Cmd
);
228 int CmdHelp(const char *Cmd
) {
229 CmdsHelp(CommandTable
);