make style

This commit is contained in:
Philippe Teuwen 2019-03-10 00:00:59 +01:00
commit 0373696662
483 changed files with 56514 additions and 52451 deletions

View file

@ -31,7 +31,8 @@
static int CmdHelp(const char *Cmd);
int usage_lf_fdx_clone(void){
int usage_lf_fdx_clone(void)
{
PrintAndLogEx(NORMAL, "Clone a FDX-B animal tag to a T55x7 tag.");
PrintAndLogEx(NORMAL, "Usage: lf fdx clone [h] <country id> <animal id> <Q5>");
PrintAndLogEx(NORMAL, "Options:");
@ -49,7 +50,8 @@ int usage_lf_fdx_clone(void){
return 0;
}
int usage_lf_fdx_sim(void) {
int usage_lf_fdx_sim(void)
{
PrintAndLogEx(NORMAL, "Enables simulation of FDX-B animal tag");
PrintAndLogEx(NORMAL, "Simulation runs until the button is pressed or another USB command is issued.");
PrintAndLogEx(NORMAL, "");
@ -66,11 +68,12 @@ int usage_lf_fdx_sim(void) {
// Ask/Biphase Demod then try to locate an ISO 11784/85 ID
// BitStream must contain previously askrawdemod and biphasedemoded data
int detectFDXB(uint8_t *dest, size_t *size) {
int detectFDXB(uint8_t *dest, size_t *size)
{
//make sure buffer has enough data
if (*size < 128*2) return -1;
if (*size < 128 * 2) return -1;
size_t startIdx = 0;
uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,0,1};
uint8_t preamble[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx))
return -2; //preamble not found
if (*size != 128) return -3; //wrong demoded size
@ -79,18 +82,20 @@ int detectFDXB(uint8_t *dest, size_t *size) {
}
// clearing the topbit needed for the preambl detection.
static void verify_values(uint32_t countryid, uint64_t animalid){
static void verify_values(uint32_t countryid, uint64_t animalid)
{
if ((animalid & 0x3FFFFFFFFF) != animalid) {
animalid &= 0x3FFFFFFFFF;
PrintAndLogEx(INFO, "Animal ID Truncated to 38bits: %"PRIx64, animalid);
}
if ( (countryid & 0x3ff) != countryid ) {
if ((countryid & 0x3ff) != countryid) {
countryid &= 0x3ff;
PrintAndLogEx(INFO, "Country ID Truncated to 10bits: %03d", countryid);
}
}
int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits) {
int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits)
{
// add preamble ten 0x00 and one 0x01
memset(bits, 0x00, 10);
@ -114,29 +119,29 @@ int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t
bits[81] = isextended;
// add national code 40bits - OK
num_to_bytebitsLSBF(national_id >> 0, 8, bits+11);
num_to_bytebitsLSBF(national_id >> 8, 8, bits+20);
num_to_bytebitsLSBF(national_id >> 16, 8, bits+29);
num_to_bytebitsLSBF(national_id >> 24, 8, bits+38);
num_to_bytebitsLSBF(national_id >> 32, 6, bits+47);
num_to_bytebitsLSBF(national_id >> 0, 8, bits + 11);
num_to_bytebitsLSBF(national_id >> 8, 8, bits + 20);
num_to_bytebitsLSBF(national_id >> 16, 8, bits + 29);
num_to_bytebitsLSBF(national_id >> 24, 8, bits + 38);
num_to_bytebitsLSBF(national_id >> 32, 6, bits + 47);
// add country code - OK
num_to_bytebitsLSBF(country >> 0, 2, bits+53);
num_to_bytebitsLSBF(country >> 2, 8, bits+56);
num_to_bytebitsLSBF(country >> 0, 2, bits + 53);
num_to_bytebitsLSBF(country >> 2, 8, bits + 56);
// add crc-16 - OK
uint8_t raw[8];
for (uint8_t i=0; i<8; ++i)
for (uint8_t i = 0; i < 8; ++i)
raw[i] = bytebits_to_byte(bits + 11 + i * 9, 8);
uint16_t crc = crc16_kermit(raw, 8);
num_to_bytebitsLSBF(crc >> 0, 8, bits+83);
num_to_bytebitsLSBF(crc >> 8, 8, bits+92);
num_to_bytebitsLSBF(crc >> 0, 8, bits + 83);
num_to_bytebitsLSBF(crc >> 8, 8, bits + 92);
// extended data - OK
num_to_bytebitsLSBF( extended >> 0 , 8, bits+101);
num_to_bytebitsLSBF( extended >> 8 , 8, bits+110);
num_to_bytebitsLSBF( extended >> 16, 8, bits+119);
num_to_bytebitsLSBF(extended >> 0, 8, bits + 101);
num_to_bytebitsLSBF(extended >> 8, 8, bits + 110);
num_to_bytebitsLSBF(extended >> 16, 8, bits + 119);
return 1;
}
@ -157,7 +162,8 @@ int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t
-- sample: 985121004515220 [ 37FF65B88EF94 ]
*/
int CmdFDXBdemodBI(const char *Cmd){
int CmdFDXBdemodBI(const char *Cmd)
{
int clk = 32;
int invert = 1, errCnt = 0, offset = 0, maxErr = 0;
@ -165,20 +171,20 @@ int CmdFDXBdemodBI(const char *Cmd){
size_t size = getFromGraphBuf(bs);
errCnt = askdemod(bs, &size, &clk, &invert, maxErr, 0, 0);
if ( errCnt < 0 || errCnt > maxErr ) {
if (errCnt < 0 || errCnt > maxErr) {
PrintAndLogEx(DEBUG, "DEBUG: Error - FDXB no data or error found %d, clock: %d", errCnt, clk);
return 0;
}
errCnt = BiphaseRawDecode(bs, &size, &offset, 1);
if (errCnt < 0 || errCnt > maxErr ) {
if (errCnt < 0 || errCnt > maxErr) {
PrintAndLogEx(DEBUG, "DEBUG: Error - FDXB BiphaseRawDecode: %d", errCnt);
return 0;
}
int preambleIndex = detectFDXB(bs, &size);
if (preambleIndex < 0){
PrintAndLogEx(DEBUG, "DEBUG: Error - FDXB preamble not found :: %d",preambleIndex);
if (preambleIndex < 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - FDXB preamble not found :: %d", preambleIndex);
return 0;
}
if (size != 128) {
@ -190,26 +196,26 @@ int CmdFDXBdemodBI(const char *Cmd){
// remove marker bits (1's every 9th digit after preamble) (pType = 2)
size = removeParity(bs, preambleIndex + 11, 9, 2, 117);
if ( size != 104 ) {
if (size != 104) {
PrintAndLogEx(DEBUG, "DEBUG: Error - FDXB error removeParity:: %d", size);
return 0;
}
PrintAndLogEx(SUCCESS, "\nFDX-B / ISO 11784/5 Animal Tag ID Found:");
//got a good demod
uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(bs+32, 6)) << 32) | bytebits_to_byteLSBF(bs, 32);
uint32_t countryCode = bytebits_to_byteLSBF(bs+38, 10);
uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(bs + 32, 6)) << 32) | bytebits_to_byteLSBF(bs, 32);
uint32_t countryCode = bytebits_to_byteLSBF(bs + 38, 10);
uint8_t dataBlockBit = bs[48];
uint32_t reservedCode = bytebits_to_byteLSBF(bs+49, 14);
uint32_t reservedCode = bytebits_to_byteLSBF(bs + 49, 14);
uint8_t animalBit = bs[63];
uint32_t crc16 = bytebits_to_byteLSBF(bs+64, 16);
uint32_t extended = bytebits_to_byteLSBF(bs+80, 24);
uint32_t crc16 = bytebits_to_byteLSBF(bs + 64, 16);
uint32_t extended = bytebits_to_byteLSBF(bs + 80, 24);
uint64_t rawid = ((uint64_t)bytebits_to_byte(bs, 32) << 32) | bytebits_to_byte(bs+32, 32);
uint64_t rawid = ((uint64_t)bytebits_to_byte(bs, 32) << 32) | bytebits_to_byte(bs + 32, 32);
uint8_t raw[8];
num_to_bytes(rawid, 8, raw);
PrintAndLogEx(SUCCESS, "Raw ID Hex: %s", sprint_hex(raw,8));
PrintAndLogEx(SUCCESS, "Raw ID Hex: %s", sprint_hex(raw, 8));
uint16_t calcCrc = crc16_kermit(raw, 8);
PrintAndLogEx(SUCCESS, "Animal ID: %04u-%012" PRIu64, countryCode, NationalCode);
@ -224,7 +230,7 @@ int CmdFDXBdemodBI(const char *Cmd){
if (g_debugMode) {
PrintAndLogEx(DEBUG, "Start marker %d; Size %d", preambleIndex, size);
char *bin = sprint_bin_break(bs, size, 16);
PrintAndLogEx(DEBUG, "DEBUG BinStream:\n%s",bin);
PrintAndLogEx(DEBUG, "DEBUG BinStream:\n%s", bin);
}
return 1;
}
@ -232,7 +238,8 @@ int CmdFDXBdemodBI(const char *Cmd){
//see ASKDemod for what args are accepted
//almost the same demod as cmddata.c/CmdFDXBdemodBI
int CmdFdxDemod(const char *Cmd) {
int CmdFdxDemod(const char *Cmd)
{
//Differential Biphase / di-phase (inverted biphase)
//get binary from ask wave
@ -242,7 +249,7 @@ int CmdFdxDemod(const char *Cmd) {
}
size_t size = DemodBufferLen;
int preambleIndex = detectFDXB(DemodBuffer, &size);
if (preambleIndex < 0){
if (preambleIndex < 0) {
if (preambleIndex == -1)
PrintAndLogEx(DEBUG, "DEBUG: Error - FDX-B too few bits found");
@ -257,23 +264,23 @@ int CmdFdxDemod(const char *Cmd) {
// set and leave DemodBuffer intact
setDemodBuf(DemodBuffer, 128, preambleIndex);
setClockGrid(g_DemodClock, g_DemodStartIdx + (preambleIndex*g_DemodClock));
setClockGrid(g_DemodClock, g_DemodStartIdx + (preambleIndex * g_DemodClock));
// remove marker bits (1's every 9th digit after preamble) (pType = 2)
size = removeParity(DemodBuffer, 11, 9, 2, 117);
if ( size != 104 ) {
if (size != 104) {
PrintAndLogEx(DEBUG, "DEBUG: Error - FDX-B error removeParity: %d", size);
return 0;
}
//got a good demod
uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(DemodBuffer+32,6)) << 32) | bytebits_to_byteLSBF(DemodBuffer,32);
uint16_t countryCode = bytebits_to_byteLSBF(DemodBuffer+38,10);
uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(DemodBuffer + 32, 6)) << 32) | bytebits_to_byteLSBF(DemodBuffer, 32);
uint16_t countryCode = bytebits_to_byteLSBF(DemodBuffer + 38, 10);
uint8_t dataBlockBit = DemodBuffer[48];
uint32_t reservedCode = bytebits_to_byteLSBF(DemodBuffer+49,14);
uint32_t reservedCode = bytebits_to_byteLSBF(DemodBuffer + 49, 14);
uint8_t animalBit = DemodBuffer[63];
uint32_t crc16 = bytebits_to_byteLSBF(DemodBuffer+64,16);
uint32_t extended = bytebits_to_byteLSBF(DemodBuffer+80,24);
uint64_t rawid = (uint64_t)(bytebits_to_byte(DemodBuffer,32)) << 32 | bytebits_to_byte(DemodBuffer+32, 32);
uint32_t crc16 = bytebits_to_byteLSBF(DemodBuffer + 64, 16);
uint32_t extended = bytebits_to_byteLSBF(DemodBuffer + 80, 24);
uint64_t rawid = (uint64_t)(bytebits_to_byte(DemodBuffer, 32)) << 32 | bytebits_to_byte(DemodBuffer + 32, 32);
uint8_t raw[8];
num_to_bytes(rawid, 8, raw);
@ -301,12 +308,14 @@ int CmdFdxDemod(const char *Cmd) {
return 1;
}
int CmdFdxRead(const char *Cmd) {
int CmdFdxRead(const char *Cmd)
{
lf_read(true, 10000);
return CmdFdxDemod(Cmd);
}
int CmdFdxClone(const char *Cmd) {
int CmdFdxClone(const char *Cmd)
{
uint32_t countryid = 0;
uint64_t animalid = 0;
@ -324,7 +333,7 @@ int CmdFdxClone(const char *Cmd) {
verify_values(countryid, animalid);
// getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits)
if ( !getFDXBits(animalid, countryid, 1, 0, 0, bs)) {
if (!getFDXBits(animalid, countryid, 1, 0, 0, bs)) {
PrintAndLogEx(WARNING, "Error with tag bitstream generation.");
return 1;
}
@ -343,14 +352,14 @@ int CmdFdxClone(const char *Cmd) {
print_blocks(blocks, 5);
UsbCommand resp;
UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0, 0, 0}};
for (int i = 4; i >= 0; --i) {
c.arg[0] = blocks[i];
c.arg[1] = i;
clearCommandBuffer();
SendCommand(&c);
if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){
if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)) {
PrintAndLogEx(WARNING, "Error occurred, device did not respond during write operation.");
return -1;
}
@ -358,7 +367,8 @@ int CmdFdxClone(const char *Cmd) {
return 0;
}
int CmdFdxSim(const char *Cmd) {
int CmdFdxSim(const char *Cmd)
{
uint32_t countryid = 0;
uint64_t animalid = 0;
@ -381,7 +391,7 @@ int CmdFdxSim(const char *Cmd) {
UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
//getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits)
//getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits)
getFDXBits(animalid, countryid, 1, 0, 0, c.d.asBytes);
clearCommandBuffer();
SendCommand(&c);
@ -390,20 +400,22 @@ int CmdFdxSim(const char *Cmd) {
static command_t CommandTable[] = {
{"help", CmdHelp, 1, "this help"},
{"demod", CmdFdxDemod,1, "demodulate a FDX-B ISO11784/85 tag from the GraphBuffer"},
{"demod", CmdFdxDemod, 1, "demodulate a FDX-B ISO11784/85 tag from the GraphBuffer"},
{"read", CmdFdxRead, 0, "attempt to read and extract tag data"},
{"clone", CmdFdxClone,0, "clone animal ID tag to T55x7 (or to q5/T5555)"},
{"clone", CmdFdxClone, 0, "clone animal ID tag to T55x7 (or to q5/T5555)"},
{"sim", CmdFdxSim, 0, "simulate Animal ID tag"},
{NULL, NULL, 0, NULL}
};
int CmdLFFdx(const char *Cmd) {
int CmdLFFdx(const char *Cmd)
{
clearCommandBuffer();
CmdsParse(CommandTable, Cmd);
return 0;
}
int CmdHelp(const char *Cmd) {
int CmdHelp(const char *Cmd)
{
CmdsHelp(CommandTable);
return 0;
}