+ if (errors) {
+ PrintAndLog("List or save protocol data.");
+ PrintAndLog("Usage: hf list <protocol> [f] [c] [l <filename>]");
+ PrintAndLog(" hf list save <filename>");
+ PrintAndLog(" f - show frame delay times as well");
+ PrintAndLog(" c - mark CRC bytes");
+ PrintAndLog(" l - load data from file instead of trace buffer");
+ PrintAndLog(" save - save data to file");
+ PrintAndLog("Supported <protocol> values:");
+ PrintAndLog(" raw - just show raw data without annotations");
+ PrintAndLog(" 14a - interpret data as iso14443a communications");
+ PrintAndLog(" mf - interpret data as iso14443a communications and decrypt crypto1 stream");
+ PrintAndLog(" 14b - interpret data as iso14443b communications");
+ PrintAndLog(" 15 - interpret data as iso15693 communications");
+ PrintAndLog(" iclass - interpret data as iclass communications");
+ PrintAndLog(" topaz - interpret data as topaz communications");
+ PrintAndLog("");
+ PrintAndLog("example: hf list 14a f");
+ PrintAndLog("example: hf list iclass");
+ PrintAndLog("example: hf list save myCardTrace.trc");
+ PrintAndLog("example: hf list 14a l myCardTrace.trc");
+ return 0;
+ }
+
+
+ uint8_t *trace;
+ uint32_t tracepos = 0;
+ uint32_t traceLen = 0;
+
+ if (loadFromFile) {
+ #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions
+ FILE *tracefile = NULL;
+ size_t bytes_read;
+ trace = malloc(TRACE_CHUNK_SIZE);
+ if (trace == NULL) {
+ PrintAndLog("Cannot allocate memory for trace");
+ return 2;
+ }
+ if ((tracefile = fopen(filename,"rb")) == NULL) {
+ PrintAndLog("Could not open file %s", filename);
+ free(trace);
+ return 0;
+ }
+ while (!feof(tracefile)) {
+ bytes_read = fread(trace+traceLen, 1, TRACE_CHUNK_SIZE, tracefile);
+ traceLen += bytes_read;
+ if (!feof(tracefile)) {
+ uint8_t *p = realloc(trace, traceLen + TRACE_CHUNK_SIZE);
+ if (p == NULL) {
+ PrintAndLog("Cannot allocate memory for trace");
+ free(trace);
+ fclose(tracefile);
+ return 2;
+ }
+ trace = p;
+ }
+ }
+ fclose(tracefile);
+ } else {
+ trace = malloc(USB_CMD_DATA_SIZE);
+ // Query for the size of the trace
+ UsbCommand response;
+ GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, -1, false);
+ traceLen = response.arg[2];
+ if (traceLen > USB_CMD_DATA_SIZE) {
+ uint8_t *p = realloc(trace, traceLen);
+ if (p == NULL) {
+ PrintAndLog("Cannot allocate memory for trace");
+ free(trace);
+ return 2;
+ }
+ trace = p;
+ GetFromBigBuf(trace, traceLen, 0, NULL, -1, false);
+ }
+ }
+
+ if (saveToFile) {
+ FILE *tracefile = NULL;
+ if ((tracefile = fopen(filename,"wb")) == NULL) {
+ PrintAndLog("Could not create file %s", filename);
+ return 1;
+ }
+ fwrite(trace, 1, traceLen, tracefile);
+ PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, filename);
+ fclose(tracefile);
+ } else {
+ PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
+ PrintAndLog("");
+ PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
+ PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
+ PrintAndLog("iClass - Timings are not as accurate");
+ PrintAndLog("");
+ PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
+ PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
+
+ ClearAuthData();
+ while(tracepos < traceLen)
+ {
+ tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes);
+ }
+ }
+
+ free(trace);
+ return 0;