6 #include "iso14443crc.h"
11 #include "cmdparser.h"
14 static int CmdHelp(const char *Cmd
);
16 int CmdHF14BDemod(const char *Cmd
)
21 bool negateI
, negateQ
;
26 // As received, the samples are pairs, correlations against I and Q
27 // square waves. So estimate angle of initial carrier (or just
28 // quadrant, actually), and then do the demod.
30 // First, estimate where the tag starts modulating.
31 for (i
= 0; i
< GraphTraceLen
; i
+= 2) {
32 if (abs(GraphBuffer
[i
]) + abs(GraphBuffer
[i
+ 1]) > 40) {
36 if (i
>= GraphTraceLen
) {
37 PrintAndLog("too weak to sync");
40 PrintAndLog("out of weak at %d", i
);
43 // Now, estimate the phase in the initial modulation of the tag
46 for (; i
< (outOfWeakAt
+ 16); i
+= 2) {
47 isum
+= GraphBuffer
[i
+ 0];
48 qsum
+= GraphBuffer
[i
+ 1];
53 // Turn the correlation pairs into soft decisions on the bit.
55 for (i
= 0; i
< GraphTraceLen
/ 2; i
++) {
56 int si
= GraphBuffer
[j
];
57 int sq
= GraphBuffer
[j
+ 1];
58 if (negateI
) si
= -si
;
59 if (negateQ
) sq
= -sq
;
60 GraphBuffer
[i
] = si
+ sq
;
66 while (GraphBuffer
[i
] > 0 && i
< GraphTraceLen
)
68 if (i
>= GraphTraceLen
) goto demodError
;
71 while (GraphBuffer
[i
] < 0 && i
< GraphTraceLen
)
73 if (i
>= GraphTraceLen
) goto demodError
;
74 if ((i
- iold
) > 23) goto demodError
;
76 PrintAndLog("make it to demod loop");
80 while (GraphBuffer
[i
] >= 0 && i
< GraphTraceLen
)
82 if (i
>= GraphTraceLen
) goto demodError
;
83 if ((i
- iold
) > 6) goto demodError
;
85 uint16_t shiftReg
= 0;
86 if (i
+ 20 >= GraphTraceLen
) goto demodError
;
88 for (j
= 0; j
< 10; j
++) {
89 int soft
= GraphBuffer
[i
] + GraphBuffer
[i
+ 1];
91 if (abs(soft
) < (abs(isum
) + abs(qsum
)) / 20) {
92 PrintAndLog("weak bit");
96 if(GraphBuffer
[i
] + GraphBuffer
[i
+1] >= 0) {
103 if ((shiftReg
& 0x200) && !(shiftReg
& 0x001))
105 // valid data byte, start and stop bits okay
106 PrintAndLog(" %02x", (shiftReg
>> 1) & 0xff);
107 data
[dataLen
++] = (shiftReg
>> 1) & 0xff;
108 if (dataLen
>= sizeof(data
)) {
111 } else if (shiftReg
== 0x000) {
119 uint8_t first
, second
;
120 ComputeCrc14443(CRC_14443_B
, data
, dataLen
-2, &first
, &second
);
121 PrintAndLog("CRC: %02x %02x (%s)\n", first
, second
,
122 (first
== data
[dataLen
-2] && second
== data
[dataLen
-1]) ?
123 "ok" : "****FAIL****");
125 RepaintGraphWindow();
129 PrintAndLog("demod error");
130 RepaintGraphWindow();
134 int CmdHF14BList(const char *Cmd
)
137 GetFromBigBuf(got
, sizeof(got
));
139 PrintAndLog("recorded activity:");
140 PrintAndLog(" time :rssi: who bytes");
141 PrintAndLog("---------+----+----+-----------");
152 int timestamp
= *((uint32_t *)(got
+i
));
153 if(timestamp
& 0x80000000) {
154 timestamp
&= 0x7fffffff;
159 int metric
= *((uint32_t *)(got
+i
+4));
170 uint8_t *frame
= (got
+i
+9);
172 char line
[1000] = "";
174 for(j
= 0; j
< len
; j
++) {
175 sprintf(line
+(j
*3), "%02x ", frame
[j
]);
181 ComputeCrc14443(CRC_14443_B
, frame
, len
-2, &b1
, &b2
);
182 if(b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
183 crc
= "**FAIL CRC**";
191 char metricString
[100];
193 sprintf(metricString
, "%3d", metric
);
195 strcpy(metricString
, " ");
198 PrintAndLog(" +%7d: %s: %s %s %s",
199 (prev
< 0 ? 0 : timestamp
- prev
),
201 (isResponse
? "TAG" : " "), line
, crc
);
209 int CmdHF14BRead(const char *Cmd
)
211 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443
, {strtol(Cmd
, NULL
, 0), 0, 0}};
216 int CmdHF14Sim(const char *Cmd
)
218 UsbCommand c
={CMD_SIMULATE_TAG_ISO_14443
};
223 int CmdHFSimlisten(const char *Cmd
)
225 UsbCommand c
= {CMD_SIMULATE_TAG_HF_LISTEN
};
230 int CmdHF14BSnoop(const char *Cmd
)
232 UsbCommand c
= {CMD_SNOOP_ISO_14443
};
237 /* New command to read the contents of a SRI512 tag
238 * SRI512 tags are ISO14443-B modulated memory tags,
239 * this command just dumps the contents of the memory
241 int CmdSri512Read(const char *Cmd
)
243 UsbCommand c
= {CMD_READ_SRI512_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
248 /* New command to read the contents of a SRIX4K tag
249 * SRIX4K tags are ISO14443-B modulated memory tags,
250 * this command just dumps the contents of the memory/
252 int CmdSrix4kRead(const char *Cmd
)
254 UsbCommand c
= {CMD_READ_SRIX4K_TAG
, {strtol(Cmd
, NULL
, 0), 0, 0}};
259 static command_t CommandTable
[] =
261 {"help", CmdHelp
, 1, "This help"},
262 {"demod", CmdHF14BDemod
, 1, "Demodulate ISO14443 Type B from tag"},
263 {"list", CmdHF14BList
, 0, "List ISO 14443 history"},
264 {"read", CmdHF14BRead
, 0, "Read HF tag (ISO 14443)"},
265 {"sim", CmdHF14Sim
, 0, "Fake ISO 14443 tag"},
266 {"simlisten", CmdHFSimlisten
, 0, "Get HF samples as fake tag"},
267 {"snoop", CmdHF14BSnoop
, 0, "Eavesdrop ISO 14443"},
268 {"sri512read", CmdSri512Read
, 0, "<int> -- Read contents of a SRI512 tag"},
269 {"srix4kread", CmdSrix4kRead
, 0, "<int> -- Read contents of a SRIX4K tag"},
270 {NULL
, NULL
, 0, NULL
}
273 int CmdHF14B(const char *Cmd
)
275 CmdsParse(CommandTable
, Cmd
);
279 int CmdHelp(const char *Cmd
)
281 CmdsHelp(CommandTable
);