- int i;
- uint32_t fcode=(fc & 0x000000FF), cnum=(cn & 0x0000FFFF), uBits=0;
- if (fcode != fc)
- PrintAndLog("NOTE: Facility code truncated for AWID26 format (8-bit facility code)");
- if (cnum!=cn)
- PrintAndLog("NOTE: Card number was truncated for AWID26 format (16-bit card number)");
-
- AWIDBits[0] = 0x01; // 6-bit Preamble with 2 parity bits
- AWIDBits[1] = 0x1D; // First byte from card format (26-bit) plus parity bits
- AWIDBits[2] = 0x80; // Set the next two bits as 0b10 to finish card format
- uBits = (fcode<<4) + (cnum>>12);
- if (!parityTest(uBits,12,0))
- AWIDBits[2] |= (1<<5); // If not already even parity, set bit to make even
- uBits = AWIDBits[2]>>5;
- if (!parityTest(uBits, 3, 1))
- AWIDBits[2] |= (1<<4);
- uBits = fcode>>5; // first 3 bits of facility-code
- AWIDBits[2] += (uBits<<1);
- if (!parityTest(uBits, 3, 1))
- AWIDBits[2]++; // Set parity bit to make odd parity
- uBits = (fcode & 0x1C)>>2;
- AWIDBits[3] = 0;
- if (!parityTest(uBits,3,1))
- AWIDBits[3] |= (1<<4);
- AWIDBits[3] += (uBits<<5);
- uBits = ((fcode & 0x3)<<1) + ((cnum & 0x8000)>>15); // Grab/shift 2 LSBs from facility code and add shifted MSB from cardnum
- if (!parityTest(uBits,3,1))
- AWIDBits[3]++; // Set LSB for parity
- AWIDBits[3]+= (uBits<<1);
- uBits = (cnum & 0x7000)>>12;
- AWIDBits[4] = uBits<<5;
- if (!parityTest(uBits,3,1))
- AWIDBits[4] |= (1<<4);
- uBits = (cnum & 0x0E00)>>9;
- AWIDBits[4] += (uBits<<1);
- if (!parityTest(uBits,3,1))
- AWIDBits[4]++; // Set LSB for parity
- uBits = (cnum & 0x1C0)>>6; // Next bits from card number
- AWIDBits[5]=(uBits<<5);
- if (!parityTest(uBits,3,1))
- AWIDBits[5] |= (1<<4); // Set odd parity bit as needed
- uBits = (cnum & 0x38)>>3;
- AWIDBits[5]+= (uBits<<1);
- if (!parityTest(uBits,3,1))
- AWIDBits[5]++; // Set odd parity bit as needed
- uBits = (cnum & 0x7); // Last three bits from card number!
- AWIDBits[6] = (uBits<<5);
- if (!parityTest(uBits,3,1))
- AWIDBits[6] |= (1<<4);
- uBits = (cnum & 0x0FFF);
- if (!parityTest(uBits,12,1))
- AWIDBits[6] |= (1<<3);
- else
- AWIDBits[6]++;
- for (i = 7; i<12; i++)
- AWIDBits[i]=0x11;
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ size_t size = getFromGraphBuf(BitStream);
+ if (size==0) return 0;
+
+ int waveIdx = 0;
+ //get binary from fsk wave
+ int idx = AWIDdemodFSK(BitStream, &size, &waveIdx);
+ if (idx<=0){
+ if (g_debugMode){
+ if (idx == -1)
+ PrintAndLog("DEBUG: Error - not enough samples");
+ else if (idx == -2)
+ PrintAndLog("DEBUG: Error - only noise found");
+ else if (idx == -3)
+ PrintAndLog("DEBUG: Error - problem during FSK demod");
+ else if (idx == -4)
+ PrintAndLog("DEBUG: Error - AWID preamble not found");
+ else if (idx == -5)
+ PrintAndLog("DEBUG: Error - Size not correct: %d", size);
+ else
+ PrintAndLog("DEBUG: Error %d",idx);
+ }
+ return 0;
+ }
+
+ // Index map
+ // 0 10 20 30 40 50 60
+ // | | | | | | |
+ // 01234567 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 456 7 890 1 234 5 678 9 012 3 - to 96
+ // -----------------------------------------------------------------------------
+ // 00000001 000 1 110 1 101 1 011 1 101 1 010 0 000 1 000 1 010 0 001 0 110 1 100 0 000 1 000 1
+ // premable bbb o bbb o bbw o fff o fff o ffc o ccc o ccc o ccc o ccc o ccc o wxx o xxx o xxx o - to 96
+ // |---26 bit---| |-----117----||-------------142-------------|
+ // b = format bit len, o = odd parity of last 3 bits
+ // f = facility code, c = card number
+ // w = wiegand parity
+ // (26 bit format shown)
+
+ //get raw ID before removing parities
+ uint32_t rawLo = bytebits_to_byte(BitStream+idx+64,32);
+ uint32_t rawHi = bytebits_to_byte(BitStream+idx+32,32);
+ uint32_t rawHi2 = bytebits_to_byte(BitStream+idx,32);
+ setDemodBuf(BitStream,96,idx);
+ setClockGrid(50, waveIdx + (idx*50));
+
+ size = removeParity(BitStream, idx+8, 4, 1, 88);
+ if (size != 66){
+ if (g_debugMode) PrintAndLog("DEBUG: Error - at parity check-tag size does not match AWID format");
+ return 0;
+ }
+ // ok valid card found!
+
+ // Index map
+ // 0 10 20 30 40 50 60
+ // | | | | | | |
+ // 01234567 8 90123456 7890123456789012 3 456789012345678901234567890123456
+ // -----------------------------------------------------------------------------
+ // 00011010 1 01110101 0000000010001110 1 000000000000000000000000000000000
+ // bbbbbbbb w ffffffff cccccccccccccccc w xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+ // |26 bit| |-117--| |-----142------|
+ // b = format bit len, o = odd parity of last 3 bits
+ // f = facility code, c = card number
+ // w = wiegand parity
+ // (26 bit format shown)
+
+ uint32_t fc = 0;
+ uint32_t cardnum = 0;
+ uint32_t code1 = 0;
+ uint32_t code2 = 0;
+ uint8_t fmtLen = bytebits_to_byte(BitStream,8);
+ if (fmtLen==26){
+ fc = bytebits_to_byte(BitStream+9, 8);
+ cardnum = bytebits_to_byte(BitStream+17, 16);
+ code1 = bytebits_to_byte(BitStream+8,fmtLen);
+ PrintAndLog("AWID Found - BitLength: %d, FC: %d, Card: %d - Wiegand: %x, Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi2, rawHi, rawLo);
+ } else {
+ cardnum = bytebits_to_byte(BitStream+8+(fmtLen-17), 16);
+ if (fmtLen>32){
+ code1 = bytebits_to_byte(BitStream+8,fmtLen-32);
+ code2 = bytebits_to_byte(BitStream+8+(fmtLen-32),32);
+ PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x%08x, Raw: %08x%08x%08x", fmtLen, cardnum, code1, code2, rawHi2, rawHi, rawLo);
+ } else{
+ code1 = bytebits_to_byte(BitStream+8,fmtLen);
+ PrintAndLog("AWID Found - BitLength: %d -unknown BitLength- (%d) - Wiegand: %x, Raw: %08x%08x%08x", fmtLen, cardnum, code1, rawHi2, rawHi, rawLo);
+ }
+ }
+ if (g_debugMode){
+ PrintAndLog("DEBUG: idx: %d, Len: %d Printing Demod Buffer:", idx, 96);
+ printDemodBuff();
+ }
+ //todo - convert hi2, hi, lo to demodbuffer for future sim/clone commands