From 1b1d64bb7d8e92efc99297d90bbe979243820e11 Mon Sep 17 00:00:00 2001 From: Paul Scheffler Date: Sat, 6 Jul 2024 04:50:45 +0200 Subject: [PATCH] util: Add disk flashing utility --- util/flash_disk.gdb | 23 +++++++++++++++++++++++ util/flash_disk.sh | 33 +++++++++++++++++++++++++++++++++ util/openocd.common.tcl | 8 +++++++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 util/flash_disk.gdb create mode 100755 util/flash_disk.sh diff --git a/util/flash_disk.gdb b/util/flash_disk.gdb new file mode 100644 index 000000000..0dabdda63 --- /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 000000000..78fd4cc26 --- /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 29b8f0b58..d4e816bbc 100644 --- a/util/openocd.common.tcl +++ b/util/openocd.common.tcl @@ -23,6 +23,12 @@ 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.\ @@ -31,4 +37,4 @@ if { [catch { riscv set_enable_virtual on } ] } { init halt -echo "Ready for Remote Connections" +echo "Ready for Remote Connections."