Skip to content

Commit

Permalink
Created mem_helper.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
HU90m committed May 7, 2024
1 parent 3a2948c commit 9e04e25
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 31 deletions.
31 changes: 0 additions & 31 deletions util/load_new_software_on_fpga.sh

This file was deleted.

102 changes: 102 additions & 0 deletions util/mem_helper.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"

0 comments on commit 9e04e25

Please sign in to comment.