Skip to content

Commit

Permalink
rework of GetFromBigBuf() (#597)
Browse files Browse the repository at this point in the history
* this should fix crashes reported in issue #497
* don't allow receiver thread to write directly into arbitrary main thread's memory
* instead use cmdBuffer[] for CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K as well
* add timeout and warning options to GetFromBigBuf(), same as in WaitForResponseTimeoutW()
* move GetFromBigBuf() from data.c to comms.c
* remove data.c and data.h
  • Loading branch information
pwpiwi committed May 8, 2018
1 parent 03cdcca commit babca44
Show file tree
Hide file tree
Showing 27 changed files with 87 additions and 116 deletions.
1 change: 0 additions & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ CMDSRCS = crapto1/crapto1.c\
crc64.c \
iso14443crc.c \
iso15693tools.c \
data.c \
graph.c \
ui.c \
cmddata.c \
Expand Down
12 changes: 4 additions & 8 deletions client/cmddata.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <string.h> // also included in util.h
#include <inttypes.h>
#include <limits.h> // for CmdNorm INT_MIN && INT_MAX
#include "data.h" // also included in util.h
#include "cmddata.h"
#include "util.h"
#include "cmdmain.h"
Expand Down Expand Up @@ -591,8 +590,7 @@ int CmdBitsamples(const char *Cmd)
int cnt = 0;
uint8_t got[12288];

GetFromBigBuf(got,sizeof(got),0);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(got, sizeof(got), 0 , NULL, -1, false);

for (int j = 0; j < sizeof(got); j++) {
for (int k = 0; k < 8; k++) {
Expand Down Expand Up @@ -1131,8 +1129,7 @@ int CmdHexsamples(const char *Cmd)
return 0;
}

GetFromBigBuf(got,requested,offset);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(got, requested, offset, NULL, -1, false);

i = 0;
for (j = 0; j < requested; j++) {
Expand Down Expand Up @@ -1200,10 +1197,9 @@ int getSamples(int n, bool silent)
n = sizeof(got);

if (!silent) PrintAndLog("Reading %d bytes from device memory\n", n);
GetFromBigBuf(got,n,0);
if (!silent) PrintAndLog("Data fetched");
UsbCommand response;
WaitForResponse(CMD_ACK, &response);
GetFromBigBuf(got, n, 0, &response, -1, false);
if (!silent) PrintAndLog("Data fetched");
uint8_t bits_per_sample = 8;

//Old devices without this feature would send 0 at arg[0]
Expand Down
7 changes: 2 additions & 5 deletions client/cmdhf.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <string.h>
#include "proxmark3.h"
#include "util.h"
#include "data.h"
#include "ui.h"
#include "iso14443crc.h"
#include "parity.h"
Expand Down Expand Up @@ -497,8 +496,7 @@ int CmdHFList(const char *Cmd)
trace = malloc(USB_CMD_DATA_SIZE);
// Query for the size of the trace
UsbCommand response;
GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0);
WaitForResponse(CMD_ACK, &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);
Expand All @@ -508,8 +506,7 @@ int CmdHFList(const char *Cmd)
return 2;
}
trace = p;
GetFromBigBuf(trace, traceLen, 0);
WaitForResponse(CMD_ACK, NULL);
GetFromBigBuf(trace, traceLen, 0, NULL, -1, false);
}
}

Expand Down
1 change: 0 additions & 1 deletion client/cmdhf14a.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "util.h"
#include "util_posix.h"
#include "iso14443crc.h"
#include "data.h"
#include "proxmark3.h"
#include "ui.h"
#include "cmdparser.h"
Expand Down
1 change: 0 additions & 1 deletion client/cmdhf14b.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <stdint.h>
#include "iso14443crc.h"
#include "proxmark3.h"
#include "data.h"
#include "graph.h"
#include "util.h"
#include "ui.h"
Expand Down
1 change: 0 additions & 1 deletion client/cmdhf15.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <stdint.h>

#include "proxmark3.h"
#include "data.h"
#include "graph.h"
#include "ui.h"
#include "util.h"
Expand Down
7 changes: 2 additions & 5 deletions client/cmdhficlass.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <sys/stat.h>
#include <ctype.h>
#include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
#include "data.h"
#include "proxmark3.h"
#include "ui.h"
#include "cmdparser.h"
Expand Down Expand Up @@ -750,8 +749,7 @@ int CmdHFiClassReader_Dump(const char *Cmd) {
blocksRead = (sizeof(tag_data)/8) - blockno;
}
// response ok - now get bigbuf content of the dump
GetFromBigBuf(tag_data+(blockno*8), blocksRead*8, startindex);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(tag_data+(blockno*8), blocksRead*8, startindex, NULL, -1, false);
size_t gotBytes = blocksRead*8 + blockno*8;

// try AA2
Expand Down Expand Up @@ -793,8 +791,7 @@ int CmdHFiClassReader_Dump(const char *Cmd) {
blocksRead = (sizeof(tag_data) - gotBytes)/8;
}
// get dumped data from bigbuf
GetFromBigBuf(tag_data+gotBytes, blocksRead*8, startindex);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(tag_data+gotBytes, blocksRead*8, startindex, NULL, -1, false);

gotBytes += blocksRead*8;
} else { //field is still on - turn it off...
Expand Down
7 changes: 2 additions & 5 deletions client/cmdhflegic.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <string.h>
#include <inttypes.h>
#include "proxmark3.h"
#include "data.h"
#include "ui.h"
#include "cmdparser.h"
#include "cmdhflegic.h"
Expand Down Expand Up @@ -64,8 +63,7 @@ int CmdLegicDecode(const char *Cmd)
char token_type[4];

// copy data from proxmark into buffer
GetFromBigBuf(data_buf,sizeof(data_buf),0);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(data_buf, sizeof(data_buf), 0, NULL, -1, false);

// Output CDF System area (9 bytes) plus remaining header area (12 bytes)

Expand Down Expand Up @@ -294,8 +292,7 @@ int CmdLegicSave(const char *Cmd)
return -1;
}

GetFromBigBuf(got,requested,offset);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(got, requested, offset, NULL, -1, false);

for (int j = 0; j < requested; j += 8) {
fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
Expand Down
1 change: 0 additions & 1 deletion client/cmdhflist.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <stdint.h>
#include <stdbool.h>
#include "util.h"
#include "data.h"
#include "ui.h"
#include "iso14443crc.h"
#include "parity.h"
Expand Down
4 changes: 1 addition & 3 deletions client/cmdhfmfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "mifare.h"
#include "util.h"
#include "protocols.h"
#include "data.h"

#define MAX_UL_BLOCKS 0x0f
#define MAX_ULC_BLOCKS 0x2b
Expand Down Expand Up @@ -1325,8 +1324,7 @@ int CmdHF14AMfUDump(const char *Cmd){
PrintAndLog("Data exceeded Buffer size!");
bufferSize = sizeof(data);
}
GetFromBigBuf(data, bufferSize, startindex);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(data, bufferSize, startindex, NULL, -1, false);

Pages = bufferSize/4;
// Load lock bytes.
Expand Down
6 changes: 1 addition & 5 deletions client/cmdhw.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "cmdhw.h"
#include "cmdmain.h"
#include "cmddata.h"
#include "data.h"

/* low-level hardware control */

Expand Down Expand Up @@ -429,13 +428,10 @@ int CmdVersion(const char *Cmd)

int CmdStatus(const char *Cmd)
{
uint8_t speed_test_buffer[USB_CMD_DATA_SIZE];
sample_buf = speed_test_buffer;

clearCommandBuffer();
UsbCommand c = {CMD_STATUS};
SendCommand(&c);
if (!WaitForResponseTimeout(CMD_ACK,&c,1900)) {
if (!WaitForResponseTimeout(CMD_ACK, &c, 1900)) {
PrintAndLog("Status command failed. USB Speed Test timed out");
}
return 0;
Expand Down
1 change: 0 additions & 1 deletion client/cmdlf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "graph.h" // for graph data
#include "cmdparser.h" // for getting cli commands included in cmdmain.h
#include "cmdmain.h" // for sending cmds to device
#include "data.h" // for GetFromBigBuf
#include "cmddata.h" // for `lf search`
#include "cmdlfawid.h" // for awid menu
#include "cmdlfem4x.h" // for em4x menu
Expand Down
6 changes: 2 additions & 4 deletions client/cmdlfcotag.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "proxmark3.h"
#include "ui.h"
#include "cmddata.h"
#include "data.h"
#include "cmdlfcotag.h"
#include "lfdemod.h"
#include "usb_cmd.h"
Expand Down Expand Up @@ -99,10 +98,9 @@ int CmdCOTAGRead(const char *Cmd) {
getSamples(0, true); break;
}
case 1: {
GetFromBigBuf(DemodBuffer, COTAG_BITS, 0);
DemodBufferLen = COTAG_BITS;
UsbCommand response;
if ( !WaitForResponseTimeout(CMD_ACK, &response, 1000) ) {
DemodBufferLen = COTAG_BITS;
if (!GetFromBigBuf(DemodBuffer, COTAG_BITS, 0, &response, 1000, true)) {
PrintAndLog("timeout while waiting for reply.");
return -1;
}
Expand Down
4 changes: 1 addition & 3 deletions client/cmdlfem4x.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "proxmark3.h"
#include "ui.h"
#include "util.h"
#include "data.h"
#include "graph.h"
#include "cmdparser.h"
#include "cmddata.h"
Expand Down Expand Up @@ -804,8 +803,7 @@ int usage_lf_em_read(void) {
bool downloadSamplesEM() {
// 8 bit preamble + 32 bit word response (max clock (128) * 40bits = 5120 samples)
uint8_t got[6000];
GetFromBigBuf(got, sizeof(got), 0);
if ( !WaitForResponseTimeout(CMD_ACK, NULL, 4000) ) {
if (!GetFromBigBuf(got, sizeof(got), 0, NULL, 4000, true)) {
PrintAndLog("command execution time out");
return false;
}
Expand Down
7 changes: 2 additions & 5 deletions client/cmdlfhitag.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#include "proxmark3.h"
#include "ui.h"
#include "cmdparser.h"
Expand All @@ -34,8 +33,7 @@ int CmdLFHitagList(const char *Cmd)

// Query for the actual size of the trace
UsbCommand response;
GetFromBigBuf(got, USB_CMD_DATA_SIZE, 0);
WaitForResponse(CMD_ACK, &response);
GetFromBigBuf(got, USB_CMD_DATA_SIZE, 0, &response, -1, false);
uint16_t traceLen = response.arg[2];
if (traceLen > USB_CMD_DATA_SIZE) {
uint8_t *p = realloc(got, traceLen);
Expand All @@ -45,8 +43,7 @@ int CmdLFHitagList(const char *Cmd)
return 2;
}
got = p;
GetFromBigBuf(got, traceLen, 0);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(got, traceLen, 0, NULL, -1, false);
}

PrintAndLog("recorded activity (TraceLen = %d bytes):");
Expand Down
1 change: 0 additions & 1 deletion client/cmdlfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <limits.h>
#include "cmdlfio.h"
#include "proxmark3.h"
#include "data.h"
#include "graph.h"
#include "ui.h"
#include "cmdparser.h"
Expand Down
4 changes: 1 addition & 3 deletions client/cmdlft55xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "cmdlf.h"
#include "cmdlft55xx.h"
#include "util.h"
#include "data.h"
#include "lfdemod.h"
#include "cmdhf14a.h" //for getTagInfo
#include "protocols.h"
Expand Down Expand Up @@ -1355,8 +1354,7 @@ int CmdResetRead(const char *Cmd) {
}

uint8_t got[BIGBUF_SIZE-1];
GetFromBigBuf(got,sizeof(got),0);
WaitForResponse(CMD_ACK,NULL);
GetFromBigBuf(got, sizeof(got), 0, NULL, -1 , 0);
setGraphBuf(got, sizeof(got));
return 1;
}
Expand Down
5 changes: 3 additions & 2 deletions client/cmdlfti.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
// Low frequency TI commands
//-----------------------------------------------------------------------------

#include "cmdlfti.h"

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "crc16.h"
#include "proxmark3.h"
#include "data.h"
#include "ui.h"
#include "graph.h"
#include "cmdparser.h"
#include "cmdlfti.h"
#include "util.h"

static int CmdHelp(const char *Cmd);

Expand Down
1 change: 0 additions & 1 deletion client/cmdmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <string.h>
#include "cmdparser.h"
#include "proxmark3.h"
#include "data.h"
#include "usb_cmd.h"
#include "ui.h"
#include "cmdhf.h"
Expand Down
1 change: 0 additions & 1 deletion client/cmdscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "proxmark3.h"
#include "scripting.h"
#include "data.h"
#include "ui.h"
#include "graph.h"
#include "cmdparser.h"
Expand Down
Loading

0 comments on commit babca44

Please sign in to comment.