revert to precision clock

This commit is contained in:
iceman1001 2020-10-20 17:34:42 +02:00
commit 2e27a493e8
3 changed files with 27 additions and 28 deletions

View file

@ -1613,7 +1613,11 @@ static void PacketReceived(PacketCommandNG *packet) {
break; break;
} }
case CMD_SMART_SETCLOCK: { case CMD_SMART_SETCLOCK: {
SmartCardSetClock(packet->oldarg[0]); struct p {
uint32_t new_clk;
} PACKED;
struct p *payload = (struct p *)packet->data.asBytes;
SmartCardSetClock(payload->new_clk);
break; break;
} }
case CMD_SMART_RAW: { case CMD_SMART_RAW: {

View file

@ -47,12 +47,6 @@ static void __attribute__((optimize("O0"))) I2CSpinDelayClk(uint16_t delay) {
#define I2C_DELAY_2CLK I2CSpinDelayClk(2) #define I2C_DELAY_2CLK I2CSpinDelayClk(2)
#define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x)) #define I2C_DELAY_XCLK(x) I2CSpinDelayClk((x))
#define I2C_DELAY_100us I2CSpinDelayClk( 100 / 3)
#define I2C_DELAY_600us I2CSpinDelayClk( 600 / 3)
#define I2C_DELAY_10ms I2CSpinDelayClk( 10 * 1000 / 3 )
#define I2C_DELAY_30ms I2CSpinDelayClk( 30 * 1000 / 3 )
#define I2C_DELAY_100ms I2CSpinDelayClk( 100 * 1000 / 3)
#define ISO7618_MAX_FRAME 255 #define ISO7618_MAX_FRAME 255
// try i2c bus recovery at 100kHz = 5us high, 5us low // try i2c bus recovery at 100kHz = 5us high, 5us low
@ -134,11 +128,11 @@ void I2C_Reset_EnterMainProgram(void) {
StartTicks(); StartTicks();
I2C_init(); I2C_init();
I2C_SetResetStatus(0, 0, 0); I2C_SetResetStatus(0, 0, 0);
I2C_DELAY_30ms; WaitMS(30);
I2C_SetResetStatus(1, 0, 0); I2C_SetResetStatus(1, 0, 0);
I2C_DELAY_30ms; WaitMS(30);
I2C_SetResetStatus(1, 1, 1); I2C_SetResetStatus(1, 1, 1);
I2C_DELAY_10ms; WaitMS(10);
} }
// Reset the SIM_Adapter, then enter the bootloader program // Reset the SIM_Adapter, then enter the bootloader program
@ -147,9 +141,9 @@ void I2C_Reset_EnterBootloader(void) {
StartTicks(); StartTicks();
I2C_init(); I2C_init();
I2C_SetResetStatus(0, 1, 1); I2C_SetResetStatus(0, 1, 1);
I2C_DELAY_100ms; WaitMS(100);
I2C_SetResetStatus(1, 1, 1); I2C_SetResetStatus(1, 1, 1);
I2C_DELAY_10ms; WaitMS(10);
} }
// Wait for the clock to go High. // Wait for the clock to go High.
@ -193,7 +187,7 @@ static bool WaitSCL_L_timeout(void) {
if (!SCL_read) if (!SCL_read)
return true; return true;
I2C_DELAY_100us; WaitMS(1);
} }
return (delay == 0); return (delay == 0);
} }
@ -440,8 +434,7 @@ int16_t I2C_BufferRead(uint8_t *data, uint8_t len, uint8_t device_cmd, uint8_t d
// extra wait 500us (514us measured) // extra wait 500us (514us measured)
// 200us (xx measured) // 200us (xx measured)
// WaitUS(600); WaitUS(600);
I2C_DELAY_600us;
bool bBreak = true; bool bBreak = true;
uint16_t readcount = 0; uint16_t readcount = 0;
@ -811,8 +804,7 @@ void SmartCardUpgrade(uint64_t arg0) {
} }
// writing takes time. // writing takes time.
// WaitMS(50); WaitMS(100);
I2C_DELAY_100ms;
// read // read
res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT); res = I2C_ReadFW(verfiydata, size, msb, lsb, I2C_DEVICE_ADDRESS_BOOT);
@ -844,12 +836,10 @@ void SmartCardSetClock(uint64_t arg0) {
LED_D_ON(); LED_D_ON();
set_tracing(true); set_tracing(true);
I2C_Reset_EnterMainProgram(); I2C_Reset_EnterMainProgram();
// Send SIM CLC // Send SIM CLC
// start [C0 05 xx] stop // start [C0 05 xx] stop
I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN); I2C_WriteByte(arg0, I2C_DEVICE_CMD_SIM_CLC, I2C_DEVICE_ADDRESS_MAIN);
reply_ng(CMD_SMART_SETCLOCK, PM3_SUCCESS, NULL, 0);
reply_mix(CMD_ACK, 1, 0, 0, 0, 0);
set_tracing(false); set_tracing(false);
LEDsoff(); LEDsoff();
} }

View file

@ -791,14 +791,14 @@ static int CmdSmartReader(const char *Cmd) {
static int CmdSmartSetClock(const char *Cmd) { static int CmdSmartSetClock(const char *Cmd) {
uint8_t cmdp = 0; uint8_t cmdp = 0;
bool errors = false; bool errors = false;
uint8_t clock1 = 0; uint8_t new_clk = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) { while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) { switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h': case 'h':
return usage_sm_setclock(); return usage_sm_setclock();
case 'c': case 'c':
clock1 = param_get8ex(Cmd, cmdp + 1, 2, 10); new_clk = param_get8ex(Cmd, cmdp + 1, 2, 10);
if (clock1 > 2) if (new_clk > 2)
errors = true; errors = true;
cmdp += 2; cmdp += 2;
@ -813,21 +813,26 @@ static int CmdSmartSetClock(const char *Cmd) {
//Validations //Validations
if (errors || cmdp == 0) return usage_sm_setclock(); if (errors || cmdp == 0) return usage_sm_setclock();
struct {
uint32_t new_clk;
} PACKED payload;
payload.new_clk = new_clk;
clearCommandBuffer(); clearCommandBuffer();
SendCommandMIX(CMD_SMART_SETCLOCK, clock1, 0, 0, NULL, 0); SendCommandNG(CMD_SMART_SETCLOCK, (uint8_t*)&payload, sizeof(payload));
PacketResponseNG resp; PacketResponseNG resp;
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) { if (!WaitForResponseTimeout(CMD_SMART_SETCLOCK, &resp, 2500)) {
PrintAndLogEx(WARNING, "smart card select failed"); PrintAndLogEx(WARNING, "smart card select failed");
return PM3_ETIMEOUT; return PM3_ETIMEOUT;
} }
uint8_t isok = resp.oldarg[0] & 0xFF; if (resp.status != PM3_SUCCESS) {
if (!isok) {
PrintAndLogEx(WARNING, "smart card set clock failed"); PrintAndLogEx(WARNING, "smart card set clock failed");
return PM3_ESOFT; return PM3_ESOFT;
} }
switch (clock1) { switch (new_clk) {
case 0: case 0:
PrintAndLogEx(SUCCESS, "Clock changed to 16MHz giving 10800 baudrate"); PrintAndLogEx(SUCCESS, "Clock changed to 16MHz giving 10800 baudrate");
break; break;