X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/70459879e98e1e7a2a5627c4008f0ad38b8dc782..89a40c3d1dcf9aeb2170515ec5e559aa65e6a2be:/client/cmdlfviking.c?ds=inline

diff --git a/client/cmdlfviking.c b/client/cmdlfviking.c
index e50bc878..b0fa3aad 100644
--- a/client/cmdlfviking.c
+++ b/client/cmdlfviking.c
@@ -1,3 +1,11 @@
+//-----------------------------------------------------------------------------
+//
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
+//-----------------------------------------------------------------------------
+// Low frequency Viking tag commands
+//-----------------------------------------------------------------------------
 #include <stdio.h>
 #include <string.h>
 #include <inttypes.h>
@@ -15,76 +23,111 @@ static int CmdHelp(const char *Cmd);
 
 int usage_lf_viking_clone(void){
 	PrintAndLog("clone a Viking AM tag to a T55x7 tag.");
-	PrintAndLog("Usage: lf viking clone <Card ID 16 bytes of hex number>");
+	PrintAndLog("Usage: lf viking clone <Card ID - 8 hex digits> <Q5>");
+	PrintAndLog("Options :");
+	PrintAndLog("  <Card Number>  : 8 digit hex viking card number");
+	PrintAndLog("  <Q5>           : specify write to Q5 (t5555 instead of t55x7)");
+	PrintAndLog("");
+	PrintAndLog("Sample  : lf viking clone 1A337 Q5");
 	return 0;
 }
 
+int usage_lf_viking_sim(void) {
+	PrintAndLog("Enables simulation of viking card with specified card number.");
+	PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
+	PrintAndLog("Per viking format, the card number is 8 digit hex number.  Larger values are truncated.");
+	PrintAndLog("");
+	PrintAndLog("Usage:  lf viking sim <Card-Number>");
+	PrintAndLog("Options :");
+	PrintAndLog("  <Card Number>   : 8 digit hex viking card number");
+	PrintAndLog("");
+	PrintAndLog("Sample  : lf viking sim 1A337");
+	return 0;
+}
+
+// calc checksum
+uint64_t getVikingBits(uint32_t id) {
+	uint8_t checksum = (id>>24) ^ ((id>>16) & 0xFF) ^ ((id>>8) & 0xFF) ^ (id & 0xFF) ^ 0xF2 ^ 0xA8;
+	uint64_t ret = (uint64_t)0xF2 << 56;
+	ret |= (id << 8);
+	ret	|= checksum;
+	return ret;
+}
 //by marshmellow
 //see ASKDemod for what args are accepted
-int CmdVikingDemod(const char *Cmd)
-{
+int CmdVikingRead(const char *Cmd) {
+	// read lf silently
 	CmdLFRead("s");
+	// get samples silently
 	getSamples("30000",false);
-	
- 	if (!ASKDemod(Cmd, false, false, 1)) {
-		if (g_debugMode) PrintAndLog("ASKDemod failed");
-		return 0;
-	}
-	size_t size = DemodBufferLen;
-
-	int ans = VikingDemod_AM(DemodBuffer, &size);
-	if (ans < 0) {
-		if (g_debugMode) PrintAndLog("Error Viking_Demod");
-		return 0;
-	}
-	//got a good demod
-	uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans, 32);
-	uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32);
-	uint32_t cardid = bytebits_to_byte(DemodBuffer+ans+24, 32);
-	uint8_t checksum = bytebits_to_byte(DemodBuffer+ans+32+24, 8);
-	PrintAndLog("Viking Tag Found: Card ID %08X, Checksum: %02X", cardid, checksum);
-	PrintAndLog("Raw: %08X%08X", raw1,raw2);
-	setDemodBuf(DemodBuffer+ans, 64, 0);
-	return 1;
+	// demod and output viking ID	
+	return CmdVikingDemod(Cmd);
 }
 
-int CmdVikingClone(const char *Cmd)
-{
-    uint32_t b1,b2;
-    // get the tag number 64 bits (8 bytes) in hex
-    uint8_t id[8];
- 
+int CmdVikingClone(const char *Cmd) {
+	uint32_t id = 0;
+	uint64_t rawID = 0;
+	bool Q5 = false;
 	char cmdp = param_getchar(Cmd, 0);
-	if (strlen(Cmd) < 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_viking_clone();
+	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_viking_clone();
 	
-	if (param_gethex(Cmd, 0, id, 16) == 1)
-		return usage_lf_viking_clone();
+	id = param_get32ex(Cmd, 0, 0, 16);
+	if (id == 0) return usage_lf_viking_clone();
 	
-    b1 = bytes_to_num(id, sizeof(uint32_t));
-    b2 = bytes_to_num(id + sizeof(uint32_t), sizeof(uint32_t));
-    UsbCommand c = {CMD_VIKING_CLONE_TAG,{b1,b2}};
+	cmdp = param_getchar(Cmd, 1);
+	if ( cmdp == 'Q' || cmdp == 'q')
+		Q5 = true;
+
+	rawID = getVikingBits(id);
+
+	UsbCommand c = {CMD_VIKING_CLONE_TAG,{rawID >> 32, rawID & 0xFFFF, Q5}};
 	clearCommandBuffer();
     SendCommand(&c);
-	//check for ACK?
+	//check for ACK
+	WaitForResponse(CMD_ACK,NULL);
     return 0;
 }
 
-static command_t CommandTable[] =
-{
+int CmdVikingSim(const char *Cmd) {
+	uint32_t id = 0;
+	uint64_t rawID = 0;
+	uint8_t clk = 32, encoding = 1, separator = 0, invert = 0;
+
+	char cmdp = param_getchar(Cmd, 0);
+	if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_viking_sim();
+
+	id = param_get32ex(Cmd, 0, 0, 16);
+	if (id == 0) return usage_lf_viking_sim();
+
+	rawID = getVikingBits(id);
+
+	uint16_t arg1, arg2;
+	size_t size = 64;
+	arg1 = clk << 8 | encoding;
+	arg2 = invert << 8 | separator;
+
+	UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
+	PrintAndLog("preparing to sim ask data: %d bits", size);
+	num_to_bytebits(rawID, 64, c.d.asBytes);
+	clearCommandBuffer();
+	SendCommand(&c);
+	return 0;
+}
+
+static command_t CommandTable[] = {
     {"help",	CmdHelp,		1, "This help"},
-    {"demod",	CmdVikingDemod, 1, "Extract tag data"},
-    {"clone",	CmdVikingClone,	1, "<16 digits card data>  clone viking tag"},
+	{"read",	CmdVikingRead,  0, "Attempt to read and Extract tag data"},
+	{"clone",	CmdVikingClone, 0, "<8 digit ID number> clone viking tag"},
+	{"sim",		CmdVikingSim,   0, "<8 digit ID number> simulate viking tag"},
     {NULL, NULL, 0, NULL}
 };
 
-int CmdLFViking(const char *Cmd)
-{
+int CmdLFViking(const char *Cmd) {
     CmdsParse(CommandTable, Cmd);
     return 0;
 }
 
-int CmdHelp(const char *Cmd)
-{
+int CmdHelp(const char *Cmd) {
     CmdsHelp(CommandTable);
     return 0;
 }