mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-21 22:03:42 -07:00
chg: 'sc upgrade' - now uses fileutils
This commit is contained in:
parent
843ab094bb
commit
f6566f89f2
1 changed files with 34 additions and 72 deletions
|
@ -522,7 +522,6 @@ static int CmdSmartUpgrade(const char *Cmd) {
|
||||||
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;
|
|
||||||
char filename[FILE_PATH_SIZE] = {0};
|
char filename[FILE_PATH_SIZE] = {0};
|
||||||
uint8_t cmdp = 0;
|
uint8_t cmdp = 0;
|
||||||
bool errors = false;
|
bool errors = false;
|
||||||
|
@ -530,7 +529,6 @@ static int CmdSmartUpgrade(const char *Cmd) {
|
||||||
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 'f':
|
case 'f':
|
||||||
//File handling and reading
|
|
||||||
if (param_getstr(Cmd, cmdp + 1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE) {
|
if (param_getstr(Cmd, cmdp + 1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE) {
|
||||||
PrintAndLogEx(FAILED, "Filename too long");
|
PrintAndLogEx(FAILED, "Filename too long");
|
||||||
errors = true;
|
errors = true;
|
||||||
|
@ -550,103 +548,68 @@ static int CmdSmartUpgrade(const char *Cmd) {
|
||||||
//Validations
|
//Validations
|
||||||
if (errors || cmdp == 0) return usage_sm_upgrade();
|
if (errors || cmdp == 0) return usage_sm_upgrade();
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generate filename for the related SHA512 hash file
|
||||||
|
char sha512filename[FILE_PATH_SIZE] = {'\0'};
|
||||||
if (!strcmp(bin_extension, "BIN") || !strcmp(bin_extension, "bin")) {
|
if (!strcmp(bin_extension, "BIN") || !strcmp(bin_extension, "bin")) {
|
||||||
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 PM3_ESOFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
size_t firmware_size = 0;
|
||||||
if (!f) {
|
uint8_t *firmware = NULL;
|
||||||
|
if (loadFile_safe(filename, "", (void**)&firmware, &firmware_size) != PM3_SUCCESS) {
|
||||||
PrintAndLogEx(FAILED, "Firmware file " _YELLOW_("%s") " not found or locked.", filename);
|
PrintAndLogEx(FAILED, "Firmware file " _YELLOW_("%s") " not found or locked.", filename);
|
||||||
return PM3_EFILE;
|
return PM3_EFILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get filesize in order to malloc memory
|
|
||||||
fseek(f, 0, SEEK_END);
|
|
||||||
long fsize = ftell(f);
|
|
||||||
fseek(f, 0, SEEK_SET);
|
|
||||||
|
|
||||||
if (fsize <= 0) {
|
|
||||||
PrintAndLogEx(ERR, "error, when getting filesize");
|
|
||||||
fclose(f);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *dump = calloc(fsize, sizeof(uint8_t));
|
|
||||||
if (!dump) {
|
|
||||||
PrintAndLogEx(ERR, "error, cannot allocate memory ");
|
|
||||||
fclose(f);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t firmware_size = fread(dump, 1, fsize, f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
// load sha512 file
|
// load sha512 file
|
||||||
f = fopen(sha512filename, "rb");
|
size_t sha512_size = 0;
|
||||||
if (!f) {
|
char *hashstring = NULL;
|
||||||
|
if (loadFile_safe(sha512filename, "", (void**)&hashstring, &sha512_size) != PM3_SUCCESS) {
|
||||||
PrintAndLogEx(FAILED, "SHA-512 file not found or locked.");
|
PrintAndLogEx(FAILED, "SHA-512 file not found or locked.");
|
||||||
free(dump);
|
free(firmware);
|
||||||
return PM3_EFILE;
|
return PM3_EFILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get filesize in order to malloc memory
|
if (sha512_size < 128) {
|
||||||
fseek(f, 0, SEEK_END);
|
PrintAndLogEx(FAILED, "SHA-512 file wrong size");
|
||||||
fsize = ftell(f);
|
free(firmware);
|
||||||
fseek(f, 0, SEEK_SET);
|
return PM3_ESOFT;
|
||||||
|
|
||||||
if (fsize < 0) {
|
|
||||||
PrintAndLogEx(FAILED, "Could not determine size of SHA-512 file");
|
|
||||||
fclose(f);
|
|
||||||
free(dump);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fsize < 128) {
|
|
||||||
PrintAndLogEx(FAILED, "SHA-512 file too short");
|
|
||||||
fclose(f);
|
|
||||||
free(dump);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char hashstring[129];
|
|
||||||
size_t bytes_read = fread(hashstring, 1, 128, f);
|
|
||||||
hashstring[128] = '\0';
|
hashstring[128] = '\0';
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
uint8_t hash_1[64];
|
uint8_t hash_1[64];
|
||||||
if (bytes_read != 128 || param_gethex(hashstring, 0, hash_1, 128)) {
|
if (param_gethex(hashstring, 0, hash_1, 128)) {
|
||||||
PrintAndLogEx(FAILED, "Couldn't read SHA-512 file");
|
PrintAndLogEx(FAILED, "Couldn't read SHA-512 file");
|
||||||
free(dump);
|
free(firmware);
|
||||||
return 1;
|
return PM3_ESOFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t hash_2[64];
|
uint8_t hash_2[64];
|
||||||
if (sha512hash(dump, firmware_size, hash_2)) {
|
if (sha512hash(firmware, firmware_size, hash_2)) {
|
||||||
PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of firmware");
|
PrintAndLogEx(FAILED, "Couldn't calculate SHA-512 of firmware");
|
||||||
free(dump);
|
free(firmware);
|
||||||
return 1;
|
return PM3_ESOFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (memcmp(hash_1, hash_2, 64)) {
|
if (memcmp(hash_1, hash_2, 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)"));
|
||||||
free(dump);
|
free(firmware);
|
||||||
return 1;
|
return PM3_ESOFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3");
|
PrintAndLogEx(SUCCESS, "Sim module firmware uploading to PM3");
|
||||||
|
@ -666,20 +629,19 @@ static int CmdSmartUpgrade(const char *Cmd) {
|
||||||
conn.block_after_ACK = false;
|
conn.block_after_ACK = false;
|
||||||
}
|
}
|
||||||
clearCommandBuffer();
|
clearCommandBuffer();
|
||||||
SendCommandOLD(CMD_SMART_UPLOAD, index + bytes_sent, bytes_in_packet, 0, dump + bytes_sent, bytes_in_packet);
|
SendCommandOLD(CMD_SMART_UPLOAD, index + bytes_sent, bytes_in_packet, 0, firmware + bytes_sent, bytes_in_packet);
|
||||||
if (!WaitForResponseTimeout(CMD_ACK, NULL, 2000)) {
|
if (!WaitForResponseTimeout(CMD_ACK, NULL, 2000)) {
|
||||||
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
|
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
|
||||||
free(dump);
|
free(firmware);
|
||||||
return 1;
|
return PM3_ETIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes_remaining -= bytes_in_packet;
|
bytes_remaining -= bytes_in_packet;
|
||||||
bytes_sent += bytes_in_packet;
|
bytes_sent += bytes_in_packet;
|
||||||
printf(".");
|
PrintAndLogEx(INPLACE, "%d bytes sent", bytes_sent);
|
||||||
fflush(stdout);
|
|
||||||
}
|
}
|
||||||
free(dump);
|
free(firmware);
|
||||||
printf("\n");
|
PrintAndLogEx(NORMAL, "");
|
||||||
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
|
||||||
|
@ -688,11 +650,11 @@ static int CmdSmartUpgrade(const char *Cmd) {
|
||||||
PacketResponseNG resp;
|
PacketResponseNG resp;
|
||||||
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) {
|
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) {
|
||||||
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
|
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
|
||||||
return 1;
|
return PM3_ETIMEOUT;
|
||||||
}
|
}
|
||||||
if ((resp.oldarg[0] & 0xFF)) {
|
if ((resp.oldarg[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(HINT, "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"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue