From 62d81e943d1a38f85eed37bdff2029c629e51663 Mon Sep 17 00:00:00 2001 From: Steve Clement Date: Wed, 2 Dec 2020 13:14:00 +0900 Subject: [PATCH] new: [config] Added config.sh file --- 1_sanity_check.sh | 13 +++++++++++-- 2_backup_flash.sh | 11 ++++------- 3_backup_internal_flash.sh | 12 +++--------- 4_unlock_device.sh | 11 ++--------- 5_restore.sh | 13 +++---------- config.sh | 18 ++++++++++++++++++ install_pwnadventure.sh | 12 ++---------- scripts/flashloader.sh | 6 ++++-- scripts/rdp1.sh | 11 ++--------- 9 files changed, 49 insertions(+), 58 deletions(-) create mode 100755 config.sh diff --git a/1_sanity_check.sh b/1_sanity_check.sh index 8f29b01..8a9165b 100755 --- a/1_sanity_check.sh +++ b/1_sanity_check.sh @@ -1,11 +1,20 @@ #!/bin/bash +source config.sh placeHolder + echo "Running sanity checks..." -if ! openocd -v >/dev/null 2>&1; then +if ! ${OPENOCD} -v >/dev/null 2>&1; then echo "OpenOCD does not seem to be working. Please validate that you have it installed correctly!" exit 1 fi +if [[ "${OPENOCD_VERSION}" == "0.10.0" ]]; then + echo "You seem to be using a vanilla version of openocd." + echo "In case you see the following error: " + echo " openocd/interface_stlink.cfg:1: Error: Can't find interface/stlink.cfg" + echo "Update your openocd version." +fi + if ! /usr/bin/env python3 -V >/dev/null 2>&1; then echo "Could not run python3. Please validate that you have it installed correctly!" exit 1 @@ -16,4 +25,4 @@ if ! arm-none-eabi-objdump -v >/dev/null 2>&1; then exit 1 fi -echo "Looks good!" \ No newline at end of file +echo "Looks good!" diff --git a/2_backup_flash.sh b/2_backup_flash.sh index 249fdad..aac8032 100755 --- a/2_backup_flash.sh +++ b/2_backup_flash.sh @@ -1,5 +1,7 @@ #!/bin/bash +source config.sh $1 + if [[ $# -ne 1 ]]; then echo "Usage: backup_flash.sh " exit 1 @@ -10,17 +12,12 @@ if test -f backups/flash_backup.bin; then exit 1 fi -ADAPTER=$1 - echo "Make sure your Game & Watch is turned on and in the time screen. Press return when ready!" read -n 1 -mkdir -p backups -mkdir -p logs - -echo "Attempting to dump flash using adapter $1." +echo "Attempting to dump flash using adapter ${ADAPTER}." echo "Running OpenOCD... (This will take roughly 30 seconds, your Game and Watch screen will blink in between.)" -if ! openocd -f openocd/flash_"$1".cfg >>logs/2_openocd.log 2>&1; then +if ! ${OPENOCD} -f openocd/flash_"${ADAPTER}".cfg >>logs/2_openocd.log 2>&1; then echo "Failed to dump SPI flash from device. Verify debug connection and try again." exit 1 fi diff --git a/3_backup_internal_flash.sh b/3_backup_internal_flash.sh index e86b440..4d45e6f 100755 --- a/3_backup_internal_flash.sh +++ b/3_backup_internal_flash.sh @@ -2,13 +2,7 @@ set -e -if [[ $# -ne 1 ]]; then - echo "Usage: $0 " - exit 1 -fi - -ADAPTER=$1 -mkdir -p logs +source config.sh $1 if test -f backups/internal_flash_backup.bin; then echo "Already have a backup in backups/internal_flash_backup.bin, refusing to overwrite." @@ -48,7 +42,7 @@ echo "- Press return (while still holding the power button)!" read -n 1 echo "Dumping internal flash..." -if ! openocd -f openocd/interface_"$1".cfg \ +if ! ${OPENOCD} -f openocd/interface_"${ADAPTER}".cfg \ -c "init;" \ -c "halt;" \ -c "dump_image backups/internal_flash_backup.bin 0x24000000 131072" \ @@ -65,4 +59,4 @@ fi rm new_flash_image.bin -echo "Device backed up successfully" \ No newline at end of file +echo "Device backed up successfully" diff --git a/4_unlock_device.sh b/4_unlock_device.sh index 2436bb8..a562584 100755 --- a/4_unlock_device.sh +++ b/4_unlock_device.sh @@ -1,13 +1,6 @@ #!/bin/bash - -if [[ $# -ne 1 ]]; then - echo "Usage: $0 " - exit 1 -fi - -ADAPTER=$1 -mkdir -p logs +source config.sh $1 echo "Unlocking your device will erase its internal flash. Even though your backup" echo "is validated, this still can go wrong. Are you sure? (Y/y)" @@ -25,7 +18,7 @@ if ! shasum --check shasums/internal_flash_backup.bin.sha1 >/dev/null 2>&1; then fi echo "Unlocking device... (Takes up to 30 seconds.)" -if ! openocd -f openocd/interface_"$1".cfg \ +if ! ${OPENOCD} -f openocd/interface_"${ADAPTER}".cfg \ -c "init;" \ -c "halt;" \ -f openocd/rdp0.cfg >>logs/4_openocd.log 2>&1; then diff --git a/5_restore.sh b/5_restore.sh index b8aede9..d2ce917 100755 --- a/5_restore.sh +++ b/5_restore.sh @@ -1,11 +1,6 @@ #!/bin/bash - -if [[ $# -ne 1 ]]; then - echo "Usage: $0 " - exit 1 -fi - +source config.sh $1 if ! test -f backups/internal_flash_backup.bin; then echo "No backup of internal flash found in backups/internal_flash_backup.bin" @@ -17,13 +12,11 @@ if ! test -f backups/flash_backup.bin; then exit 1 fi -ADAPTER=$1 -mkdir -p logs echo "Ok, restoring original firmware! (We will not lock the device, so you won't have to repeat this procedure!)" echo "Restoring internal flash..." -if ! openocd -f openocd/interface_"$1".cfg \ +if ! ${OPENOCD} -f openocd/interface_"${ADAPTER}".cfg \ -c "init;" \ -c "halt;" \ -c "program backups/internal_flash_backup.bin 0x08000000 verify;" \ @@ -40,4 +33,4 @@ if ! ./scripts/flashloader.sh $ADAPTER backups/flash_backup.bin; then fi echo "Success, your device should be running the original firmware again!" -echo "(You should power-cycle the device now)" \ No newline at end of file +echo "(You should power-cycle the device now)" diff --git a/config.sh b/config.sh new file mode 100755 index 0000000..5acb6d6 --- /dev/null +++ b/config.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +OPENOCD=${OPENOCD:-$(which openocd)} +OPENOCD_VERSION=$(openocd -v 2> >(cut -f 4 -d\ ) |head -1) +ADAPTER=$1 + + +mkdir -p logs backups + +if [[ $# -ne 1 ]] && [[ ! "$0" =~ .*"config.sh" ]]; then + echo "Usage: $0 " + exit 1 +fi + +if [[ -z ${OPENOCD} ]]; then + echo "Cannot find 'openocd' in the PATH. You can set the environment variable 'OPENOCD' to manually specify the location" + exit 2 +fi diff --git a/install_pwnadventure.sh b/install_pwnadventure.sh index 0ad3d0f..f21a5a3 100755 --- a/install_pwnadventure.sh +++ b/install_pwnadventure.sh @@ -1,17 +1,9 @@ #!/bin/bash - -if [[ $# -ne 1 ]]; then - echo "Usage: $0 " - exit 1 -fi - -ADAPTER=$1 -mkdir -p logs - +source config.sh $1 echo "Installing on internal flash..." -if ! openocd -f openocd/interface_"$1".cfg \ +if ! ${OPENOCD} -f openocd/interface_"${ADAPTER}".cfg \ -c "init;" \ -c "halt;" \ -c "program prebuilt/gw_retrogo_nes.elf;" \ diff --git a/scripts/flashloader.sh b/scripts/flashloader.sh index 76f75f4..295ebf5 100755 --- a/scripts/flashloader.sh +++ b/scripts/flashloader.sh @@ -1,5 +1,7 @@ #!/bin/bash +source config.sh $1 + DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" ELF=firmware/flash_programmer.elf ADDRESS=0 @@ -23,7 +25,7 @@ VAR_program_done=$(printf '0x%08x\n' $(get_symbol "program_done")) VAR_program_erase=$(printf '0x%08x\n' $(get_symbol "program_erase")) -if ! openocd -f openocd/interface_"$1".cfg \ +if ! ${OPENOCD} -f openocd/interface_"${ADAPTER}".cfg \ -c "init;" \ -c "echo \"Resetting device\";" \ -c "echo \"Programming ELF\";" \ @@ -51,7 +53,7 @@ echo " (If this takes more than 2 minutes something went wrong.)" echo " (If the screen blinks rapidly, something went wrong.)" echo " (If the screen blinks slowly, everything worked but the script didn't detect it)" while true; do - DONE_MAGIC=$(openocd -f openocd/interface_${1}.cfg -c "init; mdw ${VAR_program_done}" -c "exit;" 2>&1 | grep ${VAR_program_done} | cut -d" " -f2) + DONE_MAGIC=$(${OPENOCD} -f openocd/interface_${ADAPTER}.cfg -c "init; mdw ${VAR_program_done}" -c "exit;" 2>&1 | grep ${VAR_program_done} | cut -d" " -f2) if [[ "$DONE_MAGIC" == "cafef00d" ]]; then echo "Done!" break; diff --git a/scripts/rdp1.sh b/scripts/rdp1.sh index 481de4c..4234cec 100755 --- a/scripts/rdp1.sh +++ b/scripts/rdp1.sh @@ -1,13 +1,6 @@ #!/bin/bash - -if [[ $# -ne 1 ]]; then - echo "Usage: $0 " - exit 1 -fi - -ADAPTER=$1 -mkdir -p logs +source config.sh $1 echo "This will look your device! Are you sure? (Y/y)" read -n 1 -r @@ -24,7 +17,7 @@ if ! shasum --check shasums/internal_flash_backup.bin.sha1 >/dev/null 2>&1; then fi echo "Locking device... (Takes up to 30 seconds.)" -if ! openocd -f openocd/interface_"$1".cfg \ +if ! ${OPENOCD} -f openocd/interface_"${ADAPTER}".cfg \ -c "init;" \ -c "halt;" \ -f openocd/rdp1.cfg >>logs/rdp1_openocd.log 2>&1; then