5th PS script shows erratic behaviour

This commit is contained in:
unknown 2020-12-06 00:59:50 +01:00
parent 87ffaa019c
commit 06c9206d01
16 changed files with 1803 additions and 30 deletions

View File

@ -2,13 +2,7 @@
set -e
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <Adapter: jlink or stlink>"
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"
echo "Device backed up successfully"

View File

@ -1,19 +1,22 @@
Clear-Host
Write-Host "Running sanity checks..."
if (!(openocd -v) | out-null) {
Invoke-Expression 'openocd -v' *>&1 | Out-Null
if(-not $LASTEXITCODE -eq 0){
Write-Host "OpenOCD does not seem to be working. Please validate that you have it installed correctly!"
exit 1
break
}
if (!(python -V) | out-null){
Invoke-Expression 'python -v' *>&1 | Out-Null
if(-not $LASTEXITCODE -eq 0){
Write-Host "Could not run python3. Please validate that you have it installed correctly!"
exit 1
break
}
if (!(arm-none-eabi-objdump -v) | out-null){
Invoke-Expression 'arm-none-eabi-objdump -v' *>&1 | Out-Null
if(-not $LASTEXITCODE -eq 0){
Write-Host "Could not find arm-none-eabi-objdump. Please validate that it's installed and in PATH."
exit 1
break
}
Write-Host "Looks good!"

View File

@ -1,4 +1,7 @@
Clear-Host
Set-Location = $PSScriptRoot
$path = $PSScriptRoot + "\backups"
Write-Host "Instructions:"
Write-Host "- Type in your Adapter"
Write-Host "- Press and hold the power button"
@ -19,7 +22,7 @@ $ShaITCM = "ca71a54c0a22cca5c6ee129faee9f99f3a346ca0"
$pathITCM = $PSScriptRoot + "\backups\itcm_backup.bin"
if (!(($pathShaITCM) = (Get-FileHash -Path $pathITCM -algorithm SHA1))){
Write-Host "Failed to correctly dump ITCM. Restart Game & Watch and try again."
Exit 1
break
}
$in = $PSScriptRoot + "\backups\flash_backup.bin"
@ -32,7 +35,7 @@ $ShaBackup = "eea70bb171afece163fb4b293c5364ddb90637ae"
$pathfullBackup = $PSScriptRoot + "\backups\flash_backup_checksummed.bin"
if(!(($pathShaBackup) = (Get-FileHash -Path $pathfullBackup -algorithm SHA1))){
Write-Host "Failed to verify checksum. Try again."
Exit 1
break
}
Remove-Item -Path $out

View File

@ -0,0 +1,68 @@
Clear-Host
$Loc = $PSScriptRoot
Write-Host "Usage: <Adapter: jlink or stlink>"
$adapter = Read-Host
$Interface_adapter = "$Loc\openocd\interface_$adapter.cfg"
if(!(Test-Path "$Loc\logs\")){
New-Item -Path $PSScriptRoot -Name "logs" -ItemType "directory"
}
if( Test-Path "$Loc\backups\"){
if( Test-Path "$Loc\backups\internal_flash_backup.bin"){
Write-Host "Already have a backup in \backups\internal_flash_backup.bin, refusing to overwrite."
break
}
}
Write-Host "This step will overwrite the contents of the SPI flash chip that we backed up in step 2."
Write-Host "It will be restored in step 5. Continue? (Y/y)"
$2 = Read-Host
if(!($2 -match 'y')){
Write-Host "Aborted."
break
}
Write-Host "Generating encrypted flash image from backed up data..."
Invoke-Expression "python '$Loc\python\tcm_encrypt.py' '$Loc\backups\flash_backup.bin' '$Loc\backups\itcm_backup.bin' '$Loc\payload\payload.bin' '$Loc\new_flash_image.bin'" *>&1 | Out-Null
if(-not $LASTEXITCODE -eq 0){
Write-Host "Failed to build encrypted flash image."
break
}
$scriptPath ="$Loc\scripts\flashloader.ps1"
Invoke-Expression "$scriptPath $adapter new_flash_image.bin" *>&1
if(-not $LASTEXITCODE -eq 0){
Write-Host "Flashloader failed, check debug connection and try again."
break
}
Clear-Host
Write-Host "Flash successfully flashed. Now do the following procedure:"
Write-Host "- Disconnect power from the device"
Write-Host "- Power it again"
Write-Host "- Press and hold the power button"
Write-Host "- Press return (while still holding the power button)!"
Pause
Write-Host "Dumping internal flash..."
Invoke-Expression "openocd -f $Interface_adapter -c 'init;' -c 'halt;' -c 'dump_image internal_flash_backup.bin 0x24000000 131072' -c 'break;'" *>&1 | Out-Null
<# if(-not $LASTEXITCODE -eq 0){
Write-Host "Dumping internal flash failed."
break
} #>
Move-Item $Loc\internal_flash_backup.bin $Loc\backups
$ShaBackup = 'efa04c387ad7b40549e15799b471a6e1cd234c76'
Write-Host "Verifying internal flash backup..."
if(!($ShaBackup = (Get-FileHash -Path "$Loc\backups\internal_flash_backup.bin" -algorithm SHA1))){
Write-Host "The backup of the internal flash failed. Please try again."
break
}
Remove-Item new_flash_image.bin
Write-Host "Device backed up successfully"
Pause

39
PS_4_unlock_device.ps1 Normal file
View File

@ -0,0 +1,39 @@
Clear-Host
$Loc = $PSScriptRoot
$rdp0_config = "$Loc\openocd\rdp0.cfg"
$Interface_cfg = "$Loc\openocd\interface_" + $1 + ".cfg"
Write-Host "Usage: <Adapter: jlink or stlink>"
$1 = Read-Host
if(!(Test-Path "$Loc\logs\")){
New-Item -Path $PSScriptRoot -Name "logs" -ItemType "directory"
}
Write-Host "Unlocking your device will erase its internal flash. Even though your backup"
$key = Read-Host "is validated, this still can go wrong. Are you sure? (Y/y)"
if(!($key -match "y")){
Write-Host "Aborted."
break
}
$ShaBackup = "efa04c387ad7b40549e15799b471a6e1cd234c76"
Write-Host "Validating internal flash backup before proceeding..."
if(!(Test-Path "$Loc\backups\internal_flash_backup.bin")){
Write-Host "Backup not found. Aborting."
break
}
if(!($shaBackup -eq (Get-FileHash -Path "$Loc\backups\internal_flash_backup.bin" -algorithm SHA1)) | out-null){
Write-Host "Backup is not valid. Aborting."
break
}
Write-Host "Unlocking device... (Takes up to 30 seconds.)"
Invoke-Expression "openocd -f $Interface_cfg -c 'init;' -c 'halt;' -f $rdp0_config" *>&1 | Out-File "$Loc\logs\4_openocd.log" -Encoding ascii -Append
if(-not $LASTEXITCODE -eq 0){
Write-Host "Unlocking device failed."
break
}
Write-Host "Congratulations, your device has been unlocked."
Write-Host "Please power-cycle it for the changes to take full effect."
Pause

45
PS_5_restore.ps1 Normal file
View File

@ -0,0 +1,45 @@
Clear-Host
Write-Host "Usage: <Adapter: jlink or stlink>"
$adapter = Read-Host
$Loc = $PSScriptRoot
$Interface_cfg = "$Loc\openocd\interface_" + $1 + ".cfg"
$backup = "$Loc\backups\internal_flash_backup.bin"
if(!(Test-Path "$Loc\logs\")){
New-Item -Path $PSScriptRoot -Name "logs" -ItemType "directory"
}
if(!(Test-Path "$Loc\backups\internal_flash_backup.bin")){
Write-Host "No backup of internal flash found in \backups\internal_flash_backup.bin"
break
}
if(!(Test-Path "$Loc\backups\flash_backup.bin")){
Write-Host "No backup of SPI flash found in \backups\flash_backup.bin"
break
}
Write-Host "Ok, restoring original firmware! (We will not lock the device, so you won't have to repeat this procedure!)"
Write-Host "Restoring internal flash..."
<# #Invoke-Expression "
openocd -f $Interface_cfg -c "init;" -c "halt;" -c "program internal_flash_backup.bin 0x08000000 verify;" -c "exit;" *>&1
#| Out-File "$Loc\logs\5_openocd.log" -Encoding ascii -Append
if(-not $LASTEXITCODE -eq 0){
Write-Host "Restoring internal flash failed. Check debug connection and try again."
break
} #>
Write-Host "Restoring SPI flash..."
$scriptPath ="$Loc\scripts\flashloader.ps1"
Invoke-Expression "$scriptPath $adapter flash_backup.bin"
if(-not $LASTEXITCODE -eq 0){
Write-Host "Restoring SPI flash failed. Check debug connection and try again."
break
}
Write-Host "Success, your device should be running the original firmware again!"
Write-Host "(You should power-cycle the device now)"
Pause

18
config.sh Normal file
View File

@ -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 <Adapter: jlink or stlink>"
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

40
logs/4_openocd.log Normal file
View File

@ -0,0 +1,40 @@
openocd : Open On-Chip Debugger 0.10.0 (2019-08-28) [https://github.com/sysprogs/openocd]
In C:\Users\lou\Desktop\Proj\NintendoGameNWatch\game-and-watch-backup-main\PS_4_unlock_device.ps1:32 car:1
+ openocd -f $Interface_cfg -c 'init;' -c 'halt;' -f $rdp0_config *>&1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Open On-Chip De...sprogs/openocd]:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate
Info : clock speed 1800 kHz
Info : STLINK V2J33M25 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.239448
Error: init mode failed (unable to connect to the target)
openocd : Open On-Chip Debugger 0.10.0 (2019-08-28) [https://github.com/sysprogs/openocd]
In C:\Users\lou\Desktop\Proj\NintendoGameNWatch\game-and-watch-backup-main\PS_4_unlock_device.ps1:32 car:1
+ openocd -f $Interface_cfg -c 'init;' -c 'halt;' -f $rdp0_config *>&1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Open On-Chip De...sprogs/openocd]:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate
Info : clock speed 1800 kHz
Info : STLINK V2J33M25 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.241026
Info : stm32h7x.cpu: hardware has 8 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x81000003 pc: 0xfffffffe msp: 0x200111e0
Reset and halt

11
logs/5_openocd.log Normal file
View File

@ -0,0 +1,11 @@
openocd : Open On-Chip Debugger 0.10.0 (2019-08-28) [https://github.com/sysprogs/openocd]
In C:\Users\lou\Desktop\Proj\NintendoGameNWatch\game-and-watch-backup-main\PS_5_restore.ps1:27 car:1
+ openocd -f $Interface_cfg -c 'init;' -c 'halt;' -c 'program' $backup ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Open On-Chip De...sprogs/openocd]:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Unexpected command line argument: C:\Users\lou\Desktop\Proj\NintendoGameNWatch\game-and-watch-backup-main\backups\internal_flash_backup.bin

1495
logs/flashloader.log Normal file

File diff suppressed because it is too large Load Diff

1
openocd.log Normal file
View File

@ -0,0 +1 @@
Warn : deprecated option: -p/--pipe. Use '-c "gdb_port pipe; log_output openocd.log"' instead.

53
scripts/flashloader.ps1 Normal file
View File

@ -0,0 +1,53 @@
Clear-Host
Write-Host "Running flashloader..."
$1=$args[0]
$2=$args[1]
$Interface_cfg = "$Loc\openocd\interface_" + $1 + ".cfg"
$Loc = Split-Path -Path $PSScriptRoot -Parent
$ELF='firmware\\flash_programmer.elf'
$ADDRESS=0
$SIZE=((1024 * 1024))
$MAGIC="0xdeadbeef"
$ERASE=1
$IMAGE=$2
#objdump=${OBJDUMP:-arm-none-eabi-objdump}
function get_symbol {
param($name)
$objdump_cmd= objdump -t $ELF
$size = $objdump_cmd | Select-String "$name"
$size = "$size".Split(" ")[0]
return "0x" + '{0:X8}' -f $size.ToUpper()
}
$VAR_program_size = get_symbol("program_size")
$VAR_program_address = get_symbol("program_address")
$VAR_program_magic = get_symbol("program_magic")
$VAR_program_done = get_symbol("program_done")
$VAR_program_erase = get_symbol("program_erase")
$FlashLog = "$Loc\logs\flashloader.log"
(Invoke-Expression "openocd -f $Interface_cfg -c 'init;' -c 'load_image $ELF;' -c 'reset halt' -c 'sleep 100' -c 'load_image $IMAGE 0x24000000' -c 'mww $VAR_program_size $SIZE' -c 'mww $VAR_program_address $ADDRESS' -c 'mww $VAR_program_magic $MAGIC' -c 'mww $VAR_program_erase $ERASE' -c 'reg sp [mrw 0x20000000];' -c 'reg pc [mrw 0x20000004];' -c 'resume;' -c 'exit;'") *>&1 | Out-File $FlashLog -Encoding ascii -Append
if(-not $LASTEXITCODE -eq 0){
# *>&1 | Out-File "$Loc\logs\flashloader.log" -Encoding ascii -Append)){
Write-Host "Loading failed."
break
}
Write-Host "Loaded flashloader, flashing SPI, please wait."
Write-Host " (If this takes more than 2 minutes something went wrong.)"
Write-Host " (If the screen blinks rapidly, something went wrong.)"
Write-Host " (If the screen blinks slowly, everything worked but the script didn't detect it)"
$DONE_MAGIC = $null
while(1){
openocd -f $Interface_cfg -c "init; mdw $VAR_program_done" -c "exit;" *>&1 | Select-String $VAR_program_done | Tee-Object -Variable DONE_MAGIC
$DONE_MAGIC = "$DONE_MAGIC".split(": ")
if($DONE_MAGIC -match "cafef00d"){
Write-Host "Done!"
exit;
}
sleep 1
}

View File

@ -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
@ -13,7 +15,7 @@ function get_symbol {
name=$1
objdump_cmd="${objdump} -t ${ELF}"
size=$(${objdump_cmd} | grep " $name" | cut -d " " -f1 | tr 'a-f' 'A-F')
printf "ibase=16\n${size}\n" | bc
printf "$((16#${size}))\n"
}
VAR_program_size=$(printf '0x%08x\n' $(get_symbol "program_size"))
@ -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,10 +53,10 @@ 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;
fi
sleep 1
done
done

View File

@ -1,13 +1,6 @@
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <Adapter: jlink or stlink>"
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

8
workspace.code-workspace Normal file
View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}