From 207fa2b574bc4646c0a08ceaccf13dcf41a8193f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 2 May 2018 08:11:29 +0200 Subject: [PATCH] add: potential fix for OSX uses, by @piwi chg: adapting fix to support iceman forks extended commands. --- armsrc/appmain.c | 2 +- client/cmdanalyse.c | 3 +- client/cmddata.c | 9 +-- client/cmddata.h | 1 - client/cmdhf14a.h | 1 - client/cmdhf14b.h | 1 - client/cmdhf15.h | 1 - client/cmdhfemv.h | 1 - client/cmdhffelica.c | 5 +- client/cmdhffelica.h | 1 - client/cmdhficlass.c | 6 +- client/cmdhficlass.h | 1 - client/cmdhflegic.c | 6 +- client/cmdhflegic.h | 1 - client/cmdhflist.h | 1 - client/cmdhfmf.h | 1 - client/cmdhfmfdes.c | 1 - client/cmdhfmfu.h | 1 - client/cmdhw.c | 4 -- client/cmdlf.h | 3 +- client/cmdlfcotag.c | 6 +- client/cmdlfem4x.c | 3 +- client/cmdlfhitag.c | 7 +- client/cmdlfindala.h | 1 - client/cmdlfio.h | 1 - client/cmdlfparadox.c | 1 - client/cmdlft55xx.c | 6 +- client/cmdlft55xx.h | 1 - client/cmdlfti.c | 1 - client/cmdmain.c | 160 +++++++++++++++++++++++++++++++++--------- client/cmdmain.h | 3 + client/cmdscript.c | 1 - client/cmdtrace.c | 6 +- client/cmdtrace.h | 3 +- client/data.c | 29 +------- client/data.h | 4 +- client/mifarehost.h | 1 - client/scripting.c | 4 +- client/util.h | 1 - 39 files changed, 160 insertions(+), 129 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index b431aa16e..673b28d52 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -350,7 +350,7 @@ void printUSBSpeed(void) { uint32_t bytes_transferred = 0; LED_B_ON(); - while(end_time < start_time + USB_SPEED_TEST_MIN_TIME) { + while (end_time < start_time + USB_SPEED_TEST_MIN_TIME) { cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, 0, USB_CMD_DATA_SIZE, 0, test_data, USB_CMD_DATA_SIZE); end_time = GetTickCount(); bytes_transferred += USB_CMD_DATA_SIZE; diff --git a/client/cmdanalyse.c b/client/cmdanalyse.c index f85a0f08b..cc1b9a465 100644 --- a/client/cmdanalyse.c +++ b/client/cmdanalyse.c @@ -534,8 +534,7 @@ int CmdAnalyseA(const char *Cmd){ uint8_t got[0x3FFFF]; memset(got, 0, sizeof(got)); PrintAndLogEx(NORMAL, "downloading %u bytes from flashmem", sizeof(got)); - GetFromFlashMen(got, sizeof(got), 0); - WaitForResponse(CMD_ACK, NULL); + GetFromFlashMen(got, sizeof(got), 0, NULL, -1, true); print_hex(got, 8); diff --git a/client/cmddata.c b/client/cmddata.c index b8a1d7677..1c67f7900 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -805,8 +805,7 @@ int CmdBitsamples(const char *Cmd) int cnt = 0; uint8_t got[12288]; - GetFromBigBuf(got, sizeof(got), 0); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if (!GetFromBigBuf(got, sizeof(got), 0, NULL, 2500 , false)) { PrintAndLogEx(WARNING, "command execution time out"); return false; } @@ -1338,8 +1337,7 @@ int CmdHexsamples(const char *Cmd) return 0; } - GetFromBigBuf(got, requested, offset); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(got, requested, offset, NULL, 2500, false)) { PrintAndLogEx(WARNING, "command execution time out"); return false; } @@ -1416,10 +1414,9 @@ int getSamples(int n, bool silent) { n = sizeof(got); if (!silent) PrintAndLogEx(NORMAL, "Reading %d bytes from device memory\n", n); - GetFromBigBuf(got, n, 0); UsbCommand response; - if ( !WaitForResponseTimeout(CMD_ACK, &response, 10000) ) { + if ( !GetFromBigBuf(got, n, 0, &response, 10000, true) ) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return 1; } diff --git a/client/cmddata.h b/client/cmddata.h index ee39906fd..dbd735747 100644 --- a/client/cmddata.h +++ b/client/cmddata.h @@ -20,7 +20,6 @@ #include // also included in util.h #include #include // for CmdNorm INT_MIN && INT_MAX -#include "data.h" // also included in util.h #include "util.h" #include "cmdmain.h" #include "proxmark3.h" // sendcommand diff --git a/client/cmdhf14a.h b/client/cmdhf14a.h index 444728a6a..b2ae5896e 100644 --- a/client/cmdhf14a.h +++ b/client/cmdhf14a.h @@ -25,7 +25,6 @@ #include "cmdparser.h" #include "cmdmain.h" #include "iso14443crc.h" -#include "data.h" #include "mifare.h" #include "cmdhfmf.h" #include "cmdhfmfu.h" diff --git a/client/cmdhf14b.h b/client/cmdhf14b.h index 6db0528f3..418a7bc8a 100644 --- a/client/cmdhf14b.h +++ b/client/cmdhf14b.h @@ -17,7 +17,6 @@ #include #include "crc16.h" #include "proxmark3.h" -#include "data.h" #include "graph.h" #include "util.h" #include "ui.h" diff --git a/client/cmdhf15.h b/client/cmdhf15.h index 2d55f3553..2908a9aab 100644 --- a/client/cmdhf15.h +++ b/client/cmdhf15.h @@ -17,7 +17,6 @@ #include #include "proxmark3.h" -#include "data.h" #include "graph.h" #include "ui.h" #include "util.h" diff --git a/client/cmdhfemv.h b/client/cmdhfemv.h index 00bbb616a..0b68a455c 100644 --- a/client/cmdhfemv.h +++ b/client/cmdhfemv.h @@ -15,7 +15,6 @@ #include #include #include "proxmark3.h" -#include "data.h" #include "ui.h" #include "cmdparser.h" #include "cmdmain.h" diff --git a/client/cmdhffelica.c b/client/cmdhffelica.c index e19c910a7..d216ae947 100644 --- a/client/cmdhffelica.c +++ b/client/cmdhffelica.c @@ -390,9 +390,8 @@ int CmdHFFelicaDumpLite(const char *Cmd) { // only download data if there is any. if ( tracelen > 0 ) { - GetFromBigBuf(trace, tracelen, 0); - - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + + if ( !GetFromBigBuf(trace, tracelen, 0, NULL, 2500, false) ){ PrintAndLogEx(WARNING, "command execution time out"); free(trace); return 0; diff --git a/client/cmdhffelica.h b/client/cmdhffelica.h index e360e6097..f82d2d574 100644 --- a/client/cmdhffelica.h +++ b/client/cmdhffelica.h @@ -23,7 +23,6 @@ #include "cmdparser.h" #include "cmdmain.h" #include "iso14443crc.h" -#include "data.h" #include "cmdhf.h" // list cmd #include "mifare.h" // felica_card_select_t struct diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c index 44719f52a..06e1a418a 100644 --- a/client/cmdhficlass.c +++ b/client/cmdhficlass.c @@ -1066,8 +1066,7 @@ int CmdHFiClassReader_Dump(const char *Cmd) { } // response ok - now get bigbuf content of the dump - GetFromBigBuf(tag_data+(blockno*8), blocksRead*8, startindex); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(tag_data+(blockno*8), blocksRead*8, startindex, NULL, 2500, false) ) { PrintAndLogEx(WARNING, "command execution time out"); return 0; } @@ -1112,8 +1111,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); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(tag_data + gotBytes, blocksRead * 8, startindex, NULL, 2500, false)) { PrintAndLogEx(WARNING, "command execution time out"); return 0; } diff --git a/client/cmdhficlass.h b/client/cmdhficlass.h index 7aaa30a5e..e9bedf9fe 100644 --- a/client/cmdhficlass.h +++ b/client/cmdhficlass.h @@ -16,7 +16,6 @@ #include #include #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" diff --git a/client/cmdhflegic.c b/client/cmdhflegic.c index 7d4edbefb..6c0e2f23e 100644 --- a/client/cmdhflegic.c +++ b/client/cmdhflegic.c @@ -759,7 +759,7 @@ int legic_read_mem(uint32_t offset, uint32_t len, uint32_t iv, uint8_t *out, uin PrintAndLogEx(WARNING, "Fail, only managed to read %u bytes", *outlen); // copy data from device - if ( !GetEMLFromBigBuf(out, *outlen, 0) ) { + if ( !GetEMLFromBigBuf(out, *outlen, 0, NULL, 2500, false) ) { PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); return 4; } @@ -924,7 +924,7 @@ int CmdLegicDump(const char *Cmd){ PrintAndLogEx(WARNING, "Fail, only managed to read 0x%02X bytes of 0x%02X", readlen, dumplen); // copy data from device - if (!GetEMLFromBigBuf(data, readlen, 0) ) { + if (!GetEMLFromBigBuf(data, readlen, 0, NULL, 2500, false) ) { PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); free(data); return 4; @@ -1170,7 +1170,7 @@ int CmdLegicESave(const char *Cmd) { // download emulator memory PrintAndLogEx(NORMAL, "Reading emulator memory..."); - if (!GetEMLFromBigBuf(data, numofbytes, 0)) { + if (!GetEMLFromBigBuf(data, numofbytes, 0, NULL, 2500, false)) { PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); free(data); return 4; diff --git a/client/cmdhflegic.h b/client/cmdhflegic.h index 44bd85930..645a6b993 100644 --- a/client/cmdhflegic.h +++ b/client/cmdhflegic.h @@ -14,7 +14,6 @@ #include #include #include "proxmark3.h" -#include "data.h" #include "ui.h" #include "cmdparser.h" #include "cmdmain.h" diff --git a/client/cmdhflist.h b/client/cmdhflist.h index d079b7e46..1955119f2 100644 --- a/client/cmdhflist.h +++ b/client/cmdhflist.h @@ -16,7 +16,6 @@ #include #include #include "util.h" -#include "data.h" #include "ui.h" #include "cmdhf14a.h" // ISO14443-A #include "cmdhf14b.h" // ISO14443-B diff --git a/client/cmdhfmf.h b/client/cmdhfmf.h index bdc142af6..7c5f03b90 100644 --- a/client/cmdhfmf.h +++ b/client/cmdhfmf.h @@ -18,7 +18,6 @@ #include #include "proxmark3.h" #include "iso14443crc.h" -#include "data.h" #include "ui.h" #include "cmdparser.h" #include "common.h" diff --git a/client/cmdhfmfdes.c b/client/cmdhfmfdes.c index ffac1246e..8bdae2e04 100644 --- a/client/cmdhfmfdes.c +++ b/client/cmdhfmfdes.c @@ -18,7 +18,6 @@ #include "../include/common.h" #include "../include/mifare.h" #include "iso14443crc.h" -#include "data.h" #include "ui.h" #include "cmdparser.h" #include "util.h" diff --git a/client/cmdhfmfu.h b/client/cmdhfmfu.h index 492188085..da9cfd02f 100644 --- a/client/cmdhfmfu.h +++ b/client/cmdhfmfu.h @@ -9,7 +9,6 @@ #include "mifare.h" #include "util.h" #include "protocols.h" -#include "data.h" typedef struct { uint8_t version[8]; diff --git a/client/cmdhw.c b/client/cmdhw.c index 6095360b7..ca73eb234 100644 --- a/client/cmdhw.c +++ b/client/cmdhw.c @@ -18,7 +18,6 @@ #include "cmdhw.h" #include "cmdmain.h" #include "cmddata.h" -#include "data.h" /* low-level hardware control */ @@ -274,9 +273,6 @@ int CmdVersion(const char *Cmd) { } int CmdStatus(const char *Cmd) { - uint8_t speed_test_buffer[USB_CMD_DATA_SIZE]; - sample_buf = speed_test_buffer; - sample_buf_size = USB_CMD_DATA_SIZE; clearCommandBuffer(); UsbCommand c = {CMD_STATUS}; SendCommand(&c); diff --git a/client/cmdlf.h b/client/cmdlf.h index c5e64c901..de216435f 100644 --- a/client/cmdlf.h +++ b/client/cmdlf.h @@ -22,8 +22,7 @@ #include "ui.h" // for show graph controls #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 "cmdmain.h" // for sending cmds to device. GetFromBigBuf #include "cmddata.h" // for `lf search` #include "cmdlfawid.h" // for awid menu #include "cmdlfem4x.h" // for em4x menu diff --git a/client/cmdlfcotag.c b/client/cmdlfcotag.c index 3ba646d85..51c85ab35 100644 --- a/client/cmdlfcotag.c +++ b/client/cmdlfcotag.c @@ -90,12 +90,12 @@ int CmdCOTAGRead(const char *Cmd) { break; } case 1: { - GetFromBigBuf(DemodBuffer, COTAG_BITS, 0); - DemodBufferLen = COTAG_BITS; - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1000) ) { + + if ( !GetFromBigBuf(DemodBuffer, COTAG_BITS, 0, NULL, 1000, false)) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return -1; } + DemodBufferLen = COTAG_BITS; return CmdCOTAGDemod(""); } } diff --git a/client/cmdlfem4x.c b/client/cmdlfem4x.c index 9393cdc44..5d4b36ca7 100644 --- a/client/cmdlfem4x.c +++ b/client/cmdlfem4x.c @@ -995,8 +995,7 @@ 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, 2500) ) { + if ( !GetFromBigBuf(got, sizeof(got), 0, NULL, 2500, false)) { PrintAndLogEx(WARNING, "command execution time out"); return false; } diff --git a/client/cmdlfhitag.c b/client/cmdlfhitag.c index c6a023cd5..eff4619da 100644 --- a/client/cmdlfhitag.c +++ b/client/cmdlfhitag.c @@ -11,7 +11,6 @@ #include #include #include -#include "data.h" #include "proxmark3.h" #include "ui.h" #include "cmdparser.h" @@ -39,8 +38,7 @@ int CmdLFHitagList(const char *Cmd) { // Query for the actual size of the trace UsbCommand response; - GetFromBigBuf(got, USB_CMD_DATA_SIZE, 0); - if ( !WaitForResponseTimeout(CMD_ACK, &response, 2500) ) { + if ( !GetFromBigBuf(got, USB_CMD_DATA_SIZE, 0, &response, 2500, false) ) { PrintAndLogEx(WARNING, "command execution time out"); free(got); return 2; @@ -55,8 +53,7 @@ int CmdLFHitagList(const char *Cmd) { return 2; } got = p; - GetFromBigBuf(got, traceLen, 0); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(got, traceLen, 0, NULL, 2500, false) ) { PrintAndLogEx(WARNING, "command execution time out"); free(got); return 2; diff --git a/client/cmdlfindala.h b/client/cmdlfindala.h index 73b41a13d..cb852e389 100644 --- a/client/cmdlfindala.h +++ b/client/cmdlfindala.h @@ -19,7 +19,6 @@ #include "lfdemod.h" // parityTest, bitbytes_to_byte #include "util.h" // weigandparity #include "protocols.h" // for T55xx config register definitions -#include "data.h" #include "cmdmain.h" #include "cmddata.h" #include "cmdlf.h" // lf_read diff --git a/client/cmdlfio.h b/client/cmdlfio.h index c991f451b..6bdb4b88f 100644 --- a/client/cmdlfio.h +++ b/client/cmdlfio.h @@ -13,7 +13,6 @@ #include "lfdemod.h" // parityTest, bitbytes_to_byte #include "util.h" // weigandparity #include "protocols.h" // for T55xx config register definitions -#include "data.h" #include "cmdmain.h" #include "cmddata.h" diff --git a/client/cmdlfparadox.c b/client/cmdlfparadox.c index 109fe1d4d..139797a66 100644 --- a/client/cmdlfparadox.c +++ b/client/cmdlfparadox.c @@ -146,7 +146,6 @@ int CmdParadoxRead(const char *Cmd) { return CmdParadoxDemod(Cmd); } - int CmdParadoxSim(const char *Cmd) { char cmdp = param_getchar(Cmd, 0); diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c index f6a2bf5e3..bb7ffa483 100644 --- a/client/cmdlft55xx.c +++ b/client/cmdlft55xx.c @@ -1309,8 +1309,7 @@ bool AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){ //uint8_t got[12288]; uint8_t got[7679]; - GetFromBigBuf(got, sizeof(got), 0); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 6000) ) { + if ( !GetFromBigBuf(got, sizeof(got), 0, NULL, 6000, true)) { PrintAndLogEx(WARNING, "command execution time out"); return false; } @@ -1442,8 +1441,7 @@ int CmdResetRead(const char *Cmd) { } uint8_t got[BIGBUF_SIZE-1]; - GetFromBigBuf(got, sizeof(got), 0); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(got, sizeof(got), 0, NULL, 2500, false)) { PrintAndLogEx(WARNING, "command execution time out"); return 0; } diff --git a/client/cmdlft55xx.h b/client/cmdlft55xx.h index 7f08b6f5a..ad170514c 100644 --- a/client/cmdlft55xx.h +++ b/client/cmdlft55xx.h @@ -23,7 +23,6 @@ #include "cmddata.h" #include "cmdlf.h" #include "util.h" -#include "data.h" #include "lfdemod.h" #include "cmdhf14a.h" //for getTagInfo diff --git a/client/cmdlfti.c b/client/cmdlfti.c index d74b91f2e..9f32e67f8 100644 --- a/client/cmdlfti.c +++ b/client/cmdlfti.c @@ -13,7 +13,6 @@ #include #include "crc16.h" #include "proxmark3.h" -#include "data.h" #include "ui.h" #include "graph.h" #include "cmdparser.h" diff --git a/client/cmdmain.c b/client/cmdmain.c index cff5d745d..37ff2b6eb 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -45,6 +45,15 @@ static int cmd_tail;//Starts as 0 // to lock cmdBuffer operations from different threads static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER; +int CmdQuit(const char *Cmd) { + return 99; +} + +int CmdRev(const char *Cmd) { + CmdCrc(Cmd); + return 0; +} + static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help. Use ' help' for details of a particular command."}, {"analyse", CmdAnalyse, 1, "{ Analyse bytes... }"}, @@ -69,14 +78,7 @@ int CmdHelp(const char *Cmd) { return 0; } -int CmdQuit(const char *Cmd) { - return 99; -} - -int CmdRev(const char *Cmd) { - CmdCrc(Cmd); - return 0; -} +bool dl_it(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd); /** * @brief This method should be called when sending a new command to the pm3. In case any old * responses from previous commands are stored in the buffer, a call to this method should clear them. @@ -140,7 +142,7 @@ int getCommand(UsbCommand* response) { *@brief WaitForResponseTimeout * @param cmd command to wait for, or CMD_UNKNOWN to take any command. * @param response struct to copy received command into. - * @param ms_timeout + * @param ms_timeout display message after 2 seconds * @return true if command was returned, otherwise false */ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) { @@ -224,32 +226,124 @@ void UsbCommandReceived(UsbCommand* _ch) { PrintAndLogEx(NORMAL, "#db# %08x, %08x, %08x", c->arg[0], c->arg[1], c->arg[2]); break; } - case CMD_DOWNLOADED_FLASHMEM: - case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: - case CMD_DOWNLOADED_EML_BIGBUF: { - - // sample_buf is a array pointer, located in data.c - // arg0 = offset in transfer. Startindex of this chunk - // arg1 = length bytes to transfer - // arg2 = bigbuff tracelength (?) - uint32_t offset = c->arg[0]; - uint32_t len = MIN(c->arg[1], sample_buf_size); - //uint32_t tracelen = c->arg[2]; - - // extended bounds check. - if ( offset + len > sample_buf_size ) { - PrintAndLogEx(FAILED, "ERROR: Out of bounds when downloading from device, offset %u | len %u | total len %u > sample_buf_size %u", offset, len, offset+len, sample_buf_size); - break; - } - //printf("SAMPLE_BUF_SIZE %u | adjusted len %u | offset %u\n", sample_buf_size, len, offset); - - memcpy( sample_buf + offset, c->d.asBytes, len); - //PrintAndLogEx(NORMAL, "ICE:: Download from device. chunk %" PRIu32 " | size %" PRIu32 " | tracelen:%" PRIu32 " \n", offset, len, c->arg[2]); - break; - } + // iceman: hw status - down the path on device, runs printusbspeed which starts sending a lot of + // CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K packages which is not dealt with. I wonder if simply ignoring them will + // work. lets try it. default: { storeCommand(c); break; } } -} \ No newline at end of file +} + +/** +* Data transfer from Proxmark to client. This method times out after +* ms_timeout milliseconds. +* @brief GetFromBigBuf +* @param dest Destination address for transfer +* @param bytes number of bytes to be transferred +* @param start_index offset into Proxmark3 BigBuf[] +* @param response struct to copy last command (CMD_ACK) into +* @param ms_timeout timeout in milliseconds +* @param show_warning display message after 2 seconds +* @return true if command was returned, otherwise false +*/ +bool GetFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) { + + if (dest == NULL) return false; + if (bytes == 0) return true; + + UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}}; + clearCommandBuffer(); + SendCommand(&c); + + UsbCommand resp; + if (response == NULL) + response = &resp; + + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K); +} + +bool GetEMLFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) { + + if (dest == NULL) return false; + if (bytes == 0) return true; + + UsbCommand c = {CMD_DOWNLOAD_EML_BIGBUF, {start_index, bytes, 0}}; + clearCommandBuffer(); + SendCommand(&c); + + UsbCommand resp; + if (response == NULL) + response = &resp; + + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_EML_BIGBUF); +} + +bool GetFromFlashMen(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) { + + if (dest == NULL) return false; + if (bytes == 0) return true; + + UsbCommand c = {CMD_DOWNLOAND_FLASH_MEM, {start_index, bytes, 0}}; + clearCommandBuffer(); + SendCommand(&c); + + UsbCommand resp; + if (response == NULL) + response = &resp; + + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_FLASHMEM); +} + +bool dl_it(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd) { + + uint32_t bytes_completed = 0; + uint64_t start_time = msclock(); + + while (true) { + + if (getCommand(response)) { + + // sample_buf is a array pointer, located in data.c + // arg0 = offset in transfer. Startindex of this chunk + // arg1 = length bytes to transfer + // arg2 = bigbuff tracelength (?) + if (response->cmd == rec_cmd) { + + uint32_t offset = response->arg[0]; + uint32_t copy_bytes = MIN(bytes - bytes_completed, response->arg[1]); + //uint32_t tracelen = c->arg[2]; + + // extended bounds check1. upper limit is USB_CMD_DATA_SIZE + // shouldn't happen + copy_bytes = MIN(copy_bytes, USB_CMD_DATA_SIZE); + + // extended bounds check2. + if ( offset + copy_bytes > bytes ) { + PrintAndLogEx(FAILED, "ERROR: Out of bounds when downloading from device, offset %u | len %u | total len %u > buf_size %u", offset, copy_bytes, offset+copy_bytes, bytes); + break; + } + + memcpy(dest + offset, response->d.asBytes, copy_bytes); + bytes_completed += copy_bytes; + } else if (response->cmd == CMD_ACK) { + return true; + } + } + + if (msclock() - start_time > ms_timeout) { + PrintAndLogEx(FAILED, "Timed out while trying to download data from device"); + break; + } + + if (msclock() - start_time > 3000 && show_warning) { + // 3 seconds elapsed (but this doesn't mean the timeout was exceeded) + PrintAndLogEx(NORMAL, "Waiting for a response from the proxmark..."); + PrintAndLogEx(NORMAL, "You can cancel this operation by pressing the pm3 button"); + show_warning = false; + } + } + return false; +} + diff --git a/client/cmdmain.h b/client/cmdmain.h index f093dcb09..8a74922fe 100644 --- a/client/cmdmain.h +++ b/client/cmdmain.h @@ -23,6 +23,9 @@ extern bool WaitForResponse(uint32_t cmd, UsbCommand* response); extern void clearCommandBuffer(); extern command_t* getTopLevelCommandTable(); +extern bool GetFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); +extern bool GetEMLFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); +extern bool GetFromFlashMen(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); //For storing command that are received from the device #define CMD_BUFFER_SIZE 50 diff --git a/client/cmdscript.c b/client/cmdscript.c index ed587341c..a34a42815 100644 --- a/client/cmdscript.c +++ b/client/cmdscript.c @@ -20,7 +20,6 @@ #include "proxmark3.h" #include "scripting.h" -#include "data.h" #include "ui.h" #include "graph.h" #include "cmdparser.h" diff --git a/client/cmdtrace.c b/client/cmdtrace.c index a4ee409b8..8b6e640f8 100644 --- a/client/cmdtrace.c +++ b/client/cmdtrace.c @@ -487,8 +487,7 @@ int CmdTraceList(const char *Cmd) { if ( isOnline ) { // Query for the size of the trace, downloading USB_CMD_DATA_SIZE UsbCommand response; - GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0); - if ( !WaitForResponseTimeout(CMD_ACK, &response, 4000) ) { + if ( !GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, 4000, true)) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return 1; } @@ -502,8 +501,7 @@ int CmdTraceList(const char *Cmd) { return 2; } trace = p; - GetFromBigBuf(trace, traceLen, 0); - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(trace, traceLen, 0, NULL, 2500, false)) { PrintAndLogEx(WARNING, "command execution time out"); free(trace); return 3; diff --git a/client/cmdtrace.h b/client/cmdtrace.h index fa3f97c20..8e940d220 100644 --- a/client/cmdtrace.h +++ b/client/cmdtrace.h @@ -24,8 +24,7 @@ #include "util.h" // for parsing cli command utils #include "ui.h" // for show graph controls #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 "cmdmain.h" // for sending cmds to device. GetFromBigBuf #include "loclass/fileutils.h" // for saveFile extern int CmdTrace(const char *Cmd); diff --git a/client/data.c b/client/data.c index 46645a223..14fabeac9 100644 --- a/client/data.c +++ b/client/data.c @@ -15,41 +15,16 @@ #include "proxmark3.h" #include "cmdmain.h" -uint32_t sample_buf_size; -uint8_t* sample_buf; - // this triggers a download sequence from device, its received inside cmdmain.c UsbCommandReceived() void GetFromBigBuf(uint8_t *dest, uint32_t len, uint32_t start_index) { - // global - sample_buf = dest; - sample_buf_size = len; - UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, len, 0}}; - clearCommandBuffer(); - SendCommand(&c); } + + // this will download the EMULATOR memory part from device, // inside the BigBuf EML zon. bool GetEMLFromBigBuf(uint8_t *dest, uint32_t len, uint32_t start_index) { - sample_buf = dest; - sample_buf_size = len; - UsbCommand c = {CMD_DOWNLOAD_EML_BIGBUF, {start_index, len, 0}}; - clearCommandBuffer(); - SendCommand(&c); - - // the download will be done inside cmdmain.c function UsbCommandReceived(UsbCommand *UC) - - // we are waiting for the ACK - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) - return false; - - return true; } // Download data from flashmem, rdv40 void GetFromFlashMen(uint8_t *dest, uint32_t len, uint32_t start_index) { - sample_buf = dest; - sample_buf_size = len; - UsbCommand c = {CMD_DOWNLOAND_FLASH_MEM, {start_index, len, 0}}; - clearCommandBuffer(); - SendCommand(&c); } diff --git a/client/data.h b/client/data.h index bea0657b4..c69780ffe 100644 --- a/client/data.h +++ b/client/data.h @@ -14,11 +14,13 @@ #include #include "util.h" -#define FILE_PATH_SIZE 1000 +//#define FILE_PATH_SIZE 1000 +/* extern uint32_t sample_buf_size; extern uint8_t* sample_buf; extern void GetFromBigBuf(uint8_t *dest, uint32_t len, uint32_t start_index); extern bool GetEMLFromBigBuf(uint8_t *dest, uint32_t len, uint32_t start_index); extern void GetFromFlashMen(uint8_t *dest, uint32_t len, uint32_t start_index); +*/ #endif diff --git a/client/mifarehost.h b/client/mifarehost.h index 0ee1504bd..38e15a8cd 100644 --- a/client/mifarehost.h +++ b/client/mifarehost.h @@ -20,7 +20,6 @@ #include "common.h" #include "cmdmain.h" #include "ui.h" -#include "data.h" #include "util.h" #include "crapto1/crapto1.h" #include "crc16.h" diff --git a/client/scripting.c b/client/scripting.c index 5c5749284..1c3e2d189 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -79,9 +79,7 @@ static int l_GetFromBigBuf(lua_State *L){ return 2; // two return values } - GetFromBigBuf(data, len, startindex); - - if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500) ) { + if ( !GetFromBigBuf(data, len, startindex, NULL, 2500, false)) { free(data); lua_pushnil(L); lua_pushstring(L,"command execution time out"); diff --git a/client/util.h b/client/util.h index 59250b5af..997997d55 100644 --- a/client/util.h +++ b/client/util.h @@ -18,7 +18,6 @@ #include #include #include -#include "data.h" #include "ui.h" // PrintAndLog #ifdef ANDROID