From da908315b0c4192f27f107711d8b87125064c9bd Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 25 Feb 2022 00:35:30 +0100 Subject: [PATCH] flasher: add option --force rather than interactive prompt --- client/src/flash.c | 29 +++++++++-------------------- client/src/flash.h | 2 +- client/src/proxmark3.c | 16 ++++++++++++---- pm3 | 40 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/client/src/flash.c b/client/src/flash.c index 4231b82a9..b8e5d602b 100644 --- a/client/src/flash.c +++ b/client/src/flash.c @@ -235,17 +235,6 @@ static int check_segs(flash_file_t *ctx, int can_write_bl, uint32_t flash_size) return PM3_SUCCESS; } -static int ask_confirmation(void) { - PrintAndLogEx(INFO, "Make sure to flash a correct and up-to-date version"); - PrintAndLogEx(NORMAL, "Do you want to flash the current image? (yes/no)"); - char answer[10]; - if ((fgets (answer, sizeof(answer), stdin) == NULL) || (strncmp(answer, "yes", 3) != 0)) { - return PM3_EOPABORTED; - } else { - return PM3_SUCCESS; - } -} - static int print_and_validate_version(struct version_information_t *vi) { if (vi->magic != VERSION_INFORMATION_MAGIC) return PM3_EFILE; @@ -255,7 +244,7 @@ static int print_and_validate_version(struct version_information_t *vi) { if (strlen(g_version_information.armsrc) == 9) { if (strncmp(vi->armsrc, g_version_information.armsrc, 9) != 0) { PrintAndLogEx(WARNING, _RED_("ARM firmware does not match the source at the time the client was compiled")); - return ask_confirmation(); + return PM3_EINVARG; } else { return PM3_SUCCESS; } @@ -264,7 +253,7 @@ static int print_and_validate_version(struct version_information_t *vi) { } // Load an ELF file for flashing -int flash_load(flash_file_t *ctx) { +int flash_load(flash_file_t *ctx, bool force) { FILE *fd; Elf32_Ehdr_t *ehdr; Elf32_Shdr_t *shdrs = NULL; @@ -364,13 +353,13 @@ int flash_load(flash_file_t *ctx) { } if (res == PM3_SUCCESS) return res; - if (res == PM3_EOPABORTED) - goto fail; - // We could not find proper version_information, so we ask for confirmation - PrintAndLogEx(WARNING, "Unable to check version_information"); - res = ask_confirmation(); - if (res == PM3_SUCCESS) - return res; + // We could not find proper version_information + if (res == PM3_EUNDEF) + PrintAndLogEx(WARNING, "Unable to check version_information"); + PrintAndLogEx(INFO, "Make sure to flash a correct and up-to-date version"); + PrintAndLogEx(INFO, "You can force flashing this firmware by using the option '--force'"); + if (force) + return PM3_SUCCESS; fail: flash_free(ctx); return res; diff --git a/client/src/flash.h b/client/src/flash.h index b4cac2e2a..5c2535620 100644 --- a/client/src/flash.h +++ b/client/src/flash.h @@ -41,7 +41,7 @@ typedef struct { flash_seg_t *segments; } flash_file_t; -int flash_load(flash_file_t *ctx); +int flash_load(flash_file_t *ctx, bool force); int flash_prepare(flash_file_t *ctx, int can_write_bl, int flash_size); int flash_start_flashing(int enable_bl_writes, char *serial_port_name, uint32_t *max_allowed); int flash_write(flash_file_t *ctx); diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 2a3d3befa..26062f9d7 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -576,7 +576,8 @@ static void show_help(bool showFullHelp, char *exec_name) { PrintAndLogEx(NORMAL, " --incognito do not use history, prefs file nor log files"); PrintAndLogEx(NORMAL, "\nOptions in flasher mode:"); PrintAndLogEx(NORMAL, " --flash flash Proxmark3, requires at least one --image"); - PrintAndLogEx(NORMAL, " --unlock-bootloader Enable flashing of bootloader area *DANGEROUS* (need --flash or --flash-info)"); + PrintAndLogEx(NORMAL, " --unlock-bootloader Enable flashing of bootloader area *DANGEROUS* (need --flash)"); + PrintAndLogEx(NORMAL, " --force Enable flashing even if firmware seems to not match client version"); PrintAndLogEx(NORMAL, " --image image to flash. Can be specified several times."); PrintAndLogEx(NORMAL, "\nExamples:"); PrintAndLogEx(NORMAL, "\n to run Proxmark3 client:\n"); @@ -602,7 +603,7 @@ static void show_help(bool showFullHelp, char *exec_name) { } } -static int flash_pm3(char *serial_port_name, uint8_t num_files, char *filenames[FLASH_MAX_FILES], bool can_write_bl) { +static int flash_pm3(char *serial_port_name, uint8_t num_files, char *filenames[FLASH_MAX_FILES], bool can_write_bl, bool force) { int ret = PM3_EUNDEF; flash_file_t files[FLASH_MAX_FILES]; @@ -635,7 +636,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, char *filenames[ } for (int i = 0 ; i < num_files; ++i) { - ret = flash_load(&files[i]); + ret = flash_load(&files[i], force); if (ret != PM3_SUCCESS) { goto finish2; } @@ -732,6 +733,7 @@ int main(int argc, char *argv[]) { bool flash_mode = false; bool flash_can_write_bl = false; + bool flash_force = false; bool debug_mode_forced = false; int flash_num_files = 0; char *flash_filenames[FLASH_MAX_FILES]; @@ -948,6 +950,12 @@ int main(int argc, char *argv[]) { continue; } + // force flash even if firmware seems to not match client version + if (strcmp(argv[i], "--force") == 0) { + flash_force = true; + continue; + } + // flash file if (strcmp(argv[i], "--image") == 0) { if (flash_num_files == FLASH_MAX_FILES) { @@ -989,7 +997,7 @@ int main(int argc, char *argv[]) { speed = USART_BAUD_RATE; if (flash_mode) { - flash_pm3(port, flash_num_files, flash_filenames, flash_can_write_bl); + flash_pm3(port, flash_num_files, flash_filenames, flash_can_write_bl, flash_force); exit(EXIT_SUCCESS); } diff --git a/pm3 b/pm3 index ab2d9e3b7..e4469cd29 100755 --- a/pm3 +++ b/pm3 @@ -288,6 +288,8 @@ elif [ "$SCRIPT" = "pm3-flash" ]; then while [ "$1" != "" ]; do if [ "$1" == "-b" ]; then ARGS+=("--unlock-bootloader") + elif [ "$1" == "--force" ]; then + ARGS+=("--force") else ARGS+=("--image" "$1") fi @@ -320,7 +322,19 @@ elif [ "$SCRIPT" = "pm3-flash-all" ]; then FINDBTDONGLE=false FINDBTRFCOMM=false FINDBTDIRECT=false - CMD() { $CLIENT "--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE" "--image" "$FULLIMAGE"; } + + + CMD() { + ARGS=("--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE" "--image" "$FULLIMAGE") + shift; + while [ "$1" != "" ]; do + if [ "$1" == "--force" ]; then + ARGS+=("--force") + fi + shift; + done + $CLIENT "${ARGS[@]}"; + } HELP() { cat << EOF Quick helper script for flashing a Proxmark device via USB @@ -340,7 +354,17 @@ elif [ "$SCRIPT" = "pm3-flash-fullimage" ]; then FINDBTDONGLE=false FINDBTRFCOMM=false FINDBTDIRECT=false - CMD() { $CLIENT "--port" "$1" "--flash" "--image" "$FULLIMAGE"; } + CMD() { + ARGS=("--port" "$1" "--flash" "--image" "$FULLIMAGE") + shift; + while [ "$1" != "" ]; do + if [ "$1" == "--force" ]; then + ARGS+=("--force") + fi + shift; + done + $CLIENT "${ARGS[@]}"; + } HELP() { cat << EOF Quick helper script for flashing a Proxmark device via USB @@ -360,7 +384,17 @@ elif [ "$SCRIPT" = "pm3-flash-bootrom" ]; then FINDBTDONGLE=false FINDBTRFCOMM=false FINDBTDIRECT=false - CMD() { $CLIENT "--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE"; } + CMD() { + ARGS=("--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE") + shift; + while [ "$1" != "" ]; do + if [ "$1" == "--force" ]; then + ARGS+=("--force") + fi + shift; + done + $CLIENT "${ARGS[@]}"; + } HELP() { cat << EOF Quick helper script for flashing a Proxmark device via USB