Made the flash-sd script better. It takes options to wipe the sdcard and to specify a different buildroot directory.

This commit is contained in:
Jacob Pease 2023-08-21 16:04:18 -05:00
parent 298708e1cb
commit a3158af72b

View File

@ -4,14 +4,18 @@
# set -e
# Output colors
GREEN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m'
NAME="$GREEN"${0:2}"$NC"
GREEN="\e[32m"
RED="\e[31m"
BOLDRED="\e[1;91m"
BOLDGREEN="\e[1;32m"
NC="\e[0m"
NAME="$BOLDGREEN"${0:2}:"$NC"
ERRORTEXT="$BOLDRED"ERROR:"$NC"
# File location variables
RISCV=/opt/riscv
IMAGES=$RISCV/buildroot/output/images/
BUILDROOT=$RISCV/buildroot
IMAGES=$BUILDROOT/output/images
FW_JUMP=$IMAGES/fw_jump.bin
LINUX_KERNEL=$IMAGES/Image
DEVICE_TREE=$IMAGES/wally-vcu108.dtb
@ -19,31 +23,61 @@ DEVICE_TREE=$IMAGES/wally-vcu108.dtb
# Mount Directory
MNT_DIR=wallyimg
# Usage function
usage() { echo "Usage: $0 [-z] [-b <path/to/buildroot>] <device>" 1>&2; exit 1; }
# Process options and arguments. The following code grabs the single
# sdcard device argument no matter where it is in the positional
# parameters list.
ARGS=()
while [ $OPTIND -le "$#" ] ; do
if getopts "hzb:" arg ; then
case "${arg}" in
h) usage
;;
z) WIPECARD=y
;;
b) BUILDROOT=${OPTARG}
;;
esac
else
ARGS+=("${!OPTIND}")
((OPTIND++))
fi
done
SDCARD=${ARGS[0]}
if [ "$#" -eq "0" ] ; then
echo "$NAME: $RED ERROR $NC: You must supply the SD card device."
echo "usage: ./flash-sd.sh <sd device> <mount directory>"
usage
fi
# Check to make sure sd card device exists
if [ ! -e "$SDCARD" ] ; then
echo -e "$NAME $ERRORTEXT SD card device does not exist."
exit 1
fi
if [ ! -e "$1" ] ; then
echo "$NAME:$RED ERROR $NC: SD card device does not exist."
exit 1
fi
if [ ! -z "$2" ] ; then
MNT_DIR=$2
fi
# If images are not built, exit
if [ ! -e $FW_JUMP ] || [ ! -e $LINUX_KERNEL ] ; then
echo 'ERROR: Missing images in buildroot output directory.'
echo ' Build images before running this script.'
# If no images directory, images have not been built
if [ ! -d $IMAGES ] ; then
echo -e "$ERRORTEXT Buildroot images directory does not exist"
echo ' Make sure you have built the images before'
echo ' running this script.'
exit 1
else
# If images are not built, exit
if [ ! -e $FW_JUMP ] || [ ! -e $LINUX_KERNEL ] ; then
echo -e '$ERRORTEXT Missing images in buildroot output directory.'
echo ' Build images before running this script.'
exit 1
fi
fi
# Ensure device tree binaries exist
if [ ! -e $DEVICE_TREE ] ; then
echo 'ERROR: Missing device tree file'
exit 1
echo -e '$ERRORTEXT Missing device tree files'
make -C ../ generate BUILDROOT=$BUILDROOT
fi
# Size of OpenSBI and the Kernel in 512B blocks
@ -57,23 +91,25 @@ KERNEL_START=$(( $FW_JUMP_START + $FW_JUMP_SIZE ))
FS_START=$(( $KERNEL_START + $KERNEL_SIZE ))
# Print out the sizes of the binaries in 512B blocks
echo -e "$NAME: Device tree block size: $DST_SIZE"
echo -e "$NAME: OpenSBI FW_JUMP block size: $FW_JUMP_SIZE"
echo -e "$NAME: Kernel block size: $KERNEL_SIZE"
echo -e "$NAME Device tree block size: $DST_SIZE"
echo -e "$NAME OpenSBI FW_JUMP block size: $FW_JUMP_SIZE"
echo -e "$NAME Kernel block size: $KERNEL_SIZE"
read -p "Warning: Doing this will replace all data on this card.\nContinue? y/n: " -n 1 -r
read -p "Warning: Doing this will replace all data on this card. Continue? y/n: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
DEVBASENAME=$(basename $1)
CHECKMOUNT=$(lsblk | grep "sdb4" | tr -s ' ' | cut -d' ' -f 7)
DEVBASENAME=$(basename $SDCARD)
CHECKMOUNT=$(lsblk | grep "$DEVBASENAME"4 | tr -s ' ' | cut -d' ' -f 7)
if [ ! -z $CHECKMOUNT ] ; then
sudo umount -v $CHECKMOUNT
fi
#Make empty image
echo -e "$NAME: Creating blank image"
sudo dd if=/dev/zero of=$1 bs=64k status=progress && sync
if [ ! -z $WIPECARD ] ; then
echo -e "$NAME Wiping SD card. This could take a while."
sudo dd if=/dev/zero of=$SDCARD bs=64k status=progress && sync
fi
# GUID Partition Tables (GPT)
# ===============================================
@ -83,19 +119,19 @@ if [[ $REPLY =~ ^[Yy]$ ]] ; then
# to 1 sector boundaries I think? This would normally be set to 2048
# apparently.
sudo sgdisk -z $1
sudo sgdisk -z $SDCARD
sleep 1
echo -e "$NAME: Creating GUID Partition Table"
echo -e "$NAME Creating GUID Partition Table"
sudo sgdisk -g --clear --set-alignment=1 \
--new=1:34:+$DST_SIZE: --change-name=1:'fdt' \
--new=2:$FW_JUMP_START:+$FW_JUMP_SIZE --change-name=2:'opensbi' --typecode=1:2E54B353-1271-4842-806F-E436D6AF6985 \
--new=3:$KERNEL_START:+$KERNEL_SIZE --change-name=3:'kernel' \
--new=4:$FS_START:-0 --change-name=4:'filesystem' \
$1
$SDCARD
sudo partprobe $1
sudo partprobe $SDCARD
sleep 3
@ -103,18 +139,18 @@ if [[ $REPLY =~ ^[Yy]$ ]] ; then
DD_FLAGS="bs=4k iflag=fullblock oflag=direct conv=fsync status=progress"
echo -e "$NAME: Copying device tree"
sudo dd if=$DEVICE_TREE of="$1"1 $DD_FLAGS
sudo dd if=$DEVICE_TREE of="$SDCARD"1 $DD_FLAGS
echo -e "$NAME: Copying OpenSBI"
sudo dd if=$FW_JUMP of="$1"2 $DD_FLAGS
sudo dd if=$FW_JUMP of="$SDCARD"2 $DD_FLAGS
echo -e "$NAME: Copying Kernel"
sudo dd if=$LINUX_KERNEL of="$1"3 $DD_FLAGS
sudo dd if=$LINUX_KERNEL of="$SDCARD"3 $DD_FLAGS
sudo mkfs.ext4 "$1"4
sudo mkfs.ext4 "$SDCARD"4
sudo mkdir /mnt/$MNT_DIR
sudo mount -v "$1"4 /mnt/$MNT_DIR
sudo mount -v "$SDCARD"4 /mnt/$MNT_DIR
sudo umount -v /mnt/$MNT_DIR
@ -123,5 +159,5 @@ if [[ $REPLY =~ ^[Yy]$ ]] ; then
fi
echo
echo "GPT Information for $1 ==================================="
sudo sgdisk -p $1
echo "GPT Information for $SDCARD ==================================="
sudo sgdisk -p $SDCARD