From 9e04e25a29e858fc32d794f7d31a8596bb00f262 Mon Sep 17 00:00:00 2001 From: Hugo McNally Date: Tue, 7 May 2024 16:47:25 +0100 Subject: [PATCH] Created mem_helper.sh --- util/load_new_software_on_fpga.sh | 31 --------- util/mem_helper.sh | 102 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 31 deletions(-) delete mode 100755 util/load_new_software_on_fpga.sh create mode 100755 util/mem_helper.sh diff --git a/util/load_new_software_on_fpga.sh b/util/load_new_software_on_fpga.sh deleted file mode 100755 index d6c831311..000000000 --- a/util/load_new_software_on_fpga.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh -# Copyright lowRISC contributors. -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 - -if [ $# -ne 1 ] && [ $# -ne 2 ]; then - echo "Usage $0 elf_file [tcl_file]" - exit 1 -fi - -if [ ! -f $1 ]; then - echo "$1 does not exist" - exit 1 -fi - -SCRIPT_DIR="$(dirname "$(readlink -e "$0")")" -TCL_FILE=$SCRIPT_DIR/sonata-openocd-cfg.tcl - -if [ $# -eq 2 ]; then - if [ ! -f $2 ]; then - echo "$2 does not exist" - exit 1 - fi - TCL_FILE=$2 -fi - -openocd -f $TCL_FILE \ - -c "load_image $1 0x0" \ - -c "verify_image $1 0x0" \ - -c "exit" - diff --git a/util/mem_helper.sh b/util/mem_helper.sh new file mode 100755 index 000000000..0ecd65d26 --- /dev/null +++ b/util/mem_helper.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env sh +# Copyright lowRISC contributors. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +set -ue + +NAME="$(basename $0)" +SCRIPT_DIR="$(dirname "$(readlink -e "$0")")" +TCL_FILE=$SCRIPT_DIR/sonata-openocd-cfg.tcl + +USAGE="Example Usage of $NAME + + Generate a vmem file for an ELF file containing CHERIoT RTOS build: + + $NAME rtos_vmem -e \$elf_file -o \$output_file + + Load an ELF file onto the FPGA: + + $NAME load_program -e \$elf_file" + +usage_then_exit() { + echo "$USAGE" + exit 2 +} + +check_elf_file() { + if [ ! -f "${ELF_FILE-}" ]; then + echo "$NAME: Either no elf file was given or the given elf file doesn't exist." + usage_then_exit + fi +} + +check_out_file() { + if [ -z "${OUT_FILE-}" ]; then + echo "$NAME: No output file was given." + usage_then_exit + fi +} + +check_not_empty() { + if [ ! -f "$1" ]; then + echo "$2" + usage_then_exit + fi +} + +check_exists() { + if [ ! -f "$1" ]; then + echo "$2" + usage_then_exit + fi +} + +rtos_vmem() { + while getopts "e:o:h" opt; do + case "${opt}" in + e) ELF_FILE="$OPTARG";; + o) OUT_FILE="$OPTARG";; + *) usage_then_exit;; + esac + done + check_not_empty "${OUT_FILE-}" "$NAME: No output file was given." + check_not_empty "${ELF_FILE-}" "$NAME: No ELF file was given." + check_exists "$ELF_FILE" "$NAME: '$ELF_FILE' doesn't exist." + + TMP_FILE="$(mktemp)" + llvm-objcopy -O binary "$ELF_FILE" "$TMP_FILE" + # Put the code at offset 0x80 and zero fill ibex's default vectored interrupt table + srec_cat "$TMP_FILE" -binary -offset 0x80 -byte-swap 4 -fill 0x00 0x00 0x80 -o "$OUT_FILE" -vmem + rm "$TMP_FILE" +} + +load_program() { + while getopts "e:t:h" opt; do + case "${opt}" in + e) ELF_FILE="$OPTARG";; + t) TCL_FILE="$OPTARG";; + *) usage_then_exit;; + esac + done + check_not_empty "${ELF_FILE-}" "$NAME: No ELF file was given." + check_exists "$ELF_FILE" "$NAME: '$ELF_FILE' doesn't exist." + check_exists "$TCL_FILE" "$NAME: '$TCL_FILE' doesn't exist." + + openocd -f "$TCL_FILE" \ + -c "load_image "$ELF_FILE" 0x0" \ + -c "verify_image "$ELF_FILE" 0x0" \ + -c "exit" +} + +main() { + if [ -z "${1-}" ]; then + usage_then_exit + fi + case "$1" in + rtos_vmem) shift && rtos_vmem "$@";; + load_program) shift && load_program "$@";; + *) usage_then_exit;; + esac +} + +main "$@"