From 2102b19d11d82ba04417d19167af580092606d4b Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 9 Jul 2018 11:51:19 +0200 Subject: [PATCH] add: 'sc setclock' 0,1,2, allowed setting sim clocks.. --- client/cmdsmartcard.c | 64 +++++++++++++++++++++++++++++++++++++++++++ common/i2c.c | 34 +++++++++-------------- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/client/cmdsmartcard.c b/client/cmdsmartcard.c index ab83c2af4..454a9732e 100644 --- a/client/cmdsmartcard.c +++ b/client/cmdsmartcard.c @@ -51,6 +51,15 @@ int usage_sm_upgrade(void) { PrintAndLogEx(NORMAL, " sc upgrade f myfile"); return 0; } +int usage_sm_setclock(void) { + PrintAndLogEx(NORMAL, "Usage: sc setclock [h] c "); + PrintAndLogEx(NORMAL, " h : this help"); + PrintAndLogEx(NORMAL, " c <> : clockspeed (0 = 16mhz, 1=8mhz, 2=4mhz) "); + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(NORMAL, "Examples:"); + PrintAndLogEx(NORMAL, " sc setclock c 2"); + return 0; +} int CmdSmartRaw(const char *Cmd) { @@ -340,6 +349,60 @@ int CmdSmartReader(const char *Cmd){ return 0; } +int CmdSmartSetClock(const char *Cmd){ + uint8_t cmdp = 0; + bool errors = false; + uint8_t clock = 0; + while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { + switch (tolower(param_getchar(Cmd, cmdp))) { + case 'h': return usage_sm_setclock(); + case 'c': + clock = param_get8ex(Cmd, cmdp+1, 2, 10); + if ( clock > 2) + errors = true; + + cmdp += 2; + break; + default: + PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); + errors = true; + break; + } + } + + //Validations + if (errors || cmdp == 0) return usage_sm_setclock(); + + UsbCommand c = {CMD_SMART_SETCLOCK, {clock, 0, 0}}; + clearCommandBuffer(); + SendCommand(&c); + UsbCommand resp; + if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { + PrintAndLogEx(WARNING, "smart card select failed"); + return 1; + } + + uint8_t isok = resp.arg[0] & 0xFF; + if (!isok) { + PrintAndLogEx(WARNING, "smart card set clock failed"); + return 1; + } + switch (clock) { + case 0: + PrintAndLogEx(SUCCESS, "Clock changed to 16mhz given 10800 baudrate"); + break; + case 1: + PrintAndLogEx(SUCCESS, "Clock changed to 8mhz giving 21600 baudrate"); + break; + case 2: + PrintAndLogEx(SUCCESS, "Clock changed to 4mhz giving 86400 baudrate"); + break; + default: + break; + } + return 0; +} + int CmdSmartList(const char *Cmd) { CmdTraceList("7816"); return 0; @@ -352,6 +415,7 @@ static command_t CommandTable[] = { {"reader", CmdSmartReader, 1, "Act like an IS07816 reader [rdv40]"}, {"raw", CmdSmartRaw, 1, "Send raw hex data to tag [rdv40]"}, {"upgrade", CmdSmartUpgrade, 1, "Upgrade firmware [rdv40]"}, + {"setclock", CmdSmartSetClock, 1, "Set clock speed"}, {NULL, NULL, 0, NULL} }; diff --git a/common/i2c.c b/common/i2c.c index 3546ead21..d51efa932 100644 --- a/common/i2c.c +++ b/common/i2c.c @@ -576,6 +576,8 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) { LED_D_ON(); + uint8_t len = 0; + uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME); smartcard_command_t flags = arg0; if ((flags & SC_CONNECT)) @@ -583,9 +585,6 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) { set_tracing(true); - uint8_t len = 0; - uint8_t *resp = BigBuf_malloc(ISO7618_MAX_FRAME); - if ((flags & SC_CONNECT)) { I2C_Reset_EnterMainProgram(); @@ -593,12 +592,12 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) { if ( !(flags & SC_NO_SELECT) ) { smart_card_atr_t card; bool gotATR = GetATR( &card ); - //cmd_send(CMD_ACK, isOK, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t)); + //cmd_send(CMD_ACK, gotATR, sizeof(smart_card_atr_t), 0, &card, sizeof(smart_card_atr_t)); if ( !gotATR ) goto OUT; } } - + if ((flags & SC_RAW)) { LogTrace(data, arg1, 0, 0, NULL, true); @@ -611,14 +610,11 @@ void SmartCardRaw( uint64_t arg0, uint64_t arg1, uint8_t *data ) { //wait for sim card to answer. if ( !I2C_WaitForSim() ) goto OUT; - - // read response - // start [C0 03 start C1 len aa bb cc stop] - len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); + // read response + len = I2C_BufferRead(resp, ISO7618_MAX_FRAME, I2C_DEVICE_CMD_READ, I2C_DEVICE_ADDRESS_MAIN); LogTrace(resp, len, 0, 0, NULL, false); } - OUT: cmd_send(CMD_ACK, len, 0, 0, resp, len); set_tracing(false); @@ -702,19 +698,15 @@ void SmartCardSetBaud(uint64_t arg0) { } void SmartCardSetClock(uint64_t arg0) { - LED_D_ON(); - clear_trace(); - set_tracing(true); - + set_tracing(true); I2C_Reset_EnterMainProgram(); - - bool isOK = true; - //uint16_t clockrate = arg0; + // Send SIM CLC - // start [C0 04] stop - //I2C_WriteByte(0x00, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN); + // start [C0 05 xx] stop + I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN); - cmd_send(CMD_ACK, isOK, 0, 0, 0, 0); - LED_D_OFF(); + cmd_send(CMD_ACK, 1, 0, 0, 0, 0); + set_tracing(false); + LEDsoff(); } \ No newline at end of file