Skip to content

Commit e00d1d7

Browse files
authored
Merge pull request #56 from fpistm/hardening
Harden shell scripts
2 parents 4d64679 + f9f5b25 commit e00d1d7

11 files changed

+199
-204
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[*.sh]
2+
# like -i=2
3+
indent_style = space
4+
indent_size = 2
5+
6+
#shell_variant = posix # like -ln=posix
7+
#binary_next_line = true # like -bn
8+
switch_case_indent = true # like -ci
9+
space_redirects = true # like -sr
10+
#keep_padding = true # like -kp

linux/dfu-util.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#
55

66
# Get the directory where the script is running.
7-
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
7+
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
88

99
# Choose dfu program by arch
10-
if [ `uname -m` == "x86_64" ]; then
10+
if [ "$(uname -m)" == "x86_64" ]; then
1111
DFU_UTIL=${DIR}/dfu-util_x86_64/dfu-util
1212
else
1313
DFU_UTIL=${DIR}/dfu-util/dfu-util

linux/install.sh

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/bin/sh
22

33
if sudo [ -w /etc/udev/rules.d ]; then
4-
echo "Copying Maple-specific udev rules..."
5-
sudo cp -v 45-maple.rules /etc/udev/rules.d/45-maple.rules
6-
sudo chown root:root /etc/udev/rules.d/45-maple.rules
7-
sudo chmod 644 /etc/udev/rules.d/45-maple.rules
8-
echo "Reloading udev rules"
9-
sudo udevadm control --reload-rules
10-
echo "Adding current user to dialout group"
11-
sudo adduser $USER dialout
4+
echo "Copying Maple-specific udev rules..."
5+
sudo cp -v 45-maple.rules /etc/udev/rules.d/45-maple.rules
6+
sudo chown root:root /etc/udev/rules.d/45-maple.rules
7+
sudo chmod 644 /etc/udev/rules.d/45-maple.rules
8+
echo "Reloading udev rules"
9+
sudo udevadm control --reload-rules
10+
echo "Adding current user to dialout group"
11+
sudo adduser "$USER" dialout
1212
else
13-
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."
13+
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."
1414
fi
15-

linux/maple_upload

-38
This file was deleted.

linux/maple_upload.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
#set -e
4+
5+
if [ $# -lt 4 ]; then
6+
echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
7+
exit 1
8+
fi
9+
altID="$2"
10+
usbID="$3"
11+
binfile="$4"
12+
dummy_port_fullpath="/dev/$1"
13+
if [ $# -eq 5 ]; then
14+
dfuse_addr="--dfuse-address $5"
15+
else
16+
dfuse_addr=""
17+
fi
18+
19+
# Get the directory where the script is running.
20+
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
21+
22+
# ----------------- IMPORTANT -----------------
23+
# The 2nd parameter to upload-reset is the delay after resetting before it exits
24+
# This value is in milliseonds
25+
# You may need to tune this to your system
26+
# 750ms to 1500ms seems to work on my Mac
27+
28+
"${DIR}/upload-reset" "${dummy_port_fullpath}" 750
29+
30+
"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" ${dfuse_addr} -R
31+
32+
echo -n Waiting for "${dummy_port_fullpath}" serial...
33+
34+
COUNTER=0
35+
while [ ! -r "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do
36+
sleep 0.1
37+
done
38+
39+
echo Done
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/bash
2-
set -o nounset # Treat unset variables as an error
3-
#set -x
4-
VERSION="0.1"
2+
set -o nounset # Treat unset variables as an error
53

64
# List
75
bin_filepath=
@@ -10,11 +8,10 @@ mountpoint_path=
108

119
###############################################################################
1210
## Help function
13-
usage()
14-
{
11+
usage() {
1512
echo "############################################################"
1613
echo "##"
17-
echo "## `basename $0` [-I <filepath>] [-O <mountpoint(s)> ]"
14+
echo "## $(basename "$0") [-I <filepath>] [-O <mountpoint(s)> ]"
1815
echo "##"
1916
echo "## Options:"
2017
echo "## -I: filepath binary to copy"
@@ -32,29 +29,29 @@ if [ $# -lt 2 ]; then
3229
fi
3330

3431
# Parsing options
35-
if [ $1 == "-I" ]; then
32+
if [ "$1" == "-I" ]; then
3633
shift 1
3734
fi
3835

3936
bin_filepath=$1
4037

41-
if [ $2 == "-O" ]; then
38+
if [ "$2" == "-O" ]; then
4239
shift 1
4340
fi
4441
# Strip first and last ""
4542
mountpoint_name="${2%\"}"
4643
mountpoint_name="${mountpoint_name#\"}"
4744

48-
if [ -z $bin_filepath ]; then
45+
if [ -z "$bin_filepath" ]; then
4946
echo "No binary file path provided!"
5047
exit 1
5148
fi
52-
if [ -z $mountpoint_name ]; then
49+
if [ -z "$mountpoint_name" ]; then
5350
echo "No mountpoint name provided!"
5451
exit 1
5552
fi
5653

57-
if [ ! -f $bin_filepath ]; then
54+
if [ ! -f "$bin_filepath" ]; then
5855
echo "$bin_filepath not found!"
5956
exit 2
6057
fi
@@ -63,11 +60,11 @@ fi
6360
IFS=',' read -ra mnt_list <<< "$mountpoint_name"
6461
for mnt in "${mnt_list[@]}"; do
6562
# mnt_path_list=(`cat /proc/mounts | cut -d' ' -f2 | sort -u | grep $mnt`)
66-
mnt_path_list=(`df -Hl | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep $mnt`)
63+
mnt_path_list=($(df -Hl | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep "$mnt"))
6764
if [ ${#mnt_path_list[@]} -ne 0 ]; then
6865
# Ensure to have exact match
6966
for mnt_path in "${mnt_path_list[@]}"; do
70-
mnt_name=`echo $mnt_path | rev | cut -d'/' -f1 | rev`
67+
mnt_name=$(echo "$mnt_path" | rev | cut -d'/' -f1 | rev)
7168
if [ "$mnt_name" = "$mnt" ]; then
7269
echo "Found '$mnt' at '$mnt_path'"
7370
mountpoint_path=$mnt_path
@@ -77,14 +74,14 @@ for mnt in "${mnt_list[@]}"; do
7774
fi
7875
done
7976

80-
if [ -z $mountpoint_path ] || [ ! -d $mountpoint_path ]; then
77+
if [ -z "$mountpoint_path" ] || [ ! -d "$mountpoint_path" ]; then
8178
echo "$mountpoint_name not found."
8279
echo "Please ensure the device is correctly connected and mounted."
8380
exit 3
8481
fi
8582

8683
# Copy the binary to the mountpoint
8784
echo "Copying $bin_filepath to $mountpoint_path..."
88-
cp $bin_filepath $mountpoint_path
85+
cp "$bin_filepath" "$mountpoint_path"
8986

9087
exit $?

linux/stm32CubeProg.sh

+28-30
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
#!/bin/bash
2-
set -o nounset # Treat unset variables as an error
3-
#set -x
2+
set -o nounset # Treat unset variables as an error
43
STM32CP_CLI=STM32_Programmer.sh
54
ADDRESS=0x8000000
6-
ERASE=
7-
MODE=
8-
PORT=
9-
OPTS=
5+
ERASE=""
6+
MODE=""
7+
PORT=""
8+
OPTS=""
109

1110
###############################################################################
1211
## Help function
13-
usage()
14-
{
12+
usage() {
1513
echo "############################################################"
1614
echo "##"
17-
echo "## `basename $0` <protocol> <file_path> [OPTIONS]"
15+
echo "## $(basename "$0") <protocol> <file_path> [OPTIONS]"
1816
echo "##"
1917
echo "## protocol:"
2018
echo "## 0: SWD"
@@ -34,17 +32,14 @@ usage()
3432
echo "## -rst: Reset system"
3533
echo "## -s: start automatically (optional)"
3634
echo "############################################################"
37-
exit $1
35+
exit "$1"
3836
}
3937

40-
4138
check_tool() {
42-
command -v $STM32CP_CLI >/dev/null 2>&1
43-
if [ $? != 0 ]; then
39+
if ! command -v $STM32CP_CLI > /dev/null 2>&1; then
4440
export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":$PATH
4541
fi
46-
command -v $STM32CP_CLI >/dev/null 2>&1
47-
if [ $? != 0 ]; then
42+
if ! command -v $STM32CP_CLI > /dev/null 2>&1; then
4843
echo "$STM32CP_CLI not found."
4944
echo "Please install it or add '<STM32CubeProgrammer path>/bin' to your PATH environment:"
5045
echo "https://www.st.com/en/development-tools/stm32cubeprog.html"
@@ -56,17 +51,17 @@ check_tool() {
5651
check_tool
5752

5853
if [ $# -lt 2 ]; then
59-
echo "Not enough arguments!"
60-
usage 2
54+
echo "Not enough arguments!"
55+
usage 2
6156
fi
6257

6358
# Parse options
6459
PROTOCOL=$1
6560
FILEPATH=$2
6661
# Protocol $1
6762
# 1x: Erase all sectors
68-
if [ $1 -ge 10 ]; then
69-
ERASE='-e all'
63+
if [ "$1" -ge 10 ]; then
64+
ERASE="yes"
7065
PROTOCOL=$(($1 - 10))
7166
fi
7267
# Protocol $1
@@ -75,29 +70,32 @@ fi
7570
# 2: DFU
7671
case $PROTOCOL in
7772
0)
78-
PORT='SWD'
79-
MODE='mode=UR'
80-
shift 2;;
73+
PORT="SWD"
74+
MODE="mode=UR"
75+
shift 2
76+
;;
8177
1)
8278
if [ $# -lt 3 ]; then
8379
usage 3
8480
else
8581
PORT=$3
8682
shift 3
87-
fi;;
83+
fi
84+
;;
8885
2)
89-
PORT='USB1'
90-
shift 2;;
86+
PORT="USB1"
87+
shift 2
88+
;;
9189
*)
9290
echo "Protocol unknown!"
93-
usage 4;;
91+
usage 4
92+
;;
9493
esac
9594

9695
if [ $# -gt 0 ]; then
97-
OPTS="$@"
96+
OPTS="$*"
9897
fi
9998

100-
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS}
101-
102-
exit 0
99+
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" ${ADDRESS} "${OPTS}"
103100

101+
exit $?

macosx/maple_upload renamed to macosx/maple_upload.sh

+18-17
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
set -e
44

55
if [ $# -lt 4 ]; then
6-
echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
7-
exit 1
6+
echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
7+
exit 1
88
fi
9-
dummy_port=$1; altID=$2; usbID=$3; binfile=$4;dummy_port_fullpath="/dev/$1"
10-
9+
altID=$2
10+
usbID=$3
11+
binfile=$4
12+
dummy_port_fullpath="/dev/$1"
1113

1214
# Get the directory where the script is running.
13-
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
14-
15+
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
1516

1617
# ----------------- Old code to reset the USB - which doesn't seem to work --------
1718
#
@@ -31,32 +32,32 @@ DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
3132
# You may need to tune this to your system
3233
# 750ms to 1500ms seems to work on my Mac
3334

34-
${DIR}/upload-reset ${dummy_port_fullpath} 750
35+
"${DIR}"/upload-reset "${dummy_port_fullpath}" 750
3536

3637
if [ $# -eq 5 ]; then
37-
dfuse_addr="--dfuse-address $5"
38+
dfuse_addr="--dfuse-address $5"
3839
else
39-
dfuse_addr=""
40+
dfuse_addr=""
4041
fi
4142

4243
#DFU_UTIL=/usr/local/bin/dfu-util
4344
DFU_UTIL=${DIR}/dfu-util/dfu-util
44-
if [ ! -x ${DFU_UTIL} ]; then
45-
DFU_UTIL=/opt/local/bin/dfu-util
45+
if [ ! -x "${DFU_UTIL}" ]; then
46+
DFU_UTIL=/opt/local/bin/dfu-util
4647
fi
4748

4849
if [ ! -x ${DFU_UTIL} ]; then
49-
echo "$0: error: cannot find ${DFU_UTIL}" >&2
50-
exit 2
50+
echo "$0: error: cannot find ${DFU_UTIL}" >&2
51+
exit 2
5152
fi
5253

53-
${DFU_UTIL} -d ${usbID} -a ${altID} -D ${binfile} -R ${dfuse_addr} -R
54+
${DFU_UTIL} -d "${usbID}" -a "${altID}" -D "${binfile}" -R ${dfuse_addr} -R
5455

55-
echo -n Waiting for ${dummy_port_fullpath} serial...
56+
echo -n Waiting for "${dummy_port_fullpath}" serial...
5657

5758
COUNTER=0
58-
while [ ! -c ${dummy_port_fullpath} ] && ((COUNTER++ < 40)); do
59-
sleep 0.1
59+
while [ ! -c "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do
60+
sleep 0.1
6061
done
6162

6263
echo Done

0 commit comments

Comments
 (0)