diff --git a/util/flash_disk.gdb b/util/flash_disk.gdb new file mode 100644 index 00000000..0dabdda6 --- /dev/null +++ b/util/flash_disk.gdb @@ -0,0 +1,23 @@ +# Copyright 2024 ETH Zurich and University of Bologna. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +# +# Cheshire disk flashing script for use with GDB. + +target extended-remote localhost:3333 + +# Load flashing program +load sw/boot/flash.spm.elf + +# Load disk image to DRAM +eval "restore %s binary 0x80000000", $img + +# Write flash parameters to scratch regs +set *0x03000000=$target +set *0x03000004=0x80000000 +set *0x03000008=$offs +set *0x0300000c=$len + +# Launch payload and quit after return +continue +quit diff --git a/util/flash_disk.sh b/util/flash_disk.sh new file mode 100755 index 00000000..78fd4cc2 --- /dev/null +++ b/util/flash_disk.sh @@ -0,0 +1,33 @@ +#! /usr/bin/env bash +# +# Copyright 2024 ETH Zurich and University of Bologna. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +# +# Cheshire disk flashing script invoking OpenOCD and GDB. +# +# Arguments: +# $1: target board +# $2: target disk +# $3: (optional): disk image +# $4: (optional): copy length +# $5: (optional): copy offset + +set -e + +# Determine the image name and size +img=${3:-sw/boot/linux.${1}.gpt.bin} +# Ensure the image exists and determine rounded-up size +len=${4:-$(stat -c%s ${img})} +len=$((len/512+1)) + +# Run OpenOCD and GDB +openocd -f util/openocd.${1}.tcl & +sleep 2 +riscv64-unknown-elf-gdb --batch \ + -ex "set \$target = ${2:-1}" \ + -ex "set \$img = \"${img}\"" \ + -ex "set \$len = ${len}" \ + -ex "set \$offs = ${5:-0}" \ + -ex "source util/flash_disk.gdb" +wait diff --git a/util/openocd.common.tcl b/util/openocd.common.tcl index 7474f015..ecba985c 100644 --- a/util/openocd.common.tcl +++ b/util/openocd.common.tcl @@ -23,10 +23,16 @@ riscv set_command_timeout_sec 120 riscv set_prefer_sba off +# Exit when debugger detaches +$_TARGETNAME configure -event gdb-detach { + echo "GDB detached; ending debugging session." + shutdown +} + # Try enabling address translation (only works for newer versions) if { [catch {riscv set_enable_virtual on} ] } { echo "Warning: This version of OpenOCD does not support address translation. To debug on virtual addresses, please update to the latest version." } init halt -echo "Ready for Remote Connections" +echo "Ready for Remote Connections."