From 295054c190f7c76802c3d8defca5b8a04ce49dd4 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 2 May 2018 19:37:51 +0200 Subject: [PATCH] chg: unifying some different download functions from device. --- client/cmdanalyse.c | 8 +++- client/cmdhflegic.c | 6 +-- client/cmdmain.c | 94 ++++++++++++++++++--------------------------- client/cmdmain.h | 38 +++++++++++++++--- 4 files changed, 81 insertions(+), 65 deletions(-) diff --git a/client/cmdanalyse.c b/client/cmdanalyse.c index cc1b9a465..7392876af 100644 --- a/client/cmdanalyse.c +++ b/client/cmdanalyse.c @@ -534,7 +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, NULL, -1, true); + GetFromDevice(FLASH_MEM, got, sizeof(got), 0, NULL, -1, true); print_hex(got, 8); @@ -543,6 +543,12 @@ int CmdAnalyseA(const char *Cmd){ printf("I %u (0x%x) | %x \n", i, i, got[i] ); } } + + // binary + saveFile("flash_mem", "bin", got, sizeof(got)); + + // eml + saveFileEML("flash_mem", "eml", got, sizeof(got), 16); break; } // wipe diff --git a/client/cmdhflegic.c b/client/cmdhflegic.c index 6c0e2f23e..677eba14c 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, NULL, 2500, false) ) { + if ( !GetFromDevice( BIG_BUF_EML, 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, NULL, 2500, false) ) { + if (!GetFromDevice( BIG_BUF_EML, 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, NULL, 2500, false)) { + if (!GetFromDevice( BIG_BUF_EML, data, numofbytes, 0, NULL, 2500, false)) { PrintAndLogEx(WARNING, "Fail, transfer from device time-out"); free(data); return 4; diff --git a/client/cmdmain.c b/client/cmdmain.c index 37ff2b6eb..05b902097 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -7,28 +7,7 @@ //----------------------------------------------------------------------------- // Main command parser entry point //----------------------------------------------------------------------------- - -#include -#include -#include -#include -#include #include "cmdmain.h" -#include "util_posix.h" -#include "cmdparser.h" -#include "proxmark3.h" -#include "data.h" -#include "usb_cmd.h" -#include "ui.h" -#include "cmdhf.h" -#include "cmddata.h" -#include "cmdhw.h" -#include "cmdlf.h" -#include "cmdtrace.h" -#include "util.h" -#include "cmdscript.h" -#include "cmdcrc.h" -#include "cmdanalyse.h" static int CmdHelp(const char *Cmd); static int CmdQuit(const char *Cmd); @@ -236,6 +215,44 @@ void UsbCommandReceived(UsbCommand* _ch) { } } +bool GetFromDevice(DeviceMemType_t memtype, 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 resp; + if (response == NULL) + response = &resp; + + // clear + clearCommandBuffer(); + + switch (memtype) { + case BIG_BUF: { + UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}}; + SendCommand(&c); + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K); + } + case BIG_BUF_EML: { + UsbCommand c = {CMD_DOWNLOAD_EML_BIGBUF, {start_index, bytes, 0}}; + SendCommand(&c); + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K); + } + case FLASH_MEM: { + UsbCommand c = {CMD_DOWNLOAND_FLASH_MEM, {start_index, bytes, 0}}; + SendCommand(&c); + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_FLASHMEM); + } + case SIM_MEM: { + //UsbCommand c = {CMD_DOWNLOAND_SIM_MEM, {start_index, bytes, 0}}; + //SendCommand(&c); + //return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_SIMMEM); + return false; + } + } + return false; +} + /** * Data transfer from Proxmark to client. This method times out after * ms_timeout milliseconds. @@ -256,45 +273,10 @@ bool GetFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbComma 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) { diff --git a/client/cmdmain.h b/client/cmdmain.h index 8a74922fe..b4e4330f6 100644 --- a/client/cmdmain.h +++ b/client/cmdmain.h @@ -13,8 +13,36 @@ #include #include +#include +#include +#include +#include +#include +#include "util_posix.h" +#include "proxmark3.h" #include "usb_cmd.h" +#include "util.h" +#include "ui.h" #include "cmdparser.h" +#include "loclass/fileutils.h" +#include "cmdhf.h" +#include "cmddata.h" +#include "cmdhw.h" +#include "cmdlf.h" +#include "cmdtrace.h" +#include "cmdscript.h" +#include "cmdcrc.h" +#include "cmdanalyse.h" + +//For storing command that are received from the device +#define CMD_BUFFER_SIZE 50 +typedef enum { + BIG_BUF, + BIG_BUF_EML, + FLASH_MEM, + SIM_MEM, + } DeviceMemType_t; + extern void UsbCommandReceived(UsbCommand *c); extern int CommandReceived(char *Cmd); extern bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning); @@ -23,10 +51,10 @@ 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 +extern bool GetFromDevice(DeviceMemType_t memtype, uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); + +bool GetFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); +//bool GetEMLFromBigBuf(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); +//bool GetFromFlashMen(uint8_t *dest, uint32_t bytes, uint32_t start_index, UsbCommand *response, size_t ms_timeout, bool show_warning); #endif