make style

This commit is contained in:
Philippe Teuwen 2019-03-15 09:18:07 +01:00
commit 2d2b128754
2 changed files with 107 additions and 107 deletions

View file

@ -196,8 +196,8 @@ static int PrintATR(uint8_t *atr, size_t atrlen) {
uint8_t T0 = atr[1]; uint8_t T0 = atr[1];
uint8_t K = T0 & 0x0F; uint8_t K = T0 & 0x0F;
uint8_t TD1 = 0, T1len = 0, TD1len = 0, TDilen = 0; uint8_t TD1 = 0, T1len = 0, TD1len = 0, TDilen = 0;
bool protocol_T0_present = true; bool protocol_T0_present = true;
bool protocol_T15_present = false; bool protocol_T15_present = false;
if (T0 & 0x10) { if (T0 & 0x10) {
PrintAndLog("\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr[2 + T1len]); PrintAndLog("\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr[2 + T1len]);
@ -217,13 +217,13 @@ static int PrintATR(uint8_t *atr, size_t atrlen) {
if (T0 & 0x80) { if (T0 & 0x80) {
TD1 = atr[2 + T1len]; TD1 = atr[2 + T1len];
PrintAndLog("\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1, TD1 & 0x0f); PrintAndLog("\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1, TD1 & 0x0f);
protocol_T0_present = false; protocol_T0_present = false;
if ((TD1 & 0x0f) == 0) { if ((TD1 & 0x0f) == 0) {
protocol_T0_present = true; protocol_T0_present = true;
} }
if ((TD1 & 0x0f) == 15) { if ((TD1 & 0x0f) == 15) {
protocol_T15_present = true; protocol_T15_present = true;
} }
T1len++; T1len++;
@ -242,12 +242,12 @@ static int PrintATR(uint8_t *atr, size_t atrlen) {
if (TD1 & 0x80) { if (TD1 & 0x80) {
uint8_t TDi = atr[2 + T1len + TD1len]; uint8_t TDi = atr[2 + T1len + TD1len];
PrintAndLog("\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi, TDi & 0x0f); PrintAndLog("\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi, TDi & 0x0f);
if ((TDi & 0x0f) == 0) { if ((TDi & 0x0f) == 0) {
protocol_T0_present = true; protocol_T0_present = true;
} }
if ((TDi & 0x0f) == 15) { if ((TDi & 0x0f) == 15) {
protocol_T15_present = true; protocol_T15_present = true;
} }
TD1len++; TD1len++;
bool nextCycle = true; bool nextCycle = true;
@ -278,16 +278,16 @@ static int PrintATR(uint8_t *atr, size_t atrlen) {
} }
} }
if (!protocol_T0_present || protocol_T15_present) { // there is CRC Check Byte TCK if (!protocol_T0_present || protocol_T15_present) { // there is CRC Check Byte TCK
uint8_t vxor = 0; uint8_t vxor = 0;
for (int i = 1; i < atrlen; i++) for (int i = 1; i < atrlen; i++)
vxor ^= atr[i]; vxor ^= atr[i];
if (vxor) if (vxor)
PrintAndLogEx(WARNING, "Check sum error. Must be 0 got 0x%02X", vxor); PrintAndLogEx(WARNING, "Check sum error. Must be 0 got 0x%02X", vxor);
else else
PrintAndLogEx(INFO, "Check sum OK."); PrintAndLogEx(INFO, "Check sum OK.");
} }
if (atr[0] != 0x3b) if (atr[0] != 0x3b)
PrintAndLogEx(WARNING, "Not a direct convention [ 0x%02x ]", atr[0]); PrintAndLogEx(WARNING, "Not a direct convention [ 0x%02x ]", atr[0]);
@ -580,7 +580,7 @@ int CmdSmartUpgrade(const char *Cmd) {
PrintAndLogEx(WARNING, "WARNING - Sim module firmware upgrade."); PrintAndLogEx(WARNING, "WARNING - Sim module firmware upgrade.");
PrintAndLogEx(WARNING, "A dangerous command, do wrong and you could brick the sim module"); PrintAndLogEx(WARNING, "A dangerous command, do wrong and you could brick the sim module");
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
FILE *f; FILE *f;
char filename[FILE_PATH_SIZE] = {0}; char filename[FILE_PATH_SIZE] = {0};
@ -611,30 +611,30 @@ int CmdSmartUpgrade(const char *Cmd) {
if (errors || cmdp == 0) return usage_sm_upgrade(); if (errors || cmdp == 0) return usage_sm_upgrade();
char sha512filename[FILE_PATH_SIZE] = {'\0'}; char sha512filename[FILE_PATH_SIZE] = {'\0'};
char *bin_extension = filename; char *bin_extension = filename;
char *dot_position = NULL; char *dot_position = NULL;
while ((dot_position = strchr(bin_extension, '.')) != NULL) { while ((dot_position = strchr(bin_extension, '.')) != NULL) {
bin_extension = dot_position + 1; bin_extension = dot_position + 1;
} }
if (!strcmp(bin_extension, "BIN") if (!strcmp(bin_extension, "BIN")
#ifdef _WIN32 #ifdef _WIN32
|| !strcmp(bin_extension, "bin") || !strcmp(bin_extension, "bin")
#endif #endif
) { ) {
memcpy(sha512filename, filename, strlen(filename) - strlen("bin")); memcpy(sha512filename, filename, strlen(filename) - strlen("bin"));
strcat(sha512filename, "sha512.txt"); strcat(sha512filename, "sha512.txt");
} else { } else {
PrintAndLogEx(FAILED, "Filename extension of firmware upgrade file must be .BIN"); PrintAndLogEx(FAILED, "Filename extension of firmware upgrade file must be .BIN");
return 1; return 1;
} }
PrintAndLogEx(INFO, "firmware file : " _YELLOW_("%s"), filename); PrintAndLogEx(INFO, "firmware file : " _YELLOW_("%s"), filename);
PrintAndLogEx(INFO, "Checking integrity : " _YELLOW_("%s"), sha512filename); PrintAndLogEx(INFO, "Checking integrity : " _YELLOW_("%s"), sha512filename);
// load firmware file // load firmware file
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
PrintAndLogEx(FAILED, "Firmware file " _YELLOW_("%s") " not found or locked.", filename); PrintAndLogEx(FAILED, "Firmware file " _YELLOW_("%s") " not found or locked.", filename);
return 1; return 1;
@ -658,64 +658,64 @@ int CmdSmartUpgrade(const char *Cmd) {
return 1; return 1;
} }
size_t firmware_size = fread(dump, 1, fsize, f); size_t firmware_size = fread(dump, 1, fsize, f);
if (f) if (f)
fclose(f); fclose(f);
// load sha512 file // load sha512 file
f = fopen(sha512filename, "rb"); f = fopen(sha512filename, "rb");
if ( !f ){ if (!f) {
PrintAndLogEx(FAILED, "SHA-512 file not found or locked."); PrintAndLogEx(FAILED, "SHA-512 file not found or locked.");
return 1; return 1;
} }
// get filesize in order to malloc memory // get filesize in order to malloc memory
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
fsize = ftell(f); fsize = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
if (fsize < 0) { if (fsize < 0) {
PrintAndLogEx(FAILED, "Could not determine size of SHA-512 file"); PrintAndLogEx(FAILED, "Could not determine size of SHA-512 file");
fclose(f); fclose(f);
return 1; return 1;
} }
if (fsize < 128) { if (fsize < 128) {
PrintAndLogEx(FAILED, "SHA-512 file too short"); PrintAndLogEx(FAILED, "SHA-512 file too short");
fclose(f); fclose(f);
return 1; return 1;
} }
char hashstring[129]; char hashstring[129];
size_t bytes_read = fread(hashstring, 1, 128, f); size_t bytes_read = fread(hashstring, 1, 128, f);
hashstring[128] = '\0'; hashstring[128] = '\0';
if (f) if (f)
fclose(f); fclose(f);
uint8_t hash1[64]; uint8_t hash1[64];
if (bytes_read != 128 || param_gethex(hashstring, 0, hash1, 128)) { if (bytes_read != 128 || param_gethex(hashstring, 0, hash1, 128)) {
PrintAndLogEx(FAILED, "Couldn't read SHA-512 file"); PrintAndLogEx(FAILED, "Couldn't read SHA-512 file");
return 1; return 1;
} }
uint8_t hash2[64]; uint8_t hash2[64];
if (sha512hash(dump, firmware_size, hash2)) { if (sha512hash(dump, firmware_size, hash2)) {
PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of firmware"); PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of firmware");
return 1; return 1;
} }
if (memcmp(hash1, hash2, 64)) { if (memcmp(hash1, hash2, 64)) {
PrintAndLogEx(FAILED, "Couldn't verify integrity of firmware file " _RED_("(wrong SHA-512 hash)") ); PrintAndLogEx(FAILED, "Couldn't verify integrity of firmware file " _RED_("(wrong SHA-512 hash)"));
return 1; return 1;
} }
PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3"); PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3");
//Send to device //Send to device
uint32_t index = 0; uint32_t index = 0;
uint32_t bytes_sent = 0; uint32_t bytes_sent = 0;
uint32_t bytes_remaining = firmware_size; uint32_t bytes_remaining = firmware_size;
while (bytes_remaining > 0) { while (bytes_remaining > 0) {
uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining); uint32_t bytes_in_packet = MIN(USB_CMD_DATA_SIZE, bytes_remaining);
@ -742,7 +742,7 @@ int CmdSmartUpgrade(const char *Cmd) {
PrintAndLogEx(SUCCESS, "Sim module firmware updating, don\'t turn off your PM3!"); PrintAndLogEx(SUCCESS, "Sim module firmware updating, don\'t turn off your PM3!");
// trigger the firmware upgrade // trigger the firmware upgrade
UsbCommand c = {CMD_SMART_UPGRADE, {firmware_size, 0, 0}}; UsbCommand c = {CMD_SMART_UPGRADE, {firmware_size, 0, 0}};
clearCommandBuffer(); clearCommandBuffer();
SendCommand(&c); SendCommand(&c);
UsbCommand resp; UsbCommand resp;
@ -751,10 +751,10 @@ int CmdSmartUpgrade(const char *Cmd) {
return 1; return 1;
} }
if ((resp.arg[0] & 0xFF)) { if ((resp.arg[0] & 0xFF)) {
PrintAndLogEx(SUCCESS, "Sim module firmware upgrade " _GREEN_("successful") ); PrintAndLogEx(SUCCESS, "Sim module firmware upgrade " _GREEN_("successful"));
PrintAndLogEx(SUCCESS, "\n run " _YELLOW_("`hw status`") " to validate the fw version " ); PrintAndLogEx(SUCCESS, "\n run " _YELLOW_("`hw status`") " to validate the fw version ");
} else { } else {
PrintAndLogEx(FAILED, "Sim module firmware upgrade " _RED_("failed") ); PrintAndLogEx(FAILED, "Sim module firmware upgrade " _RED_("failed"));
} }
return 0; return 0;
} }

View file

@ -104,19 +104,19 @@ int sha256hash(uint8_t *input, int length, uint8_t *hash) {
mbedtls_sha256_finish(&sctx, hash); mbedtls_sha256_finish(&sctx, hash);
mbedtls_sha256_free(&sctx); mbedtls_sha256_free(&sctx);
return 0; return 0;
} }
int sha512hash(uint8_t *input, int length, uint8_t *hash) { int sha512hash(uint8_t *input, int length, uint8_t *hash) {
if (!hash || !input) if (!hash || !input)
return 1; return 1;
mbedtls_sha512_context sctx; mbedtls_sha512_context sctx;
mbedtls_sha512_init(&sctx); mbedtls_sha512_init(&sctx);
mbedtls_sha512_starts(&sctx, 0); //SHA-512, not 384 mbedtls_sha512_starts(&sctx, 0); //SHA-512, not 384
mbedtls_sha512_update(&sctx, input, length); mbedtls_sha512_update(&sctx, input, length);
mbedtls_sha512_finish(&sctx, hash); mbedtls_sha512_finish(&sctx, hash);
mbedtls_sha512_free(&sctx); mbedtls_sha512_free(&sctx);
return 0; return 0;
} }