From c74dbb63b8e9768edad04c40b09554c8e1cb32b9 Mon Sep 17 00:00:00 2001 From: "Colin J. Brigato" Date: Thu, 6 Sep 2018 05:24:50 +0200 Subject: [PATCH] Pass 2; commit 2; --- client/cmdflashmem.c | 62 +++++++++++++++++++++++++----------- client/cmdmain.c | 4 +-- client/hid-flasher/usb_cmd.h | 12 +++---- include/common.h | 3 ++ include/usb_cmd.h | 16 ++++++---- tools/mkversion.pl | 14 ++++++-- 6 files changed, 77 insertions(+), 34 deletions(-) diff --git a/client/cmdflashmem.c b/client/cmdflashmem.c index 5f83f9263..43230218f 100644 --- a/client/cmdflashmem.c +++ b/client/cmdflashmem.c @@ -12,13 +12,36 @@ #include "rsa.h" #include "sha1.h" +#define MCK 48000000 +//#define FLASH_BAUD 24000000 +#define FLASH_MINFAST 24000000 //33000000 +#define FLASH_BAUD MCK/2 +#define FLASH_FASTBAUD MCK +#define FLASH_MINBAUD FLASH_FASTBAUD + +#define FASTFLASH (FLASHMEM_SPIBAUDRATE > FLASH_MINFAST) + static int CmdHelp(const char *Cmd); + +int usage_flashmem_spibaud(void){ + PrintAndLogEx(NORMAL, "Usage: mem spibaud [h] "); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " h this help"); + PrintAndLogEx(NORMAL, " SPI baudrate in MHz [24|48]"); + PrintAndLogEx(NORMAL, " "); + PrintAndLogEx(NORMAL, " If >= 24Mhz, FASTREADS instead of READS instruction will be used."); + PrintAndLogEx(NORMAL, " Reading Flash ID will virtually always fail under 48Mhz setting"); + PrintAndLogEx(NORMAL, " Unless you know what you are doing, please stay at 24Mhz"); + PrintAndLogEx(NORMAL, "Examples:"); + PrintAndLogEx(NORMAL, " mem spibaud 48"); + return 0; +} + int usage_flashmem_read(void){ PrintAndLogEx(NORMAL, "Read flash memory on device"); - PrintAndLogEx(NORMAL, "Usage: mem read o l [f]"); + PrintAndLogEx(NORMAL, "Usage: mem read o l "); PrintAndLogEx(NORMAL, " o : offset in memory"); PrintAndLogEx(NORMAL, " l : length"); - PrintAndLogEx(NORMAL, " f : fastRead mode"); PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "Examples:"); PrintAndLogEx(NORMAL, " mem read o 0 l 32"); // read 32 bytes starting at offset 0 @@ -42,7 +65,6 @@ int usage_flashmem_save(void){ PrintAndLogEx(NORMAL, " o : offset in memory"); PrintAndLogEx(NORMAL, " l : length"); PrintAndLogEx(NORMAL, " f : file name"); - PrintAndLogEx(NORMAL, " + : fast read mode"); PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "Examples:"); PrintAndLogEx(NORMAL, " mem save f myfile"); // download whole flashmem to file myfile @@ -81,14 +103,9 @@ int CmdFlashMemRead(const char *Cmd) { uint8_t cmdp = 0; bool errors = false; uint32_t start_index = 0, len = 0; - uint8_t fast = 0; while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { switch (tolower(param_getchar(Cmd, cmdp))) { - case 'f': - fast = 1; - cmdp += 1; - break; case 'o': start_index = param_get32ex(Cmd, cmdp+1, 0, 10); cmdp += 2; @@ -114,11 +131,24 @@ int CmdFlashMemRead(const char *Cmd) { return 1; } - UsbCommand c = {CMD_READ_FLASH_MEM, {start_index, len, fast}}; + UsbCommand c = {CMD_FLASHMEM_READ, {start_index, len, 0}}; clearCommandBuffer(); SendCommand(&c); return 0; } + +int CmdFlashmemSpiBaudrate(const char *Cmd) { + + char ctmp = param_getchar(Cmd, 0); + if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_flashmem_spibaud(); + uint32_t baudrate = param_get32ex(Cmd, 0, 0, 10); + baudrate = baudrate*1000000; + if (baudrate != FLASH_BAUD && baudrate != FLASH_MINBAUD ) return usage_flashmem_spibaud(); + UsbCommand c = {CMD_FLASHMEM_SET_SPIBAUDRATE, {baudrate, 0, 0}}; + SendCommand(&c); + return 0; +} + int CmdFlashMemLoad(const char *Cmd){ FILE *f; @@ -196,7 +226,7 @@ int CmdFlashMemLoad(const char *Cmd){ while (bytes_remaining > 0){ uint32_t bytes_in_packet = MIN(FLASH_MEM_BLOCK_SIZE, bytes_remaining); - UsbCommand c = {CMD_WRITE_FLASH_MEM, {start_index + bytes_sent, bytes_in_packet, 0}}; + UsbCommand c = {CMD_FLASHMEM_WRITE, {start_index + bytes_sent, bytes_in_packet, 0}}; memcpy(c.d.asBytes, dump + bytes_sent, bytes_in_packet); clearCommandBuffer(); @@ -228,7 +258,6 @@ int CmdFlashMemSave(const char *Cmd){ uint8_t cmdp = 0; bool errors = false; uint32_t start_index = 0, len = FLASH_MEM_MAX_SIZE; - uint8_t fast = 0; while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { switch (tolower(param_getchar(Cmd, cmdp))) { @@ -241,10 +270,6 @@ int CmdFlashMemSave(const char *Cmd){ start_index = param_get32ex(Cmd, cmdp+1, 0, 10); cmdp += 2; break; - case '+': - fast = 1; - cmdp += 1; - break; case 'f': //File handling if ( param_getstr(Cmd, cmdp+1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE ) { @@ -314,7 +339,7 @@ int CmdFlashMemWipe(const char *Cmd){ //Validations if (errors || cmdp == 0 ) return usage_flashmem_wipe(); - UsbCommand c = {CMD_WIPE_FLASH_MEM, {page, initalwipe, 0}}; + UsbCommand c = {CMD_FLASHMEM_WIPE, {page, initalwipe, 0}}; clearCommandBuffer(); SendCommand(&c); UsbCommand resp; @@ -359,7 +384,7 @@ int CmdFlashMemInfo(const char *Cmd){ //Validations if (errors ) return usage_flashmem_info(); - UsbCommand c = {CMD_INFO_FLASH_MEM, {0, 0, 0}}; + UsbCommand c = {CMD_FLASHMEM_INFO, {0, 0, 0}}; clearCommandBuffer(); SendCommand(&c); UsbCommand resp; @@ -488,7 +513,7 @@ int CmdFlashMemInfo(const char *Cmd){ if (shall_write) { // save to mem - c = (UsbCommand){CMD_WRITE_FLASH_MEM, {FLASH_MEM_SIGNATURE_OFFSET, FLASH_MEM_SIGNATURE_LEN, 0}}; + c = (UsbCommand){CMD_FLASHMEM_WRITE, {FLASH_MEM_SIGNATURE_OFFSET, FLASH_MEM_SIGNATURE_LEN, 0}}; memcpy(c.d.asBytes, sign, sizeof(sign)); clearCommandBuffer(); SendCommand(&c); @@ -520,6 +545,7 @@ int CmdFlashMemInfo(const char *Cmd){ static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help"}, + {"spibaud", CmdFlashmemSpiBaudrate, 1, "Set Flash memory Spi baudrate [rdv40]"}, {"read", CmdFlashMemRead, 1, "Read Flash memory [rdv40]"}, {"info", CmdFlashMemInfo, 1, "Flash memory information [rdv40]"}, {"load", CmdFlashMemLoad, 1, "Load data into flash memory [rdv40]"}, diff --git a/client/cmdmain.c b/client/cmdmain.c index 447e83ac8..351152179 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -298,9 +298,9 @@ bool GetFromDevice(DeviceMemType_t memtype, uint8_t *dest, uint32_t bytes, uint3 return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_EML_BIGBUF); } case FLASH_MEM: { - UsbCommand c = {CMD_DOWNLOAND_FLASH_MEM, {start_index, bytes, 0}}; + UsbCommand c = {CMD_FLASHMEM_DOWNLOAD, {start_index, bytes, 0}}; SendCommand(&c); - return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_FLASHMEM); + return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_FLASHMEM_DOWNLOADED); } case SIM_MEM: { //UsbCommand c = {CMD_DOWNLOAND_SIM_MEM, {start_index, bytes, 0}}; diff --git a/client/hid-flasher/usb_cmd.h b/client/hid-flasher/usb_cmd.h index 736b11a06..2ced2cd18 100644 --- a/client/hid-flasher/usb_cmd.h +++ b/client/hid-flasher/usb_cmd.h @@ -57,12 +57,12 @@ typedef struct { #define CMD_DOWNLOADED_EML_BIGBUF 0x0111 // RDV40, Flash memory operations -#define CMD_READ_FLASH_MEM 0x0120 -#define CMD_WRITE_FLASH_MEM 0x0121 -#define CMD_WIPE_FLASH_MEM 0x0122 -#define CMD_DOWNLOAND_FLASH_MEM 0x0123 -#define CMD_DOWNLOADED_FLASHMEM 0x0124 -#define CMD_INFO_FLASH_MEM 0x0125 +#define CMD_FLASHMEM_READ 0x0120 +#define CMD_FLASHMEM_WRITE 0x0121 +#define CMD_FLASHMEM_WIPE 0x0122 +#define CMD_FLASHMEM_DOWNLOAD 0x0123 +#define CMD_FLASHMEM_DOWNLOADED 0x0124 +#define CMD_FLASHMEM_INFO 0x0125 // For low-frequency tags #define CMD_READ_TI_TYPE 0x0202 diff --git a/include/common.h b/include/common.h index 8fdb45ab0..8a3f33abd 100644 --- a/include/common.h +++ b/include/common.h @@ -30,6 +30,9 @@ typedef unsigned char byte_t; #define MF_DBG_EXTENDED 4 extern int MF_DBGLEVEL; +// Flashmem spi baudrate +extern uint32_t FLASHMEM_SPIBAUDRATE; + // reader voltage field detector #define MF_MINFIELDV 4000 diff --git a/include/usb_cmd.h b/include/usb_cmd.h index 4de39eef4..51df5896b 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -67,13 +67,17 @@ typedef struct{ #define CMD_DOWNLOAD_EML_BIGBUF 0x0110 #define CMD_DOWNLOADED_EML_BIGBUF 0x0111 + + + // RDV40, Flash memory operations -#define CMD_READ_FLASH_MEM 0x0120 -#define CMD_WRITE_FLASH_MEM 0x0121 -#define CMD_WIPE_FLASH_MEM 0x0122 -#define CMD_DOWNLOAND_FLASH_MEM 0x0123 -#define CMD_DOWNLOADED_FLASHMEM 0x0124 -#define CMD_INFO_FLASH_MEM 0x0125 +#define CMD_FLASHMEM_READ 0x0120 +#define CMD_FLASHMEM_WRITE 0x0121 +#define CMD_FLASHMEM_WIPE 0x0122 +#define CMD_FLASHMEM_DOWNLOAD 0x0123 +#define CMD_FLASHMEM_DOWNLOADED 0x0124 +#define CMD_FLASHMEM_INFO 0x0125 +#define CMD_FLASHMEM_SET_SPIBAUDRATE 0x0126 // RDV40, Smart card operations #define CMD_SMART_RAW 0x0140 diff --git a/tools/mkversion.pl b/tools/mkversion.pl index 6bbc1dfec..1acca6efa 100644 --- a/tools/mkversion.pl +++ b/tools/mkversion.pl @@ -18,13 +18,23 @@ my $fullgitinfo = 'iceman'; my $ctime; # GIT status 0 = dirty, 1 = clean , 2 = undecided my $clean = 2; + # Do we have acces to git command? -my $commandGIT = `bash which git`; +####### +# solves some bug on macos i.e: +## +# perl ../tools/mkversion.pl .. > version.c || cp ../common/default_version.c version.c +# /usr/bin/which: /usr/bin/which: cannot execute binary file +# fatal: No names found, cannot describe anything. +## +# anyway forcing any kind of shell is at least useless, at worst fatal. +my $commandGIT = "env -S which git"; if ( defined($commandGIT) ) { my $githistory = `git fetch --all`; - my $gitversion = `git describe --dirty`; + # now avoiding the "fatal: No names found, cannot describe anything." error by fallbacking to abbrev hash in such case + my $gitversion = `git describe --dirty --always`; my $gitbranch = `git rev-parse --abbrev-ref HEAD`; $clean = $gitversion =~ '-dirty' ? 0 : 1;