From 52b5a4a07e86222942ddc75277000178138ff2c5 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 4 Mar 2020 11:55:31 +0100 Subject: [PATCH 1/3] Unified shell script name across host and add missing extension Signed-off-by: Frederic Pillon --- linux/{maple_upload => maple_upload.sh} | 0 linux/{massStorageCopy => massStorageCopy.sh} | 0 macosx/{maple_upload => maple_upload.sh} | 0 macosx/{massStorageCopyMacOsX => massStorageCopy.sh} | 0 macosx/{stm32CubeProg => stm32CubeProg.sh} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename linux/{maple_upload => maple_upload.sh} (100%) rename linux/{massStorageCopy => massStorageCopy.sh} (100%) rename macosx/{maple_upload => maple_upload.sh} (100%) rename macosx/{massStorageCopyMacOsX => massStorageCopy.sh} (100%) rename macosx/{stm32CubeProg => stm32CubeProg.sh} (100%) diff --git a/linux/maple_upload b/linux/maple_upload.sh similarity index 100% rename from linux/maple_upload rename to linux/maple_upload.sh diff --git a/linux/massStorageCopy b/linux/massStorageCopy.sh similarity index 100% rename from linux/massStorageCopy rename to linux/massStorageCopy.sh diff --git a/macosx/maple_upload b/macosx/maple_upload.sh similarity index 100% rename from macosx/maple_upload rename to macosx/maple_upload.sh diff --git a/macosx/massStorageCopyMacOsX b/macosx/massStorageCopy.sh similarity index 100% rename from macosx/massStorageCopyMacOsX rename to macosx/massStorageCopy.sh diff --git a/macosx/stm32CubeProg b/macosx/stm32CubeProg.sh similarity index 100% rename from macosx/stm32CubeProg rename to macosx/stm32CubeProg.sh From e00ca18bd3d3d8f56f618bb02e7ede04a8c45e2d Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 4 Mar 2020 10:38:44 +0100 Subject: [PATCH 2/3] Make shell scripts ShellCheck compliant Signed-off-by: Frederic Pillon --- linux/dfu-util.sh | 2 +- linux/install.sh | 2 +- linux/maple_upload.sh | 10 +++---- linux/massStorageCopy.sh | 22 +++++++-------- linux/stm32CubeProg.sh | 38 ++++++++++++------------- macosx/maple_upload.sh | 12 ++++---- macosx/massStorageCopy.sh | 22 +++++++-------- macosx/stm32CubeProg.sh | 40 +++++++++++++------------- run_arduino_gen.sh | 59 ++++++++++++++++++++------------------- 9 files changed, 100 insertions(+), 107 deletions(-) diff --git a/linux/dfu-util.sh b/linux/dfu-util.sh index 9b828f090..b4ddf1b06 100755 --- a/linux/dfu-util.sh +++ b/linux/dfu-util.sh @@ -7,7 +7,7 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # Choose dfu program by arch -if [ `uname -m` == "x86_64" ]; then +if [ "$(uname -m)" == "x86_64" ]; then DFU_UTIL=${DIR}/dfu-util_x86_64/dfu-util else DFU_UTIL=${DIR}/dfu-util/dfu-util diff --git a/linux/install.sh b/linux/install.sh index 32870a1c2..df39e472f 100755 --- a/linux/install.sh +++ b/linux/install.sh @@ -8,7 +8,7 @@ if sudo [ -w /etc/udev/rules.d ]; then echo "Reloading udev rules" sudo udevadm control --reload-rules echo "Adding current user to dialout group" - sudo adduser $USER dialout + sudo adduser "$USER" dialout else echo "Couldn't copy to /etc/udev/rules.d/; you probably have to run this script as root? Or your distribution of Linux doesn't include udev; try running the IDE itself as root." fi diff --git a/linux/maple_upload.sh b/linux/maple_upload.sh index 65d31439b..43deeb0bd 100755 --- a/linux/maple_upload.sh +++ b/linux/maple_upload.sh @@ -6,7 +6,7 @@ if [ $# -lt 4 ]; then echo "Usage: $0 $# " >&2 exit 1 fi -dummy_port="$1"; altID="$2"; usbID="$3"; binfile="$4"; dummy_port_fullpath="/dev/$1" +altID="$2"; usbID="$3"; binfile="$4"; dummy_port_fullpath="/dev/$1" if [ $# -eq 5 ]; then dfuse_addr="--dfuse-address $5" else @@ -24,14 +24,14 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # 750ms to 1500ms seems to work on my Mac -"${DIR}/upload-reset" ${dummy_port_fullpath} 750 +"${DIR}/upload-reset" "${dummy_port_fullpath}" 750 -"${DIR}/dfu-util.sh" -d ${usbID} -a ${altID} -D ${binfile} ${dfuse_addr} -R +"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" ${dfuse_addr} -R -echo -n Waiting for ${dummy_port_fullpath} serial... +echo -n Waiting for "${dummy_port_fullpath}" serial... COUNTER=0 -while [ ! -r ${dummy_port_fullpath} ] && ((COUNTER++ < 40)); do +while [ ! -r "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do sleep 0.1 done diff --git a/linux/massStorageCopy.sh b/linux/massStorageCopy.sh index f4d864931..be33a135b 100755 --- a/linux/massStorageCopy.sh +++ b/linux/massStorageCopy.sh @@ -1,7 +1,5 @@ #!/bin/bash set -o nounset # Treat unset variables as an error -#set -x -VERSION="0.1" # List bin_filepath= @@ -14,7 +12,7 @@ usage() { echo "############################################################" echo "##" - echo "## `basename $0` [-I ] [-O ]" + echo "## $(basename "$0") [-I ] [-O ]" echo "##" echo "## Options:" echo "## -I: filepath binary to copy" @@ -32,29 +30,29 @@ if [ $# -lt 2 ]; then fi # Parsing options -if [ $1 == "-I" ]; then +if [ "$1" == "-I" ]; then shift 1 fi bin_filepath=$1 -if [ $2 == "-O" ]; then +if [ "$2" == "-O" ]; then shift 1 fi # Strip first and last "" mountpoint_name="${2%\"}" mountpoint_name="${mountpoint_name#\"}" -if [ -z $bin_filepath ]; then +if [ -z "$bin_filepath" ]; then echo "No binary file path provided!" exit 1 fi -if [ -z $mountpoint_name ]; then +if [ -z "$mountpoint_name" ]; then echo "No mountpoint name provided!" exit 1 fi -if [ ! -f $bin_filepath ]; then +if [ ! -f "$bin_filepath" ]; then echo "$bin_filepath not found!" exit 2 fi @@ -63,11 +61,11 @@ fi IFS=',' read -ra mnt_list <<< "$mountpoint_name" for mnt in "${mnt_list[@]}"; do # mnt_path_list=(`cat /proc/mounts | cut -d' ' -f2 | sort -u | grep $mnt`) - mnt_path_list=(`df -Hl | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep $mnt`) + mnt_path_list=($(df -Hl | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep "$mnt")) if [ ${#mnt_path_list[@]} -ne 0 ]; then # Ensure to have exact match for mnt_path in "${mnt_path_list[@]}"; do - mnt_name=`echo $mnt_path | rev | cut -d'/' -f1 | rev` + mnt_name=$(echo "$mnt_path" | rev | cut -d'/' -f1 | rev) if [ "$mnt_name" = "$mnt" ]; then echo "Found '$mnt' at '$mnt_path'" mountpoint_path=$mnt_path @@ -77,7 +75,7 @@ for mnt in "${mnt_list[@]}"; do fi done -if [ -z $mountpoint_path ] || [ ! -d $mountpoint_path ]; then +if [ -z "$mountpoint_path" ] || [ ! -d "$mountpoint_path" ]; then echo "$mountpoint_name not found." echo "Please ensure the device is correctly connected and mounted." exit 3 @@ -85,6 +83,6 @@ fi # Copy the binary to the mountpoint echo "Copying $bin_filepath to $mountpoint_path..." -cp $bin_filepath $mountpoint_path +cp "$bin_filepath" "$mountpoint_path" exit $? diff --git a/linux/stm32CubeProg.sh b/linux/stm32CubeProg.sh index 1d2adeacf..95607ce14 100755 --- a/linux/stm32CubeProg.sh +++ b/linux/stm32CubeProg.sh @@ -1,12 +1,11 @@ #!/bin/bash set -o nounset # Treat unset variables as an error -#set -x STM32CP_CLI=STM32_Programmer.sh ADDRESS=0x8000000 -ERASE= -MODE= -PORT= -OPTS= +ERASE="" +MODE="" +PORT="" +OPTS="" ############################################################################### ## Help function @@ -14,7 +13,7 @@ usage() { echo "############################################################" echo "##" - echo "## `basename $0` [OPTIONS]" + echo "## $(basename "$0") [OPTIONS]" echo "##" echo "## protocol:" echo "## 0: SWD" @@ -34,17 +33,16 @@ usage() echo "## -rst: Reset system" echo "## -s: start automatically (optional)" echo "############################################################" - exit $1 + exit "$1" } - check_tool() { - command -v $STM32CP_CLI >/dev/null 2>&1 - if [ $? != 0 ]; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1 + then export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":$PATH fi - command -v $STM32CP_CLI >/dev/null 2>&1 - if [ $? != 0 ]; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1 + then echo "$STM32CP_CLI not found." echo "Please install it or add '/bin' to your PATH environment:" echo "https://www.st.com/en/development-tools/stm32cubeprog.html" @@ -65,8 +63,8 @@ PROTOCOL=$1 FILEPATH=$2 # Protocol $1 # 1x: Erase all sectors -if [ $1 -ge 10 ]; then - ERASE='-e all' +if [ "$1" -ge 10 ]; then + ERASE="yes" PROTOCOL=$(($1 - 10)) fi # Protocol $1 @@ -75,8 +73,8 @@ fi # 2: DFU case $PROTOCOL in 0) - PORT='SWD' - MODE='mode=UR' + PORT="SWD" + MODE="mode=UR" shift 2;; 1) if [ $# -lt 3 ]; then @@ -86,7 +84,7 @@ case $PROTOCOL in shift 3 fi;; 2) - PORT='USB1' + PORT="USB1" shift 2;; *) echo "Protocol unknown!" @@ -94,10 +92,10 @@ case $PROTOCOL in esac if [ $# -gt 0 ]; then - OPTS="$@" + OPTS="$*" fi -${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS} +${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" ${ADDRESS} "${OPTS}" -exit 0 +exit $? diff --git a/macosx/maple_upload.sh b/macosx/maple_upload.sh index 3521aa1ef..b2bec965f 100755 --- a/macosx/maple_upload.sh +++ b/macosx/maple_upload.sh @@ -6,7 +6,7 @@ if [ $# -lt 4 ]; then echo "Usage: $0 $# " >&2 exit 1 fi -dummy_port=$1; altID=$2; usbID=$3; binfile=$4;dummy_port_fullpath="/dev/$1" +altID=$2; usbID=$3; binfile=$4;dummy_port_fullpath="/dev/$1" # Get the directory where the script is running. @@ -31,7 +31,7 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # You may need to tune this to your system # 750ms to 1500ms seems to work on my Mac -${DIR}/upload-reset ${dummy_port_fullpath} 750 +"${DIR}"/upload-reset "${dummy_port_fullpath}" 750 if [ $# -eq 5 ]; then dfuse_addr="--dfuse-address $5" @@ -41,7 +41,7 @@ fi #DFU_UTIL=/usr/local/bin/dfu-util DFU_UTIL=${DIR}/dfu-util/dfu-util -if [ ! -x ${DFU_UTIL} ]; then +if [ ! -x "${DFU_UTIL}" ]; then DFU_UTIL=/opt/local/bin/dfu-util fi @@ -50,12 +50,12 @@ if [ ! -x ${DFU_UTIL} ]; then exit 2 fi -${DFU_UTIL} -d ${usbID} -a ${altID} -D ${binfile} -R ${dfuse_addr} -R +${DFU_UTIL} -d "${usbID}" -a "${altID}" -D "${binfile}" -R ${dfuse_addr} -R -echo -n Waiting for ${dummy_port_fullpath} serial... +echo -n Waiting for "${dummy_port_fullpath}" serial... COUNTER=0 -while [ ! -c ${dummy_port_fullpath} ] && ((COUNTER++ < 40)); do +while [ ! -c "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do sleep 0.1 done diff --git a/macosx/massStorageCopy.sh b/macosx/massStorageCopy.sh index c994b3229..884f24942 100755 --- a/macosx/massStorageCopy.sh +++ b/macosx/massStorageCopy.sh @@ -1,7 +1,5 @@ #!/bin/bash set -o nounset # Treat unset variables as an error -#set -x -VERSION="0.1" # List bin_filepath= @@ -14,7 +12,7 @@ usage() { echo "############################################################" echo "##" - echo "## `basename $0` [-I ] [-O ]" + echo "## $(basename "$0") [-I ] [-O ]" echo "##" echo "## Options:" echo "## -I: filepath binary to copy" @@ -32,29 +30,29 @@ if [ $# -lt 2 ]; then fi # Parsing options -if [ $1 == "-I" ]; then +if [ "$1" == "-I" ]; then shift 1 fi bin_filepath=$1 -if [ $2 == "-O" ]; then +if [ "$2" == "-O" ]; then shift 1 fi # Strip first and last "" mountpoint_name="${2%\"}" mountpoint_name="${mountpoint_name#\"}" -if [ -z $bin_filepath ]; then +if [ -z "$bin_filepath" ]; then echo "No binary file path provided!" exit 1 fi -if [ -z $mountpoint_name ]; then +if [ -z "$mountpoint_name" ]; then echo "No mountpoint name provided!" exit 1 fi -if [ ! -f $bin_filepath ]; then +if [ ! -f "$bin_filepath" ]; then echo "$bin_filepath not found!" exit 2 fi @@ -62,11 +60,11 @@ fi # Search the mountpoint IFS=',' read -ra mnt_list <<< "$mountpoint_name" for mnt in "${mnt_list[@]}"; do - mnt_path_list=(`df -Hl | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep $mnt`) + mnt_path_list=($(df -Hl | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep "$mnt")) if [ ${#mnt_path_list[@]} -ne 0 ]; then # Ensure to have exact match for mnt_path in "${mnt_path_list[@]}"; do - mnt_name=`echo $mnt_path | rev | cut -d'/' -f1 | rev` + mnt_name=$(echo "$mnt_path" | rev | cut -d'/' -f1 | rev) if [ "$mnt_name" = "$mnt" ]; then echo "Found '$mnt' at '$mnt_path'" mountpoint_path=$mnt_path @@ -76,7 +74,7 @@ for mnt in "${mnt_list[@]}"; do fi done -if [ -z $mountpoint_path ] || [ ! -d $mountpoint_path ]; then +if [ -z "$mountpoint_path" ] || [ ! -d "$mountpoint_path" ]; then echo "$mountpoint_name not found." echo "Please ensure the device is correctly connected and mounted." exit 3 @@ -84,6 +82,6 @@ fi # Copy the binary to the mountpoint echo "Copying $bin_filepath to $mountpoint_path..." -cp $bin_filepath $mountpoint_path +cp "$bin_filepath" "$mountpoint_path" exit $? diff --git a/macosx/stm32CubeProg.sh b/macosx/stm32CubeProg.sh index 4dda68c1d..665f3f534 100755 --- a/macosx/stm32CubeProg.sh +++ b/macosx/stm32CubeProg.sh @@ -2,10 +2,10 @@ set -o nounset # Treat unset variables as an error STM32CP_CLI=STM32_Programmer_CLI ADDRESS=0x8000000 -ERASE= -MODE= -PORT= -OPTS= +ERASE="" +MODE="" +PORT="" +OPTS="" ############################################################################### ## Help function @@ -13,9 +13,9 @@ usage() { echo "############################################################" echo "##" - echo "## `basename $0` [OPTIONS]" + echo "## $(basename "$0") [OPTIONS]" echo "##" - echo "## protocol: " + echo "## protocol:" echo "## 0: SWD" echo "## 1: Serial" echo "## 2: DFU" @@ -33,17 +33,16 @@ usage() echo "## -rst: Reset system" echo "## -s: start automatically (optional)" echo "############################################################" - exit $1 + exit "$1" } - check_tool() { - command -v $STM32CP_CLI >/dev/null 2>&1 - if [ $? != 0 ]; then + if ! ommand -v $STM32CP_CLI >/dev/null 2>&1 + then export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":$PATH fi - command -v $STM32CP_CLI >/dev/null 2>&1 - if [ $? != 0 ]; then + if ! command -v $STM32CP_CLI >/dev/null 2>&1 + then echo "$STM32CP_CLI not found." echo "Please install it or add '/bin' to your PATH environment:" echo "https://www.st.com/en/development-tools/stm32cubeprog.html" @@ -64,8 +63,8 @@ PROTOCOL=$1 FILEPATH=$2 # Protocol $1 # 1x: Erase all sectors -if [ $1 -ge 10 ]; then - ERASE='-e all' +if [ "$1" -ge 10 ]; then + ERASE="yes" PROTOCOL=$(($1 - 10)) fi # Protocol $1 @@ -73,10 +72,9 @@ fi # 1: Serial # 2: DFU case $PROTOCOL in - 0) - PORT='SWD' - MODE='mode=UR' + PORT="SWD" + MODE="mode=UR" shift 2;; 1) if [ $# -lt 3 ]; then @@ -86,7 +84,7 @@ case $PROTOCOL in shift 3 fi;; 2) - PORT='USB1' + PORT="USB1" shift 2;; *) echo "Protocol unknown!" @@ -94,10 +92,10 @@ case $PROTOCOL in esac if [ $# -gt 0 ]; then - OPTS="$@" + OPTS="$*" fi -${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS} +${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" ${ADDRESS} "${OPTS}" -exit 0 +exit $? diff --git a/run_arduino_gen.sh b/run_arduino_gen.sh index 269caeb3b..8d0e2f745 100755 --- a/run_arduino_gen.sh +++ b/run_arduino_gen.sh @@ -26,18 +26,19 @@ autodetect_board() { fi #search on device tree compatible entry the board type - if $(grep -q "stm32mp157c-ev" /proc/device-tree/compatible) ; then + if grep -q "stm32mp157c-ev" /proc/device-tree/compatible ; then BOARD="STM32MP157_EVAL" - elif $(grep -q "stm32mp157c-dk" /proc/device-tree/compatible) ; then + elif grep -q "stm32mp157c-dk" /proc/device-tree/compatible ; then BOARD="STM32MP157_DK" - elif $(grep -q "stm32mp157a-dk" /proc/device-tree/compatible) ; then + elif grep -q "stm32mp157a-dk" /proc/device-tree/compatible ; then BOARD="STM32MP157_DK" - elif $(grep -q "stm32mp157" /proc/device-tree/compatible) ; then + elif grep -q "stm32mp157" /proc/device-tree/compatible ; then BOARD="STM32MP157_GENERIC" else echo "Board is not an STM32MP157 BOARD" > /dev/kmsg exit 1 fi + echo "Board is $BOARD" > /dev/kmsg } @@ -56,9 +57,9 @@ firmware_load() { # Decode base64-encoded binary to a temp directory and check hash tmp_elf_file="/tmp/$ELF_NAME" if which uudecode >/dev/null 2>&1; then - echo -n "$ELF_BINARY" | uudecode -o /dev/stdout | gzip -d > "$tmp_elf_file" + printf "%s" "$ELF_BINARY" | uudecode -o /dev/stdout | gzip -d > "$tmp_elf_file" else - echo -n "$ELF_BINARY" | tail -n +2 | base64 -d - 2>/dev/null | gzip -d > "$tmp_elf_file" + printf "%s" "$ELF_BINARY" | tail -n +2 | base64 -d - 2>/dev/null | gzip -d > "$tmp_elf_file" fi echo "$ELF_HASH $tmp_elf_file" | sha256sum --status -c - @@ -70,10 +71,10 @@ firmware_load() { firmware_start() { # Change the name of the firmware - echo -n arduino.ino.elf > $REMOTEPROC_DIR/firmware + printf arduino.ino.elf > $REMOTEPROC_DIR/firmware # Change path to found firmware - #echo -n /home/root >/sys/module/firmware_class/parameters/path + #printf /home/root >/sys/module/firmware_class/parameters/path # Restart firmware echo "Arduino: Starting $ELF_INSTALL_PATH" > /dev/kmsg @@ -100,24 +101,24 @@ generate_packaged_script() { # Generate a copy of this script with a self-contained elf binary and its hash # The elf binary is gzip'ed, making its size to 1/6, and then Base64-encoded # using uuencode. - head -n $(grep -n "{%" "$this_script" | cut -d: -f1 | head -n 1) $this_script > $output_script - echo "ELF_HASH='$(sha256sum $elf_path | cut -d' ' -f1)'" >> $output_script - echo -n "ELF_BINARY='" >> $output_script + head -n "$(grep -n "{%" "$this_script" | cut -d: -f1 | head -n 1)" "$this_script" > "$output_script" + echo "ELF_HASH='$(sha256sum "$elf_path" | cut -d' ' -f1)'" >> "$output_script" + printf "ELF_BINARY='" >> "$output_script" if which uuencode >/dev/null 2>&1; then - gzip -c "$elf_path" | uuencode -m $ELF_NAME >> $output_script + gzip -c "$elf_path" | uuencode -m $ELF_NAME >> "$output_script" else - echo "begin-base64 644 $ELF_NAME" >> $output_script - gzip -c "$elf_path" | base64 >> $output_script + echo "begin-base64 644 $ELF_NAME" >> "$output_script" + gzip -c "$elf_path" | base64 >> "$output_script" fi - echo "'" >> $output_script - tail -n +$(grep -n "%}" "$this_script" | cut -d: -f1 | head -n 1) $this_script >> $output_script - dos2unix $output_script 2> /dev/null + echo "'" >> "$output_script" + tail -n +"$(grep -n "%}" "$this_script" | cut -d: -f1 | head -n 1)" "$this_script" >> "$output_script" + dos2unix "$output_script" 2> /dev/null } systemd_install() { - mkdir -p $(dirname $INSTALL_PATH) - cp $0 "$INSTALL_PATH" + mkdir -p "$(dirname $INSTALL_PATH)" + cp "$0" "$INSTALL_PATH" echo "File created: $INSTALL_PATH" cat > "$SYSTEMD_SERVICE_PATH" << EOF [Unit] @@ -136,16 +137,16 @@ EOF echo "File created: $SYSTEMD_SERVICE_PATH" echo "Please wait until systemd services are reloaded..." systemctl daemon-reload - systemctl enable $(basename $SYSTEMD_SERVICE_PATH) + systemctl enable "$(basename "$SYSTEMD_SERVICE_PATH")" } systemd_uninstall() { - systemctl stop $(basename $SYSTEMD_SERVICE_PATH) - systemctl disable $(basename $SYSTEMD_SERVICE_PATH) + systemctl stop "$(basename "$SYSTEMD_SERVICE_PATH")" + systemctl disable "$(basename "$SYSTEMD_SERVICE_PATH")" rm "$SYSTEMD_SERVICE_PATH" echo "File deleted: $SYSTEMD_SERVICE_PATH" - rm -r $(dirname $INSTALL_PATH) + rm -r "$(dirname $INSTALL_PATH)" echo "File deleted: $INSTALL_PATH" } @@ -162,12 +163,12 @@ try_send() { return 0 fi sleep 1; - count=$(expr $count + 1) + count=$((count + 1)) done # Linux host must send any dummy data first to finish initialization of rpmsg # on the coprocessor side. This message should be discarded. # See: https://github.com/OpenAMP/open-amp/issues/182 - echo -n "DUMMY" >$RPMSG_DIR + printf "DUMMY" >$RPMSG_DIR echo "Virtual serial $RPMSG_DIR connection established." } @@ -195,12 +196,12 @@ case "$1" in install) autodetect_board systemd_install - echo "Auto-start service $(basename $SYSTEMD_SERVICE_PATH) installed." + echo "Auto-start service $(basename "$SYSTEMD_SERVICE_PATH") installed." ;; uninstall) autodetect_board systemd_uninstall - echo "Auto-start service $(basename $SYSTEMD_SERVICE_PATH) uninstalled." + echo "Auto-start service $(basename "$SYSTEMD_SERVICE_PATH") uninstalled." ;; monitor) autodetect_board @@ -227,10 +228,10 @@ case "$1" in ;; generate) output=$(echo "$3" | sed 's/\.ino\././g') - generate_packaged_script $2 $output + generate_packaged_script "$2" "$output" echo "$(readlink -f "$output") generated successfully." echo "This file should be uploaded manually by SCP, SFTP, Kermit, or etc." - echo "Then run \"sh ./$(basename $output) start\" command in the board's console." + echo "Then run \"sh ./$(basename "$output") start\" command in the board's console." echo "For detailed instructions, please visit:" echo " https://github.com/stm32duino/Arduino_Core_STM32/tree/master/variants/STM32MP157_DK/README.md" ;; From f9f5b2511da027db170bd4b5a060e98df230f57d Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 4 Mar 2020 10:51:32 +0100 Subject: [PATCH 3/3] Use shfmt to format shell scripts Add the formatting options file Signed-off-by: Frederic Pillon --- .editorconfig | 10 ++++++++++ linux/dfu-util.sh | 4 ++-- linux/install.sh | 19 +++++++++---------- linux/maple_upload.sh | 19 ++++++++++--------- linux/massStorageCopy.sh | 9 ++++----- linux/stm32CubeProg.sh | 28 ++++++++++++++-------------- macosx/maple_upload.sh | 25 +++++++++++++------------ macosx/massStorageCopy.sh | 9 ++++----- macosx/stm32CubeProg.sh | 28 ++++++++++++++-------------- run_arduino_gen.sh | 39 ++++++++++++++++----------------------- 10 files changed, 96 insertions(+), 94 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..a35d22588 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +[*.sh] +# like -i=2 +indent_style = space +indent_size = 2 + +#shell_variant = posix # like -ln=posix +#binary_next_line = true # like -bn +switch_case_indent = true # like -ci +space_redirects = true # like -sr +#keep_padding = true # like -kp diff --git a/linux/dfu-util.sh b/linux/dfu-util.sh index b4ddf1b06..2f355b4ce 100755 --- a/linux/dfu-util.sh +++ b/linux/dfu-util.sh @@ -4,10 +4,10 @@ # # Get the directory where the script is running. -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # Choose dfu program by arch -if [ "$(uname -m)" == "x86_64" ]; then +if [ "$(uname -m)" == "x86_64" ]; then DFU_UTIL=${DIR}/dfu-util_x86_64/dfu-util else DFU_UTIL=${DIR}/dfu-util/dfu-util diff --git a/linux/install.sh b/linux/install.sh index df39e472f..a7cca0a6d 100755 --- a/linux/install.sh +++ b/linux/install.sh @@ -1,15 +1,14 @@ #!/bin/sh if sudo [ -w /etc/udev/rules.d ]; then - echo "Copying Maple-specific udev rules..." - sudo cp -v 45-maple.rules /etc/udev/rules.d/45-maple.rules - sudo chown root:root /etc/udev/rules.d/45-maple.rules - sudo chmod 644 /etc/udev/rules.d/45-maple.rules - echo "Reloading udev rules" - sudo udevadm control --reload-rules - echo "Adding current user to dialout group" - sudo adduser "$USER" dialout + echo "Copying Maple-specific udev rules..." + sudo cp -v 45-maple.rules /etc/udev/rules.d/45-maple.rules + sudo chown root:root /etc/udev/rules.d/45-maple.rules + sudo chmod 644 /etc/udev/rules.d/45-maple.rules + echo "Reloading udev rules" + sudo udevadm control --reload-rules + echo "Adding current user to dialout group" + sudo adduser "$USER" dialout else - echo "Couldn't copy to /etc/udev/rules.d/; you probably have to run this script as root? Or your distribution of Linux doesn't include udev; try running the IDE itself as root." + echo "Couldn't copy to /etc/udev/rules.d/; you probably have to run this script as root? Or your distribution of Linux doesn't include udev; try running the IDE itself as root." fi - diff --git a/linux/maple_upload.sh b/linux/maple_upload.sh index 43deeb0bd..6f767332e 100755 --- a/linux/maple_upload.sh +++ b/linux/maple_upload.sh @@ -3,19 +3,21 @@ #set -e if [ $# -lt 4 ]; then - echo "Usage: $0 $# " >&2 - exit 1 + echo "Usage: $0 $# " >&2 + exit 1 fi -altID="$2"; usbID="$3"; binfile="$4"; dummy_port_fullpath="/dev/$1" +altID="$2" +usbID="$3" +binfile="$4" +dummy_port_fullpath="/dev/$1" if [ $# -eq 5 ]; then - dfuse_addr="--dfuse-address $5" + dfuse_addr="--dfuse-address $5" else - dfuse_addr="" + dfuse_addr="" fi - # Get the directory where the script is running. -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # ----------------- IMPORTANT ----------------- # The 2nd parameter to upload-reset is the delay after resetting before it exits @@ -23,7 +25,6 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # You may need to tune this to your system # 750ms to 1500ms seems to work on my Mac - "${DIR}/upload-reset" "${dummy_port_fullpath}" 750 "${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" ${dfuse_addr} -R @@ -32,7 +33,7 @@ echo -n Waiting for "${dummy_port_fullpath}" serial... COUNTER=0 while [ ! -r "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do - sleep 0.1 + sleep 0.1 done echo Done diff --git a/linux/massStorageCopy.sh b/linux/massStorageCopy.sh index be33a135b..cf476220b 100755 --- a/linux/massStorageCopy.sh +++ b/linux/massStorageCopy.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -o nounset # Treat unset variables as an error +set -o nounset # Treat unset variables as an error # List bin_filepath= @@ -8,8 +8,7 @@ mountpoint_path= ############################################################################### ## Help function -usage() -{ +usage() { echo "############################################################" echo "##" echo "## $(basename "$0") [-I ] [-O ]" @@ -30,13 +29,13 @@ if [ $# -lt 2 ]; then fi # Parsing options -if [ "$1" == "-I" ]; then +if [ "$1" == "-I" ]; then shift 1 fi bin_filepath=$1 -if [ "$2" == "-O" ]; then +if [ "$2" == "-O" ]; then shift 1 fi # Strip first and last "" diff --git a/linux/stm32CubeProg.sh b/linux/stm32CubeProg.sh index 95607ce14..9d131c3a1 100755 --- a/linux/stm32CubeProg.sh +++ b/linux/stm32CubeProg.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -o nounset # Treat unset variables as an error +set -o nounset # Treat unset variables as an error STM32CP_CLI=STM32_Programmer.sh ADDRESS=0x8000000 ERASE="" @@ -9,8 +9,7 @@ OPTS="" ############################################################################### ## Help function -usage() -{ +usage() { echo "############################################################" echo "##" echo "## $(basename "$0") [OPTIONS]" @@ -37,12 +36,10 @@ usage() } check_tool() { - if ! command -v $STM32CP_CLI >/dev/null 2>&1 - then + if ! command -v $STM32CP_CLI > /dev/null 2>&1; then export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":$PATH fi - if ! command -v $STM32CP_CLI >/dev/null 2>&1 - then + if ! command -v $STM32CP_CLI > /dev/null 2>&1; then echo "$STM32CP_CLI not found." echo "Please install it or add '/bin' to your PATH environment:" echo "https://www.st.com/en/development-tools/stm32cubeprog.html" @@ -54,8 +51,8 @@ check_tool() { check_tool if [ $# -lt 2 ]; then - echo "Not enough arguments!" - usage 2 + echo "Not enough arguments!" + usage 2 fi # Parse options @@ -75,20 +72,24 @@ case $PROTOCOL in 0) PORT="SWD" MODE="mode=UR" - shift 2;; + shift 2 + ;; 1) if [ $# -lt 3 ]; then usage 3 else PORT=$3 shift 3 - fi;; + fi + ;; 2) PORT="USB1" - shift 2;; + shift 2 + ;; *) echo "Protocol unknown!" - usage 4;; + usage 4 + ;; esac if [ $# -gt 0 ]; then @@ -98,4 +99,3 @@ fi ${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" ${ADDRESS} "${OPTS}" exit $? - diff --git a/macosx/maple_upload.sh b/macosx/maple_upload.sh index b2bec965f..5e567e02c 100755 --- a/macosx/maple_upload.sh +++ b/macosx/maple_upload.sh @@ -3,15 +3,16 @@ set -e if [ $# -lt 4 ]; then - echo "Usage: $0 $# " >&2 - exit 1 + echo "Usage: $0 $# " >&2 + exit 1 fi -altID=$2; usbID=$3; binfile=$4;dummy_port_fullpath="/dev/$1" - +altID=$2 +usbID=$3 +binfile=$4 +dummy_port_fullpath="/dev/$1" # Get the directory where the script is running. -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) - +DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # ----------------- Old code to reset the USB - which doesn't seem to work -------- # @@ -34,20 +35,20 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) "${DIR}"/upload-reset "${dummy_port_fullpath}" 750 if [ $# -eq 5 ]; then - dfuse_addr="--dfuse-address $5" + dfuse_addr="--dfuse-address $5" else - dfuse_addr="" + dfuse_addr="" fi #DFU_UTIL=/usr/local/bin/dfu-util DFU_UTIL=${DIR}/dfu-util/dfu-util if [ ! -x "${DFU_UTIL}" ]; then - DFU_UTIL=/opt/local/bin/dfu-util + DFU_UTIL=/opt/local/bin/dfu-util fi if [ ! -x ${DFU_UTIL} ]; then - echo "$0: error: cannot find ${DFU_UTIL}" >&2 - exit 2 + echo "$0: error: cannot find ${DFU_UTIL}" >&2 + exit 2 fi ${DFU_UTIL} -d "${usbID}" -a "${altID}" -D "${binfile}" -R ${dfuse_addr} -R @@ -56,7 +57,7 @@ echo -n Waiting for "${dummy_port_fullpath}" serial... COUNTER=0 while [ ! -c "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do - sleep 0.1 + sleep 0.1 done echo Done diff --git a/macosx/massStorageCopy.sh b/macosx/massStorageCopy.sh index 884f24942..abc8869ae 100755 --- a/macosx/massStorageCopy.sh +++ b/macosx/massStorageCopy.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -o nounset # Treat unset variables as an error +set -o nounset # Treat unset variables as an error # List bin_filepath= @@ -8,8 +8,7 @@ mountpoint_path= ############################################################################### ## Help function -usage() -{ +usage() { echo "############################################################" echo "##" echo "## $(basename "$0") [-I ] [-O ]" @@ -30,13 +29,13 @@ if [ $# -lt 2 ]; then fi # Parsing options -if [ "$1" == "-I" ]; then +if [ "$1" == "-I" ]; then shift 1 fi bin_filepath=$1 -if [ "$2" == "-O" ]; then +if [ "$2" == "-O" ]; then shift 1 fi # Strip first and last "" diff --git a/macosx/stm32CubeProg.sh b/macosx/stm32CubeProg.sh index 665f3f534..aa0037f27 100755 --- a/macosx/stm32CubeProg.sh +++ b/macosx/stm32CubeProg.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -o nounset # Treat unset variables as an error +set -o nounset # Treat unset variables as an error STM32CP_CLI=STM32_Programmer_CLI ADDRESS=0x8000000 ERASE="" @@ -9,8 +9,7 @@ OPTS="" ############################################################################### ## Help function -usage() -{ +usage() { echo "############################################################" echo "##" echo "## $(basename "$0") [OPTIONS]" @@ -37,12 +36,10 @@ usage() } check_tool() { - if ! ommand -v $STM32CP_CLI >/dev/null 2>&1 - then + if ! command -v $STM32CP_CLI > /dev/null 2>&1; then export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":$PATH fi - if ! command -v $STM32CP_CLI >/dev/null 2>&1 - then + if ! command -v $STM32CP_CLI > /dev/null 2>&1; then echo "$STM32CP_CLI not found." echo "Please install it or add '/bin' to your PATH environment:" echo "https://www.st.com/en/development-tools/stm32cubeprog.html" @@ -54,8 +51,8 @@ check_tool() { check_tool if [ $# -lt 2 ]; then - echo "Not enough arguments!" - usage 2 + echo "Not enough arguments!" + usage 2 fi # Parse options @@ -75,20 +72,24 @@ case $PROTOCOL in 0) PORT="SWD" MODE="mode=UR" - shift 2;; + shift 2 + ;; 1) if [ $# -lt 3 ]; then usage 3 else PORT=$3 shift 3 - fi;; + fi + ;; 2) PORT="USB1" - shift 2;; + shift 2 + ;; *) echo "Protocol unknown!" - usage 4;; + usage 4 + ;; esac if [ $# -gt 0 ]; then @@ -98,4 +99,3 @@ fi ${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" ${ADDRESS} "${OPTS}" exit $? - diff --git a/run_arduino_gen.sh b/run_arduino_gen.sh index 8d0e2f745..f0a0ea8de 100755 --- a/run_arduino_gen.sh +++ b/run_arduino_gen.sh @@ -18,7 +18,6 @@ ELF_HASH="" ELF_BINARY="" ### END OF BINARY PART %} ### - autodetect_board() { if [ ! -d /proc/device-tree/ ]; then echo "Proc Device tree are not available, Could not detect on which board we are" > /dev/kmsg @@ -26,13 +25,13 @@ autodetect_board() { fi #search on device tree compatible entry the board type - if grep -q "stm32mp157c-ev" /proc/device-tree/compatible ; then + if grep -q "stm32mp157c-ev" /proc/device-tree/compatible; then BOARD="STM32MP157_EVAL" - elif grep -q "stm32mp157c-dk" /proc/device-tree/compatible ; then + elif grep -q "stm32mp157c-dk" /proc/device-tree/compatible; then BOARD="STM32MP157_DK" - elif grep -q "stm32mp157a-dk" /proc/device-tree/compatible ; then + elif grep -q "stm32mp157a-dk" /proc/device-tree/compatible; then BOARD="STM32MP157_DK" - elif grep -q "stm32mp157" /proc/device-tree/compatible ; then + elif grep -q "stm32mp157" /proc/device-tree/compatible; then BOARD="STM32MP157_GENERIC" else echo "Board is not an STM32MP157 BOARD" > /dev/kmsg @@ -41,14 +40,13 @@ autodetect_board() { echo "Board is $BOARD" > /dev/kmsg } - firmware_load() { if [ -z "$ELF_BINARY" ]; then echo "No Arduino binary contained. Run generate command first." exit 1 fi - if ( echo "$ELF_HASH $ELF_INSTALL_PATH" | sha256sum --status -c - 2>/dev/null ); then + if (echo "$ELF_HASH $ELF_INSTALL_PATH" | sha256sum --status -c - 2> /dev/null); then # The same firmware already exists, skip this step echo "The same firmware is already installed. Starting..." return 0 @@ -56,19 +54,18 @@ firmware_load() { # Decode base64-encoded binary to a temp directory and check hash tmp_elf_file="/tmp/$ELF_NAME" - if which uudecode >/dev/null 2>&1; then + if which uudecode > /dev/null 2>&1; then printf "%s" "$ELF_BINARY" | uudecode -o /dev/stdout | gzip -d > "$tmp_elf_file" - else - printf "%s" "$ELF_BINARY" | tail -n +2 | base64 -d - 2>/dev/null | gzip -d > "$tmp_elf_file" + else + printf "%s" "$ELF_BINARY" | tail -n +2 | base64 -d - 2> /dev/null | gzip -d > "$tmp_elf_file" fi echo "$ELF_HASH $tmp_elf_file" | sha256sum --status -c - - + # Copy elf into /lib/firmware mv $tmp_elf_file $ELF_INSTALL_PATH echo "Arduino: Executable created: $ELF_INSTALL_PATH" > /dev/kmsg } - firmware_start() { # Change the name of the firmware printf arduino.ino.elf > $REMOTEPROC_DIR/firmware @@ -78,17 +75,15 @@ firmware_start() { # Restart firmware echo "Arduino: Starting $ELF_INSTALL_PATH" > /dev/kmsg - echo start > $REMOTEPROC_DIR/state 2>/dev/null || true + echo start > $REMOTEPROC_DIR/state 2> /dev/null || true } - firmware_stop() { # Stop the firmware echo "Arduino: Stopping $ELF_INSTALL_PATH" > /dev/kmsg - echo stop > $REMOTEPROC_DIR/state 2>/dev/null || true + echo stop > $REMOTEPROC_DIR/state 2> /dev/null || true } - generate_packaged_script() { elf_path="$1" this_script=$(readlink -f "$0") @@ -97,14 +92,14 @@ generate_packaged_script() { echo "The output file name must be diffent from this script file" exit 1 fi - + # Generate a copy of this script with a self-contained elf binary and its hash # The elf binary is gzip'ed, making its size to 1/6, and then Base64-encoded # using uuencode. head -n "$(grep -n "{%" "$this_script" | cut -d: -f1 | head -n 1)" "$this_script" > "$output_script" echo "ELF_HASH='$(sha256sum "$elf_path" | cut -d' ' -f1)'" >> "$output_script" printf "ELF_BINARY='" >> "$output_script" - if which uuencode >/dev/null 2>&1; then + if which uuencode > /dev/null 2>&1; then gzip -c "$elf_path" | uuencode -m $ELF_NAME >> "$output_script" else echo "begin-base64 644 $ELF_NAME" >> "$output_script" @@ -115,7 +110,6 @@ generate_packaged_script() { dos2unix "$output_script" 2> /dev/null } - systemd_install() { mkdir -p "$(dirname $INSTALL_PATH)" cp "$0" "$INSTALL_PATH" @@ -140,7 +134,6 @@ EOF systemctl enable "$(basename "$SYSTEMD_SERVICE_PATH")" } - systemd_uninstall() { systemctl stop "$(basename "$SYSTEMD_SERVICE_PATH")" systemctl disable "$(basename "$SYSTEMD_SERVICE_PATH")" @@ -162,13 +155,13 @@ try_send() { echo "If you didn't enable the virtual serial, ignore this message." return 0 fi - sleep 1; + sleep 1 count=$((count + 1)) done # Linux host must send any dummy data first to finish initialization of rpmsg # on the coprocessor side. This message should be discarded. # See: https://github.com/OpenAMP/open-amp/issues/182 - printf "DUMMY" >$RPMSG_DIR + printf "DUMMY" > $RPMSG_DIR echo "Virtual serial $RPMSG_DIR connection established." } @@ -210,7 +203,7 @@ case "$1" in ;; send-msg) autodetect_board - echo "${@:2}" >$RPMSG_DIR + echo "${@:2}" > $RPMSG_DIR ;; send-file) autodetect_board